davemoore@chromium.org | c3b9ec6 | 2011-03-25 04:45:02 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame^] | 5 | #include "base/files/file_util.h" |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 6 | |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 7 | #include <errno.h> |
thestig@chromium.org | 772e4fe | 2014-02-06 19:41:03 +0900 | [diff] [blame] | 8 | #include <linux/magic.h> |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 9 | #include <sys/vfs.h> |
| 10 | |
thestig@chromium.org | 772e4fe | 2014-02-06 19:41:03 +0900 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
| 12 | |
mostynb@opera.com | f0672de | 2014-02-21 02:06:03 +0900 | [diff] [blame] | 13 | // 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 |
thestig@chromium.org | eac386c | 2014-02-21 10:36:23 +0900 | [diff] [blame] | 21 | #ifndef RAMFS_MAGIC |
| 22 | #define RAMFS_MAGIC 0x858458f6 |
| 23 | #endif |
mostynb@opera.com | f0672de | 2014-02-21 02:06:03 +0900 | [diff] [blame] | 24 | #ifndef TMPFS_MAGIC |
| 25 | #define TMPFS_MAGIC 0x01021994 |
| 26 | #endif |
| 27 | |
brettw@chromium.org | aa82a77 | 2014-03-14 02:26:21 +0900 | [diff] [blame] | 28 | namespace base { |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 29 | |
brettw@chromium.org | aa82a77 | 2014-03-14 02:26:21 +0900 | [diff] [blame] | 30 | bool GetFileSystemType(const FilePath& path, FileSystemType* type) { |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 31 | struct statfs statfs_buf; |
| 32 | if (statfs(path.value().c_str(), &statfs_buf) < 0) { |
| 33 | if (errno == ENOENT) |
| 34 | return false; |
| 35 | *type = FILE_SYSTEM_UNKNOWN; |
| 36 | return true; |
| 37 | } |
| 38 | |
thestig@chromium.org | 772e4fe | 2014-02-06 19:41:03 +0900 | [diff] [blame] | 39 | // Not all possible |statfs_buf.f_type| values are in linux/magic.h. |
| 40 | // Missing values are copied from the statfs man page. |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 41 | switch (statfs_buf.f_type) { |
| 42 | case 0: |
| 43 | *type = FILE_SYSTEM_0; |
| 44 | break; |
thestig@chromium.org | 772e4fe | 2014-02-06 19:41:03 +0900 | [diff] [blame] | 45 | case EXT2_SUPER_MAGIC: // Also ext3 and ext4 |
| 46 | case MSDOS_SUPER_MAGIC: |
| 47 | case REISERFS_SUPER_MAGIC: |
| 48 | case BTRFS_SUPER_MAGIC: |
| 49 | case 0x5346544E: // NTFS |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 50 | case 0x58465342: // XFS |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 51 | case 0x3153464A: // JFS |
| 52 | *type = FILE_SYSTEM_ORDINARY; |
| 53 | break; |
thestig@chromium.org | 772e4fe | 2014-02-06 19:41:03 +0900 | [diff] [blame] | 54 | case NFS_SUPER_MAGIC: |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 55 | *type = FILE_SYSTEM_NFS; |
| 56 | break; |
thestig@chromium.org | 772e4fe | 2014-02-06 19:41:03 +0900 | [diff] [blame] | 57 | case SMB_SUPER_MAGIC: |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 58 | case 0xFF534D42: // CIFS |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 59 | *type = FILE_SYSTEM_SMB; |
| 60 | break; |
thestig@chromium.org | 772e4fe | 2014-02-06 19:41:03 +0900 | [diff] [blame] | 61 | case CODA_SUPER_MAGIC: |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 62 | *type = FILE_SYSTEM_CODA; |
| 63 | break; |
thestig@chromium.org | eac386c | 2014-02-21 10:36:23 +0900 | [diff] [blame] | 64 | case HUGETLBFS_MAGIC: |
| 65 | case RAMFS_MAGIC: |
thestig@chromium.org | 772e4fe | 2014-02-06 19:41:03 +0900 | [diff] [blame] | 66 | case TMPFS_MAGIC: |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 67 | *type = FILE_SYSTEM_MEMORY; |
| 68 | break; |
thestig@chromium.org | 772e4fe | 2014-02-06 19:41:03 +0900 | [diff] [blame] | 69 | case CGROUP_SUPER_MAGIC: |
davemoore@chromium.org | c3b9ec6 | 2011-03-25 04:45:02 +0900 | [diff] [blame] | 70 | *type = FILE_SYSTEM_CGROUP; |
| 71 | break; |
evan@chromium.org | 33b7705 | 2010-08-20 09:36:01 +0900 | [diff] [blame] | 72 | default: |
| 73 | *type = FILE_SYSTEM_OTHER; |
| 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
brettw@chromium.org | aa82a77 | 2014-03-14 02:26:21 +0900 | [diff] [blame] | 78 | } // namespace base |