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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! Provides File Management functions

use std::ffi;
use std::os::windows::ffi::{
    OsStrExt,
    OsStringExt
};
use core::{default, ptr, mem, convert};

use crate::sys::*;
use crate::utils::{self, Result};

const NO_MORE_FILES: i32 = ERROR_NO_MORE_FILES as i32;

///Level of information to store about file during search.
pub enum FileInfoLevel {
    ///Corresponds to FindExInfoStandard. Default.
    Standard,
    ///Corresponds to FindExInfoBasic.
    Basic,
    ///Corresponds to FindExInfoMaxInfoLevel.
    Max
}

impl convert::Into<FINDEX_INFO_LEVELS> for FileInfoLevel {
    fn into(self) -> FINDEX_INFO_LEVELS {
        match self {
            FileInfoLevel::Standard => FindExInfoStandard,
            FileInfoLevel::Basic => FindExInfoBasic,
            FileInfoLevel::Max => FindExInfoMaxInfoLevel
        }
    }
}

impl default::Default for FileInfoLevel {
    fn default() -> Self {
        FileInfoLevel::Standard
    }
}

///File search type
pub enum FileSearchType {
    ///Search file by name. Corresponds to FindExSearchNameMatch. Default.
    NameMatch,
    ///Ask to search directories only. Corresponds to FindExSearchLimitToDirectories.
    ///
    ///Note that this flag may be ignored by OS.
    DirectoriesOnly
}

impl convert::Into<FINDEX_SEARCH_OPS> for FileSearchType {
    fn into(self) -> FINDEX_SEARCH_OPS {
        match self {
            FileSearchType::NameMatch => FindExSearchNameMatch,
            FileSearchType::DirectoriesOnly => FindExSearchLimitToDirectories
        }
    }
}

impl default::Default for FileSearchType {
    fn default() -> Self {
        FileSearchType::NameMatch
    }
}

///File System Entry.
pub struct Entry(WIN32_FIND_DATAW);

impl Entry {
    ///Determines whether Entry is directory or not.
    pub fn is_dir(&self) -> bool {
        (self.0.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0
    }

    ///Determines whether Entry is file or not.
    pub fn is_file(&self) -> bool {
        !self.is_dir()
    }

    ///Returns size of entry
    pub fn size(&self) -> u64 {
        ((self.0.nFileSizeHigh as u64) << 32) | (self.0.nFileSizeLow as u64)
    }

    ///Returns whether Entry is read-only
    pub fn is_read_only(&self) -> bool {
        (self.0.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0
    }

    ///Returns name of Entry.
    pub fn name(&self) -> ffi::OsString {
        ffi::OsString::from_wide(match self.0.cFileName.iter().position(|c| *c == 0) {
            Some(n) => &self.0.cFileName[..n],
            None => &self.0.cFileName
        })
    }
}

///File System Search iterator.
pub struct Search(HANDLE);

impl Search {
    ///Creates new instance of Search.
    ///
    ///Due to the way how underlying WinAPI works first entry is also returned alongside it.
    pub fn new<T: ?Sized + AsRef<ffi::OsStr>>(name: &T, level: FileInfoLevel, typ: FileSearchType, flags: DWORD) -> Result<(Search, Entry)> {
        let mut utf16_buff: Vec<u16> = name.as_ref().encode_wide().collect();
        utf16_buff.push(0);

        let mut file_data: WIN32_FIND_DATAW = unsafe { mem::zeroed() };

        let result = unsafe {
            FindFirstFileExW(utf16_buff.as_ptr(),
                             level.into(),
                             &mut file_data as *mut _ as *mut c_void,
                             typ.into(),
                             ptr::null_mut(),
                             flags)
        };

        if result == INVALID_HANDLE_VALUE {
            Err(utils::get_last_error())
        }
        else {
            Ok((Search(result), Entry(file_data)))
        }
    }

    ///Attempts to search again.
    pub fn again(&self) -> Result<Entry> {
        let mut file_data: WIN32_FIND_DATAW = unsafe { mem::zeroed() };

        unsafe {
            if FindNextFileW(self.0, &mut file_data) != 0 {
                Ok(Entry(file_data))
            }
            else {
                Err(utils::get_last_error())
            }
        }
    }

    ///Closes search.
    pub fn close(self) {
        unsafe {
            FindClose(self.0);
        }
    }
}

impl Iterator for Search {
    type Item = Result<Entry>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.again() {
            Ok(data) => Some(Ok(data)),
            Err(error) => {
                match error.raw_code() {
                    NO_MORE_FILES => None,
                    _ => Some(Err(error))
                }
            }
        }
    }
}

impl Drop for Search {
    fn drop(&mut self) {
        unsafe {
            debug_assert!(FindClose(self.0) != 0);
        }
    }
}