1 /** Operations for Creating Temporary Files and Directories. 2 Copyright: Per Nordlöw 2018-. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: $(WEB Per Nordlöw) 5 */ 6 module nxt.tempfs; 7 8 @safe: 9 10 /** Create a New Temporary File starting with ($D namePrefix) and ending with 6 randomly defined characters. 11 * 12 * Returns: File Descriptor to opened file. 13 */ 14 int tempfile(string namePrefix = null) @trusted 15 { 16 version(linux) 17 { 18 import core.sys.posix.stdlib: mkstemp; 19 20 char[4096] buf; 21 buf[0 .. namePrefix.length] = namePrefix[]; // copy the name into the mutable buffer 22 buf[namePrefix.length .. namePrefix.length + 6] = "XXXXXX"[]; 23 buf[namePrefix.length + 6] = 0; // make sure it is zero terminated yourself 24 25 auto tmp = mkstemp(buf.ptr); 26 27 // dbg(buf[0 .. namePrefix.length + 6]); 28 return tmp; 29 } 30 } 31 32 /** TODO: Scoped variant of tempfile. 33 * 34 * Search http://forum.dlang.org/thread/mailman.262.1386205638.3242.digitalmars-d-learn@puremagic.com 35 */ 36 37 /** Create a New Temporary Directory Tree. 38 * 39 * Returns: Path to root of tree. 40 */ 41 char* temptree(char* name_x, 42 char* template_ = null) @trusted 43 { 44 return null; 45 } 46 47 /** Returns the path to a new (unique) temporary file. 48 * 49 * See_Also: https://forum.dlang.org/post/ytmwfzmeqjumzfzxithe@forum.dlang.org 50 * See_Also: https://dlang.org/library/std/stdio/file.tmpfile.html 51 */ 52 string tempFilePath(const scope string prefix, 53 const scope string extension = null) 54 { 55 import std.uuid : randomUUID; 56 import std.file : tempDir; 57 import std.path : buildPath; 58 return buildPath(tempDir(), 59 prefix ~ "_" ~ randomUUID.toString() ~ extension); // TODO: use nxt.appending.append() 60 }