aboutsummaryrefslogtreecommitdiff
path: root/catapult/systrace/atrace_helper/jni/file_utils.h
blob: c6a5527e638cf7ad26719c78774b38db80f797ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FILE_UTILS_H_
#define FILE_UTILS_H_

#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>

#include <functional>
#include <map>
#include <memory>

#include "logging.h"

namespace file_utils {

// RAII classes for auto-releasing fd/dirs.
template <typename RESOURCE_TYPE, int (*CLOSE_FN)(RESOURCE_TYPE)>
struct ScopedResource {
  explicit ScopedResource(RESOURCE_TYPE r) : r_(r) { CHECK(r); }
  ~ScopedResource() { CLOSE_FN(r_); }
  RESOURCE_TYPE r_;
};

using ScopedFD = ScopedResource<int, close>;
using ScopedDir = ScopedResource<DIR*, closedir>;

// Invokes predicate(pid) for each folder in |proc_path|/[0-9]+ which has
// a numeric name (typically pids and tids).
void ForEachPidInProcPath(const char* proc_path,
                          std::function<void(int)> predicate);

// Reads the contents of |path| fully into |buf| up to |length| chars.
// |buf| is guaranteed to be null terminated.
ssize_t ReadFile(const char* path, char* buf, size_t length);

// Reads a single-line file, stripping out any \0, \r, \n and replacing
// non-printable charcters with '?'. |buf| is guaranteed to be null terminated.
bool ReadFileTrimmed(int pid, const char* proc_file, char* buf, size_t length);

// Convenience wrappers for /proc/|pid|/|proc_file| paths.
ssize_t ReadProcFile(int pid, const char* proc_file, char* buf, size_t length);
bool ReadProcFileTrimmed(int pid,
                         const char* proc_file,
                         char* buf,
                         size_t length);

}  // namespace file_utils

#endif  // FILE_UTILS_H_