windows_attributes.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // windows_attributes ----------------------------------------------------------------//
  2. // Copyright Beman Dawes 2010
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. // Library home page: http://www.boost.org/libs/filesystem
  6. //--------------------------------------------------------------------------------------//
  7. // Useful for debugging status related issues //
  8. //--------------------------------------------------------------------------------------//
  9. #include <boost/filesystem.hpp>
  10. #include <boost/detail/lightweight_main.hpp>
  11. #include <windows.h>
  12. #include <map>
  13. #include <utility>
  14. #include <iostream>
  15. #include <string>
  16. using std::make_pair;
  17. namespace fs = boost::filesystem;
  18. int cpp_main( int argc, char* argv[])
  19. {
  20. typedef std::map<DWORD, std::string> decode_type;
  21. decode_type table;
  22. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_ARCHIVE, "FILE_ATTRIBUTE_ARCHIVE"));
  23. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_COMPRESSED, "FILE_ATTRIBUTE_COMPRESSED"));
  24. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_DEVICE, "FILE_ATTRIBUTE_DEVICE"));
  25. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_DIRECTORY, "FILE_ATTRIBUTE_DIRECTORY"));
  26. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_ENCRYPTED, "FILE_ATTRIBUTE_ENCRYPTED"));
  27. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_HIDDEN, "FILE_ATTRIBUTE_HIDDEN"));
  28. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, "FILE_ATTRIBUTE_NOT_CONTENT_INDEXED"));
  29. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_OFFLINE, "FILE_ATTRIBUTE_OFFLINE"));
  30. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_READONLY, "FILE_ATTRIBUTE_READONLY"));
  31. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_REPARSE_POINT, "FILE_ATTRIBUTE_REPARSE_POINT"));
  32. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_SPARSE_FILE, "FILE_ATTRIBUTE_SPARSE_FILE"));
  33. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_SYSTEM, "FILE_ATTRIBUTE_SYSTEM"));
  34. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_TEMPORARY, "FILE_ATTRIBUTE_TEMPORARY"));
  35. table.insert(make_pair<DWORD, std::string>(FILE_ATTRIBUTE_VIRTUAL, "FILE_ATTRIBUTE_VIRTUAL"));
  36. if (argc < 2)
  37. {
  38. std::cout << "Usage: windows_attributes path\n";
  39. return 1;
  40. }
  41. // report Win32 ::GetFileAttributesA()
  42. DWORD at(::GetFileAttributesA(argv[1]));
  43. if (at == INVALID_FILE_ATTRIBUTES)
  44. {
  45. std::cout << "GetFileAttributes(\"" << argv[1]
  46. << "\") returned INVALID_FILE_ATTRIBUTES\n";
  47. return 0;
  48. }
  49. std::cout << "GetFileAttributes(\"" << argv[1]
  50. << "\") returned ";
  51. bool bar = false;
  52. for (decode_type::iterator it = table.begin(); it != table.end(); ++it)
  53. {
  54. if (!(it->first & at))
  55. continue;
  56. if (bar)
  57. std::cout << " | ";
  58. bar = true;
  59. std::cout << it->second;
  60. at &= ~it->first;
  61. }
  62. std::cout << std::endl;
  63. if (at)
  64. std::cout << "plus unknown attributes " << at << std::endl;
  65. // report Boost Filesystem file_type
  66. fs::file_status stat = fs::status(argv[1]);
  67. const char* types[] =
  68. {
  69. "status_error",
  70. "file_not_found",
  71. "regular_file",
  72. "directory_file",
  73. // the following may not apply to some operating systems or file systems
  74. "symlink_file",
  75. "block_file",
  76. "character_file",
  77. "fifo_file",
  78. "socket_file",
  79. "reparse_file", // Windows: FILE_ATTRIBUTE_REPARSE_POINT that is not a symlink
  80. "type_unknown", // file does exist", but isn't one of the above types or
  81. // we don't have strong enough permission to find its type
  82. "_detail_directory_symlink" // internal use only; never exposed to users
  83. };
  84. std::cout << "boost::filesystem::status().type() is " << types[stat.type()] << std::endl;
  85. return 0;
  86. }