1 /* Copyright (C) 2010 Wildfire Games.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "precompiled.h"
24 #include "lib/sysdep/filesystem.h"
25 
26 #include "lib/sysdep/cpu.h"	// cpu_CAS
27 #include "lib/sysdep/os/win/wutil.h"	// StatusFromWin
28 #include "lib/sysdep/os/win/wposix/waio.h"	// waio_reopen
29 #include "lib/sysdep/os/win/wposix/wtime_internal.h"	// wtime_utc_filetime_to_time_t
30 #include "lib/sysdep/os/win/wposix/crt_posix.h"			// _close, _lseeki64 etc.
31 
32 
33 //-----------------------------------------------------------------------------
34 // WDIR suballocator
35 //-----------------------------------------------------------------------------
36 
37 // most applications only need a single WDIR at a time. we avoid expensive
38 // heap allocations by reusing a single static instance. if it is already
39 // in use, we allocate further instances dynamically.
40 // NB: this is thread-safe due to CAS.
41 
42 struct WDIR	// POD
43 {
44 	HANDLE hFind;
45 
46 	WIN32_FIND_DATAW findData;	// indeterminate if hFind == INVALID_HANDLE_VALUE
47 
48 	// wreaddir will return the address of this member.
49 	// (must be stored in WDIR to allow multiple independent
50 	// wopendir/wreaddir sequences).
51 	struct wdirent ent;
52 
53 	// used by wreaddir to skip the first FindNextFileW. (a counter is
54 	// easy to test/update and also provides useful information.)
55 	size_t numCalls;
56 };
57 
58 static WDIR wdir_storage;
59 static volatile intptr_t wdir_in_use;
60 
wdir_alloc()61 static inline WDIR* wdir_alloc()
62 {
63 	if(cpu_CAS(&wdir_in_use, 0, 1))	// gained ownership
64 		return &wdir_storage;
65 
66 	// already in use (rare) - allocate from heap
67 	return new WDIR;
68 }
69 
wdir_free(WDIR * d)70 static inline void wdir_free(WDIR* d)
71 {
72 	if(d == &wdir_storage)
73 	{
74 		const bool ok = cpu_CAS(&wdir_in_use, 1, 0);	// relinquish ownership
75 		ENSURE(ok);	// ensure it wasn't double-freed
76 	}
77 	else	// allocated from heap
78 		delete d;
79 }
80 
81 
82 //-----------------------------------------------------------------------------
83 // dirent.h
84 //-----------------------------------------------------------------------------
85 
IsValidDirectory(const OsPath & path)86 static bool IsValidDirectory(const OsPath& path)
87 {
88 	const DWORD fileAttributes = GetFileAttributesW(OsString(path).c_str());
89 
90 	// path not found
91 	if(fileAttributes == INVALID_FILE_ATTRIBUTES)
92 		return false;
93 
94 	// not a directory
95 	if((fileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
96 		return false;
97 
98 	// NB: no longer reject hidden or system attributes since
99 	// wsnd's add_oal_dlls_in_dir opens the Windows system directory,
100 	// which sometimes has these attributes set.
101 
102 	return true;
103 }
104 
105 
wopendir(const OsPath & path)106 WDIR* wopendir(const OsPath& path)
107 {
108 	WinScopedPreserveLastError s;
109 
110 	if(!IsValidDirectory(path))
111 	{
112 		errno = ENOENT;
113 		return 0;
114 	}
115 
116 	WDIR* d = wdir_alloc();
117 	d->numCalls = 0;
118 
119 	// NB: "c:\\path" only returns information about that directory;
120 	// trailing slashes aren't allowed. append "\\*" to retrieve its entries.
121 	OsPath searchPath = path/"*";
122 
123 	// (we don't defer FindFirstFileW until wreaddir because callers
124 	// expect us to return 0 if directory reading will/did fail.)
125 	d->hFind = FindFirstFileW(OsString(searchPath).c_str(), &d->findData);
126 	if(d->hFind != INVALID_HANDLE_VALUE)
127 		return d;	// success
128 	if(GetLastError() == ERROR_NO_MORE_FILES)
129 		return d;	// success, but directory is empty
130 
131 	Status status = StatusFromWin();
132 
133 	// release the WDIR allocated above (this is preferable to
134 	// always copying the large WDIR or findData from a temporary)
135 	wdir_free(d);
136 
137 	WARN_IF_ERR(status);
138 	errno = ErrnoFromStatus(status);
139 
140 	return 0;
141 }
142 
143 
wreaddir(WDIR * d)144 struct wdirent* wreaddir(WDIR* d)
145 {
146 	// directory is empty and d->findData is indeterminate
147 	if(d->hFind == INVALID_HANDLE_VALUE)
148 		return 0;
149 
150 	WinScopedPreserveLastError s;
151 
152 	// until end of directory or a valid entry was found:
153 	for(;;)
154 	{
155 		if(d->numCalls++ != 0)	// (skip first call to FindNextFileW - see wopendir)
156 		{
157 			if(!FindNextFileW(d->hFind, &d->findData))
158 			{
159 				if(GetLastError() == ERROR_NO_MORE_FILES)
160 					SetLastError(0);
161 				else	// unexpected error
162 					DEBUG_WARN_ERR(StatusFromWin());
163 				return 0;	// end of directory or error
164 			}
165 		}
166 
167 		// only accept non-hidden and non-system entries - otherwise,
168 		// callers might encounter errors when attempting to open them.
169 		if((d->findData.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)) == 0)
170 		{
171 			d->ent.d_name = d->findData.cFileName;	// (NB: d_name is a pointer)
172 			return &d->ent;
173 		}
174 	}
175 }
176 
177 
wreaddir_stat_np(WDIR * d,struct stat * s)178 int wreaddir_stat_np(WDIR* d, struct stat* s)
179 {
180 	// NTFS stores UTC but FAT stores local times, which are incorrectly
181 	// translated to UTC based on the _current_ DST settings. we no longer
182 	// bother checking the filesystem, since that's either unreliable or
183 	// expensive. timestamps may therefore be off after a DST transition,
184 	// which means our cached files would be regenerated.
185 	FILETIME* filetime = &d->findData.ftLastWriteTime;
186 
187 	memset(s, 0, sizeof(*s));
188 	s->st_size  = (off_t)u64_from_u32(d->findData.nFileSizeHigh, d->findData.nFileSizeLow);
189 	s->st_mode  = (unsigned short)((d->findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)? S_IFDIR : S_IFREG);
190 	s->st_mtime = wtime_utc_filetime_to_time_t(filetime);
191 	return 0;
192 }
193 
194 
wclosedir(WDIR * d)195 int wclosedir(WDIR* d)
196 {
197 	FindClose(d->hFind);
198 
199 	wdir_free(d);
200 	return 0;
201 }
202 
203 
204 //-----------------------------------------------------------------------------
205 // fcntl.h
206 //-----------------------------------------------------------------------------
207 
wopen(const OsPath & pathname,int oflag)208 int wopen(const OsPath& pathname, int oflag)
209 {
210 	ENSURE(!(oflag & O_CREAT));	// must specify mode_arg if O_CREAT
211 	return wopen(OsString(pathname).c_str(), oflag, _S_IREAD|_S_IWRITE);
212 }
213 
214 
wopen(const OsPath & pathname,int oflag,mode_t mode)215 int wopen(const OsPath& pathname, int oflag, mode_t mode)
216 {
217 	if(oflag & O_DIRECT)
218 	{
219 		Status ret = waio_open(pathname, oflag);
220 		if(ret < 0)
221 		{
222 			errno = ErrnoFromStatus(ret);
223 			return -1;
224 		}
225 		return (int)ret;	// file descriptor
226 	}
227 	else
228 	{
229 		WinScopedPreserveLastError s;	// _wsopen_s's CreateFileW
230 		int fd;
231 		oflag |= _O_BINARY;
232 		if(oflag & O_WRONLY)
233 			oflag |= O_CREAT|O_TRUNC;
234 		// NB: _wsopen_s ignores mode unless oflag & O_CREAT
235 		errno_t ret = _wsopen_s(&fd, OsString(pathname).c_str(), oflag, _SH_DENYRD, mode);
236 		if(ret != 0)
237 		{
238 			errno = ret;
239 			return -1;	// NOWARN
240 		}
241 		return fd;
242 	}
243 }
244 
245 
wclose(int fd)246 int wclose(int fd)
247 {
248 	ENSURE(fd >= 3);	// not invalid nor stdin/out/err
249 
250 	if(waio_close(fd) != 0)
251 		return _close(fd);
252 	return 0;
253 }
254 
255 
256 //-----------------------------------------------------------------------------
257 // unistd.h
258 //-----------------------------------------------------------------------------
259 
260 // we don't want to #define read to _read, since that's a fairly common
261 // identifier. therefore, translate from MS CRT names via thunk functions.
262 // efficiency is less important, and the overhead could be optimized away.
263 
read(int fd,void * buf,size_t nbytes)264 int read(int fd, void* buf, size_t nbytes)
265 {
266 	return _read(fd, buf, (int)nbytes);
267 }
268 
write(int fd,void * buf,size_t nbytes)269 int write(int fd, void* buf, size_t nbytes)
270 {
271 	return _write(fd, buf, (int)nbytes);
272 }
273 
lseek(int fd,off_t ofs,int whence)274 off_t lseek(int fd, off_t ofs, int whence)
275 {
276 	return _lseeki64(fd, ofs, whence);
277 }
278 
279 
wtruncate(const OsPath & pathname,off_t length)280 int wtruncate(const OsPath& pathname, off_t length)
281 {
282 	// (re-open the file to avoid the FILE_FLAG_NO_BUFFERING
283 	// sector-alignment restriction)
284 	HANDLE hFile = CreateFileW(OsString(pathname).c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
285 	ENSURE(hFile != INVALID_HANDLE_VALUE);
286 	LARGE_INTEGER ofs; ofs.QuadPart = length;
287 	WARN_IF_FALSE(SetFilePointerEx(hFile, ofs, 0, FILE_BEGIN));
288 	WARN_IF_FALSE(SetEndOfFile(hFile));
289 	WARN_IF_FALSE(CloseHandle(hFile));
290 	return 0;
291 }
292 
293 
wunlink(const OsPath & pathname)294 int wunlink(const OsPath& pathname)
295 {
296 	return _wunlink(OsString(pathname).c_str());
297 }
298 
299 
wrmdir(const OsPath & path)300 int wrmdir(const OsPath& path)
301 {
302 	return _wrmdir(OsString(path).c_str());
303 }
304 
305 
wrename(const OsPath & pathnameOld,const OsPath & pathnameNew)306 int wrename(const OsPath& pathnameOld, const OsPath& pathnameNew)
307 {
308 	return _wrename(OsString(pathnameOld).c_str(), OsString(pathnameNew).c_str());
309 }
310 
311 
wrealpath(const OsPath & pathname)312 OsPath wrealpath(const OsPath& pathname)
313 {
314 	wchar_t resolved[PATH_MAX];
315 	if(!GetFullPathNameW(OsString(pathname).c_str(), PATH_MAX, resolved, 0))
316 		return OsPath();
317 	return resolved;
318 }
319 
320 
ErrnoFromCreateDirectory()321 static int ErrnoFromCreateDirectory()
322 {
323 	switch(GetLastError())
324 	{
325 	case ERROR_ALREADY_EXISTS:
326 		return EEXIST;
327 	case ERROR_PATH_NOT_FOUND:
328 		return ENOENT;
329 	case ERROR_ACCESS_DENIED:
330 		return EACCES;
331 	case ERROR_WRITE_PROTECT:
332 		return EROFS;
333 	case ERROR_DIRECTORY:
334 		return ENOTDIR;
335 	default:
336 		return 0;
337 	}
338 }
339 
wmkdir(const OsPath & path,mode_t UNUSED (mode))340 int wmkdir(const OsPath& path, mode_t UNUSED(mode))
341 {
342 	if(!CreateDirectoryW(OsString(path).c_str(), (LPSECURITY_ATTRIBUTES)NULL))
343 	{
344 		errno = ErrnoFromCreateDirectory();
345 		return -1;
346 	}
347 
348 	return 0;
349 }
350 
351 
wstat(const OsPath & pathname,struct stat * buf)352 int wstat(const OsPath& pathname, struct stat* buf)
353 {
354 	return _wstat64(OsString(pathname).c_str(), buf);
355 }
356