Module ztcl::file
ztcl::file writes the data of a file to the disk. A script uses it when the order of the writes matters, for example a file that a program renames over an older file, and then the folder of the rename. It needs the capability file, and each path stays inside the root folder.
ztcl::file sync CHANNEL works on Linux, macOS, Windows, and WASI preview 1. ztcl::file sync -dir PATH works on Linux, macOS, and WASI preview 1. On Windows sync -dir checks the path, then does nothing and gives no error.
set f [open data.txt w]
puts $f hello
ztcl::file sync $f
close $f
ztcl::file sync -dir .
puts [file size data.txt]
6
Synopsis
ztcl::file sync CHANNEL
ztcl::file sync -dir PATH
CHANNEL is the name of a channel from open. The command exists when the embedder turns on file. The ztcl program turns it on with --allow file. It needs no package require.
Commands
sync
ztcl::file sync CHANNEL
Flushes the buffer of CHANNEL, then syncs the file to the disk. The result is empty. The channel stays open. On Linux the command calls fsync, on macOS F_FULLFSYNC, and on Windows FlushFileBuffers.
A channel that is not a file of the disk, such as a socket or a pipe, is an error. A channel of a file that the interpreter opened for reading has no buffer to flush.
sync -dir
ztcl::file sync -dir PATH
Syncs the folder PATH, for use after a rename into the folder. The result is empty. PATH follows the rules of file and open: with a root folder (--root DIR, or fsroot.setRoot in Zig), a path outside the root is an error, and a symlink cannot lead out of it. In host mode, PATH is a path of the host.
On Windows the command checks the path, then does nothing and gives no error, since Windows has no sync of a folder.
Errors
| Error code | Message | Cause |
|---|---|---|
TCL WRONGARGS |
wrong # args: should be "ztcl::file sync ?-dir? channel" |
The command or sync got the wrong count of arguments. |
TCL WRONGARGS |
wrong # args: should be "ztcl::file sync -dir path" |
sync -dir got no path, or got more arguments. |
TCL LOOKUP SUBCOMMAND |
unknown subcommand "X": must be sync |
An unknown subcommand. |
TCL LOOKUP CHANNEL |
can not find channel named "X" |
X is not a channel. |
ZTCL FILE SYNC CHANNEL |
channel "X" is not a file |
The channel is a socket, a pipe, or a reflected channel. |
POSIX ENOENT ... |
could not sync folder: no such file or directory |
PATH does not exist. |
POSIX ENOTDIR ... |
could not sync folder: not a directory |
PATH is not a folder. |
POSIX EACCES ... |
couldn't access "PATH": permission denied |
PATH is outside the root. |
puts [catch {ztcl::file sync nosuch} m]
puts $m
lassign [chan pipe] r w
puts [catch {ztcl::file sync $r} m]
puts [string match {channel "file*" is not a file} $m]
close $r
close $w
1
can not find channel named "nosuch"
1
1
Platforms
- Linux:
fsyncfor a file and for a folder. - macOS:
F_FULLFSYNCfor a file, andfsyncfor a folder. - Windows:
FlushFileBuffersfor a file.sync -dirchecks the path, then does nothing and gives no error. - WASI:
fd_syncof WASI preview 1. WASI 0.3 gives the errorENOSYS.