nim-lang / RFCs

A repository for your Nim proposals.
136 stars 26 forks source link

add `os.touchFile`, `os.touchDir`, mapping to `touch file` and `touch dir` linux commands #263

Closed timotheecour closed 3 years ago

timotheecour commented 3 years ago

semantics

raises if touchDir is applied on a file or touchFile is applied on a dir, or if these calls fail on windows, emulation can be used, eg: https://stackoverflow.com/questions/30011267/windows-equivalent-of-touch-i-e-the-node-js-way-to-create-an-index-html

design choice

dom96 commented 3 years ago

This makes sense in the context of a shell, but for a language? Why not just use writeFile(path, "").

Araq commented 3 years ago

"touching" a file is (almost?) always a workaround for a broken tool that uses timestamps rather than checksums. I can't imagine use cases that are not gross hacks.

timotheecour commented 3 years ago

Why not just use writeFile(path, "").

that has different semantics; touch creates the file if it doesn't exist, else updates the timestamp

This makes sense in the context of a shell, but for a language?

nim is general purpose and users shouldn't have to resort to shelling out to run their scripts (which is also less safe)

it's also built in python >= 3.4 standard library, see https://stackoverflow.com/questions/1158076/implement-touch-using-python

from pathlib import Path
Path('path/to/file.txt').touch()

Path.touch(mode=0o777, exist_ok=True) Create a file at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags. If the file already exists, the function succeeds if exist_ok is true (and its modification time is updated to the current time), otherwise FileExistsError is raised.

"touching" a file is (almost?) always a workaround for a broken tool that uses timestamps rather than checksums. I can't imagine use cases that are not gross hacks.

a doc comment pointing to MD5 hash can be added, but this has its use cases, just like linux's touch + python's pathlib.touch does, if anything, to port scripts, write tests etc.

if it's ommitted, users who want it will likely resort to the unsafe/not portable alternative execShellCmd(fmt"touch {file.quoteSell}") out of laziness.

timotheecour commented 3 years ago

note: as an alternative, I can add it to new fusion/os module (which is expected to grow)

note

would avoid packages having to roll their own versions:

implementation

likeliky similar to https://remarkablemark.org/blog/2017/12/17/touch-file-nodejs/ based on try ... except to avoid race condition

Araq commented 3 years ago

Look, "touch" is really a metaphor for an incredibly poor mechanism which could also be named as updateTimestampOrCreateFile. Known cases where I don't know which of these two unrelated functions I really need: None.

Can we close this now?

timotheecour commented 3 years ago

willing to drop this one for the sake of moving on