perf fs: Rename NAME_find_mountpoint() to NAME__mountpoint()
[linux-2.6/btrfs-unstable.git] / tools / perf / util / fs.c
blob77bac4e4c115c3dd2676907b09e4e4739bc1adef
2 /* TODO merge/factor into tools/lib/lk/debugfs.c */
4 #include "util.h"
5 #include "util/fs.h"
7 static const char * const sysfs__fs_known_mountpoints[] = {
8 "/sys",
9 0,
12 struct fs {
13 const char *name;
14 const char * const *mounts;
15 char path[PATH_MAX + 1];
16 bool found;
17 long magic;
20 enum {
21 FS__SYSFS = 0,
24 static struct fs fs__entries[] = {
25 [FS__SYSFS] = {
26 .name = "sysfs",
27 .mounts = sysfs__fs_known_mountpoints,
28 .magic = SYSFS_MAGIC,
32 static bool fs__read_mounts(struct fs *fs)
34 bool found = false;
35 char type[100];
36 FILE *fp;
38 fp = fopen("/proc/mounts", "r");
39 if (fp == NULL)
40 return NULL;
42 while (!found &&
43 fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
44 fs->path, type) == 2) {
46 if (strcmp(type, fs->name) == 0)
47 found = true;
50 fclose(fp);
51 return fs->found = found;
54 static int fs__valid_mount(const char *fs, long magic)
56 struct statfs st_fs;
58 if (statfs(fs, &st_fs) < 0)
59 return -ENOENT;
60 else if (st_fs.f_type != magic)
61 return -ENOENT;
63 return 0;
66 static bool fs__check_mounts(struct fs *fs)
68 const char * const *ptr;
70 ptr = fs->mounts;
71 while (*ptr) {
72 if (fs__valid_mount(*ptr, fs->magic) == 0) {
73 fs->found = true;
74 strcpy(fs->path, *ptr);
75 return true;
77 ptr++;
80 return false;
83 static const char *fs__get_mountpoint(struct fs *fs)
85 if (fs__check_mounts(fs))
86 return fs->path;
88 return fs__read_mounts(fs) ? fs->path : NULL;
91 static const char *fs__mountpoint(int idx)
93 struct fs *fs = &fs__entries[idx];
95 if (fs->found)
96 return (const char *)fs->path;
98 return fs__get_mountpoint(fs);
101 #define FS__MOUNTPOINT(name, idx) \
102 const char *name##__mountpoint(void) \
104 return fs__mountpoint(idx); \
107 FS__MOUNTPOINT(sysfs, FS__SYSFS);