summaryrefslogtreecommitdiffstats
path: root/lib/libUPnP/Neptune/Source/System/WinRT/NptWinRtFile.cpp
blob: a9c76f12283b2621aba0359a5c22222097b8c181 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*****************************************************************
|
|   Neptune - Files :: WinRT Implementation
|
|   (c) 2001-2013 Gilles Boccon-Gibod
|   Author: Gilles Boccon-Gibod (bok@bok.net)
|
 ****************************************************************/

/*----------------------------------------------------------------------
|   includes
+---------------------------------------------------------------------*/
#include "NptWinRtPch.h"

#include "NptConfig.h"
#include "NptUtils.h"
#include "NptFile.h"
#include "NptStrings.h"
#include "NptDebug.h"
#include "NptWinRtUtils.h"

/*----------------------------------------------------------------------
|   MapError
+---------------------------------------------------------------------*/
static NPT_Result
MapError(DWORD err) {
    switch (err) {
      case ERROR_ALREADY_EXISTS:      return NPT_ERROR_FILE_ALREADY_EXISTS;
      case ERROR_PATH_NOT_FOUND:    
      case ERROR_FILE_NOT_FOUND:    
      case ERROR_INVALID_DRIVE:
      case ERROR_BAD_PATHNAME:
      case ERROR_BAD_NET_NAME:
      case ERROR_FILENAME_EXCED_RANGE:
      case ERROR_NO_MORE_FILES:
      case ERROR_BAD_NETPATH:         return NPT_ERROR_NO_SUCH_FILE;
      case ERROR_LOCK_VIOLATION:
      case ERROR_SEEK_ON_DEVICE:
      case ERROR_CURRENT_DIRECTORY:
      case ERROR_CANNOT_MAKE:
      case ERROR_FAIL_I24:
      case ERROR_NETWORK_ACCESS_DENIED:
      case ERROR_DRIVE_LOCKED:
      case ERROR_ACCESS_DENIED:       return NPT_ERROR_PERMISSION_DENIED;
      case ERROR_NOT_LOCKED:
      case ERROR_LOCK_FAILED:
      case ERROR_SHARING_VIOLATION:   return NPT_ERROR_FILE_BUSY;
      case ERROR_INVALID_FUNCTION:    return NPT_ERROR_INTERNAL;
      case ERROR_NOT_ENOUGH_QUOTA:    return NPT_ERROR_OUT_OF_MEMORY;
      case ERROR_ARENA_TRASHED:
      case ERROR_NOT_ENOUGH_MEMORY:
      case ERROR_INVALID_BLOCK:       return NPT_ERROR_OUT_OF_MEMORY;
      case ERROR_DISK_FULL:           return NPT_ERROR_FILE_NOT_ENOUGH_SPACE;
      case ERROR_TOO_MANY_OPEN_FILES: return NPT_ERROR_OUT_OF_RESOURCES;
      case ERROR_INVALID_HANDLE:      
      case ERROR_INVALID_ACCESS:
      case ERROR_INVALID_DATA:        return NPT_ERROR_INVALID_PARAMETERS;
      case ERROR_DIR_NOT_EMPTY:       return NPT_ERROR_DIRECTORY_NOT_EMPTY;
      case ERROR_NEGATIVE_SEEK:       return NPT_ERROR_OUT_OF_RANGE;
      default:                        return NPT_FAILURE;
}
}

#include <sys/stat.h>
#include <direct.h>

/*----------------------------------------------------------------------
|   NPT_stat_utf8
+---------------------------------------------------------------------*/
int
NPT_stat_utf8(const char* path, struct __stat64* info)
{
    NPT_WINRT_USE_CHAR_CONVERSION;
    return _wstat64(NPT_WINRT_A2W(path), info);
}

/*----------------------------------------------------------------------
|   NPT_fsopen_utf8
+---------------------------------------------------------------------*/
FILE*
NPT_fsopen_utf8(const char* path, const char* mode, int sh_flags)
{
    NPT_WINRT_USE_CHAR_CONVERSION;
    return _wfsopen(NPT_WINRT_A2W(path), NPT_WINRT_A2W(mode), sh_flags);
}

/*----------------------------------------------------------------------
|   NPT_FilePath::Separator
+---------------------------------------------------------------------*/
const char* const NPT_FilePath::Separator = "\\";

/*----------------------------------------------------------------------
|   NPT_File::GetRoots
+---------------------------------------------------------------------*/
NPT_Result
NPT_File::GetRoots(NPT_List<NPT_String>& roots)
{
    roots.Clear();
    return NPT_ERROR_NOT_IMPLEMENTED;
}

/*----------------------------------------------------------------------
|   NPT_File::GetWorkingDir
+---------------------------------------------------------------------*/
NPT_Result
NPT_File::GetWorkingDir(NPT_String& path)
{
    path.SetLength(0);
    return NPT_ERROR_NOT_IMPLEMENTED;
}

/*----------------------------------------------------------------------
|   NPT_File::GetInfo
+---------------------------------------------------------------------*/
NPT_Result
NPT_File::GetInfo(const char* path, NPT_FileInfo* info)
{
	DWORD           attributes = 0;
	WIN32_FILE_ATTRIBUTE_DATA attribute_data;

	NPT_WINRT_USE_CHAR_CONVERSION; 

	if (0 == GetFileAttributesEx(
		NPT_WINRT_A2W(path), 
		GetFileExInfoStandard, 
		&attribute_data)) {
		DWORD error_code = GetLastError();
        return NPT_FAILURE;
    }
	attributes = attribute_data.dwFileAttributes;
	if (attributes == INVALID_FILE_ATTRIBUTES) {
        return NPT_FAILURE;
	}

	if (info != NULL) {
		// (prasad) FIXME - fill in the attribute values in return value
	}

	return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_File::CreateDir
+---------------------------------------------------------------------*/
NPT_Result
NPT_File::CreateDir(const char* path)
{
    NPT_WINRT_USE_CHAR_CONVERSION;
    BOOL result = ::CreateDirectoryW(NPT_WINRT_A2W(path), NULL);
    if (result == 0) {
        return MapError(GetLastError());
    }
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_File::RemoveFile
+---------------------------------------------------------------------*/
NPT_Result
NPT_File::RemoveFile(const char* path)
{
    NPT_WINRT_USE_CHAR_CONVERSION;
    BOOL result = ::DeleteFileW(NPT_WINRT_A2W(path));
    if (result == 0) {
        return MapError(GetLastError());
    }
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_File::RemoveDir
+---------------------------------------------------------------------*/
NPT_Result
NPT_File::RemoveDir(const char* path)
{
    NPT_WINRT_USE_CHAR_CONVERSION;
    BOOL result = ::RemoveDirectoryW(NPT_WINRT_A2W(path));
    if (result == 0) {
        return MapError(GetLastError());
    }
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_File::Rename
+---------------------------------------------------------------------*/
NPT_Result
NPT_File::Rename(const char* from_path, const char* to_path)
{
    NPT_WINRT_USE_CHAR_CONVERSION;
	BOOL result = MoveFileEx(NPT_WINRT_A2W(from_path), 
		                     NPT_WINRT_A2W(to_path), 
							 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
    if (result == 0) {
        return MapError(GetLastError());
    }
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_File_ProcessFindData
+---------------------------------------------------------------------*/
static bool
NPT_File_ProcessFindData(WIN32_FIND_DATAW* find_data)
{
    NPT_WINRT_USE_CHAR_CONVERSION;

    // discard system specific files/shortcuts
    if (NPT_StringsEqual(NPT_WINRT_W2A(find_data->cFileName), ".") || 
        NPT_StringsEqual(NPT_WINRT_W2A(find_data->cFileName), "..")) {
        return false;
    }

    return true;
}

/*----------------------------------------------------------------------
|   NPT_File::ListDir
+---------------------------------------------------------------------*/
NPT_Result 
NPT_File::ListDir(const char*           path, 
                  NPT_List<NPT_String>& entries, 
                  NPT_Ordinal           start /* = 0 */, 
                  NPT_Cardinal          max   /* = 0 */)
{
    NPT_WINRT_USE_CHAR_CONVERSION;

    // default return value
    entries.Clear();

    // check the arguments
    if (path == NULL || path[0] == '\0') return NPT_ERROR_INVALID_PARAMETERS;

    // construct a path name with a \* wildcard at the end
    NPT_String path_pattern = path;
    if (path_pattern.EndsWith("\\") || path_pattern.EndsWith("/")) {
        path_pattern += "*";
    } else {
        path_pattern += "\\*";
    }

    // list the entries
    WIN32_FIND_DATAW find_data;
	HANDLE find_handle = FindFirstFileEx(NPT_WINRT_A2W(path_pattern.GetChars()), 
			                             FindExInfoStandard, 
									     &find_data, 
									     FindExSearchNameMatch,
									     NULL,
									     0);
    if (find_handle == INVALID_HANDLE_VALUE) return MapError(GetLastError());
    NPT_Cardinal count = 0;
    do {
        if (NPT_File_ProcessFindData(&find_data)) {
            // continue if we still have entries to skip
            if (start > 0) {
                --start;
                continue;
            }
            entries.Add(NPT_WINRT_W2A(find_data.cFileName));

            // stop when we have reached the maximum requested
            if (max && ++count == max) return NPT_SUCCESS;
        }
    } while (FindNextFileW(find_handle, &find_data));
    DWORD last_error = GetLastError();
    FindClose(find_handle);
    if (last_error != ERROR_NO_MORE_FILES) return MapError(last_error);

    return NPT_SUCCESS;
}