file_util_linux.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include "base/files/file_util.h"
  5. #include <errno.h>
  6. #include <linux/magic.h>
  7. #include <sys/vfs.h>
  8. #include "base/files/file_path.h"
  9. namespace base {
  10. bool GetFileSystemType(const FilePath& path, FileSystemType* type) {
  11. struct statfs statfs_buf;
  12. if (statfs(path.value().c_str(), &statfs_buf) < 0) {
  13. if (errno == ENOENT)
  14. return false;
  15. *type = FILE_SYSTEM_UNKNOWN;
  16. return true;
  17. }
  18. // Not all possible |statfs_buf.f_type| values are in linux/magic.h.
  19. // Missing values are copied from the statfs man page.
  20. switch (statfs_buf.f_type) {
  21. case 0:
  22. *type = FILE_SYSTEM_0;
  23. break;
  24. case EXT2_SUPER_MAGIC: // Also ext3 and ext4
  25. case MSDOS_SUPER_MAGIC:
  26. case REISERFS_SUPER_MAGIC:
  27. case static_cast<int>(BTRFS_SUPER_MAGIC):
  28. case 0x5346544E: // NTFS
  29. case 0x58465342: // XFS
  30. case 0x3153464A: // JFS
  31. *type = FILE_SYSTEM_ORDINARY;
  32. break;
  33. case NFS_SUPER_MAGIC:
  34. *type = FILE_SYSTEM_NFS;
  35. break;
  36. case SMB_SUPER_MAGIC:
  37. case static_cast<int>(0xFF534D42): // CIFS
  38. *type = FILE_SYSTEM_SMB;
  39. break;
  40. case CODA_SUPER_MAGIC:
  41. *type = FILE_SYSTEM_CODA;
  42. break;
  43. case static_cast<int>(HUGETLBFS_MAGIC):
  44. case static_cast<int>(RAMFS_MAGIC):
  45. case TMPFS_MAGIC:
  46. *type = FILE_SYSTEM_MEMORY;
  47. break;
  48. case CGROUP_SUPER_MAGIC:
  49. *type = FILE_SYSTEM_CGROUP;
  50. break;
  51. default:
  52. *type = FILE_SYSTEM_OTHER;
  53. }
  54. return true;
  55. }
  56. } // namespace base