blob: 33edc90271e287ec602196607ab9a7d7ee0188b9 [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
5#include "base/file_util.h"
6
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
mostynb@opera.comf0672de2014-02-21 02:06:03 +090013// Make sure some of the newer macros from magic.h are defined.
14// TODO(mostynb@opera.com): remove this after 2014.
15#ifndef BTRFS_SUPER_MAGIC
16#define BTRFS_SUPER_MAGIC 0x9123683E
17#endif
18#ifndef HUGETLBFS_MAGIC
19#define HUGETLBFS_MAGIC 0x958458f6
20#endif
21#ifndef TMPFS_MAGIC
22#define TMPFS_MAGIC 0x01021994
23#endif
24
evan@chromium.org33b77052010-08-20 09:36:01 +090025namespace file_util {
26
brettw@chromium.orgc02b6032013-02-16 13:12:26 +090027bool GetFileSystemType(const base::FilePath& path, FileSystemType* type) {
evan@chromium.org33b77052010-08-20 09:36:01 +090028 struct statfs statfs_buf;
29 if (statfs(path.value().c_str(), &statfs_buf) < 0) {
30 if (errno == ENOENT)
31 return false;
32 *type = FILE_SYSTEM_UNKNOWN;
33 return true;
34 }
35
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090036 // Not all possible |statfs_buf.f_type| values are in linux/magic.h.
37 // Missing values are copied from the statfs man page.
evan@chromium.org33b77052010-08-20 09:36:01 +090038 switch (statfs_buf.f_type) {
39 case 0:
40 *type = FILE_SYSTEM_0;
41 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090042 case EXT2_SUPER_MAGIC: // Also ext3 and ext4
43 case MSDOS_SUPER_MAGIC:
44 case REISERFS_SUPER_MAGIC:
45 case BTRFS_SUPER_MAGIC:
46 case 0x5346544E: // NTFS
evan@chromium.org33b77052010-08-20 09:36:01 +090047 case 0x58465342: // XFS
evan@chromium.org33b77052010-08-20 09:36:01 +090048 case 0x3153464A: // JFS
49 *type = FILE_SYSTEM_ORDINARY;
50 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090051 case NFS_SUPER_MAGIC:
evan@chromium.org33b77052010-08-20 09:36:01 +090052 *type = FILE_SYSTEM_NFS;
53 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090054 case SMB_SUPER_MAGIC:
evan@chromium.org33b77052010-08-20 09:36:01 +090055 case 0xFF534D42: // CIFS
evan@chromium.org33b77052010-08-20 09:36:01 +090056 *type = FILE_SYSTEM_SMB;
57 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090058 case CODA_SUPER_MAGIC:
evan@chromium.org33b77052010-08-20 09:36:01 +090059 *type = FILE_SYSTEM_CODA;
60 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090061 case HUGETLBFS_MAGIC: // AKA ramfs
62 case TMPFS_MAGIC:
evan@chromium.org33b77052010-08-20 09:36:01 +090063 *type = FILE_SYSTEM_MEMORY;
64 break;
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090065 case CGROUP_SUPER_MAGIC:
davemoore@chromium.orgc3b9ec62011-03-25 04:45:02 +090066 *type = FILE_SYSTEM_CGROUP;
67 break;
evan@chromium.org33b77052010-08-20 09:36:01 +090068 default:
69 *type = FILE_SYSTEM_OTHER;
70 }
71 return true;
72}
73
thestig@chromium.org772e4fe2014-02-06 19:41:03 +090074} // namespace file_util