• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * This file is licensed under the GPL license.  For the full content
5  * of this license, see the COPYING file at the top level of this
6  * source tree.
7  */
8 
9 #ifdef __linux__
10 #include <mntent.h>
11 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
12 #include <sys/param.h>
13 #include <sys/mount.h>
14 #include <errno.h>
15 #include <string.h>
16 #endif
17 #include <stdio.h>
18 
19 #ifdef __linux__
20 
21 /*
22  * Returns if prefix is prefix of a string and the length of prefix.
23  */
strpref(const char * str,const char * pref)24 int strpref(const char *str, const char *pref)
25 {
26 	int i;
27 
28 	for (i = 0; pref[i] != '\0'; i++) {
29 		/* string ended too soon */
30 		if (str[i] == 0)
31 			return -1;
32 
33 		/* string is diferent */
34 		if (str[i] != pref[i])
35 			return -1;
36 	}
37 
38 	/* returns length of prefix */
39 	return i;
40 }
41 
42 /*
43  * Scans through mounted filesystems and check for longest prefix
44  * contained in path.
45  */
mounted_noatime(const char * path)46 int mounted_noatime(const char *path)
47 {
48 	struct mntent *mnt;
49 	int prefix_max = 0, prefix;
50 	int has_noatime;
51 	FILE *f;
52 
53 	f = setmntent("/proc/mounts", "r");
54 
55 	if (f == NULL) {
56 		printf("Couldn't mount /proc/mounts\n");
57 		return -1;
58 	}
59 
60 	while ((mnt = getmntent(f))) {
61 		/* ignore duplicit record for root fs */
62 		if (!strcmp(mnt->mnt_fsname, "rootfs"))
63 			continue;
64 
65 		prefix = strpref(path, mnt->mnt_dir);
66 
67 		if (prefix > prefix_max) {
68 			prefix_max = prefix;
69 			has_noatime = hasmntopt(mnt, "noatime") != NULL;
70 		}
71 	}
72 
73 	return has_noatime;
74 }
75 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
mounted_noatime(const char * path)76 int mounted_noatime(const char *path)
77 {
78 	struct statfs _statfs;
79 
80 	if (statfs(path, &_statfs) == -1) {
81 		printf("statfs for %s failed: %s", strerror(errno));
82 		return -1;
83 	}
84 
85 	return (_statfs.f_flags & MNT_NOATIME);
86 }
87 #else
mounted_noatime(const char * path)88 int mounted_noatime(const char *path)
89 {
90 	return 0;
91 }
92 #endif
93