File handling and file I/O

web::include

web::include fileName ?msg?

If the file fileName exists, it is sourced (must be a script). Otherwise if the library fileName+"shared lib extension" exists, it is loaded (must be a shared library). Returns 0 on success, 1 otherwise. If an error occurs, an error message is written to msg.

web::readfile

web::readfile file varName msg

Reads file and writes it to varName. Returns 0 on success, 1 otherwise. If an error occurs, an error message is written to the variable msg.

web::lockfile and web::unlockfile

web::lockfile fh

web::unlockfile fh

Interfaces lockf(). lockf() works best on local filesystems. Please read the documentation of lockf() on your system to learn about the problems and limitations of file locking. Note that web::lockfile also performs a seek() and resets the file cursor to the beginning of the file. Note that the file needs to be open for writing.

web::truncatefile

web::truncatefile fh

Interfaces truncate(). Truncates a file based on the file handle, while Tcl's file commands are based on file names. This is used to truncate a file while holding the lock.

Example 14. web::lockfile

set fh [open [web::tempfile] w]
web::lockfile $fh
puts $fh foo
web::unlockfile $fh
close $fh