File: windows_attributes.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (106 lines) | stat: -rw-r--r-- 4,053 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//  windows_attributes  ----------------------------------------------------------------//

//  Copyright Beman Dawes 2010

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

//  Library home page: http://www.boost.org/libs/filesystem

//--------------------------------------------------------------------------------------//

//                   Useful for debugging status related issues                         //

//--------------------------------------------------------------------------------------//

#include <boost/filesystem.hpp>
#include <boost/detail/lightweight_main.hpp>
#include <windows.h>
#include <map>
#include <utility>
#include <iostream>
#include <string>

using std::make_pair;
namespace fs = boost::filesystem;

int cpp_main(int argc, char* argv[])
{
    typedef std::map< DWORD, std::string > decode_type;
    decode_type table;

    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_ARCHIVE, "FILE_ATTRIBUTE_ARCHIVE"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_COMPRESSED, "FILE_ATTRIBUTE_COMPRESSED"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_DEVICE, "FILE_ATTRIBUTE_DEVICE"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_DIRECTORY, "FILE_ATTRIBUTE_DIRECTORY"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_ENCRYPTED, "FILE_ATTRIBUTE_ENCRYPTED"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_HIDDEN, "FILE_ATTRIBUTE_HIDDEN"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, "FILE_ATTRIBUTE_NOT_CONTENT_INDEXED"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_OFFLINE, "FILE_ATTRIBUTE_OFFLINE"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_READONLY, "FILE_ATTRIBUTE_READONLY"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_REPARSE_POINT, "FILE_ATTRIBUTE_REPARSE_POINT"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_SPARSE_FILE, "FILE_ATTRIBUTE_SPARSE_FILE"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_SYSTEM, "FILE_ATTRIBUTE_SYSTEM"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_TEMPORARY, "FILE_ATTRIBUTE_TEMPORARY"));
    table.insert(make_pair< DWORD, std::string >(FILE_ATTRIBUTE_VIRTUAL, "FILE_ATTRIBUTE_VIRTUAL"));

    if (argc < 2)
    {
        std::cout << "Usage: windows_attributes path\n";
        return 1;
    }

    //  report Win32  ::GetFileAttributesA()

    DWORD at(::GetFileAttributesA(argv[1]));
    if (at == INVALID_FILE_ATTRIBUTES)
    {
        std::cout << "GetFileAttributes(\"" << argv[1]
                  << "\") returned INVALID_FILE_ATTRIBUTES\n";
        return 0;
    }

    std::cout << "GetFileAttributes(\"" << argv[1]
              << "\") returned ";

    bool bar = false;
    for (decode_type::iterator it = table.begin(); it != table.end(); ++it)
    {
        if (!(it->first & at))
            continue;
        if (bar)
            std::cout << " | ";
        bar = true;
        std::cout << it->second;
        at &= ~it->first;
    }
    std::cout << std::endl;

    if (at)
        std::cout << "plus unknown attributes " << at << std::endl;

    //  report Boost Filesystem file_type

    fs::file_status stat = fs::status(argv[1]);

    const char* types[] =
    {
        "status_error",
        "file_not_found",
        "regular_file",
        "directory_file",
        // the following may not apply to some operating systems or file systems
        "symlink_file",
        "block_file",
        "character_file",
        "fifo_file",
        "socket_file",
        "reparse_file", // Windows: FILE_ATTRIBUTE_REPARSE_POINT that is not a symlink
        "type_unknown"  // file does exist", but isn't one of the above types or
                        // we don't have strong enough permission to find its type
    };

    std::cout << "boost::filesystem::status().type() is " << types[stat.type()] << std::endl;

    return 0;
}