1 /**
2  * File stuff
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.sys.file;
15 
16 import core.stdc.wchar_;
17 import core.thread;
18 
19 import std.array;
20 import std.conv;
21 import std.file;
22 import std.path;
23 import std.range.primitives;
24 import std.stdio : File;
25 import std.string;
26 import std.typecons;
27 import std.utf;
28 
29 import ae.sys.cmd : getCurrentThreadID;
30 import ae.utils.path;
31 
32 public import std.typecons : No, Yes;
33 
34 alias wcscmp = core.stdc.wchar_.wcscmp;
35 alias wcslen = core.stdc.wchar_.wcslen;
36 
37 version(Windows) import ae.sys.windows.imports;
38 
39 // ************************************************************************
40 
41 version (Windows)
42 {
43 	// Work around std.file overload
44 	mixin(importWin32!(q{winnt}, null, q{FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_REPARSE_POINT}));
45 }
46 version (Posix)
47 {
48 	private import core.stdc.errno;
49 	private import core.sys.posix.dirent;
50 	private import core.stdc.string;
51 }
52 
53 // ************************************************************************
54 
55 deprecated string[] fastListDir(bool recursive = false, bool symlinks=false)(string pathname, string pattern = null)
56 {
57 	string[] result;
58 
59 	listDir!((e) {
60 		static if (!symlinks)
61 		{
62 			// Note: shouldn't this just skip recursion?
63 			if (e.isSymlink)
64 				return;
65 		}
66 
67 		if (pattern && !globMatch(e.baseName, pattern))
68 			return;
69 
70 		static if (recursive)
71 		{
72 			if (e.entryIsDir)
73 			{
74 				// Note: why exclude directories from results?
75 				e.recurse();
76 				return;
77 			}
78 		}
79 
80 		result ~= e.fullName;
81 	})(pathname);
82 	return result;
83 }
84 
85 // ************************************************************************
86 
87 version (Windows)
88 {
89 	mixin(importWin32!(q{winnt}, null, q{WCHAR}));
90 	mixin(importWin32!(q{winbase}, null, q{WIN32_FIND_DATAW}));
91 }
92 
93 /// The OS's "native" filesystem character type (private in Phobos).
94 version (Windows)
95 	alias FSChar = WCHAR;
96 else version (Posix)
97 	alias FSChar = char;
98 else
99 	static assert(0);
100 
101 /// Reads a time field from a stat_t with full precision (private in Phobos).
102 SysTime statTimeToStdTime(string which)(ref const stat_t statbuf)
103 {
104 	auto unixTime = mixin(`statbuf.st_` ~ which ~ `time`);
105 	auto stdTime = unixTimeToStdTime(unixTime);
106 
107 	static if (is(typeof(mixin(`statbuf.st_` ~ which ~ `tim`))))
108 		stdTime += mixin(`statbuf.st_` ~ which ~ `tim.tv_nsec`) / 100;
109 	else
110 	static if (is(typeof(mixin(`statbuf.st_` ~ which ~ `timensec`))))
111 		stdTime += mixin(`statbuf.st_` ~ which ~ `timensec`) / 100;
112 	else
113 	static if (is(typeof(mixin(`statbuf.st_` ~ which ~ `time_nsec`))))
114 		stdTime += mixin(`statbuf.st_` ~ which ~ `time_nsec`) / 100;
115 	else
116 	static if (is(typeof(mixin(`statbuf.__st_` ~ which ~ `timensec`))))
117 		stdTime += mixin(`statbuf.__st_` ~ which ~ `timensec`) / 100;
118 
119 	return SysTime(stdTime);
120 }
121 
122 version (OSX)
123     version = Darwin;
124 else version (iOS)
125     version = Darwin;
126 else version (TVOS)
127     version = Darwin;
128 else version (WatchOS)
129     version = Darwin;
130 
131 private
132 version (Posix)
133 {
134 	// TODO: upstream into Druntime
135 	extern (C)
136 	{
137 		int dirfd(DIR *dirp) pure nothrow @nogc;
138 		int openat(int fd, const char *path, int oflag, ...) nothrow @nogc;
139 
140 		version (Darwin)
141 		{
142 			pragma(mangle, "fstatat$INODE64")
143 			int fstatat(int fd, const char *path, stat_t *buf, int flag) nothrow @nogc;
144 
145 			pragma(mangle, "fdopendir$INODE64")
146 			DIR *fdopendir(int fd) nothrow @nogc;
147 		}
148 		else
149 		{
150 			int fstatat(int fd, const(char)* path, stat_t* buf, int flag) nothrow @nogc;
151 			DIR *fdopendir(int fd) nothrow @nogc;
152 		}
153 	}
154 	version (linux)
155 	{
156 		enum AT_SYMLINK_NOFOLLOW = 0x100;
157 		enum O_DIRECTORY = 0x10000;
158 	}
159 	version (Darwin)
160 	{
161 		enum AT_SYMLINK_NOFOLLOW = 0x20;
162 		enum O_DIRECTORY = 0x100000;
163 	}
164 	version (FreeBSD)
165 	{
166 		enum AT_SYMLINK_NOFOLLOW = 0x200;
167 		enum O_DIRECTORY = 0x20000;
168 	}
169 }
170 
171 import ae.utils.range : nullTerminated;
172 
173 // http://d.puremagic.com/issues/show_bug.cgi?id=7016
174 version (Windows) static import ae.sys.windows.misc;
175 
176 /// Fast templated directory iterator
177 template listDir(alias handler)
178 {
179 	/*non-static*/ struct Context
180 	{
181 		// Tether to handler alias context
182 		void callHandler(Entry* e) { handler(e); }
183 
184 		bool timeToStop = false;
185 
186 		FSChar[] pathBuf;
187 	}
188 
189 	static struct Entry
190 	{
191 		version (Posix)
192 		{
193 			dirent* ent;
194 
195 			stat_t[enumLength!StatTarget] statBuf;
196 			enum StatResult : int
197 			{
198 				noInfo = 0,
199 				statOK = int.max,
200 				unknownError = int.min,
201 				// other values are the same as errno
202 			}
203 		}
204 		version (Windows)
205 		{
206 			WIN32_FIND_DATAW findData;
207 		}
208 
209 		// Cleared (memset to 0) for every directory entry.
210 		struct Data
211 		{
212 			FSChar[] baseNameFS;
213 			string baseName;
214 			string fullName;
215 			size_t pathTailPos;
216 
217 			version (Posix)
218 			{
219 				StatResult[enumLength!StatTarget] statResult;
220 			}
221 		}
222 		Data data;
223 
224 		// Recursion
225 
226 		Entry* parent;
227 		Context* context;
228 
229 		version (Posix)
230 		{
231 			int dirFD;
232 
233 			void recurse()
234 			{
235 				import core.sys.posix.fcntl;
236 				int flags = O_RDONLY;
237 				static if (is(typeof(O_DIRECTORY)))
238 					flags |= O_DIRECTORY;
239 				auto fd = openat(dirFD, this.ent.d_name.ptr, flags);
240 				errnoEnforce(fd >= 0,
241 					"Failed to open %s as subdirectory of directory %s"
242 					.format(this.baseNameFS, this.parent.fullName));
243 				auto subdir = fdopendir(fd);
244 				errnoEnforce(subdir,
245 					"Failed to open subdirectory %s of directory %s as directory"
246 					.format(this.baseNameFS, this.parent.fullName));
247 				scan(subdir, fd, &this);
248 			}
249 		}
250 		version (Windows)
251 		{
252 			void recurse()
253 			{
254 				needFullPath();
255 				appendString(context.pathBuf,
256 					data.pathTailPos, "\\*.*\0"w);
257 				scan(&this);
258 			}
259 		}
260 
261 		void stop() { context.timeToStop = true; }
262 
263 		// Name
264 
265 		const(FSChar)* baseNameFSPtr() pure nothrow @nogc // fastest
266 		{
267 			version (Posix) return ent.d_name.ptr;
268 			version (Windows) return findData.cFileName.ptr;
269 		}
270 
271 		// Bounded variant of std.string.fromStringz for static arrays.
272 		private static T[] fromStringz(T, size_t n)(ref T[n] buf)
273 		{
274 			foreach (i, c; buf)
275 				if (!c)
276 					return buf[0 .. i];
277 			// This should only happen in case of an OS / libc bug.
278 			assert(false, "File name buffer is not null-terminated");
279 		}
280 
281 		const(FSChar)[] baseNameFS() pure nothrow @nogc // fast
282 		{
283 			if (!data.baseNameFS)
284 			{
285 				version (Posix) data.baseNameFS = fromStringz(ent.d_name);
286 				version (Windows) data.baseNameFS = fromStringz(findData.cFileName);
287 			}
288 			return data.baseNameFS;
289 		}
290 
291 		string baseName() // allocates
292 		{
293 			if (!data.baseName)
294 				data.baseName = baseNameFS.to!string;
295 			return data.baseName;
296 		}
297 
298 		private void needFullPath() nothrow @nogc
299 		{
300 			if (!data.pathTailPos)
301 			{
302 				version (Posix)
303 					parent.needFullPath();
304 				version (Windows)
305 				{
306 					// directory separator was added during recursion
307 					auto startPos = parent.data.pathTailPos + 1;
308 				}
309 				version (Posix)
310 				{
311 					immutable FSChar[] separator = "/";
312 					auto startPos = appendString(context.pathBuf,
313 						parent.data.pathTailPos, separator);
314 				}
315 				data.pathTailPos = appendString(context.pathBuf,
316 					startPos,
317 					baseNameFSPtr.nullTerminated
318 				);
319 			}
320 		}
321 
322 		const(FSChar)[] fullNameFS() nothrow @nogc // fast
323 		{
324 			needFullPath();
325 			return context.pathBuf[0 .. data.pathTailPos];
326 		}
327 
328 		string fullName() // allocates
329 		{
330 			if (!data.fullName)
331 				data.fullName = fullNameFS.to!string;
332 			return data.fullName;
333 		}
334 
335 		// Attributes
336 
337 		version (Posix)
338 		{
339 			enum StatTarget
340 			{
341 				dirEntry,   // do not dereference (lstat)
342 				linkTarget, // dereference
343 			}
344 			private bool tryStat(StatTarget target)() nothrow @nogc
345 			{
346 				if (data.statResult[target] == StatResult.noInfo)
347 				{
348 					// If we already did the other kind of stat, can we reuse its result?
349 					if (data.statResult[1 - target] != StatResult.noInfo)
350 					{
351 						// Yes, if we know this isn't a link from the directory entry.
352 						static if (__traits(compiles, ent.d_type))
353 							if (ent.d_type != DT_UNKNOWN && ent.d_type != DT_LNK)
354 								goto reuse;
355 						// Yes, if we already found out this isn't a link from an lstat call.
356 						static if (target == StatTarget.linkTarget)
357 							if (data.statResult[StatTarget.dirEntry] == StatResult.statOK
358 								&& (statBuf[StatTarget.dirEntry].st_mode & S_IFMT) != S_IFLNK)
359 								goto reuse;
360 					}
361 
362 					if (false)
363 					{
364 					reuse:
365 						statBuf[target] = statBuf[1 - target];
366 						data.statResult[target] = data.statResult[1 - target];
367 					}
368 					else
369 					{
370 						int flags = target == StatTarget.dirEntry ? AT_SYMLINK_NOFOLLOW : 0;
371 						auto res = fstatat(dirFD, ent.d_name.ptr, &statBuf[target], flags);
372 						if (res)
373 						{
374 							auto error = errno;
375 							data.statResult[target] = cast(StatResult)error;
376 							if (error == StatResult.noInfo || error == StatResult.statOK)
377 								data.statResult[target] = StatResult.unknownError; // unknown error?
378 						}
379 						else
380 							data.statResult[target] = StatResult.statOK; // no error
381 					}
382 				}
383 				return data.statResult[target] == StatResult.statOK;
384 			}
385 
386 			ErrnoException statError(StatTarget target)()
387 			{
388 				errno = data.statResult[target];
389 				return new ErrnoException("Failed to stat " ~
390 					(target == StatTarget.linkTarget ? "link target" : "directory entry") ~
391 					": " ~ fullName);
392 			}
393 
394 			stat_t* needStat(StatTarget target)()
395 			{
396 				if (!tryStat!target)
397 					throw statError!target();
398 				return &statBuf[target];
399 			}
400 
401 			// Check if this is an object of the given type.
402 			private bool deIsType(typeof(DT_REG) dType, typeof(S_IFREG) statType)
403 			{
404 				static if (__traits(compiles, ent.d_type))
405 					if (ent.d_type != DT_UNKNOWN)
406 						return ent.d_type == dType;
407 
408 				return (needStat!(StatTarget.dirEntry)().st_mode & S_IFMT) == statType;
409 			}
410 
411 			/// Returns true if this is a symlink.
412 			@property bool isSymlink()
413 			{
414 				return deIsType(DT_LNK, S_IFLNK);
415 			}
416 
417 			/// Returns true if this is a directory.
418 			/// You probably want to use this one to decide whether to recurse.
419 			@property bool entryIsDir()
420 			{
421 				return deIsType(DT_DIR, S_IFDIR);
422 			}
423 
424 			// Check if this is an object of the given type, or a link pointing to one.
425 			private bool ltIsType(typeof(DT_REG) dType, typeof(S_IFREG) statType)
426 			{
427 				static if (__traits(compiles, ent.d_type))
428 					if (ent.d_type != DT_UNKNOWN && ent.d_type != DT_LNK)
429 						return ent.d_type == dType;
430 
431 				if (tryStat!(StatTarget.linkTarget)())
432 					return (statBuf[StatTarget.linkTarget].st_mode & S_IFMT) == statType;
433 
434 				if (isSymlink()) // broken symlink?
435 					return false; // a broken symlink does not point at anything.
436 
437 				throw statError!(StatTarget.linkTarget)();
438 			}
439 
440 			/// Returns true if this is a file, or a link pointing to one.
441 			@property bool isFile()
442 			{
443 				return ltIsType(DT_REG, S_IFREG);
444 			}
445 
446 			/// Returns true if this is a directory, or a link pointing to one.
447 			@property bool isDir()
448 			{
449 				return ltIsType(DT_DIR, S_IFDIR);
450 			}
451 
452 			@property uint attributes()
453 			{
454 				return needStat!(StatTarget.linkTarget)().st_mode;
455 			}
456 
457 			@property uint linkAttributes()
458 			{
459 				return needStat!(StatTarget.dirEntry)().st_mode;
460 			}
461 
462 			// Other attributes
463 
464 			@property SysTime timeStatusChanged()
465 			{
466 				return statTimeToStdTime!"c"(*needStat!(StatTarget.linkTarget)());
467 			}
468 
469 			@property SysTime timeLastAccessed()
470 			{
471 				return statTimeToStdTime!"a"(*needStat!(StatTarget.linkTarget)());
472 			}
473 
474 			@property SysTime timeLastModified()
475 			{
476 				return statTimeToStdTime!"m"(*needStat!(StatTarget.linkTarget)());
477 			}
478 
479 			static if (is(typeof(&statTimeToStdTime!"birth")))
480 			@property SysTime timeCreated()
481 			{
482 				return statTimeToStdTime!"m"(*needStat!(StatTarget.linkTarget)());
483 			}
484 
485 			@property ulong size()
486 			{
487 				return needStat!(StatTarget.linkTarget)().st_size;
488 			}
489 
490 			@property ulong fileID()
491 			{
492 				static if (__traits(compiles, ent.d_ino))
493 					return ent.d_ino;
494 				else
495 					return needStat!(StatTarget.linkTarget)().st_ino;
496 			}
497 		}
498 
499 		version (Windows)
500 		{
501 			@property bool isDir() const pure nothrow
502 			{
503 				return (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
504 			}
505 
506 			@property bool isFile() const pure nothrow
507 			{
508 				return !isDir;
509 			}
510 
511 			@property bool isSymlink() const pure nothrow
512 			{
513 				return (findData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
514 			}
515 
516 			@property bool entryIsDir() const pure nothrow
517 			{
518 				return isDir && !isSymlink;
519 			}
520 
521 			@property ulong size() const pure nothrow
522 			{
523 				return makeUlong(findData.nFileSizeLow, findData.nFileSizeHigh);
524 			}
525 
526 			@property SysTime timeCreated() const
527 			{
528 				return FILETIMEToSysTime(&findData.ftCreationTime);
529 			}
530 
531 			@property SysTime timeLastAccessed() const
532 			{
533 				return FILETIMEToSysTime(&findData.ftLastAccessTime);
534 			}
535 
536 			@property SysTime timeLastModified() const
537 			{
538 				return FILETIMEToSysTime(&findData.ftLastWriteTime);
539 			}
540 
541 			@property ulong fileID()
542 			{
543 				return getFileID(fullName);
544 			}
545 		}
546 	}
547 
548 	version (Posix)
549 	{
550 		// The length of the buffer on the stack.
551 		enum initialPathBufLength = 256;
552 
553 		static void scan(DIR* dir, int dirFD, Entry* parentEntry)
554 		{
555 			Entry entry = void;
556 			entry.parent = parentEntry;
557 			entry.context = entry.parent.context;
558 			entry.dirFD = dirFD;
559 
560 			scope(exit) closedir(dir);
561 
562 			dirent* ent;
563 			while ((ent = readdir(dir)) != null)
564 			{
565 				// Apparently happens on some OS X versions.
566 				enforce(ent.d_name[0],
567 					"Empty dir entry name (OS bug?)");
568 
569 				// Skip "." and ".."
570 				if (ent.d_name[0] == '.' && (
571 						ent.d_name[1] == 0 ||
572 						(ent.d_name[1] == '.' && ent.d_name[2] == 0)))
573 					continue;
574 
575 				entry.ent = ent;
576 				entry.data = Entry.Data.init;
577 				entry.context.callHandler(&entry);
578 				if (entry.context.timeToStop)
579 					break;
580 			}
581 		}
582 	}
583 
584 	enum isPath(Path) = (isForwardRange!Path || isSomeString!Path) &&
585 		isSomeChar!(ElementEncodingType!Path);
586 
587 	import core.stdc.stdlib : malloc, realloc, free;
588 
589 	static FSChar[] reallocPathBuf(FSChar[] buf, size_t newLength) nothrow @nogc
590 	{
591 		if (buf.length == initialPathBufLength) // current buffer is on stack
592 		{
593 			auto ptr = cast(FSChar*) malloc(newLength * FSChar.sizeof);
594 			ptr[0 .. buf.length] = buf[];
595 			return ptr[0 .. newLength];
596 		}
597 		else // current buffer on C heap (malloc'd above)
598 		{
599 			auto ptr = cast(FSChar*) realloc(buf.ptr, newLength * FSChar.sizeof);
600 			return ptr[0 .. newLength];
601 		}
602 	}
603 
604 	// Append a string to the buffer, reallocating as necessary.
605 	// Returns the new length of the string in the buffer.
606 	static size_t appendString(Str)(ref FSChar[] buf, size_t pos, Str str) nothrow @nogc
607 	if (isPath!Str)
608 	{
609 		static if (ElementEncodingType!Str.sizeof == FSChar.sizeof
610 			&& is(typeof(str.length)))
611 		{
612 			// No transcoding needed and length known
613 			auto remainingSpace = buf.length - pos;
614 			if (str.length > remainingSpace)
615 				buf = reallocPathBuf(buf, (pos + str.length) * 3 / 2);
616 			buf[pos .. pos + str.length] = str[];
617 			pos += str.length;
618 		}
619 		else
620 		{
621 			// Need to transcode
622 			auto p = buf.ptr + pos;
623 			auto bufEnd = buf.ptr + buf.length;
624 			foreach (c; byUTF!FSChar(str))
625 			{
626 				if (p == bufEnd) // out of room
627 				{
628 					auto newBuf = reallocPathBuf(buf, buf.length * 3 / 2);
629 
630 					// Update pointers to point into the new buffer.
631 					p = newBuf.ptr + (p - buf.ptr);
632 					buf = newBuf;
633 					bufEnd = buf.ptr + buf.length;
634 				}
635 				*p++ = c;
636 			}
637 			pos = p - buf.ptr;
638 		}
639 		return pos;
640 	}
641 
642 	version (Windows)
643 	{
644 		mixin(importWin32!(q{winbase}));
645 		import ae.sys.windows.misc : makeUlong;
646 
647 		// The length of the buffer on the stack.
648 		enum initialPathBufLength = MAX_PATH;
649 
650 		enum FIND_FIRST_EX_LARGE_FETCH = 2;
651 		enum FindExInfoBasic = cast(FINDEX_INFO_LEVELS)1;
652 
653 		static void scan(Entry* parentEntry)
654 		{
655 			Entry entry = void;
656 			entry.parent = parentEntry;
657 			entry.context = parentEntry.context;
658 
659 			HANDLE hFind = FindFirstFileExW(
660 				entry.context.pathBuf.ptr,
661 				FindExInfoBasic,
662 				&entry.findData,
663 				FINDEX_SEARCH_OPS.FindExSearchNameMatch,
664 				null,
665 				FIND_FIRST_EX_LARGE_FETCH, // https://blogs.msdn.microsoft.com/oldnewthing/20131024-00/?p=2843
666 			);
667 			if (hFind == INVALID_HANDLE_VALUE)
668 				throw new WindowsException(GetLastError(),
669 					text("FindFirstFileW: ", parentEntry.fullNameFS));
670 			scope(exit) FindClose(hFind);
671 			do
672 			{
673 				// Skip "." and ".."
674 				auto fn = entry.findData.cFileName.ptr;
675 				if (fn[0] == '.' && (
676 						fn[1] == 0 ||
677 						(fn[1] == '.' && fn[2] == 0)))
678 					continue;
679 
680 				entry.data = Entry.Data.init;
681 				entry.context.callHandler(&entry);
682 				if (entry.context.timeToStop)
683 					break;
684 			}
685 			while (FindNextFileW(hFind, &entry.findData));
686 			if (GetLastError() != ERROR_NO_MORE_FILES)
687 				throw new WindowsException(GetLastError(),
688 					text("FindNextFileW: ", parentEntry.fullNameFS));
689 		}
690 	}
691 
692 	void listDir(Path)(Path dirPath)
693 	if (isPath!Path)
694 	{
695 		import std.internal.cstring;
696 
697 		if (dirPath.empty)
698 			return listDir(".");
699 
700 		Context context;
701 
702 		FSChar[initialPathBufLength] pathBufStore = void;
703 		context.pathBuf = pathBufStore[];
704 
705 		scope (exit)
706 		{
707 			if (context.pathBuf.length != initialPathBufLength)
708 				free(context.pathBuf.ptr);
709 		}
710 
711 		Entry rootEntry = void;
712 		rootEntry.context = &context;
713 
714 		auto endPos = appendString(context.pathBuf, 0, dirPath);
715 		rootEntry.data.pathTailPos = endPos - (endPos > 0 && context.pathBuf[endPos - 1].isDirSeparator() ? 1 : 0);
716 		assert(rootEntry.data.pathTailPos > 0);
717 
718 		version (Posix)
719 		{
720 			auto dir = opendir(tempCString(dirPath));
721 			checkDir(dir, dirPath);
722 
723 			scan(dir, dirfd(dir), &rootEntry);
724 		}
725 		else
726 		version (Windows)
727 		{
728 			const WCHAR[] tailString = endPos == 0 || context.pathBuf[endPos - 1].isDirSeparator() ? "*.*\0"w : "\\*.*\0"w;
729 			appendString(context.pathBuf, endPos, tailString);
730 
731 			scan(&rootEntry);
732 		}
733 	}
734 
735 	// Workaround for https://github.com/ldc-developers/ldc/issues/2960
736 	version (Posix)
737 	private void checkDir(Path)(DIR* dir, auto ref Path dirPath)
738 	{
739 		errnoEnforce(dir, "Failed to open directory " ~ dirPath);
740 	}
741 }
742 
743 unittest
744 {
745 	auto tmpDir = deleteme ~ "-dir";
746 	if (tmpDir.exists) tmpDir.removeRecurse();
747 	mkdirRecurse(tmpDir);
748 	scope(exit) rmdirRecurse(tmpDir);
749 
750 	touch(tmpDir ~ "/a");
751 	touch(tmpDir ~ "/b");
752 	mkdir(tmpDir ~ "/c");
753 	touch(tmpDir ~ "/c/1");
754 	touch(tmpDir ~ "/c/2");
755 
756 	string[] entries;
757 	listDir!((e) {
758 		assert(equal(e.fullNameFS, e.fullName));
759 		entries ~= e.fullName.fastRelativePath(tmpDir);
760 		if (e.entryIsDir)
761 			e.recurse();
762 	})(tmpDir);
763 
764 	assert(equal(
765 		entries.sort,
766 		["a", "b", "c", "c/1", "c/2"].map!(name => name.replace("/", dirSeparator)),
767 	), text(entries));
768 
769 	entries = null;
770 	import std.ascii : isDigit;
771 	listDir!((e) {
772 		entries ~= e.fullName.fastRelativePath(tmpDir);
773 		if (e.baseNameFS[0].isDigit)
774 			e.stop();
775 		else
776 		if (e.entryIsDir)
777 			e.recurse();
778 	})(tmpDir);
779 
780 	assert(entries.length < 5 && entries[$-1][$-1].isDigit, text(entries));
781 
782 	// Symlink test
783 	(){
784 		// Wine's implementation of symlinks/junctions is incomplete
785 		version (Windows)
786 			if (getWineVersion())
787 				return;
788 
789 		dirLink("c", tmpDir ~ "/d");
790 		dirLink("x", tmpDir ~ "/e");
791 
792 		string[] entries;
793 		listDir!((e) {
794 			entries ~= e.fullName.fastRelativePath(tmpDir);
795 			if (e.entryIsDir)
796 				e.recurse();
797 		})(tmpDir);
798 
799 		assert(equal(
800 			entries.sort,
801 			["a", "b", "c", "c/1", "c/2", "d", "e"].map!(name => name.replace("/", dirSeparator)),
802 		));
803 
804 		// Recurse into symlinks
805 
806 		entries = null;
807 		listDir!((e) {
808 			entries ~= e.fullName.fastRelativePath(tmpDir);
809 			if (e.isDir)
810 				try
811 					e.recurse();
812 				catch (Exception e) // broken junctions on Windows throw
813 					{}
814 		})(tmpDir);
815 
816 		assert(equal(
817 			entries.sort,
818 			["a", "b", "c", "c/1", "c/2", "d", "d/1", "d/2", "e"].map!(name => name.replace("/", dirSeparator)),
819 		));
820 	}();
821 }
822 
823 // ************************************************************************
824 
825 string buildPath2(string[] segments...) { return segments.length ? buildPath(segments) : null; }
826 
827 /// Shell-like expansion of ?, * and ** in path components
828 DirEntry[] fileList(string pattern)
829 {
830 	auto components = cast(string[])array(pathSplitter(pattern));
831 	foreach (i, component; components[0..$-1])
832 		if (component.contains("?") || component.contains("*")) // TODO: escape?
833 		{
834 			DirEntry[] expansions; // TODO: filter range instead?
835 			auto dir = buildPath2(components[0..i]);
836 			if (component == "**")
837 				expansions = array(dirEntries(dir, SpanMode.depth));
838 			else
839 				expansions = array(dirEntries(dir, component, SpanMode.shallow));
840 
841 			DirEntry[] result;
842 			foreach (expansion; expansions)
843 				if (expansion.isDir())
844 					result ~= fileList(buildPath(expansion.name ~ components[i+1..$]));
845 			return result;
846 		}
847 
848 	auto dir = buildPath2(components[0..$-1]);
849 	if (!dir || exists(dir))
850 		return array(dirEntries(dir, components[$-1], SpanMode.shallow));
851 	else
852 		return null;
853 }
854 
855 /// ditto
856 DirEntry[] fileList(string pattern0, string[] patterns...)
857 {
858 	DirEntry[] result;
859 	foreach (pattern; [pattern0] ~ patterns)
860 		result ~= fileList(pattern);
861 	return result;
862 }
863 
864 /// ditto
865 deprecated string[] fastFileList(string pattern)
866 {
867 	auto components = cast(string[])array(pathSplitter(pattern));
868 	foreach (i, component; components[0..$-1])
869 		if (component.contains("?") || component.contains("*")) // TODO: escape?
870 		{
871 			string[] expansions; // TODO: filter range instead?
872 			auto dir = buildPath2(components[0..i]);
873 			if (component == "**")
874 				expansions = fastListDir!true(dir);
875 			else
876 				expansions = fastListDir(dir, component);
877 
878 			string[] result;
879 			foreach (expansion; expansions)
880 				if (expansion.isDir())
881 					result ~= fastFileList(buildPath(expansion ~ components[i+1..$]));
882 			return result;
883 		}
884 
885 	auto dir = buildPath2(components[0..$-1]);
886 	if (!dir || exists(dir))
887 		return fastListDir(dir, components[$-1]);
888 	else
889 		return null;
890 }
891 
892 /// ditto
893 deprecated string[] fastFileList(string pattern0, string[] patterns...)
894 {
895 	string[] result;
896 	foreach (pattern; [pattern0] ~ patterns)
897 		result ~= fastFileList(pattern);
898 	return result;
899 }
900 
901 // ************************************************************************
902 
903 import std.datetime;
904 import std.exception;
905 
906 deprecated SysTime getMTime(string name)
907 {
908 	return timeLastModified(name);
909 }
910 
911 /// If target exists, update its modification time;
912 /// otherwise create it as an empty file.
913 void touch(in char[] target)
914 {
915 	if (exists(target))
916 	{
917 		auto now = Clock.currTime();
918 		setTimes(target, now, now);
919 	}
920 	else
921 		std.file.write(target, "");
922 }
923 
924 /// Returns true if the target file doesn't exist,
925 /// or source is newer than the target.
926 bool newerThan(string source, string target)
927 {
928 	if (!target.exists)
929 		return true;
930 	return source.timeLastModified() > target.timeLastModified();
931 }
932 
933 /// Returns true if the target file doesn't exist,
934 /// or any of the sources are newer than the target.
935 bool anyNewerThan(string[] sources, string target)
936 {
937 	if (!target.exists)
938 		return true;
939 	auto targetTime = target.timeLastModified();
940 	return sources.any!(source => source.timeLastModified() > targetTime)();
941 }
942 
943 version (Posix)
944 {
945 	import core.sys.posix.sys.stat;
946 	import core.sys.posix.unistd;
947 
948 	int getOwner(string fn)
949 	{
950 		stat_t s;
951 		errnoEnforce(stat(toStringz(fn), &s) == 0, "stat: " ~ fn);
952 		return s.st_uid;
953 	}
954 
955 	int getGroup(string fn)
956 	{
957 		stat_t s;
958 		errnoEnforce(stat(toStringz(fn), &s) == 0, "stat: " ~ fn);
959 		return s.st_gid;
960 	}
961 
962 	void setOwner(string fn, int uid, int gid)
963 	{
964 		errnoEnforce(chown(toStringz(fn), uid, gid) == 0, "chown: " ~ fn);
965 	}
966 }
967 
968 /// Try to rename; copy/delete if rename fails
969 void move(string src, string dst)
970 {
971 	try
972 		src.rename(dst);
973 	catch (Exception e)
974 	{
975 		atomicCopy(src, dst);
976 		src.remove();
977 	}
978 }
979 
980 /// Make sure that the given directory exists
981 /// (and create parent directories as necessary).
982 void ensureDirExists(string path)
983 {
984 	if (!path.exists)
985 		path.mkdirRecurse();
986 }
987 
988 /// Make sure that the path to the given file name
989 /// exists (and create directories as necessary).
990 void ensurePathExists(string fn)
991 {
992 	fn.dirName.ensureDirExists();
993 }
994 
995 import ae.utils.text;
996 
997 /// Forcibly remove a file or directory.
998 /// If atomic is true, the entire directory is deleted "atomically"
999 /// (it is first moved/renamed to another location).
1000 /// On Windows, this will move the file/directory out of the way,
1001 /// if it is in use and cannot be deleted (but can be renamed).
1002 void forceDelete(Flag!"atomic" atomic=Yes.atomic)(string fn, Flag!"recursive" recursive = No.recursive)
1003 {
1004 	import std.process : environment;
1005 	version(Windows)
1006 	{
1007 		mixin(importWin32!q{winnt});
1008 		mixin(importWin32!q{winbase});
1009 	}
1010 
1011 	auto name = fn.baseName();
1012 	fn = fn.absolutePath().longPath();
1013 
1014 	version(Windows)
1015 	{
1016 		auto fnW = toUTF16z(fn);
1017 		auto attr = GetFileAttributesW(fnW);
1018 		wenforce(attr != INVALID_FILE_ATTRIBUTES, "GetFileAttributes");
1019 		if (attr & FILE_ATTRIBUTE_READONLY)
1020 			SetFileAttributesW(fnW, attr & ~FILE_ATTRIBUTE_READONLY).wenforce("SetFileAttributes");
1021 	}
1022 
1023 	static if (atomic)
1024 	{
1025 		// To avoid zombifying locked directories, try renaming it first.
1026 		// Attempting to delete a locked directory will make it inaccessible.
1027 
1028 		bool tryMoveTo(string target)
1029 		{
1030 			target = target.longPath();
1031 			if (target.endsWith(dirSeparator))
1032 				target = target[0..$-1];
1033 			if (target.length && !target.exists)
1034 				return false;
1035 
1036 			string newfn;
1037 			do
1038 				newfn = format("%s%sdeleted-%s.%s.%s", target, dirSeparator, name, thisProcessID, randomString());
1039 			while (newfn.exists);
1040 
1041 			version(Windows)
1042 			{
1043 				auto newfnW = toUTF16z(newfn);
1044 				if (!MoveFileW(fnW, newfnW))
1045 					return false;
1046 			}
1047 			else
1048 			{
1049 				try
1050 					rename(fn, newfn);
1051 				catch (FileException e)
1052 					return false;
1053 			}
1054 
1055 			fn = newfn;
1056 			version(Windows) fnW = newfnW;
1057 			return true;
1058 		}
1059 
1060 		void tryMove()
1061 		{
1062 			auto tmp = environment.get("TEMP");
1063 			if (tmp)
1064 				if (tryMoveTo(tmp))
1065 					return;
1066 
1067 			version(Windows)
1068 				string tempDir = fn[0..7]~"Temp";
1069 			else
1070 				enum tempDir = "/tmp";
1071 
1072 			if (tryMoveTo(tempDir))
1073 				return;
1074 
1075 			if (tryMoveTo(fn.dirName()))
1076 				return;
1077 
1078 			throw new Exception("Unable to delete " ~ fn ~ " atomically (all rename attempts failed)");
1079 		}
1080 
1081 		tryMove();
1082 	}
1083 
1084 	version(Windows)
1085 	{
1086 		if (attr & FILE_ATTRIBUTE_DIRECTORY)
1087 		{
1088 			if (recursive && (attr & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
1089 			{
1090 				foreach (de; fn.dirEntries(SpanMode.shallow))
1091 					forceDelete!(No.atomic)(de.name, Yes.recursive);
1092 			}
1093 			// Will fail if !recursive and directory is not empty
1094 			RemoveDirectoryW(fnW).wenforce("RemoveDirectory");
1095 		}
1096 		else
1097 			DeleteFileW(fnW).wenforce("DeleteFile");
1098 	}
1099 	else
1100 	{
1101 		if (recursive)
1102 			fn.removeRecurse();
1103 		else
1104 			if (fn.isDir)
1105 				fn.rmdir();
1106 			else
1107 				fn.remove();
1108 	}
1109 }
1110 
1111 
1112 deprecated void forceDelete(bool atomic)(string fn, bool recursive = false) { forceDelete!(cast(Flag!"atomic")atomic)(fn, cast(Flag!"recursive")recursive); }
1113 //deprecated void forceDelete()(string fn, bool recursive) { forceDelete!(Yes.atomic)(fn, cast(Flag!"recursive")recursive); }
1114 
1115 deprecated unittest
1116 {
1117 	mkdir("testdir"); touch("testdir/b"); forceDelete!(false     )("testdir", true);
1118 	mkdir("testdir"); touch("testdir/b"); forceDelete!(true      )("testdir", true);
1119 }
1120 
1121 unittest
1122 {
1123 	mkdir("testdir"); touch("testdir/b"); forceDelete             ("testdir", Yes.recursive);
1124 	mkdir("testdir"); touch("testdir/b"); forceDelete!(No .atomic)("testdir", Yes.recursive);
1125 	mkdir("testdir"); touch("testdir/b"); forceDelete!(Yes.atomic)("testdir", Yes.recursive);
1126 }
1127 
1128 /// If fn is a directory, delete it recursively.
1129 /// Otherwise, delete the file or symlink fn.
1130 void removeRecurse(string fn)
1131 {
1132 	auto attr = fn.getAttributes();
1133 	if (attr.attrIsSymlink)
1134 	{
1135 		version (Windows)
1136 			if (attr.attrIsDir)
1137 				fn.rmdir();
1138 			else
1139 				fn.remove();
1140 		else
1141 			fn.remove();
1142 	}
1143 	else
1144 	if (attr.attrIsDir)
1145 		version (Windows)
1146 			fn.forceDelete!(No.atomic)(Yes.recursive); // For read-only files
1147 		else
1148 			fn.rmdirRecurse();
1149 	else
1150 		fn.remove();
1151 }
1152 
1153 /// Create an empty directory, deleting
1154 /// all its contents if it already exists.
1155 void recreateEmptyDirectory()(string dir)
1156 {
1157 	if (dir.exists)
1158 		dir.forceDelete(Yes.recursive);
1159 	mkdir(dir);
1160 }
1161 
1162 void copyRecurse(DirEntry src, string dst)
1163 {
1164 	version (Posix)
1165 		if (src.isSymlink)
1166 			return symlink(dst, readLink(src));
1167 	if (src.isFile)
1168 		return copy(src, dst, PreserveAttributes.yes);
1169 	dst.mkdir();
1170 	foreach (de; src.dirEntries(SpanMode.shallow))
1171 		copyRecurse(de, dst.buildPath(de.baseName));
1172 }
1173 void copyRecurse(string src, string dst) { copyRecurse(DirEntry(src), dst); }
1174 
1175 bool isHidden()(string fn)
1176 {
1177 	if (baseName(fn).startsWith("."))
1178 		return true;
1179 	version (Windows)
1180 	{
1181 		mixin(importWin32!q{winnt});
1182 		if (getAttributes(fn) & FILE_ATTRIBUTE_HIDDEN)
1183 			return true;
1184 	}
1185 	return false;
1186 }
1187 
1188 /// Return a file's unique ID.
1189 ulong getFileID()(string fn)
1190 {
1191 	version (Windows)
1192 	{
1193 		mixin(importWin32!q{winnt});
1194 		mixin(importWin32!q{winbase});
1195 
1196 		auto fnW = toUTF16z(fn);
1197 		auto h = CreateFileW(fnW, FILE_READ_ATTRIBUTES, 0, null, OPEN_EXISTING, 0, HANDLE.init);
1198 		wenforce(h!=INVALID_HANDLE_VALUE, fn);
1199 		scope(exit) CloseHandle(h);
1200 		BY_HANDLE_FILE_INFORMATION fi;
1201 		GetFileInformationByHandle(h, &fi).wenforce("GetFileInformationByHandle");
1202 
1203 		ULARGE_INTEGER li;
1204 		li.LowPart  = fi.nFileIndexLow;
1205 		li.HighPart = fi.nFileIndexHigh;
1206 		auto result = li.QuadPart;
1207 		enforce(result, "Null file ID");
1208 		return result;
1209 	}
1210 	else
1211 	{
1212 		return DirEntry(fn).statBuf.st_ino;
1213 	}
1214 }
1215 
1216 unittest
1217 {
1218 	touch("a");
1219 	scope(exit) remove("a");
1220 	hardLink("a", "b");
1221 	scope(exit) remove("b");
1222 	touch("c");
1223 	scope(exit) remove("c");
1224 	assert(getFileID("a") == getFileID("b"));
1225 	assert(getFileID("a") != getFileID("c"));
1226 }
1227 
1228 deprecated alias std.file.getSize getSize2;
1229 
1230 /// Using UNC paths bypasses path length limitation when using Windows wide APIs.
1231 string longPath(string s)
1232 {
1233 	version (Windows)
1234 	{
1235 		if (!s.startsWith(`\\`))
1236 			return `\\?\` ~ s.absolutePath().buildNormalizedPath().replace(`/`, `\`);
1237 	}
1238 	return s;
1239 }
1240 
1241 version (Windows)
1242 {
1243 	static if (__traits(compiles, { mixin importWin32!q{winnt}; }))
1244 		static mixin(importWin32!q{winnt});
1245 
1246 	void createReparsePoint(string reparseBufferName, string extraInitialization, string reparseTagName)(in char[] target, in char[] print, in char[] link)
1247 	{
1248 		mixin(importWin32!q{winbase});
1249 		mixin(importWin32!q{windef});
1250 		mixin(importWin32!q{winioctl});
1251 
1252 		enum SYMLINK_FLAG_RELATIVE = 1;
1253 
1254 		HANDLE hLink = CreateFileW(link.toUTF16z(), GENERIC_READ | GENERIC_WRITE, 0, null, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, null);
1255 		wenforce(hLink && hLink != INVALID_HANDLE_VALUE, "CreateFileW");
1256 		scope(exit) CloseHandle(hLink);
1257 
1258 		enum pathOffset =
1259 			mixin(q{REPARSE_DATA_BUFFER..} ~ reparseBufferName)            .offsetof +
1260 			mixin(q{REPARSE_DATA_BUFFER..} ~ reparseBufferName)._PathBuffer.offsetof;
1261 
1262 		auto targetW = target.toUTF16();
1263 		auto printW  = print .toUTF16();
1264 
1265 		// Despite MSDN, two NUL-terminating characters are needed, one for each string.
1266 
1267 		auto pathBufferSize = targetW.length + 1 + printW.length + 1; // in chars
1268 		auto buf = new ubyte[pathOffset + pathBufferSize * WCHAR.sizeof];
1269 		auto r = cast(REPARSE_DATA_BUFFER*)buf.ptr;
1270 
1271 		r.ReparseTag = mixin(reparseTagName);
1272 		r.ReparseDataLength = to!WORD(buf.length - mixin(q{r..} ~ reparseBufferName).offsetof);
1273 
1274 		auto pathBuffer = mixin(q{r..} ~ reparseBufferName).PathBuffer;
1275 		auto p = pathBuffer;
1276 
1277 		mixin(q{r..} ~ reparseBufferName).SubstituteNameOffset = to!WORD((p-pathBuffer) * WCHAR.sizeof);
1278 		mixin(q{r..} ~ reparseBufferName).SubstituteNameLength = to!WORD(targetW.length * WCHAR.sizeof);
1279 		p[0..targetW.length] = targetW;
1280 		p += targetW.length;
1281 		*p++ = 0;
1282 
1283 		mixin(q{r..} ~ reparseBufferName).PrintNameOffset      = to!WORD((p-pathBuffer) * WCHAR.sizeof);
1284 		mixin(q{r..} ~ reparseBufferName).PrintNameLength      = to!WORD(printW .length * WCHAR.sizeof);
1285 		p[0..printW.length] = printW;
1286 		p += printW.length;
1287 		*p++ = 0;
1288 
1289 		assert(p-pathBuffer == pathBufferSize);
1290 
1291 		mixin(extraInitialization);
1292 
1293 		DWORD dwRet; // Needed despite MSDN
1294 		DeviceIoControl(hLink, FSCTL_SET_REPARSE_POINT, buf.ptr, buf.length.to!DWORD(), null, 0, &dwRet, null).wenforce("DeviceIoControl");
1295 	}
1296 
1297 	void acquirePrivilege(S)(S name)
1298 	{
1299 		mixin(importWin32!q{winbase});
1300 		mixin(importWin32!q{windef});
1301 
1302 		import ae.sys.windows;
1303 
1304 		HANDLE hToken = null;
1305 		wenforce(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken));
1306 		scope(exit) CloseHandle(hToken);
1307 
1308 		TOKEN_PRIVILEGES tp;
1309 		wenforce(LookupPrivilegeValue(null, name.toUTF16z(), &tp.Privileges[0].Luid), "LookupPrivilegeValue");
1310 
1311 		tp.PrivilegeCount = 1;
1312 		tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1313 		wenforce(AdjustTokenPrivileges(hToken, FALSE, &tp, cast(DWORD)TOKEN_PRIVILEGES.sizeof, null, null), "AdjustTokenPrivileges");
1314 	}
1315 
1316 	/// Link a directory.
1317 	/// Uses symlinks on POSIX, and directory junctions on Windows.
1318 	void dirLink()(in char[] original, in char[] link)
1319 	{
1320 		mkdir(link);
1321 		scope(failure) rmdir(link);
1322 
1323 		auto target = `\??\` ~ (cast(string)original).absolutePath((cast(string)link.dirName).absolutePath).buildNormalizedPath;
1324 		if (target[$-1] != '\\')
1325 			target ~= '\\';
1326 
1327 		createReparsePoint!(q{MountPointReparseBuffer}, q{}, q{IO_REPARSE_TAG_MOUNT_POINT})(target, null, link);
1328 	}
1329 
1330 	void symlink()(in char[] original, in char[] link)
1331 	{
1332 		mixin(importWin32!q{winnt});
1333 
1334 		acquirePrivilege(SE_CREATE_SYMBOLIC_LINK_NAME);
1335 
1336 		touch(link);
1337 		scope(failure) remove(link);
1338 
1339 		createReparsePoint!(q{SymbolicLinkReparseBuffer}, q{r.SymbolicLinkReparseBuffer.Flags = link.isAbsolute() ? 0 : SYMLINK_FLAG_RELATIVE;}, q{IO_REPARSE_TAG_SYMLINK})(original, original, link);
1340 	}
1341 }
1342 else
1343 	alias std.file.symlink dirLink;
1344 
1345 version(Windows) version(unittest) static mixin(importWin32!q{winnt});
1346 
1347 unittest
1348 {
1349 	mkdir("a"); scope(exit) rmdir("a"[]);
1350 	touch("a/f"); scope(exit) remove("a/f");
1351 	dirLink("a", "b"); scope(exit) version(Windows) rmdir("b"); else remove("b");
1352 	//symlink("a/f", "c"); scope(exit) remove("c");
1353 	assert("b".isSymlink());
1354 	//assert("c".isSymlink());
1355 	assert("b/f".exists());
1356 }
1357 
1358 version (Windows)
1359 {
1360 	void hardLink()(string src, string dst)
1361 	{
1362 		mixin(importWin32!q{w32api});
1363 
1364 		static assert(_WIN32_WINNT >= 0x501, "CreateHardLinkW not available for target Windows platform. Specify -version=WindowsXP");
1365 
1366 		mixin(importWin32!q{winnt});
1367 		mixin(importWin32!q{winbase});
1368 
1369 		wenforce(CreateHardLinkW(toUTF16z(dst), toUTF16z(src), null), "CreateHardLink failed: " ~ src ~ " -> " ~ dst);
1370 	}
1371 }
1372 version (Posix)
1373 {
1374 	void hardLink()(string src, string dst)
1375 	{
1376 		import core.sys.posix.unistd;
1377 		enforce(link(toUTFz!(const char*)(src), toUTFz!(const char*)(dst)) == 0, "link() failed: " ~ dst);
1378 	}
1379 }
1380 
1381 version (Posix)
1382 {
1383 	string realPath(string path)
1384 	{
1385 		// TODO: Windows version
1386 		import core.sys.posix.stdlib;
1387 		auto p = realpath(toUTFz!(const char*)(path), null);
1388 		errnoEnforce(p, "realpath");
1389 		string result = fromStringz(p).idup;
1390 		free(p);
1391 		return result;
1392 	}
1393 }
1394 
1395 // /proc/self/mounts parsing
1396 version (linux)
1397 {
1398 	struct MountInfo
1399 	{
1400 		string spec; /// device path
1401 		string file; /// mount path
1402 		string vfstype; /// file system
1403 		string mntops; /// options
1404 		int freq; /// dump flag
1405 		int passno; /// fsck order
1406 	}
1407 
1408 	string unescapeMountString(in char[] s)
1409 	{
1410 		string result;
1411 
1412 		size_t p = 0;
1413 		for (size_t i=0; i+3<s.length;)
1414 		{
1415 			auto c = s[i];
1416 			if (c == '\\')
1417 			{
1418 				result ~= s[p..i];
1419 				result ~= to!int(s[i+1..i+4], 8);
1420 				i += 4;
1421 				p = i;
1422 			}
1423 			else
1424 				i++;
1425 		}
1426 		result ~= s[p..$];
1427 		return result;
1428 	}
1429 
1430 	unittest
1431 	{
1432 		assert(unescapeMountString(`a\040b\040c`) == "a b c");
1433 		assert(unescapeMountString(`\040`) == " ");
1434 	}
1435 
1436 	MountInfo parseMountInfo(in char[] line)
1437 	{
1438 		const(char)[][6] parts;
1439 		copy(line.splitter(" "), parts[]);
1440 		return MountInfo(
1441 			unescapeMountString(parts[0]),
1442 			unescapeMountString(parts[1]),
1443 			unescapeMountString(parts[2]),
1444 			unescapeMountString(parts[3]),
1445 			parts[4].to!int,
1446 			parts[5].to!int,
1447 		);
1448 	}
1449 
1450 	/// Returns an iterator of MountInfo structs.
1451 	auto getMounts()
1452 	{
1453 		return File("/proc/self/mounts", "rb").byLine().map!parseMountInfo();
1454 	}
1455 
1456 	/// Get MountInfo with longest mount point matching path.
1457 	/// Returns MountInfo.init if none match.
1458 	MountInfo getPathMountInfo(string path)
1459 	{
1460 		path = realPath(path);
1461 		size_t bestLength; MountInfo bestInfo;
1462 		foreach (ref info; getMounts())
1463 		{
1464 			if (path.pathStartsWith(info.file))
1465 			{
1466 				if (bestLength < info.file.length)
1467 				{
1468 					bestLength = info.file.length;
1469 					bestInfo = info;
1470 				}
1471 			}
1472 		}
1473 		return bestInfo;
1474 	}
1475 
1476 	/// Get the name of the filesystem that the given path is mounted under.
1477 	/// Returns null if none match.
1478 	string getPathFilesystem(string path)
1479 	{
1480 		return getPathMountInfo(path).vfstype;
1481 	}
1482 }
1483 
1484 // ****************************************************************************
1485 
1486 version (linux)
1487 {
1488 	import core.sys.linux.sys.xattr;
1489 	import core.stdc.errno;
1490 	alias ENOATTR = ENODATA;
1491 
1492 	/// AA-like object for accessing a file's extended attributes.
1493 	struct XAttrs(Obj, string funPrefix)
1494 	{
1495 		Obj obj;
1496 
1497 		mixin("alias getFun = " ~ funPrefix ~ "getxattr;");
1498 		mixin("alias setFun = " ~ funPrefix ~ "setxattr;");
1499 		mixin("alias removeFun = " ~ funPrefix ~ "removexattr;");
1500 		mixin("alias listFun = " ~ funPrefix ~ "listxattr;");
1501 
1502 		bool supported()
1503 		{
1504 			auto size = getFun(obj, "user.\x01", null, 0);
1505 			return size >= 0 || errno != EOPNOTSUPP;
1506 		}
1507 
1508 		void[] opIndex(string key)
1509 		{
1510 			auto cKey = key.toStringz();
1511 			size_t size = 0;
1512 			void[] buf;
1513 			do
1514 			{
1515 				buf.length = size;
1516 				size = getFun(obj, cKey, buf.ptr, buf.length);
1517 				errnoEnforce(size >= 0, __traits(identifier, getFun));
1518 			} while (size != buf.length);
1519 			return buf;
1520 		}
1521 
1522 		bool opIn_r(string key)
1523 		{
1524 			auto cKey = key.toStringz();
1525 			auto size = getFun(obj, cKey, null, 0);
1526 			if (size >= 0)
1527 				return true;
1528 			else
1529 			if (errno == ENOATTR)
1530 				return false;
1531 			else
1532 				errnoEnforce(false, __traits(identifier, getFun));
1533 			assert(false);
1534 		}
1535 
1536 		void opIndexAssign(in void[] value, string key)
1537 		{
1538 			auto ret = setFun(obj, key.toStringz(), value.ptr, value.length, 0);
1539 			errnoEnforce(ret == 0, __traits(identifier, setFun));
1540 		}
1541 
1542 		void remove(string key)
1543 		{
1544 			auto ret = removeFun(obj, key.toStringz());
1545 			errnoEnforce(ret == 0, __traits(identifier, removeFun));
1546 		}
1547 
1548 		string[] keys()
1549 		{
1550 			size_t size = 0;
1551 			char[] buf;
1552 			do
1553 			{
1554 				buf.length = size;
1555 				size = listFun(obj, buf.ptr, buf.length);
1556 				errnoEnforce(size >= 0, __traits(identifier, listFun));
1557 			} while (size != buf.length);
1558 
1559 			char[][] result;
1560 			size_t start;
1561 			foreach (p, c; buf)
1562 				if (!c)
1563 				{
1564 					result ~= buf[start..p];
1565 					start = p+1;
1566 				}
1567 
1568 			return cast(string[])result;
1569 		}
1570 	}
1571 
1572 	auto xAttrs(string path)
1573 	{
1574 		return XAttrs!(const(char)*, "")(path.toStringz());
1575 	}
1576 
1577 	auto linkXAttrs(string path)
1578 	{
1579 		return XAttrs!(const(char)*, "l")(path.toStringz());
1580 	}
1581 
1582 	auto xAttrs(in ref File f)
1583 	{
1584 		return XAttrs!(int, "f")(f.fileno);
1585 	}
1586 
1587 	unittest
1588 	{
1589 		if (!xAttrs(".").supported)
1590 		{
1591 			import std.stdio : stderr;
1592 			stderr.writeln("ae.sys.file: xattrs not supported on current filesystem, skipping test.");
1593 			return;
1594 		}
1595 
1596 		enum fn = "test.txt";
1597 		std.file.write(fn, "test");
1598 		scope(exit) remove(fn);
1599 
1600 		auto attrs = xAttrs(fn);
1601 		enum key = "user.foo";
1602 		assert(key !in attrs);
1603 		assert(attrs.keys == []);
1604 
1605 		attrs[key] = "bar";
1606 		assert(key in attrs);
1607 		assert(attrs[key] == "bar");
1608 		assert(attrs.keys == [key]);
1609 
1610 		attrs.remove(key);
1611 		assert(key !in attrs);
1612 		assert(attrs.keys == []);
1613 	}
1614 }
1615 
1616 // ****************************************************************************
1617 
1618 version (Windows)
1619 {
1620 	/// Enumerate all hard links to the specified file.
1621 	// TODO: Return a range
1622 	string[] enumerateHardLinks()(string fn)
1623 	{
1624 		mixin(importWin32!q{winnt});
1625 		mixin(importWin32!q{winbase});
1626 
1627 		alias extern(System) HANDLE function(LPCWSTR lpFileName, DWORD dwFlags, LPDWORD StringLength, PWCHAR LinkName) TFindFirstFileNameW;
1628 		alias extern(System) BOOL function(HANDLE hFindStream, LPDWORD StringLength, PWCHAR LinkName) TFindNextFileNameW;
1629 
1630 		auto kernel32 = GetModuleHandle("kernel32.dll");
1631 		auto FindFirstFileNameW = cast(TFindFirstFileNameW)GetProcAddress(kernel32, "FindFirstFileNameW").wenforce("GetProcAddress(FindFirstFileNameW)");
1632 		auto FindNextFileNameW = cast(TFindNextFileNameW)GetProcAddress(kernel32, "FindNextFileNameW").wenforce("GetProcAddress(FindNextFileNameW)");
1633 
1634 		static WCHAR[0x8000] buf;
1635 		DWORD len = buf.length;
1636 		auto h = FindFirstFileNameW(toUTF16z(fn), 0, &len, buf.ptr);
1637 		wenforce(h != INVALID_HANDLE_VALUE, "FindFirstFileNameW");
1638 		scope(exit) FindClose(h);
1639 
1640 		string[] result;
1641 		do
1642 		{
1643 			enforce(len > 0 && len < buf.length && buf[len-1] == 0, "Bad FindFirst/NextFileNameW result");
1644 			result ~= buf[0..len-1].toUTF8();
1645 			len = buf.length;
1646 			auto ok = FindNextFileNameW(h, &len, buf.ptr);
1647 			if (!ok && GetLastError() == ERROR_HANDLE_EOF)
1648 				break;
1649 			wenforce(ok, "FindNextFileNameW");
1650 		} while(true);
1651 		return result;
1652 	}
1653 }
1654 
1655 uint hardLinkCount(string fn)
1656 {
1657 	version (Windows)
1658 	{
1659 		// TODO: Optimize (don't transform strings)
1660 		return cast(uint)fn.enumerateHardLinks.length;
1661 	}
1662 	else
1663 	{
1664 		import core.sys.posix.sys.stat;
1665 
1666 		stat_t s;
1667 		errnoEnforce(stat(fn.toStringz(), &s) == 0, "stat");
1668 		return s.st_nlink.to!uint;
1669 	}
1670 }
1671 
1672 // http://d.puremagic.com/issues/show_bug.cgi?id=7016
1673 version (unittest)
1674 	version (Windows)
1675 		import ae.sys.windows.misc : getWineVersion;
1676 
1677 unittest
1678 {
1679 	// FindFirstFileNameW not implemented in Wine
1680 	version (Windows)
1681 		if (getWineVersion())
1682 			return;
1683 
1684 	touch("a.test");
1685 	scope(exit) remove("a.test");
1686 	assert("a.test".hardLinkCount() == 1);
1687 
1688 	hardLink("a.test", "b.test");
1689 	scope(exit) remove("b.test");
1690 	assert("a.test".hardLinkCount() == 2);
1691 	assert("b.test".hardLinkCount() == 2);
1692 
1693 	version(Windows)
1694 	{
1695 		auto paths = enumerateHardLinks("a.test");
1696 		assert(paths.length == 2);
1697 		paths.sort();
1698 		assert(paths[0].endsWith(`\a.test`), paths[0]);
1699 		assert(paths[1].endsWith(`\b.test`));
1700 	}
1701 }
1702 
1703 void toFile(in void[] data, in char[] name)
1704 {
1705 	std.file.write(name, data);
1706 }
1707 
1708 /// Uses UNC paths to open a file.
1709 /// Requires https://github.com/D-Programming-Language/phobos/pull/1888
1710 File openFile()(string fn, string mode = "rb")
1711 {
1712 	File f;
1713 	static if (is(typeof(&f.windowsHandleOpen)))
1714 	{
1715 		import core.sys.windows.windows;
1716 		import ae.sys.windows.exception;
1717 
1718 		string winMode;
1719 		foreach (c; mode)
1720 			switch (c)
1721 			{
1722 				case 'r':
1723 				case 'w':
1724 				case 'a':
1725 				case '+':
1726 					winMode ~= c;
1727 					break;
1728 				case 'b':
1729 				case 't':
1730 					break;
1731 				default:
1732 					assert(false, "Unknown character in mode");
1733 			}
1734 		DWORD access, creation;
1735 		bool append;
1736 		switch (winMode)
1737 		{
1738 			case "r" : access = GENERIC_READ                ; creation = OPEN_EXISTING; break;
1739 			case "r+": access = GENERIC_READ | GENERIC_WRITE; creation = OPEN_EXISTING; break;
1740 			case "w" : access =                GENERIC_WRITE; creation = CREATE_ALWAYS; break;
1741 			case "w+": access = GENERIC_READ | GENERIC_WRITE; creation = CREATE_ALWAYS; break;
1742 			case "a" : access =                GENERIC_WRITE; creation = OPEN_ALWAYS  ; version (CRuntime_Microsoft) append = true; break;
1743 			case "a+": access = GENERIC_READ | GENERIC_WRITE; creation = OPEN_ALWAYS  ; version (CRuntime_Microsoft) assert(false, "MSVCRT can't fdopen with a+"); else break;
1744 			default: assert(false, "Bad file mode: " ~ mode);
1745 		}
1746 
1747 		auto pathW = toUTF16z(longPath(fn));
1748 		auto h = CreateFileW(pathW, access, FILE_SHARE_READ, null, creation, 0, HANDLE.init);
1749 		wenforce(h != INVALID_HANDLE_VALUE);
1750 
1751 		if (append)
1752 			h.SetFilePointer(0, null, FILE_END);
1753 
1754 		f.windowsHandleOpen(h, mode);
1755 	}
1756 	else
1757 		f.open(fn, mode);
1758 	return f;
1759 }
1760 
1761 unittest
1762 {
1763 	enum Existence { any, mustExist, mustNotExist }
1764 	enum Pos { none /* not readable/writable */, start, end, empty }
1765 	static struct Behavior
1766 	{
1767 		Existence existence;
1768 		bool truncating;
1769 		Pos read, write;
1770 	}
1771 
1772 	void test(string mode, in Behavior expected)
1773 	{
1774 		static if (isVersion!q{CRuntime_Microsoft} || isVersion!q{OSX})
1775 			if (mode == "a+")
1776 				return;
1777 
1778 		Behavior behavior;
1779 
1780 		static int counter;
1781 		auto fn = text(deleteme, counter++);
1782 
1783 		collectException(fn.remove());
1784 		bool mustExist    = !!collectException(openFile(fn, mode));
1785 		touch(fn);
1786 		bool mustNotExist = !!collectException(openFile(fn, mode));
1787 
1788 		if (!mustExist)
1789 			if (!mustNotExist)
1790 				behavior.existence = Existence.any;
1791 			else
1792 				behavior.existence = Existence.mustNotExist;
1793 		else
1794 			if (!mustNotExist)
1795 				behavior.existence = Existence.mustExist;
1796 			else
1797 				assert(false, "Can't open file whether it exists or not");
1798 
1799 		void create()
1800 		{
1801 			if (mustNotExist)
1802 				collectException(fn.remove());
1803 			else
1804 				write(fn, "foo");
1805 		}
1806 
1807 		create();
1808 		openFile(fn, mode);
1809 		behavior.truncating = getSize(fn) == 0;
1810 
1811 		create();
1812 		{
1813 			auto f = openFile(fn, mode);
1814 			ubyte[] buf;
1815 			if (collectException(f.rawRead(new ubyte[1]), buf))
1816 			{
1817 				behavior.read = Pos.none;
1818 				// Work around https://issues.dlang.org/show_bug.cgi?id=19751
1819 				f.reopen(fn, mode);
1820 			}
1821 			else
1822 			if (buf.length)
1823 				behavior.read = Pos.start;
1824 			else
1825 			if (f.size)
1826 				behavior.read = Pos.end;
1827 			else
1828 				behavior.read = Pos.empty;
1829 		}
1830 
1831 		create();
1832 		{
1833 			string s;
1834 			{
1835 				auto f = openFile(fn, mode);
1836 				if (collectException(f.rawWrite("b")))
1837 				{
1838 					s = null;
1839 					// Work around https://issues.dlang.org/show_bug.cgi?id=19751
1840 					f.reopen(fn, mode);
1841 				}
1842 				else
1843 				{
1844 					f.close();
1845 					s = fn.readText;
1846 				}
1847 			}
1848 
1849 			if (s is null)
1850 				behavior.write = Pos.none;
1851 			else
1852 			if (s == "b")
1853 				behavior.write = Pos.empty;
1854 			else
1855 			if (s.endsWith("b"))
1856 				behavior.write = Pos.end;
1857 			else
1858 			if (s.startsWith("b"))
1859 				behavior.write = Pos.start;
1860 			else
1861 				assert(false, "Can't detect write position");
1862 		}
1863 
1864 
1865 		if (behavior != expected)
1866 		{
1867 			import ae.utils.array : isOneOf;
1868 			version (Windows)
1869 				if (getWineVersion() && mode.isOneOf("w", "a"))
1870 				{
1871 					// Ignore bug in Wine msvcrt implementation
1872 					return;
1873 				}
1874 
1875 			assert(false, text(mode, ": expected ", expected, ", got ", behavior));
1876 		}
1877 	}
1878 
1879 	test("r" , Behavior(Existence.mustExist   , false, Pos.start, Pos.none ));
1880 	test("r+", Behavior(Existence.mustExist   , false, Pos.start, Pos.start));
1881 	test("w" , Behavior(Existence.any         , true , Pos.none , Pos.empty));
1882 	test("w+", Behavior(Existence.any         , true , Pos.empty, Pos.empty));
1883 	test("a" , Behavior(Existence.any         , false, Pos.none , Pos.end  ));
1884 	test("a+", Behavior(Existence.any         , false, Pos.start, Pos.end  ));
1885 }
1886 
1887 auto fileDigest(Digest)(string fn)
1888 {
1889 	import std.range.primitives;
1890 	Digest context;
1891 	context.start();
1892 	put(context, openFile(fn, "rb").byChunk(64 * 1024));
1893 	auto digest = context.finish();
1894 	return digest;
1895 }
1896 
1897 template mdFile()
1898 {
1899 	import std.digest.md;
1900 	alias mdFile = fileDigest!MD5;
1901 }
1902 
1903 version (HAVE_WIN32)
1904 unittest
1905 {
1906 	import std.digest.digest : toHexString;
1907 	write("test.txt", "Hello, world!");
1908 	scope(exit) remove("test.txt");
1909 	assert(mdFile("test.txt").toHexString() == "6CD3556DEB0DA54BCA060B4C39479839");
1910 }
1911 
1912 auto fileDigestCached(Digest)(string fn)
1913 {
1914 	static typeof(Digest.init.finish())[ulong] cache;
1915 	auto id = getFileID(fn);
1916 	auto phash = id in cache;
1917 	if (phash)
1918 		return *phash;
1919 	return cache[id] = fileDigest!Digest(fn);
1920 }
1921 
1922 template mdFileCached()
1923 {
1924 	import std.digest.md;
1925 	alias mdFileCached = fileDigestCached!MD5;
1926 }
1927 
1928 version (HAVE_WIN32)
1929 unittest
1930 {
1931 	import std.digest.digest : toHexString;
1932 	write("test.txt", "Hello, world!");
1933 	scope(exit) remove("test.txt");
1934 	assert(mdFileCached("test.txt").toHexString() == "6CD3556DEB0DA54BCA060B4C39479839");
1935 	write("test.txt", "Something else");
1936 	assert(mdFileCached("test.txt").toHexString() == "6CD3556DEB0DA54BCA060B4C39479839");
1937 }
1938 
1939 /// Read a File (which might be a stream) into an array
1940 void[] readFile(File f)
1941 {
1942 	import std.range.primitives;
1943 	auto result = appender!(ubyte[]);
1944 	put(result, f.byChunk(64*1024));
1945 	return result.data;
1946 }
1947 
1948 unittest
1949 {
1950 	auto s = "0123456789".replicate(10_000);
1951 	write("test.txt", s);
1952 	scope(exit) remove("test.txt");
1953 	assert(readFile(File("test.txt")) == s);
1954 }
1955 
1956 /// Like std.file.readText for non-UTF8
1957 ascii readAscii()(string fileName)
1958 {
1959 	return cast(ascii)readFile(openFile(fileName, "rb"));
1960 }
1961 
1962 // http://d.puremagic.com/issues/show_bug.cgi?id=7016
1963 version(Posix) static import ae.sys.signals;
1964 
1965 /// Start a thread which writes data to f asynchronously.
1966 Thread writeFileAsync(File f, in void[] data)
1967 {
1968 	static class Writer : Thread
1969 	{
1970 		File target;
1971 		const void[] data;
1972 
1973 		this(ref File f, in void[] data)
1974 		{
1975 			this.target = f;
1976 			this.data = data;
1977 			super(&run);
1978 		}
1979 
1980 		void run()
1981 		{
1982 			version (Posix)
1983 			{
1984 				import ae.sys.signals;
1985 				collectSignal(SIGPIPE, &write);
1986 			}
1987 			else
1988 				write();
1989 		}
1990 
1991 		void write()
1992 		{
1993 			target.rawWrite(data);
1994 			target.close();
1995 		}
1996 	}
1997 
1998 	auto t = new Writer(f, data);
1999 	t.start();
2000 	return t;
2001 }
2002 
2003 /// Write data to a file, and ensure it gets written to disk
2004 /// before this function returns.
2005 /// Consider using as atomic!syncWrite.
2006 /// See also: syncUpdate
2007 void syncWrite()(string target, in void[] data)
2008 {
2009 	auto f = File(target, "wb");
2010 	f.rawWrite(data);
2011 	version (Windows)
2012 	{
2013 		mixin(importWin32!q{windows});
2014 		FlushFileBuffers(f.windowsHandle);
2015 	}
2016 	else
2017 	{
2018 		import core.sys.posix.unistd;
2019 		fsync(f.fileno);
2020 	}
2021 	f.close();
2022 }
2023 
2024 /// Atomically save data to a file (if the file doesn't exist,
2025 /// or its contents differs). The update operation as a whole
2026 /// is not atomic, only the write is.
2027 void syncUpdate()(string fn, in void[] data)
2028 {
2029 	if (!fn.exists || fn.read() != data)
2030 		atomic!(syncWrite!())(fn, data);
2031 }
2032 
2033 version(Windows) import ae.sys.windows.exception;
2034 
2035 struct NamedPipeImpl
2036 {
2037 	immutable string fileName;
2038 
2039 	/// Create a named pipe, and reserve a filename.
2040 	this()(string name)
2041 	{
2042 		version(Windows)
2043 		{
2044 			mixin(importWin32!q{winbase});
2045 
2046 			fileName = `\\.\pipe\` ~ name;
2047 			auto h = CreateNamedPipeW(fileName.toUTF16z, PIPE_ACCESS_OUTBOUND, PIPE_TYPE_BYTE, 10, 4096, 4096, 0, null).wenforce("CreateNamedPipeW");
2048 			f.windowsHandleOpen(h, "wb");
2049 		}
2050 		else
2051 		{
2052 			import core.sys.posix.sys.stat;
2053 
2054 			fileName = `/tmp/` ~ name ~ `.fifo`;
2055 			mkfifo(fileName.toStringz, S_IWUSR | S_IRUSR);
2056 		}
2057 	}
2058 
2059 	/// Wait for a peer to open the other end of the pipe.
2060 	File connect()()
2061 	{
2062 		version(Windows)
2063 		{
2064 			mixin(importWin32!q{winbase});
2065 			mixin(importWin32!q{windef});
2066 
2067 			BOOL bSuccess = ConnectNamedPipe(f.windowsHandle, null);
2068 
2069 			// "If a client connects before the function is called, the function returns zero
2070 			// and GetLastError returns ERROR_PIPE_CONNECTED. This can happen if a client
2071 			// connects in the interval between the call to CreateNamedPipe and the call to
2072 			// ConnectNamedPipe. In this situation, there is a good connection between client
2073 			// and server, even though the function returns zero."
2074 			if (!bSuccess)
2075 				wenforce(GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe");
2076 
2077 			return f;
2078 		}
2079 		else
2080 		{
2081 			return File(fileName, "w");
2082 		}
2083 	}
2084 
2085 	~this()
2086 	{
2087 		version(Windows)
2088 		{
2089 			// File.~this will take care of cleanup
2090 		}
2091 		else
2092 			fileName.remove();
2093 	}
2094 
2095 private:
2096 	File f;
2097 }
2098 alias NamedPipe = RefCounted!NamedPipeImpl;
2099 
2100 import ae.utils.textout : StringBuilder;
2101 
2102 /// Avoid std.stdio.File.readln's memory corruption bug
2103 /// https://issues.dlang.org/show_bug.cgi?id=13856
2104 string safeReadln(File f)
2105 {
2106 	StringBuilder buf;
2107 	char[1] arr;
2108 	while (true)
2109 	{
2110 		auto result = f.rawRead(arr[]);
2111 		if (!result.length)
2112 			break;
2113 		buf.put(result);
2114 		if (result[0] == '\x0A')
2115 			break;
2116 	}
2117 	return buf.get();
2118 }
2119 
2120 // ****************************************************************************
2121 
2122 /// Change the current directory to the given directory. Does nothing if dir is null.
2123 /// Return a scope guard which, upon destruction, restores the previous directory.
2124 /// Asserts that only one thread has changed the process's current directory at any time.
2125 auto pushd(string dir)
2126 {
2127 	import core.atomic;
2128 
2129 	static int threadCount = 0;
2130 	static shared int processCount = 0;
2131 
2132 	static struct Popd
2133 	{
2134 		string oldPath;
2135 		this(string cwd) { oldPath = cwd; }
2136 		~this() { if (oldPath) pop(); }
2137 		@disable this();
2138 		@disable this(this);
2139 
2140 		void pop()
2141 		{
2142 			assert(oldPath);
2143 			scope(exit) oldPath = null;
2144 			chdir(oldPath);
2145 
2146 			auto newThreadCount = --threadCount;
2147 			auto newProcessCount = atomicOp!"-="(processCount, 1);
2148 			assert(newThreadCount == newProcessCount); // Shouldn't happen
2149 		}
2150 	}
2151 
2152 	string cwd;
2153 	if (dir)
2154 	{
2155 		auto newThreadCount = ++threadCount;
2156 		auto newProcessCount = atomicOp!"+="(processCount, 1);
2157 		assert(newThreadCount == newProcessCount, "Another thread already has an active pushd");
2158 
2159 		cwd = getcwd();
2160 		chdir(dir);
2161 	}
2162 	return Popd(cwd);
2163 }
2164 
2165 // ****************************************************************************
2166 
2167 import std.algorithm;
2168 import std.process : thisProcessID;
2169 import std.traits;
2170 import std.typetuple;
2171 import ae.utils.meta;
2172 
2173 enum targetParameterNames = "target/to/name/dst";
2174 
2175 /// Wrap an operation which creates a file or directory,
2176 /// so that it is created safely and, for files, atomically
2177 /// (by performing the underlying operation to a temporary
2178 /// location, then renaming the completed file/directory to
2179 /// the actual target location). targetName specifies the name
2180 /// of the parameter containing the target file/directory.
2181 auto atomic(alias impl, string targetName = targetParameterNames)(staticMap!(Unqual, ParameterTypeTuple!impl) args)
2182 {
2183 	enum targetIndex = findParameter([ParameterIdentifierTuple!impl], targetName, __traits(identifier, impl));
2184 	return atomic!(impl, targetIndex)(args);
2185 }
2186 
2187 /// ditto
2188 auto atomic(alias impl, size_t targetIndex)(staticMap!(Unqual, ParameterTypeTuple!impl) args)
2189 {
2190 	// idup for https://d.puremagic.com/issues/show_bug.cgi?id=12503
2191 	auto target = args[targetIndex].idup;
2192 	auto temp = "%s.%s.%s.temp".format(target, thisProcessID, getCurrentThreadID);
2193 	if (temp.exists) temp.removeRecurse();
2194 	scope(success) rename(temp, target);
2195 	scope(failure) if (temp.exists) temp.removeRecurse();
2196 	args[targetIndex] = temp;
2197 	return impl(args);
2198 }
2199 
2200 /// ditto
2201 // Workaround for https://d.puremagic.com/issues/show_bug.cgi?id=12230
2202 // Can't be an overload because of https://issues.dlang.org/show_bug.cgi?id=13374
2203 //R atomicDg(string targetName = "target", R, Args...)(R delegate(Args) impl, staticMap!(Unqual, Args) args)
2204 auto atomicDg(size_t targetIndexA = size_t.max, Impl, Args...)(Impl impl, Args args)
2205 {
2206 	enum targetIndex = targetIndexA == size_t.max ? ParameterTypeTuple!impl.length-1 : targetIndexA;
2207 	return atomic!(impl, targetIndex)(args);
2208 }
2209 
2210 deprecated alias safeUpdate = atomic;
2211 
2212 unittest
2213 {
2214 	enum fn = "atomic.tmp";
2215 	scope(exit) if (fn.exists) fn.remove();
2216 
2217 	atomic!touch(fn);
2218 	assert(fn.exists);
2219 	fn.remove();
2220 
2221 	atomicDg(&touch, fn);
2222 	assert(fn.exists);
2223 }
2224 
2225 /// Wrap an operation so that it is skipped entirely
2226 /// if the target already exists. Implies atomic.
2227 auto cached(alias impl, string targetName = targetParameterNames)(ParameterTypeTuple!impl args)
2228 {
2229 	enum targetIndex = findParameter([ParameterIdentifierTuple!impl], targetName, __traits(identifier, impl));
2230 	auto target = args[targetIndex];
2231 	if (!target.exists)
2232 		atomic!(impl, targetIndex)(args);
2233 	return target;
2234 }
2235 
2236 /// ditto
2237 // Exists due to the same reasons as atomicDg
2238 auto cachedDg(size_t targetIndexA = size_t.max, Impl, Args...)(Impl impl, Args args)
2239 {
2240 	enum targetIndex = targetIndexA == size_t.max ? ParameterTypeTuple!impl.length-1 : targetIndexA;
2241 	auto target = args[targetIndex];
2242 	if (!target.exists)
2243 		atomic!(impl, targetIndex)(args);
2244 	return target;
2245 }
2246 
2247 deprecated alias obtainUsing = cached;
2248 
2249 /// Create a file, or replace an existing file's contents
2250 /// atomically.
2251 /// Note: Consider using atomic!syncWrite or
2252 /// atomic!syncUpdate instead.
2253 alias atomic!writeProxy atomicWrite;
2254 deprecated alias safeWrite = atomicWrite;
2255 void writeProxy(string target, in void[] data)
2256 {
2257 	std.file.write(target, data);
2258 }
2259 
2260 // Work around for https://github.com/D-Programming-Language/phobos/pull/2784#issuecomment-68117241
2261 private void copy2(string source, string target) { std.file.copy(source, target); }
2262 
2263 /// Copy a file, or replace an existing file's contents
2264 /// with another file's, atomically.
2265 alias atomic!copy2 atomicCopy;
2266 
2267 unittest
2268 {
2269 	enum fn = "cached.tmp";
2270 	scope(exit) if (fn.exists) fn.remove();
2271 
2272 	cached!touch(fn);
2273 	assert(fn.exists);
2274 
2275 	std.file.write(fn, "test");
2276 
2277 	cachedDg!0(&writeProxy, fn, "test2");
2278 	assert(fn.readText() == "test");
2279 }
2280 
2281 // ****************************************************************************
2282 
2283 template withTarget(alias targetGen, alias fun)
2284 {
2285 	auto withTarget(Args...)(auto ref Args args)
2286 	{
2287 		auto target = targetGen(args);
2288 		fun(args, target);
2289 		return target;
2290 	}
2291 }
2292 
2293 /// Two-argument buildPath with reversed arguments.
2294 /// Useful for UFCS chaining.
2295 string prependPath(string target, string path)
2296 {
2297 	return buildPath(path, target);
2298 }