hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

+freebsd.ha (2838B)


      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::buffer { ... };
     51 	static let namebuf: [32]u8 = [0...];
     52 	for (true) {
     53 		let rand: [size(u64)]u8 = [0...];
     54 		random::buffer(rand);
     55 
     56 		const id = *(&rand[0]: *u64);
     57 		const name = fmt::bsprintf(namebuf, "temp.{}", id);
     58 		const path = path::set(&pathbuf, path, name)!;
     59 
     60 		match (fs::create_file(fs, path, mode, oflags)) {
     61 		case errors::exists =>
     62 			continue;
     63 		case let err: fs::error =>
     64 			return err;
     65 		case let f: io::file =>
     66 			return (f, path);
     67 		};
     68 	};
     69 };
     70 
     71 // Creates a temporary directory. This function only guarantees that the
     72 // directory will have a unique name and be placed in the system temp directory,
     73 // but not that it will be removed automatically; the caller must remove it when
     74 // they're done using it via [[os::rmdir]] or [[os::rmdirall]].
     75 //
     76 // The return value is statically allocated and will be overwritten on
     77 // subsequent calls.
     78 export fn dir() str = {
     79 	let buf: [8]u8 = [0...], name: [16]u8 = [0...];
     80 	random::buffer(buf[..]);
     81 
     82 	let sink = memio::fixed(name);
     83 	const enc = hex::newencoder(&sink);
     84 	io::write(&enc, buf)!;
     85 	let name = memio::string(&sink)!;
     86 
     87 	static let buf = path::buffer { ... };
     88 	path::set(&buf, get_tmpdir(), name)!;
     89 	const path = path::string(&buf);
     90 	match (os::mkdir(path,0o755)) {
     91 	case let err: fs::error => abort("Could not create temp directory");
     92 	case void => void;
     93 	};
     94 	return path;
     95 };