• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // 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/files/file_util.h"
6 
7 #include <errno.h>
8 #include <linux/magic.h>
9 #include <stdint.h>
10 #include <sys/vfs.h>
11 
12 #include "base/files/file_path.h"
13 
14 namespace base {
15 
GetFileSystemType(const FilePath & path,FileSystemType * type)16 bool GetFileSystemType(const FilePath& path, FileSystemType* type) {
17   struct statfs statfs_buf;
18   if (statfs(path.value().c_str(), &statfs_buf) < 0) {
19     if (errno == ENOENT)
20       return false;
21     *type = FILE_SYSTEM_UNKNOWN;
22     return true;
23   }
24 
25   // Not all possible |statfs_buf.f_type| values are in linux/magic.h.
26   // Missing values are copied from the statfs man page.
27   // In some platforms, |statfs_buf.f_type| is declared as signed, but some of
28   // the values will overflow it, causing narrowing warnings. Cast to the
29   // largest possible unsigned integer type to avoid it.
30   switch (static_cast<uintmax_t>(statfs_buf.f_type)) {
31     case 0:
32       *type = FILE_SYSTEM_0;
33       break;
34     case EXT2_SUPER_MAGIC:  // Also ext3 and ext4
35     case MSDOS_SUPER_MAGIC:
36     case REISERFS_SUPER_MAGIC:
37     case BTRFS_SUPER_MAGIC:
38     case 0x5346544E:  // NTFS
39     case 0x58465342:  // XFS
40     case 0x3153464A:  // JFS
41       *type = FILE_SYSTEM_ORDINARY;
42       break;
43     case NFS_SUPER_MAGIC:
44       *type = FILE_SYSTEM_NFS;
45       break;
46     case SMB_SUPER_MAGIC:
47     case 0xFF534D42:  // CIFS
48       *type = FILE_SYSTEM_SMB;
49       break;
50     case CODA_SUPER_MAGIC:
51       *type = FILE_SYSTEM_CODA;
52       break;
53     case HUGETLBFS_MAGIC:
54     case RAMFS_MAGIC:
55     case TMPFS_MAGIC:
56       *type = FILE_SYSTEM_MEMORY;
57       break;
58     case CGROUP_SUPER_MAGIC:
59       *type = FILE_SYSTEM_CGROUP;
60       break;
61     default:
62       *type = FILE_SYSTEM_OTHER;
63   }
64   return true;
65 }
66 
67 }  // namespace base
68