timotheecour / Nim

Nim is a compiled, garbage-collected systems programming language with a design that focuses on efficiency, expressiveness, and elegance (in that order of priority).
http://nim-lang.org/
Other
2 stars 0 forks source link

tempfiles: make prefix, suffix optional params #765

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago

refs https://github.com/nim-lang/Nim/pull/17920#issue-628517210

juancarlospaco commented 3 years ago

I do not understand why it is not in place, just mutate the string, kinda like func genTempPath(path: var string)

# No alloc, no prefix error, no postfix error, no return error.
var s = "/tmp"
genTempPath s  # genTempPath never touches the disk, works for browser, nodejs, nimscript.
assert s == "/tmp/hdj4k345fjr9"

var s = "/tmp/"
genTempPath s
assert s == "/tmp/34nn4en32kjgf"

var s = "/"
genTempPath s
assert s == "/lk4j5k438gh"

s = "/tmp/prefix$1"   # Notice the  $1
genTempPath s
assert s == "/tmp/prefixnn4r322fd"

s = "/tmp/prefix"
genTempPath s
assert s == "/tmp/prefix/kl54kj72fkwq"

s = "/tmp/prefix$1postfix"
genTempPath s
assert s == "/tmp/prefixn453wer5postfix"

s = "/tmp/prefix$1inbetweenfix$1postfix"
genTempPath s
assert s == "/tmp/prefixn453wer5inbetweenfixkj324jkl453postfix"

# If I really want to write to disk:
writeFile s, ""

# Make several temp files with same string
genTempPath s
writeFile s, ""

when defined nodejs:
  writeFileSync s, ""
timotheecour commented 3 years ago

I do not understand why it is not in place, just mutate the string, kinda like func genTempPath(path: var string)

to handle race conditions (eg if calling from multiple threads, we don't want clashes); implementation uses O_EXCL; future implementations could also use O_TMPFILE

juancarlospaco commented 3 years ago

No, I am not talking about the IO.

I mean the part of generating the temp path and the prefix/postfix arguments.

timotheecour commented 3 years ago

No, I am not talking about the IO.

i was confused because of your comment: genTempPath never touches the disk, works for browser, nodejs, nimscript.

I mean the part of generating the temp path and the prefix/postfix arguments.

given the other costs involved (IO, random path generation), it probably doesn't matter much here performance wise and outline APIs are generally easier to use

juancarlospaco commented 3 years ago

But you have to do defensive on the prefix and postfix arguments, what if postfix contains slash or new line or is empty or etc.

The path generation is not tied to the IO, what if I need to prepare a seq of temp paths for my program, without doing the actual IO yet.

timotheecour commented 3 years ago

what if postfix contains slash or new line or is empty or etc.

only slash matters, all the rest are legal at least in posix (almost, maybe also \0); that's a known TODO we need to address

The path generation is not tied to the IO, what if I need to prepare a seq of temp paths for my program, without doing the actual IO yet.

this goes back to my previous point in https://github.com/timotheecour/Nim/issues/765#issuecomment-870888810; we could add APIs for deferred use (no IO involved), but the interesting (and difficult) API is the one that mixes in IO to guarantee that path is exclusive, which is the one std/tempfile was designed to tackle first (deferred use would not offer such guarantees)