+freebsd.ha (2792B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use crypto::random; 5 use encoding::hex; 6 use errors; 7 use fmt; 8 use fs; 9 use io; 10 use memio; 11 use os; 12 use path; 13 14 fn get_tmpdir() str = os::tryenv("TMPDIR", "/tmp"); 15 16 // Creates an unnamed temporary file. The file may or may not have a name; not 17 // all systems support the creation of temporary inodes which are not linked to 18 // any directory. If it is necessary to create a real file, it will be removed 19 // when the stream is closed. 20 // 21 // The I/O mode must be either [[io::mode::WRITE]] or [[io::mode::RDWR]]. 22 export fn file(iomode: io::mode, mode: fs::mode) (io::file | fs::error) = { 23 // TODO: Add a custom "close" function which removes the named file 24 let file = named(os::cwd, get_tmpdir(), iomode, mode)?; 25 free(file.1); 26 return file.0; 27 }; 28 29 // Creates a named temporary file in the given directory of the given 30 // filesystem. The caller is responsible for closing and removing the file when 31 // they're done with it. The name is statically allocated, and will be 32 // overwritten on subsequent calls. 33 // 34 // The I/O mode must be either [[io::mode::WRITE]] or [[io::mode::RDWR]]. 35 export fn named( 36 fs: *fs::fs, 37 path: str, 38 iomode: io::mode, 39 mode: fs::mode, 40 ) ((io::file, str) | fs::error) = { 41 assert(iomode == io::mode::WRITE || iomode == io::mode::RDWR); 42 43 let oflags = fs::flag::EXCL; 44 if (iomode == io::mode::RDWR) { 45 oflags |= fs::flag::RDWR; 46 } else { 47 oflags |= fs::flag::WRONLY; 48 }; 49 50 static let pathbuf = path::path { ... }; 51 static let namebuf: [32]u8 = [0...]; 52 for (true) { 53 let id = 0u64; 54 random::buffer(&id: *[size(u64)]u8); 55 56 const name = fmt::bsprintf(namebuf, "temp.{}", id); 57 const path = path::set(&pathbuf, path, name)!; 58 59 match (fs::create_file(fs, path, mode, oflags)) { 60 case errors::exists => 61 continue; 62 case let err: fs::error => 63 return err; 64 case let f: io::file => 65 return (f, path); 66 }; 67 }; 68 }; 69 70 // Creates a temporary directory. This function only guarantees that the 71 // directory will have a unique name and be placed in the system temp directory, 72 // but not that it will be removed automatically; the caller must remove it when 73 // they're done using it via [[os::rmdir]] or [[os::rmdirall]]. 74 // 75 // The return value is statically allocated and will be overwritten on 76 // subsequent calls. 77 export fn dir() str = { 78 let buf: [8]u8 = [0...], name: [16]u8 = [0...]; 79 random::buffer(buf[..]); 80 81 let sink = memio::fixed(name); 82 const enc = hex::newencoder(&sink); 83 io::write(&enc, buf)!; 84 let name = memio::string(&sink)!; 85 86 static let p = path::path { ... }; 87 path::set(&p, get_tmpdir(), name)!; 88 const path = path::string(&p); 89 match (os::mkdir(path,0o755)) { 90 case let err: fs::error => abort("Could not create temp directory"); 91 case void => void; 92 }; 93 return path; 94 };