beetlebug▾
/

ztcl::file sync

Flushes a channel and syncs its file, or syncs a folder.

ztcl::file sync CHANNEL
ztcl::file sync -dir PATH

Description

With CHANNEL, flushes the buffer of the channel and syncs its file to the disk. The channel stays open, and the result is empty. A channel that is not a file of the disk, such as a socket, a pipe, or a reflected channel, is an error. A file channel that is open for reading has no buffer to flush.

With -dir, syncs the folder PATH, for use after a rename into the folder. The result is empty. On Windows the command checks the path, then does nothing and gives no error, since Windows has no sync of a folder.

Arguments

ArgumentTypeDefaultDescription
CHANNELchannelrequiredThe file channel to flush and sync.
PATHpathrequiredThe folder to sync.

Options

OptionTypeDefaultDescription
-dirflagnoneSyncs the folder PATH in place of a channel.

-dir

Syncs the folder PATH in place of a channel.

With -dir, the argument is a folder path. The command syncs the folder, for use after a rename. On Windows it checks the path, then does nothing and gives no error.

% file mkdir d
> set f [open d/a.txt w]
> puts $f x
> close $f
> file rename d/a.txt d/b.txt
> ztcl::file sync -dir d
% ztcl::file sync -dir
wrong # args: should be "ztcl::file sync -dir path"
-errorcode TCL WRONGARGS

Returns

An empty string.

Examples

% set f [open data.txt w]
> puts -nonewline $f hello
> ztcl::file sync $f
> set g [open data.txt r]
> set d [read $g]
> close $g
> close $f
> set d
hello

Errors

% ztcl::file sync nosuch
can not find channel named "nosuch"
-errorcode TCL LOOKUP CHANNEL nosuch

% proc refl {cmd c args} {
>     if {$cmd eq "initialize"} {return {initialize finalize watch read}}
> }
> set c [chan create read refl]
> ztcl::file sync $c
channel "rc0" is not a file
-errorcode ZTCL FILE SYNC CHANNEL

% ztcl::file sync
wrong # args: should be "ztcl::file sync ?-dir? channel"
-errorcode TCL WRONGARGS

% ztcl::file sync -dir . extra
wrong # args: should be "ztcl::file sync -dir path"
-errorcode TCL WRONGARGS

% ztcl::file sync -dir ../..
couldn't access "../..": permission denied
-errorcode POSIX EACCES {permission denied}

Facts

Capability
file. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.9

See also