forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fstype.c
102 lines (94 loc) · 2.33 KB
/
fstype.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Copyright 2014-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#ifdef HAVE_SYS_VFS_H
# include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
# include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
# include <sys/mount.h>
#endif
#ifdef __linux__
#include <linux/magic.h>
#endif
// The primary purpose of checking the filesystem type is to prevent
// watching filesystems that are known to be problematic, such as
// network or remote mounted filesystems. As such, we don't strictly
// need to have a fully comprehensive mapping of the underlying filesystem
// type codes to names, just the known problematic types
w_string_t *w_fstype(const char *path)
{
#ifdef __linux__
struct statfs sfs;
const char *name = "unknown";
if (statfs(path, &sfs) == 0) {
switch (sfs.f_type) {
#ifdef CIFS_MAGIC_NUMBER
case CIFS_MAGIC_NUMBER:
name = "cifs";
break;
#endif
#ifdef NFS_SUPER_MAGIC
case NFS_SUPER_MAGIC:
name = "nfs";
break;
#endif
#ifdef SMB_SUPER_MAGIC
case SMB_SUPER_MAGIC:
name = "smb";
break;
#endif
default:
name = "unknown";
}
}
return w_string_new(name);
#elif STATVFS_HAS_FSTYPE_AS_STRING
struct statvfs sfs;
if (statvfs(path, &sfs) == 0) {
#ifdef HAVE_STRUCT_STATVFS_F_FSTYPENAME
return w_string_new(sfs.f_fstypename);
#endif
#ifdef HAVE_STRUCT_STATVFS_F_BASETYPE
return w_string_new(sfs.f_basetype);
#endif
}
#elif HAVE_STATFS
struct statfs sfs;
if (statfs(path, &sfs) == 0) {
return w_string_new(sfs.f_fstypename);
}
#endif
#ifdef _WIN32
WCHAR *wpath = w_utf8_to_win_unc(path, -1);
w_string_t *fstype_name = NULL;
if (wpath) {
WCHAR fstype[MAX_PATH+1];
HANDLE h = CreateFileW(wpath, GENERIC_READ,
FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (h) {
if (GetVolumeInformationByHandleW(h, NULL, 0, 0, 0, 0, fstype,
MAX_PATH+1)) {
fstype_name = w_string_new_wchar(fstype, -1);
}
CloseHandle(h);
}
free(wpath);
}
if (fstype_name) {
return fstype_name;
}
return w_string_new("unknown");
#else
unused_parameter(path);
return w_string_new("unknown");
#endif
}
/* vim:ts=2:sw=2:et:
*/