blob: b230fd96484c7d8e75f408edc912019d60ffb728 [file] [log] [blame]
davemoore@chromium.orgc3b9ec62011-03-25 04:45:02 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
evan@chromium.org33b77052010-08-20 09:36:01 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettw@chromium.org01f3da42014-08-14 05:22:14 +09005#include "base/files/file_util.h"
evan@chromium.org33b77052010-08-20 09:36:01 +09006
evan@chromium.org33b77052010-08-20 09:36:01 +09007#include <errno.h>
thestig@chromium.org772e4fe2014-02-06 19:41:03 +09008#include <linux/magic.h>
evan@chromium.org33b77052010-08-20 09:36:01 +09009#include <sys/vfs.h>
10
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090011#include "base/files/file_path.h"
12
brettw@chromium.orgaa82a772014-03-14 02:26:21 +090013namespace base {
evan@chromium.org33b77052010-08-20 09:36:01 +090014
brettw@chromium.orgaa82a772014-03-14 02:26:21 +090015bool GetFileSystemType(const FilePath& path, FileSystemType* type) {
evan@chromium.org33b77052010-08-20 09:36:01 +090016 struct statfs statfs_buf;
17 if (statfs(path.value().c_str(), &statfs_buf) < 0) {
18 if (errno == ENOENT)
19 return false;
20 *type = FILE_SYSTEM_UNKNOWN;
21 return true;
22 }
23
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090024 // Not all possible |statfs_buf.f_type| values are in linux/magic.h.
25 // Missing values are copied from the statfs man page.
evan@chromium.org33b77052010-08-20 09:36:01 +090026 switch (statfs_buf.f_type) {
27 case 0:
28 *type = FILE_SYSTEM_0;
29 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090030 case EXT2_SUPER_MAGIC: // Also ext3 and ext4
31 case MSDOS_SUPER_MAGIC:
32 case REISERFS_SUPER_MAGIC:
33 case BTRFS_SUPER_MAGIC:
34 case 0x5346544E: // NTFS
evan@chromium.org33b77052010-08-20 09:36:01 +090035 case 0x58465342: // XFS
evan@chromium.org33b77052010-08-20 09:36:01 +090036 case 0x3153464A: // JFS
37 *type = FILE_SYSTEM_ORDINARY;
38 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090039 case NFS_SUPER_MAGIC:
evan@chromium.org33b77052010-08-20 09:36:01 +090040 *type = FILE_SYSTEM_NFS;
41 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090042 case SMB_SUPER_MAGIC:
evan@chromium.org33b77052010-08-20 09:36:01 +090043 case 0xFF534D42: // CIFS
evan@chromium.org33b77052010-08-20 09:36:01 +090044 *type = FILE_SYSTEM_SMB;
45 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090046 case CODA_SUPER_MAGIC:
evan@chromium.org33b77052010-08-20 09:36:01 +090047 *type = FILE_SYSTEM_CODA;
48 break;
thestig@chromium.orgeac386c2014-02-21 10:36:23 +090049 case HUGETLBFS_MAGIC:
50 case RAMFS_MAGIC:
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090051 case TMPFS_MAGIC:
evan@chromium.org33b77052010-08-20 09:36:01 +090052 *type = FILE_SYSTEM_MEMORY;
53 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090054 case CGROUP_SUPER_MAGIC:
davemoore@chromium.orgc3b9ec62011-03-25 04:45:02 +090055 *type = FILE_SYSTEM_CGROUP;
56 break;
evan@chromium.org33b77052010-08-20 09:36:01 +090057 default:
58 *type = FILE_SYSTEM_OTHER;
59 }
60 return true;
61}
62
brettw@chromium.orgaa82a772014-03-14 02:26:21 +090063} // namespace base