summaryrefslogtreecommitdiff
path: root/memtrack-pixel/core/filesystem.h
blob: 46032231ca9c13bdcfef55196e4043c04082c356 (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
54
55
56
57
58
59
60
// TODO(b/147469372): filesystem library in Android's libcxx is not available
// for vendors. It had an unstable ABI and libcxx isn't updated ever since.

// This simply implements some of the required functions in not-so-safe fashion.

#pragma once

#include <dirent.h>
#include <log/log.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <string>
#include <vector>

namespace filesystem {
class path {
public:
    path(const std::string _path) : strPath(_path) {}

    path filename() const {
        auto pos = strPath.rfind('/');
        if (pos == std::string::npos)
            return path(strPath);

        pos++;
        auto l = strPath.size();
        return path(strPath.substr(pos, l - pos));
    }

    std::string string() const { return strPath; }

private:
    std::string strPath;
};

class directory_entry {
public:
    directory_entry(const std::string _path) : p(_path) {}

    class path path() {
        return p;
    }

private:
    class path p;
};

bool exists(const path& p);

bool is_directory(const path& p);

bool is_symlink(const path& p);

path read_symlink(const path& p);

// Vector is easier to create than an iterator and serves our purposes well
std::vector<directory_entry> directory_iterator(const path& p);
} // namespace filesystem