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
14namespace base {
15
16bool 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  switch (static_cast<uintmax_t>(statfs_buf.f_type)) {
28    case 0:
29      *type = FILE_SYSTEM_0;
30      break;
31    case EXT2_SUPER_MAGIC:  // Also ext3 and ext4
32    case MSDOS_SUPER_MAGIC:
33    case REISERFS_SUPER_MAGIC:
34    case BTRFS_SUPER_MAGIC:
35    case 0x5346544E:  // NTFS
36    case 0x58465342:  // XFS
37    case 0x3153464A:  // JFS
38      *type = FILE_SYSTEM_ORDINARY;
39      break;
40    case NFS_SUPER_MAGIC:
41      *type = FILE_SYSTEM_NFS;
42      break;
43    case SMB_SUPER_MAGIC:
44    case 0xFF534D42:  // CIFS
45      *type = FILE_SYSTEM_SMB;
46      break;
47    case CODA_SUPER_MAGIC:
48      *type = FILE_SYSTEM_CODA;
49      break;
50    case HUGETLBFS_MAGIC:
51    case RAMFS_MAGIC:
52    case TMPFS_MAGIC:
53      *type = FILE_SYSTEM_MEMORY;
54      break;
55    case CGROUP_SUPER_MAGIC:
56      *type = FILE_SYSTEM_CGROUP;
57      break;
58    default:
59      *type = FILE_SYSTEM_OTHER;
60  }
61  return true;
62}
63
64}  // namespace base
65