Filesystem(3tcl)

πŸ“¦
version 9.0.4 Β· 3 versions
, in ubuntu stonking

πŸ’– Filesystem β€” Tcl Library Procedures πŸ’–

okay bestie, grab a boba because this one is the whole filesystem toolkit for Tcl, and it is long. This is from the Tcl library procedures manual, section 3tcl, and it's basically the group chat where every filesystem operation Tcl can do shows up. πŸ¦‹


✨ NAME ✨

Here's the whole squad, no cap β€” all of these are procedures to interact with any filesystem:

  • Tcl_FSRegister, Tcl_FSUnregister, Tcl_FSData, Tcl_FSMountsChanged, Tcl_FSGetFileSystemForPath, Tcl_FSGetPathType
  • Tcl_FSCopyFile, Tcl_FSCopyDirectory, Tcl_FSCreateDirectory, Tcl_FSDeleteFile, Tcl_FSRemoveDirectory, Tcl_FSRenameFile, Tcl_FSListVolumes
  • Tcl_FSEvalFile, Tcl_FSEvalFileEx, Tcl_FSLoadFile, Tcl_FSUnloadFile, Tcl_FSMatchInDirectory, Tcl_FSLink, Tcl_FSLstat, Tcl_FSUtime
  • Tcl_FSFileAttrsGet, Tcl_FSFileAttrsSet, Tcl_FSFileAttrStrings, Tcl_FSStat, Tcl_FSAccess, Tcl_FSOpenFileChannel, Tcl_FSGetCwd, Tcl_FSChdir
  • Tcl_FSPathSeparator, Tcl_FSJoinPath, Tcl_FSSplitPath, Tcl_FSEqualPaths, Tcl_FSGetNormalizedPath, Tcl_FSJoinToPath, Tcl_FSConvertToPathType, Tcl_FSGetInternalRep, Tcl_FSGetTranslatedPath, Tcl_FSGetTranslatedStringPath, Tcl_FSNewNativePath, Tcl_FSGetNativePath, Tcl_FSFileSystemInfo
  • Tcl_GetAccessTimeFromStat, Tcl_GetBlockSizeFromStat, Tcl_GetBlocksFromStat, Tcl_GetChangeTimeFromStat, Tcl_GetDeviceTypeFromStat, Tcl_GetFSDeviceFromStat, Tcl_GetFSInodeFromStat, Tcl_GetGroupIdFromStat, Tcl_GetLinkCountFromStat, Tcl_GetModeFromStat, Tcl_GetModificationTimeFromStat, Tcl_GetSizeFromStat, Tcl_GetUserIdFromStat, Tcl_AllocStatBuf, Tcl_FSTildeExpand
not me listing fifty-plus functions in one NAME section πŸ’€ but they're all here, every single one, periodt.

🌸 SYNOPSIS 🌸

first things first, you gotta pull in the header, obviously:

#include <tcl.h>

and now the whole cast, in their exact signatures, because we do not paraphrase code in this house:

int
Tcl_FSRegister(clientData, fsPtr)

int
Tcl_FSUnregister(fsPtr)

void *
Tcl_FSData(fsPtr)

Tcl_FSMountsChanged(fsPtr)

const Tcl_Filesystem *
Tcl_FSGetFileSystemForPath(pathPtr)

Tcl_PathType
Tcl_FSGetPathType(pathPtr)

int
Tcl_FSCopyFile(srcPathPtr, destPathPtr)

int
Tcl_FSCopyDirectory(srcPathPtr, destPathPtr, errorPtr)

int
Tcl_FSCreateDirectory(pathPtr)

int
Tcl_FSDeleteFile(pathPtr)

int
Tcl_FSRemoveDirectory(pathPtr, recursive, errorPtr)

int
Tcl_FSRenameFile(srcPathPtr, destPathPtr)

Tcl_Obj *
Tcl_FSListVolumes(void)

int
Tcl_FSEvalFileEx(interp, pathPtr, encodingName)

int
Tcl_FSEvalFile(interp, pathPtr)

int
Tcl_FSLoadFile(interp, pathPtr, sym1, sym2, proc1Ptr, proc2Ptr, loadHandlePtr, unloadProcPtr)

int
Tcl_FSUnloadFile(interp, loadHandle)

int
Tcl_FSMatchInDirectory(interp, resultPtr, pathPtr, pattern, types)

Tcl_Obj *
Tcl_FSLink(linkNamePtr, toPtr, linkAction)

int
Tcl_FSLstat(pathPtr, statPtr)

int
Tcl_FSUtime(pathPtr, tval)

int
Tcl_FSFileAttrsGet(interp, index, pathPtr, objPtrRef)

int
Tcl_FSFileAttrsSet(interp, index, pathPtr, objPtr)

const char *const *
Tcl_FSFileAttrStrings(pathPtr, objPtrRef)

int
Tcl_FSStat(pathPtr, statPtr)

int
Tcl_FSAccess(pathPtr, mode)

Tcl_Channel
Tcl_FSOpenFileChannel(interp, pathPtr, modeString, permissions)

Tcl_Obj *
Tcl_FSGetCwd(interp)

int
Tcl_FSChdir(pathPtr)

Tcl_Obj *
Tcl_FSPathSeparator(pathPtr)

Tcl_Obj *
Tcl_FSJoinPath(listObj, elements)

Tcl_Obj *
Tcl_FSSplitPath(pathPtr, lenPtr)

int
Tcl_FSEqualPaths(firstPtr, secondPtr)

Tcl_Obj *
Tcl_FSGetNormalizedPath(interp, pathPtr)

Tcl_Obj *
Tcl_FSJoinToPath(basePtr, objc, objv)

int
Tcl_FSConvertToPathType(interp, pathPtr)

void *
Tcl_FSGetInternalRep(pathPtr, fsPtr)

Tcl_Obj *
Tcl_FSGetTranslatedPath(interp, pathPtr)

const char *
Tcl_FSGetTranslatedStringPath(interp, pathPtr)

Tcl_Obj *
Tcl_FSNewNativePath(fsPtr, clientData)

const void *
Tcl_FSGetNativePath(pathPtr)

Tcl_Obj *
Tcl_FSFileSystemInfo(pathPtr)

int
Tcl_FSTildeExpand(interp, pathStr, dsPtr)

Tcl_StatBuf *
Tcl_AllocStatBuf()

long long
Tcl_GetAccessTimeFromStat(statPtr)

unsigned
Tcl_GetBlockSizeFromStat(statPtr)

unsigned long long
Tcl_GetBlocksFromStat(statPtr)

long long
Tcl_GetChangeTimeFromStat(statPtr)

int
Tcl_GetDeviceTypeFromStat(statPtr)

unsigned
Tcl_GetFSDeviceFromStat(statPtr)

unsigned
Tcl_GetFSInodeFromStat(statPtr)

int
Tcl_GetGroupIdFromStat(statPtr)

int
Tcl_GetLinkCountFromStat(statPtr)

unsigned
Tcl_GetModeFromStat(statPtr)

long long
Tcl_GetModificationTimeFromStat(statPtr)

unsigned long long
Tcl_GetSizeFromStat(statPtr)

int
Tcl_GetUserIdFromStat(statPtr)
lowkey iconic how many of these return int and how many hand you back a Tcl_Obj *. that's just the vibe, girlies. πŸ’…

πŸŽ€ ARGUMENTS πŸŽ€

every parameter, its type, and whether it's coming in or going out β€” read the (in) / (out) tags like a vibe check, because they tell you who's giving and who's receiving:

  • const Tcl_Filesystem *fsPtr (in) β€” points at a struct that holds the addresses of all the procedures you can call to do the various filesystem operations. it's the plug, basically. πŸ”Œ
  • const char *pathStr (in) β€” a pointer to a NUL terminated string that represents a file system path.
  • Tcl_Obj *pathPtr (in) β€” the path inside this value is what gets used for whatever operation you're doing. and if the value doesn't already have an internal path representation? it gets converted so it has one. ✨
  • Tcl_Obj *srcPathPtr (in) β€” same deal as pathPtr, but it's the source file for a copy or rename operation.
  • Tcl_Obj *destPathPtr (in) β€” again just like pathPtr, but this one's the destination filename for a copy or rename operation.
  • int recursive (in) β€” whether to also remove subdirectories and everything inside them. πŸ™ˆ
  • const char *encodingName (in) β€” the encoding of the data sitting in the file identified by pathPtr, the one that's gonna get evaluated.
  • const char *pattern (in) β€” only files or directories matching this pattern get returned. ✨
  • Tcl_GlobTypeData *types (in) β€” only files or directories matching the type descriptions inside this structure get returned. this parameter may be NULL.
  • Tcl_Interp *interp (in) β€” the interpreter to use, whether it's for results, for evaluation, or for reporting error messages.
  • void *clientData (in) β€” the native description of the path value that's about to be created.
  • Tcl_Obj *firstPtr (in) β€” the first of two path values being compared. the value may be converted to path type.
  • Tcl_Obj *secondPtr (in) β€” the second of two path values being compared. the value may be converted to path type.
  • Tcl_Obj *listObj (in) β€” the list of path elements to operate on with a join operation.
  • Tcl_Size elements (in) β€” how many elements in listObj should get joined together. if it's negative, then all the elements get joined. πŸ”₯
  • Tcl_Obj **errorPtr (out) β€” if an error happens, this gets filled with a value containing the name of the file that caused the error in the various copy/rename operations.
  • int index (in) β€” the index of the attribute in question.
  • Tcl_Obj *objPtr (in) β€” the value to set in the operation.
  • Tcl_Obj **objPtrRef (out) β€” gets filled with a value containing the result of the operation.
  • Tcl_Obj *resultPtr (out) β€” a preallocated value where you store (using Tcl_ListObjAppendElement) the list of files or directories that got matched successfully. 🌈
  • int mode (in) β€” a mask made of one or more of R_OK, W_OK, X_OK and F_OK. R_OK, W_OK and X_OK ask whether the file exists and has read, write and execute permissions, respectively. F_OK is just asking if the file exists at all. 🫢
  • Tcl_StatBuf *statPtr (out) β€” the structure holding the result of a stat or lstat operation.
okay that's the whole cast list, and there's so much more coming β€” the description section alone is gonna be a whole journey. stay tuned bestie, we're not done. πŸ’œ
  • const char *sym1 (in) β€” bestie, this is the name of a procedure to look up in the file's symbol table πŸ’…
  • const char *sym2 (in) β€” same vibe as sym1: the name of a procedure to look up in the file's symbol table ✨
  • Tcl_LibraryInitProc **proc1Ptr (out) β€” filled with the init function for this code. It's giving "here's your init function" πŸŽ€
  • Tcl_LibraryInitProc **proc2Ptr (out) β€” filled with the safe-init function for this code. Safety first, bestie πŸ›‘οΈ
  • void **clientDataPtr (out) β€” filled with the clientData value to pass to this code's unload function when it is called. Like a little gift for later 🎁
  • Tcl_LoadHandle *loadHandlePtr (out) β€” filled with an abstract token representing the loaded file. It's giving "loaded file receipt" 🧾
  • Tcl_FSUnloadFileProc **unloadProcPtr (out) β€” filled with the function to use to unload this piece of code. Bye-bye code, slay πŸ’…
  • Tcl_LoadHandle loadHandle (in) β€” handle to the loaded library to be unloaded. The main character of unloading πŸ‘‘
  • utimbuf *tval (in) β€” the access and modification times in this structure are read and used to set those values for a given file. Time travel but for file timestamps ⏰
  • const char *modeString (in) β€” specifies how the file is to be accessed. May have any of the values allowed for the mode argument to the Tcl open command. So basically same energy as open mode, iykyk πŸ”“
  • int permissions (in) β€” POSIX-style permission flags such as 0644. If a new file is created, these permissions will be set on the created file. Permission slip but make it Unix πŸŽ€
  • Tcl_Size | int *lenPtr (out) β€” filled with the number of elements in the split path. May be (Tcl_Size *)NULL when not used. If it points to a variable which type is not Tcl_Size, a compiler warning will be generated. If your extensions is compiled with -DTCL_8_API, this function will return NULL for paths having more than INT_MAX elements (which should trigger proper error-handling), otherwise expect it to crash. ⚠️ no cap, this one has lore
  • Tcl_Obj *basePtr (in) β€” the base path on to which to join the given elements. May be NULL. Base camp for path joining πŸ•οΈ
  • Tcl_Size objc (in) β€” the number of elements in objv. Count them, bestie πŸ”’
  • Tcl_Obj *const objv[] (in) β€” the elements to join to the given base path. The squad getting joined πŸ’–
  • Tcl_Obj *linkNamePtr (in) β€” the name of the link to be created or read. Link name, not Link from Zelda 🧚
  • Tcl_Obj *toPtr (in) β€” what the link called linkNamePtr should be linked to, or NULL if the symbolic link specified by linkNamePtr is to be read. Target or read mode, choose your fighter 🎯
  • int linkAction (in) β€” OR-ed combination of flags indicating what kind of link should be created (will be ignored if toPtr is NULL). Valid bits to set are TCL_CREATE_SYMBOLIC_LINK and TCL_CREATE_HARD_LINK. When both flags are set and the underlying filesystem can do either, symbolic links are preferred. Soft link stans rise πŸ¦‹
  • Tcl_DString *dsPtr (out) β€” pointer to a Tcl_DString to hold an output string result. Output goes here, bestie πŸ“

✨ DESCRIPTION ✨

so like, why would you even call the Tcl_FS API functions (think Tcl_FSAccess and Tcl_FSStat) instead of just raw-dogging the system level functions like access and stat directly? bestie, there are several reasons, and they're all iconic πŸ’…

First, they work cross-platform, so an extension that calls them should work unmodified on Unix and Windows. Second, the Windows implementation of some of these functions fixes some bugs in the system level calls. Third, these function calls deal with any β€œUtf to platform-native” path conversions that may be required (and they may cache the results of those conversions for greater efficiency on subsequent calls). Fourth, and maybe most importantly, all of these functions are β€œvirtual filesystem aware”. Any virtual filesystem (VFS for short) that has been registered (through Tcl_FSRegister) may reroute file access to alternative media or access methods. This means that all of these functions (and therefore the corresponding file, glob, pwd, cd, open, etc. Tcl commands) may operate on β€œfiles” that are not native files in the native filesystem. This also means that any Tcl extension that accesses the filesystem (FS for short) through this API is automatically β€œvirtual filesystem aware”. Of course, if an extension accesses the native filesystem directly (through platform-specific APIs, for example), then Tcl cannot intercept such calls.

If appropriate VFSes have been registered, the β€œfiles” may, to give two examples, be remote (e.g. situated on a remote ftp server) or archived (e.g. lying inside a .zip archive). Such registered filesystems provide a lookup table of functions to implement all or some of the functionality listed here. Finally, the Tcl_FSStat and Tcl_FSLstat calls abstract away from what the β€œstruct stat” buffer is actually declared to be, allowing the same code to be used both on systems with and systems without support for files larger than 2GB in size.

The Tcl_FS API is Tcl_Obj-ified and may cache internal representations and other path-related strings (e.g. the current working directory). One side-effect of this is that one must not pass in values with a reference count of zero to any of these functions. If such calls were handled, they might result in memory leaks (under some circumstances, the filesystem code may wish to retain a reference to the passed in value, and so one must not assume that after any of these calls return, the value still has a reference count of zero - it may have been incremented) or in a direct segmentation fault (or other memory access error) due to the value being freed part way through the complex value manipulation required to ensure that the path is fully normalized and absolute for filesystem determination. The practical lesson to learn from this is that

Tcl_Obj *path = Tcl_NewStringObj(...);
Tcl_FSWhatever(path);
Tcl_DecrRefCount(path);

is wrong, and may cause memory errors. The path must have its reference count incremented before passing it in, or decrementing it. For this reason, values with a reference count of zero are considered not to be valid filesystem paths and calling any Tcl_FS API function with such a value will result in no action being taken.

πŸ› οΈ FS API FUNCTIONS πŸ› οΈ

Tcl_FSCopyFile attempts to copy the file given by srcPathPtr to the path name given by destPathPtr. If the two paths given lie in the same filesystem (according to Tcl_FSGetFileSystemForPath) then that filesystem's β€œcopy file” function is called (if it is non-NULL). Otherwise the function returns -1 and sets the errno global C variable to the β€œEXDEV” POSIX error code (which signifies a β€œcross-domain link”). πŸ’…

Tcl_FSCopyDirectory attempts to copy the directory given by srcPathPtr to the path name given by destPathPtr. If the two paths given lie in the same filesystem (according to Tcl_FSGetFileSystemForPath) then that filesystem's β€œcopy file” function is called (if it is non-NULL). Otherwise the function returns -1 and sets the errno global C variable to the β€œEXDEV” POSIX error code (which signifies a β€œcross-domain link”). (Yes bestie, it says β€œcopy file” even for a directory, that's the lore.) πŸŽ€

Tcl_FSCreateDirectory attempts to create the directory given by pathPtr by calling the owning filesystem's β€œcreate directory” function. New folder, who dis? πŸ“

Tcl_FSDeleteFile attempts to delete the file given by pathPtr by calling the owning filesystem's β€œdelete file” function. Gone, periodt πŸ’€

Tcl_FSRemoveDirectory attempts to remove the directory given by pathPtr by calling the owning filesystem's β€œremove directory” function. Directory removal, but make it virtual πŸ—‘οΈ

Tcl_FSRenameFile attempts to rename the file or directory given by srcPathPtr to the path name given by destPathPtr. If the two paths given lie in the same filesystem (according to Tcl_FSGetFileSystemForPath) then that filesystem's β€œrename file” function is called (if it is non-NULL). Otherwise the function returns -1 and sets the errno global C variable to the β€œEXDEV” POSIX error code (which signifies a β€œcross-domain link”). Rename, slay πŸ’…

Tcl_FSListVolumes calls each filesystem which has a non-NULL β€œlist volumes” function and asks them to return their list of root volumes. It accumulates the return values in a list which is returned to the caller (with a reference count of 0). Root volume roundup πŸ”₯

Okay so next up, bestie, we got the file-reading icon herself:

Tcl_FSEvalFileEx reads the file given by pathPtr using the encoding identified by encodingName and evaluates its contents as a Tcl script. It hands back the same info as Tcl_EvalObjEx. If encodingName is NULL, then utf-8 is what we're reading with, no cap. And if the file could not be read? Then a Tcl error comes back at you describing why, because we're not gonna leave you guessing. ✨

Now the tea on eofchar: for files it's "\x1A" (that's ^Z) on all platforms. If you need a literal ^Z in your code for string comparison, use "\x1A" instead β€” the Tcl interpreter will safely substitute it into ^Z for you. And Tcl_FSEvalFile is just the simpler version of Tcl_FSEvalFileEx that always uses the utf-8 encoding when reading the file. Simple queen. πŸ’…


Tcl_FSLoadFile dynamically loads a binary code file into memory and returns the addresses of two procedures within that file, if they are defined. The appropriate function for the filesystem that pathPtr belongs to will be called. And if that filesystem doesn't implement this function (which most virtual filesystems won't, because OS limitations around dynamically loading binary code are giving real barriers 🫠), Tcl will attempt to copy the file to a temporary directory and load that temporary file instead. Workaround era.

Tcl_FSUnloadFile reverses the whole operation, asking for the library indicated by loadHandle to be removed from the process.

⚠️ Heads up bestie: unlike with the unload command, this does not give the library any opportunity to clean up. No goodbye tour, no closure. Just gone.

Both of the above functions return a standard Tcl completion code. If an error occurs, an error message is left in the interp's result.

The token provided via the variable indicated by loadHandlePtr may be used with Tcl_FindSymbol. πŸ”


Tcl_FSMatchInDirectory is the one the globbing code uses to search a directory for all the files matching a given pattern. The appropriate function for the filesystem that pathPtr belongs to will be called, obviously.

The return value is a standard Tcl result indicating whether an error occurred in globbing. Error messages get placed in interp (unless interp is NULL, which is allowed), but the good results β€” the actual matches β€” get placed in the resultPtr you gave. ✨

Note that the glob code implements recursive patterns internally, so this function will only ever be passed simple patterns, which can be matched using the logic of string match. To handle recursion, Tcl will call this function frequently asking only for directories to be returned. And a special case: being called with a NULL pattern indicates that the path needs to be checked only for the correct type. Type check era. πŸŽ€


Tcl_FSLink replaces the library version of readlink, and extends it to support the creation of links too. Glow-up behavior, honestly. The appropriate function for the filesystem that linkNamePtr belongs to will be called.

If toPtr is NULL, a "read link" action is performed. The result is a Tcl_Obj specifying the contents of the symbolic link given by linkNamePtr, or NULL if the link could not be read. That result is owned by the caller, who should call Tcl_DecrRefCount when the result is no longer needed. Don't forget, bestie, that's your cleanup duty. 🧹

If toPtr is not NULL, Tcl should create a link of one of the types passed in the linkAction flag. That flag is an OR'ed combination of TCL_CREATE_SYMBOLIC_LINK and TCL_CREATE_HARD_LINK. Where a choice exists (i.e. more than one flag is passed in), the Tcl convention is to prefer symbolic links β€” slay, soft links stay winning. 🌸

When a link is successfully created, the return value should be toPtr (which is therefore already owned by the caller). If unsuccessful, NULL is returned. 😭


Tcl_FSLstat fills the Tcl_StatBuf structure statPtr with information about the specified file. You don't need any access rights to the file to get this information, but you do need search rights to all directories named in the path leading to the file. Keep that in mind, that's the gotcha. 🫢

The Tcl_StatBuf structure includes info regarding device, inode (always 0 on Windows), privilege mode, nlink (always 1 on Windows), user id (always 0 on Windows), group id (always 0 on Windows), rdev (same as device on Windows), size, last access time, last modification time, and last metadata change time. Windows really said "we're keeping it simple" on half of these. 😭

See PORTABLE STAT RESULT API for a description of how to write portable code to allocate and access the Tcl_StatBuf structure β€” you'll want that if you're doing this cross-platform.

If path exists, Tcl_FSLstat returns 0 and the stat structure is filled with data. Otherwise, -1 is returned, and no stat info is given. That's it. That's the whole vibe check.


Tcl_FSUtime replaces the library version of utime.

This returns 0 on success and -1 on error (as per the utime documentation). If successful, the function will update the "atime" and "mtime" values of the file given. Timestamps getting a makeover. ✨


Tcl_FSFileAttrsGet implements read access for the hookable file attributes subcommand. The appropriate function for the filesystem that pathPtr belongs to will be called.

If the result is TCL_OK, then a value was placed in objPtrRef, which will only be temporarily valid (unless Tcl_IncrRefCount is called).

⚠️ Temporary means temporary bestie, don't go holding onto that pointer like it's forever. πŸ’€

Tcl_FSFileAttrsSet implements write access for the hookable file attributes subcommand. The appropriate function for the filesystem that pathPtr belongs to will be called.

Tcl_FSFileAttrStrings implements part of the hookable file attributes subcommand. The appropriate function for the filesystem that pathPtr belongs to will be called.

The called procedure may either return an array of strings, or may instead return NULL and place a Tcl list into the given objPtrRef. Tcl will take that list and first increment its reference count before using it. On completion of that use, Tcl will decrement its reference count. Hence if the list should be disposed of by Tcl when done, it should have a reference count of zero, and if the list should not be disposed of, the filesystem should ensure it retains a reference count to the value. Refcounting is the whole personality of this section, iykyk. πŸ‘‘


Tcl_FSAccess checks whether the process would be allowed to read, write or test for existence of the file (or other filesystem object) whose name is pathname. And if pathname is a symbolic link on Unix, then the permissions of the file referred by that symbolic link are tested β€” we follow the link, we don't judge the link. πŸ¦‹

On success (all requested permissions granted), zero is returned. On error (at least one bit in mode asked for a permission that is denied, or some other error occurred), -1 is returned.

Tcl_FSStat fills the Tcl_StatBuf structure statPtr with information about the specified file. You do not need any access rights to the file to get this information but you need search rights to all directories named in the path leading to the file. The Tcl_StatBuf structure includes info regarding device, inode (always 0 on Windows), privilege mode, nlink (always 1 on Windows), user id (always 0 on Windows), group id (always 0 on Windows), rdev (same as device on Windows), size, last access time, last modification time, and last metadata change time. See PORTABLE STAT RESULT API for a description of how to write portable code to allocate and access the Tcl_StatBuf structure.

If path exists, Tcl_FSStat returns 0 and the stat structure is filled with data. Otherwise, -1 is returned, and no stat info is given.


Tcl_FSOpenFileChannel opens a file specified by pathPtr and returns a channel handle that can be used to perform input and output on the file. This API is modeled after the fopen procedure of the Unix standard I/O library β€” cute, vintage, we love a classic. πŸŽ€ The syntax and meaning of all arguments is similar to those given in the Tcl open command when opening a file.

If an error occurs while opening the channel, Tcl_FSOpenFileChannel returns NULL and records a POSIX error code that can be retrieved with Tcl_GetErrno. In addition, if interp is non-NULL, Tcl_FSOpenFileChannel leaves an error message in interp's result after any error. So you get both the number and the drama. 😭

The newly created channel is not registered in the supplied interpreter; to register it, use Tcl_RegisterChannel. And if one of the standard channels, stdin, stdout or stderr, was previously closed, the act of creating the new channel also assigns it as a replacement for the standard channel. Surprise promotion! πŸ‘‘


Tcl_FSGetCwd replaces the library version of getcwd.

It returns the Tcl library's current working directory. This may be different from the native platform's working directory, which happens when the current working directory is not in the native filesystem. Two cwd's, two realities, we contain multitudes. πŸŒ€

The result is a pointer to a Tcl_Obj specifying the current directory, or NULL if the current directory could not be determined. If NULL is returned, an error message is left in the interp's result.

The result already has its reference count incremented for the caller. When it is no longer needed, that reference count should be decremented. This is needed for thread-safety purposes, to allow multiple threads to access this and related functions, while ensuring the results are always valid. Thread-safe and self-aware, we stan. πŸ’œ

Tcl_FSChdir replaces the library version of chdir. The path is normalized and then passed to the filesystem which claims it. If that filesystem does not implement this function, Tcl will fallback to a combination of stat and access to check whether the directory exists and has appropriate permissions. Plan B is always loaded. πŸ”₯

For results, see chdir documentation. If successful, we keep a record of the successful path in cwdPathPtr for subsequent calls to Tcl_FSGetCwd. Memory keeping the receipts, as always. πŸ“


Tcl_FSPathSeparator returns the separator character to be used for most specific element of the path specified by pathPtr (i.e. the last part of the path).

The separator is returned as a Tcl_Obj containing a string of length 1. If the path is invalid, NULL is returned. One character. That's all she is. Slay. ✨


Tcl_FSJoinPath takes the given Tcl_Obj, which must be a valid list (which is allowed to have a reference count of zero), and returns the path value given by considering the first elements elements as valid path segments (each path segment may be a complete path, a partial path or just a single possible directory or file name). If any path segment is actually an absolute path, then all prior path segments are discarded β€” absolute says reset, periodt. πŸ’… If elements is less than 0, we use the entire list.

It is possible that the returned value is actually an element of the given list, so the caller should be careful to increment the reference count of the result before freeing the list.

⚠️ Real talk: the returned value typically has a reference count of zero (but it could be shared under some conditions), and it contains the joined path. The caller must add a reference count to the value before using it. In particular, the returned value could be an element of the given list, so freeing the list might free the value prematurely if no reference count has been taken. Do not skip this step bestie, that's how you get a use-after-free moment. πŸ’€

If the number of elements is zero, then the returned value will be an empty-string Tcl_Obj. Empty but valid. Main character energy. πŸŽ€

Tcl_FSSplitPath takes the given Tcl_Obj, which should be a valid path, and returns a Tcl list value containing each segment of that path as an element. It returns a list value with a reference count of zero. If the passed in lenPtr is non-NULL, the variable it points to will be updated to contain the number of elements in the returned list. Counted, listed, delivered. ✨

Tcl_FSEqualPaths is your vibe-check for paths: it asks "are these two actually the same filesystem object?" Returns 1 if they're equal, 0 if they're different. And if either path is NULL? Bestie, it's always 0. Every time. No exceptions. 🫢

Tcl_FSGetNormalizedPath tries to pull a unique normalized path representation out of the given Tcl_Obj β€” one whose string value can be used as a unique identifier for the file. It hands back the normalized path value, owned by Tcl, or NULL if the path was invalid or just couldn't be successfully converted. Extraction of absolute, normalized paths is super efficient (because the filesystem operates on these representations internally), though if the filesystem is stuffed with symbolic links, the result might not be the most user-friendly version of a path. The return value is owned by Tcl and its lifetime is equivalent to that of the pathPtr you passed in (unless that's a relative path, in which case the normalized path value may be freed any time the cwd changes) β€” but the caller can obviously increment the reference count if it wants to keep a copy around longer. πŸ’…

Tcl_FSJoinToPath takes the given value β€” which should usually be a valid path or NULL β€” and joins onto it the array of path segments you gave it. It returns a value, typically with reference count of zero (but it could be shared under some conditions), containing the joined path. The caller must add a reference count to the value before using it. And heads up: if any of the values passed into this function (pathPtr or path elements) have a reference count of zero, they will be freed when this function returns. πŸ“

Tcl_FSConvertToPathType tries to convert the given Tcl_Obj to a valid Tcl path type, taking account of the fact that the cwd may have changed even if this value is already supposedly of the correct type. If the conversion succeeds (i.e. the value is a valid path in one of the current filesystems), then TCL_OK is returned. Otherwise TCL_ERROR is returned, and an error message may be left in the interpreter. πŸ’€

Tcl_FSGetInternalRep extracts the internal representation of a given path value, in the given filesystem. If the path value belongs to a different filesystem, we return NULL. If the internal representation is currently NULL, we attempt to generate it, by calling the filesystem's Tcl_FSCreateInternalRepProc. Returns NULL or a valid internal path representation. This internal representation is cached, so repeated calls to this function will not require additional conversions. ✨

Tcl_FSGetTranslatedPath tries to extract the translated path from the given Tcl_Obj. If the translation succeeds (i.e. the value is a valid path), then it's returned. Otherwise NULL will be returned, and an error message may be left in the interpreter. The value returned is owned by the caller, which must store it or call Tcl_DecrRefCount to ensure memory is freed. Real talk though β€” this function is of little practical use, and Tcl_FSGetNormalizedPath or Tcl_FSGetNativePath are usually better functions to use for most purposes.

Tcl_FSGetTranslatedStringPath does the same as Tcl_FSGetTranslatedPath, but returns a character string or NULL. The string returned is dynamically allocated and owned by the caller, which must store it or call Tcl_Free to ensure it is freed. Again, Tcl_FSGetNormalizedPath or Tcl_FSGetNativePath are usually better functions to use for most purposes. πŸ™ˆ

Tcl_FSNewNativePath does something like the reverse of the usual obj->path->nativerep conversions. If some code retrieves a path in native form (from, e.g. readlink or a native dialog), and that path is to be used at the Tcl level, then calling this function is an efficient way of creating the appropriate path value type. The resulting value is a pure "path" value, which will only receive a UTF-8 string representation if that is required by some Tcl code. πŸ¦‹

Tcl_FSGetNativePath is for use by the Win/Unix native filesystems, so that they can easily retrieve the native (char* or TCHAR*) representation of a path. This function is a convenience wrapper around Tcl_FSGetInternalRep. It may be desirable in the future to have non-string-based native representations (for example, on macOS, a representation using a fileSpec of FSRef structure would probably be more efficient). On Windows a full Unicode representation would allow for paths of unlimited length. Currently the representation is simply a character string which may contain either the relative path or a complete, absolute normalized path in the native encoding (complex conditions dictate which of these will be provided, so neither can be relied upon, unless the path is known to be absolute). If you need a native path which must be absolute, then you should ask for the native version of a normalized path. If for some reason a non-absolute, non-normalized version of the path is needed, that must be constructed separately (e.g. using Tcl_FSGetTranslatedPath). The native representation is cached so that repeated calls to this function will not require additional conversions. The return value is owned by Tcl and has a lifetime equivalent to that of the pathPtr passed in (unless that is a relative path, in which case the native representation may be freed any time the cwd changes). 🌸

Tcl_FSFileSystemInfo returns a list of two elements. The first element is the name of the filesystem (e.g. "native", "vfs", "zip", or "prowrap", perhaps), and the second is the particular type of the given path within that filesystem (which is filesystem dependent). The second element may be empty if the filesystem does not provide a further categorization of files. A valid list value is returned, unless the path value is not recognized, when NULL will be returned. πŸ‘‘

Tcl_FSGetFileSystemForPath returns a pointer to the Tcl_Filesystem which accepts this path as valid. If no filesystem will accept the path, NULL is returned.

Tcl_FSGetPathType determines whether the given path is relative to the current directory, relative to the current volume, or absolute. It returns one of TCL_PATH_ABSOLUTE, TCL_PATH_RELATIVE, or TCL_PATH_VOLUME_RELATIVE. ⭐

Tcl_FSTildeExpand performs tilde substitution on the input path passed via pathStr as described in the documentation for the file tildeexpand Tcl command. On success, the function returns TCL_OK with the result of the substitution in dsPtr which must be subsequently freed by the caller. The dsPtr structure is initialized by the function. No guarantees are made about the form of the returned path such as the path separators used. The returned result should be passed to other Tcl C API functions such as Tcl_FSGetNormalizedPath or Tcl_FSGetNativePath if necessary. On error, the function returns TCL_ERROR with an error message in interp which may be passed as NULL if error messages are not of interest. πŸ’œ


πŸŽ€ PORTABLE STAT RESULT API πŸŽ€

Tcl_AllocStatBuf allocates a Tcl_StatBuf on the system heap (which may be deallocated by being passed to Tcl_Free). This allows extensions to invoke Tcl_FSStat and Tcl_FSLstat without being dependent on the size of the buffer. That in turn depends on the flags used to build Tcl. No cap. πŸ’…

The portable fields of a Tcl_StatBuf may be read using the following functions, each of which returns the value of the corresponding field listed here. Note that on some platforms there may be other fields in the Tcl_StatBuf as it is an alias for a suitable system structure, but only the portable ones are made available here. See your system documentation for a full description of these fields. πŸ”₯

  • Tcl_GetFSDeviceFromStat β€” st_dev
  • Tcl_GetFSInodeFromStat β€” st_ino
  • Tcl_GetModeFromStat β€” st_mode
  • Tcl_GetLinkCountFromStat β€” st_nlink
  • Tcl_GetUserIdFromStat β€” st_uid
  • Tcl_GetGroupIdFromStat β€” st_gid
  • Tcl_GetDeviceTypeFromStat β€” st_rdev
  • Tcl_GetAccessTimeFromStat β€” st_atime
  • Tcl_GetModificationTimeFromStat β€” st_mtime
  • Tcl_GetChangeTimeFromStat β€” st_ctime
  • Tcl_GetSizeFromStat β€” st_size
  • Tcl_GetBlocksFromStat β€” st_blocks
  • Tcl_GetBlockSizeFromStat β€” st_blksize

πŸ’« THE VIRTUAL FILESYSTEM API πŸ’«

okay bestie, so here's the tea β˜• β€” a filesystem hands Tcl a Tcl_Filesystem structure, and that structure is literally just a lil bundle of pointers to functions that do all the actual operations on that filesystem. Those functions get called as needed by the generic layer, which (as a rule) happens through the functions we listed above. no cap. πŸ’…

The Tcl_Filesystem structures get manipulated using these methods right here, bestie β€” memorize them, they're iconic:

  • Tcl_FSRegister β€” takes a pointer to a filesystem structure, plus an optional lil piece of data to associate with that filesystem. The second you call this, Tcl attaches the filesystem to the list of known filesystems and it becomes fully functional immediately, fr fr. and like… Tcl does not check whether you already registered that same filesystem before (and generally speaking, registering it twice is not the move πŸ™ˆ). You get TCL_OK back.
  • Tcl_FSUnregister β€” removes the given filesystem structure from the list of known filesystems if it's actually in there, and returns TCL_OK. If that filesystem isn't currently registered? Yeah, TCL_ERROR, sorry bestie 😭.
  • Tcl_FSData β€” returns the clientData that's associated with the given filesystem, as long as that filesystem is registered. Otherwise it just returns NULL. that's it, that's the whole vibe.
  • Tcl_FSMountsChanged β€” this is the one you call to inform Tcl's core that the set of mount points for the given filesystem (which, reminder, is already registered) has changed, and that cached file representations may therefore no longer be correct. yes this is important, don't skip it ✨.

🌸 THE Tcl_Filesystem STRUCTURE 🌸

This structure has the following fields, bestie, look at her πŸ‘‘

typedef struct {
    const char *typeName;
    Tcl_Size structureLength;
    Tcl_FSVersion version;
    Tcl_FSPathInFilesystemProc *pathInFilesystemProc;
    Tcl_FSDupInternalRepProc *dupInternalRepProc;
    Tcl_FSFreeInternalRepProc *freeInternalRepProc;
    Tcl_FSInternalToNormalizedProc *internalToNormalizedProc;
    Tcl_FSCreateInternalRepProc *createInternalRepProc;
    Tcl_FSNormalizePathProc *normalizePathProc;
    Tcl_FSFilesystemPathTypeProc *filesystemPathTypeProc;
    Tcl_FSFilesystemSeparatorProc *filesystemSeparatorProc;
    Tcl_FSStatProc *statProc;
    Tcl_FSAccessProc *accessProc;
    Tcl_FSOpenFileChannelProc *openFileChannelProc;
    Tcl_FSMatchInDirectoryProc *matchInDirectoryProc;
    Tcl_FSUtimeProc *utimeProc;
    Tcl_FSLinkProc *linkProc;
    Tcl_FSListVolumesProc *listVolumesProc;
    Tcl_FSFileAttrStringsProc *fileAttrStringsProc;
    Tcl_FSFileAttrsGetProc *fileAttrsGetProc;
    Tcl_FSFileAttrsSetProc *fileAttrsSetProc;
    Tcl_FSCreateDirectoryProc *createDirectoryProc;
    Tcl_FSRemoveDirectoryProc *removeDirectoryProc;
    Tcl_FSDeleteFileProc *deleteFileProc;
    Tcl_FSCopyFileProc *copyFileProc;
    Tcl_FSRenameFileProc *renameFileProc;
    Tcl_FSCopyDirectoryProc *copyDirectoryProc;
    Tcl_FSLstatProc *lstatProc;
    Tcl_FSLoadFileProc *loadFileProc;
    Tcl_FSGetCwdProc *getCwdProc;
    Tcl_FSChdirProc *chdirProc;
} Tcl_Filesystem;

so the first three fields in there β€” those are the simple lil data elements, nothing fancy. Every single other entry is an address of a function that the generic filesystem layer calls to do the complete range of filesystem-related actions. yes, all of them. she's thorough πŸ’œ

the three categories, because of course there are three

The many functions in this structure break down into three categories, and you're gonna wanna know which is which:

  1. infrastructure functions β€” almost all of which must be implemented 🫢
  2. operational functions β€” which must be implemented if a complete filesystem is provided
  3. efficiency functions β€” which only need implementing if they can actually be done efficiently, or if they have side-effects that the filesystem requires. because Tcl has less-efficient emulations it can fall back on, so like… you can be lazy, iykyk 😌
⚠️ okay but pay attention here bestie, this part matters: in the current version of Tcl, most of these fallbacks are only used to handle commands initiated in Tcl, not in C. So let's say a file rename command is issued in Tcl, and the relevant filesystem(s) don't implement their Tcl_FSRenameFileProc β€” Tcl's core will fall back on a combination of other filesystem functions instead. Specifically, it'll use Tcl_FSCopyFileProc followed by Tcl_FSDeleteFileProc, and if Tcl_FSCopyFileProc isn't implemented either, there's a further fallback. But β€” and this is the plot twist β€” if a Tcl_FSRenameFileProc command is issued at the C level, no such fallbacks occur. πŸ™…

small but crucial exception: this is true except for the last four entries in the filesystem table (lstat, load, getcwd and chdir), for which fallbacks do in fact occur at the C level. so those four are the main characters of the fallback story πŸ’…

Also! Any functions which take path names in Tcl_Obj form take those names in UTF-8 form. The filesystem infrastructure API is designed to support efficient, cached conversion of these UTF-8 paths to other native representations. slay, efficient, iconic ✨


πŸ¦‹ EXAMPLE FILESYSTEM DEFINITION πŸ¦‹

Here's the filesystem lookup table used by the β€œvfs” extension, which lets filesystem actions be implemented in Tcl. look at her go πŸ₯ΉπŸŒΈ

static Tcl_Filesystem vfsFilesystem = {
    "tclvfs",
    sizeof(Tcl_Filesystem),
    TCL_FILESYSTEM_VERSION_1,
    &VfsPathInFilesystem,
    &VfsDupInternalRep,
    &VfsFreeInternalRep,
    /* No internal to normalized, since we don't create
     * any pure 'internal' Tcl_Obj path representations */
    NULL,
    /* No create native rep function, since we don't use
     * it and don't choose to support uses of
     * Tcl_FSNewNativePath */
    NULL,
    /* Normalize path isn't needed - we assume paths only
     * have one representation */
    NULL,
    &VfsFilesystemPathType,
    &VfsFilesystemSeparator,
    &VfsStat,
    &VfsAccess,
    &VfsOpenFileChannel,
    &VfsMatchInDirectory,
    &VfsUtime,
    /* We choose not to support symbolic links inside our
     * VFS's */
    NULL,
    &VfsListVolumes,
    &VfsFileAttrStrings,
    &VfsFileAttrsGet,
    &VfsFileAttrsSet,
    &VfsCreateDirectory,
    &VfsRemoveDirectory,
    &VfsDeleteFile,
    /* No copy file; use the core fallback mechanism */
    NULL,
    /* No rename file; use the core fallback mechanism */
    NULL,
    /* No copy directory; use the core fallback mechanism */
    NULL,
    /* Core will use stat for lstat */
    NULL,
    /* No load; use the core fallback mechanism */
    NULL,
    /* We don't need a getcwd or chdir; the core's own
     * internal value is suitable */
    NULL,
    NULL
};

see how many of those are just NULL with a lil comment explaining the reasoning? that's the efficiency-functions energy we were just talking about πŸ’– you don't have to implement what you don't need, bestie β€” the core's got you.

✨ FILESYSTEM INFRASTRUCTURE ✨

okay so these fields are the ✨admin✨ fields, bestie β€” they hold the basic info about how a filesystem is structured, plus the addresses of the functions used to (a) link a particular filesystem to a file path, and (b) handle all the messy internal path-representation business. like copying those representations and freeing them. housekeeping, but make it iconic πŸ’…


🏷️ TYPENAME

the typeName field is giving "name tag at a party" β€” it's a null-terminated string that says what type of filesystem we've implemented. think native, zip or vfs. slay πŸŽ€

πŸ“ STRUCTURE LENGTH

structureLength is basically always implemented as sizeof(Tcl_Filesystem). why does it exist? so that if the struct size changes in some future Tcl release, binary backwards compatibility is way easier to keep. future-proofing queen πŸ‘‘

πŸ”’ VERSION

the version field should be set to TCL_FILESYSTEM_VERSION_1. that's it, that's the whole vibe. don't overthink her πŸ’–

πŸ›‚ PATHINFILESYSTEMPROC

pathInFilesystemProc holds the address of a function that gets called to figure out whether a given path value even belongs to this filesystem or not. Tcl will only ever call the rest of the filesystem functions with a path that this function already said TCL_OK to β€” no gatecrashing allowed 🚫

if the path does not belong, return -1. and like… for real, return -1, because the behavior of Tcl for any other return value is not defined πŸ’€

if TCL_OK gets returned, then the optional clientDataPtr output parameter can be used to hand back an internal (filesystem-specific 🫢) representation of the path. that rep gets cached inside the path value, and the other filesystem functions can retrieve it efficiently later. Tcl will simultaneously cache the fact that this path belongs to this filesystem. and btw, those caches get invalidated when filesystem structures are added to or removed from Tcl's internal list of known filesystems. periodt.

typedef int Tcl_FSPathInFilesystemProc(
        Tcl_Obj *pathPtr,
        void **clientDataPtr);

πŸ‘― DUPINTERNALREPPROC

this one makes a copy of a path's internal representation, and it's called whenever Tcl needs to duplicate a path value. if it's NULL, Tcl will simply not copy the internal representation β€” which may then need to be regenerated later. lowkey a glow-down, but it's valid 🌸

typedef void *Tcl_FSDupInternalRepProc(
        void *clientData);

🧹 FREEINTERNALREPPROC

free that internal representation, bestie. you must implement this one if internal representations need freeing (like, if some memory gets allocated when an internal representation is generated) β€” otherwise it may be NULL, and that's fine πŸ¦‹

typedef void Tcl_FSFreeInternalRepProc(
        void *clientData);

πŸͺ„ INTERNALTONORMALIZEDPROC

function that converts an internal representation into a normalized path. only required if the filesystem creates pure path values with no string/path representation. the return value is a Tcl value whose string representation is the normalized path ✨

typedef Tcl_Obj *Tcl_FSInternalToNormalizedProc(
        void *clientData);

πŸ—οΈ CREATEINTERNALREPPROC

takes a path value, calculates an internal representation for it, and stores that native representation inside the value. it may be NULL if paths just have no internal representation at all, or if the Tcl_FSPathInFilesystemProc for this filesystem always immediately creates an internal representation for paths it accepts. main character energy: it does the whole job up front πŸ‘‘

typedef void *Tcl_FSCreateInternalRepProc(
        Tcl_Obj *pathPtr);

πŸͺž NORMALIZEPATHPROC

function to normalize a path. you should implement this for all filesystems that can have multiple string representations for the same path value.

here's the tea β˜• in Tcl, every "path" has to have a single unique "normalized" string representation. but depending on the filesystem, there might be way more than one unnormalized string representation pointing at that same path β€” like a relative path, or a path with different character case when the filesystem is case insensitive, or a path containing symbolic links, etc. so many outfits for one girl πŸ™ˆ

if the very last component in the path is a symbolic link, it should not be converted into the value it points to (but its case or other aspects should still be made unique). every other path component though? those should be converted away from symbolic links. this one exception is required so we agree with Tcl's semantics for file delete, file rename and file copy operating on symbolic links. iykyk πŸ”₯

this function may be called with nextCheckpoint either at the beginning of the path (i.e. zero), at the end of the path, or at any intermediate file separator in the path. it will never point to any other arbitrary position in the path. and in that last of the three valid cases, the implementation can assume that the path up to and including the file separator is known and normalized. no cap.

typedef int Tcl_FSNormalizePathProc(
        Tcl_Interp *interp,
        Tcl_Obj *pathPtr,
        int nextCheckpoint);

πŸ’« FILESYSTEM OPERATIONS πŸ’«

the fields in this section of the structure hold addresses of functions that get called to carry out the basic filesystem operations.

a filesystem that wants to be used with the complete standard Tcl command set must implement all of these 🫑 if some of them aren't implemented, then certain Tcl commands may just… fail when operating on paths inside that filesystem. which is honestly sometimes exactly what you want! for example: a read-only filesystem should not implement the last four functions, and a filesystem that doesn't support symbolic links need not implement the readlink function, etc. the Tcl core expects filesystems to behave in this way. it's giving intentional design, not laziness πŸ’œ

🍭 FILESYSTEMPATHTYPEPROC

function to determine the type of a path inside this filesystem. may be NULL, in which case no type information will be available to users of the filesystem.

the "type" is used purely for informational purposes 🌈 and it should be returned as the string representation of the Tcl_Obj that gets returned. a typical return value might be networked, zip or ftp. the Tcl_Obj result is owned by the filesystem, so Tcl will increment the reference count of that value if it wants to retain a reference to it. sharing is caring but refcounts are law πŸ’…

typedef Tcl_Obj *Tcl_FSFilesystemPathTypeProc(
        Tcl_Obj *pathPtr);

βœ‚οΈ FILESYSTEMSEPARATORPROC

function to return the separator character(s) for this filesystem. you only need to implement this if your filesystem wants to use a different separator than the standard string "/". amongst other uses, it's returned by the file separator command πŸ‘‘

the return value should be a value with reference count of zero. that's the rule, don't fight it πŸŽ€

typedef Tcl_Obj *Tcl_FSFilesystemSeparatorProc(
        Tcl_Obj *pathPtr);

πŸ“Š STATPROC

function to process a Tcl_FSStat call. this one must be implemented for any reasonable filesystem, because sooo many Tcl level commands depend crucially on it β€” like file atime, file isdirectory, file size, and glob. she's booked and busy ⭐

typedef int Tcl_FSStatProc(
        Tcl_Obj *pathPtr,
        Tcl_StatBuf *statPtr);

the Tcl_FSStatProc fills the stat structure statPtr with information about the specified file. fun fact: you don't need any access rights to the file itself to get this info! but you DO need search rights to every directory named in the path leading to the file. searching is free, searching directories is not πŸ•΅οΈ

the stat structure includes info regarding device, inode (always 0 on Windows), privilege mode, nlink (always 1 on Windows), user id (always 0 on Windows), group id (always 0 on Windows), rdev (same as device on Windows), size, last access time, last modification time, and last metadata change time. Windows catching strays, as always πŸ’€

if the file represented by pathPtr exists, Tcl_FSStatProc returns 0 and the stat structure is filled with data. otherwise, -1 is returned, and no stat info is given. that's the whole transaction ✨

πŸšͺ ACCESSPROC

function to process a Tcl_FSAccess call. must be implemented for any reasonable filesystem, because a whole bunch of Tcl level commands depend crucially on it (hi file exists and file readable 🫢).

typedef int Tcl_FSAccessProc(
        Tcl_Obj *pathPtr,
        int mode);

Tcl_FSAccessProc checks whether the process would be allowed to read, write, or test for existence of the file (or other filesystem object) whose name is in pathPtr. and listen: if the pathname refers to a symbolic link, then the permissions of the file that the symbolic link refers to should be tested. don't check the link, check the destination πŸ‘€

on success (all requested permissions granted), zero is returned. on error (at least one bit in mode asked for a permission that is denied, or some other error occurred), -1 is returned. simple as πŸ¦‹

πŸ“‚ OPENFILECHANNELPROC

function to process a Tcl_FSOpenFileChannel call. must be implemented for any reasonable filesystem, since any operation that requires opening or accessing a file's contents will use it β€” e.g. open, encoding, and many Tk commands. she's the whole backbone of the operation πŸ’–

typedef Tcl_Channel Tcl_FSOpenFileChannelProc(
        Tcl_Interp *interp,
        Tcl_Obj *pathPtr,
        int mode,
        int permissions);

Tcl_FSOpenFileChannelProc opens a file specified by pathPtr and returns a channel handle that can be used to perform input and output on the file. this API is modeled after the fopen procedure of the Unix standard I/O library. the syntax and meaning of all arguments is similar to those given in the Tcl open command when opening a file, where the mode argument is a combination of the POSIX flags O_RDONLY, O_WRONLY, etc.

if an error occurs while opening the channel, Tcl_FSOpenFileChannelProc returns NULL and records a POSIX error code that can be retrieved with Tcl_GetErrno. and also β€” if interp is non-NULL, Tcl_FSOpenFileChannelProc leaves an error message in interp's result after any error. receipts left everywhere πŸ“

⚠️ the newly created channel must not be registered in the supplied interpreter by a Tcl_FSOpenFileChannelProc; that task is up to the caller of Tcl_FSOpenFileChannel (if necessary). stay in your lane, bestie 🚧

if one of the standard channels, stdin, stdout or stderr was previously closed, the act of creating the new channel also assigns it as a replacement for the standard channel. recycling era β™»οΈβœ¨

πŸ” MATCHINDIRECTORYPROC

function to process a Tcl_FSMatchInDirectory call. if this one isn't implemented, then glob and recursive copy functionality will be totally lacking in the filesystem β€” and that may impact commands like encoding names which use glob functionality internally. not implementing her = fomo πŸ’€

typedef int Tcl_FSMatchInDirectoryProc(
        Tcl_Interp *interp,
        Tcl_Obj *resultPtr,
        Tcl_Obj *pathPtr,
        const char *pattern,
        Tcl_GlobTypeData *types);

The function should return all files or directories (or other filesystem objects, bestie, we don't discriminate πŸ’…) which match the given pattern and accord with the types specification given. There are two ways this function may be called:

  • If pattern is NULL, then pathPtr is a full path specification of a single file or directory which should be checked for existence and correct type.
  • Otherwise, pathPtr is a directory, and the contents of it should be searched for files or directories which have the correct type.

In either case, pathPtr can be assumed to be both non-NULL and non-empty. It is not currently documented whether pathPtr will have a file separator at its end or not, so code should be flexible to both possibilities. iykyk 🫢

The return value is a standard Tcl result indicating whether an error occurred in the matching process. Error messages are placed in interp, unless interp is NULL in which case no error message need be generated. On a TCL_OK result, results should be added to the resultPtr value given (which can be assumed to be a valid unshared Tcl list). The matches added to resultPtr should include any path prefix given in pathPtr (this usually means they will be absolute path specifications). Note that if no matches are found, that simply leads to an empty result β€” errors are only signaled for actual file or filesystem problems which may occur during the matching process. no cap ✨

The Tcl_GlobTypeData structure passed in the types parameter contains the following fields:

typedef struct {
    /* Corresponds to bcdpfls as in 'find -t' */
    int type;
    /* Corresponds to file permissions */
    int perm;
    /* Acceptable mac type */
    Tcl_Obj *macType;
    /* Acceptable mac creator */
    Tcl_Obj *macCreator;
} Tcl_GlobTypeData;

There are two specific cases which it is important to handle correctly, both when types is non-NULL. The two cases are when types->types & TCL_GLOB_TYPE_DIR or types->types & TCL_GLOB_TYPE_MOUNT are true (and in particular when the other flags are false). πŸ‘‘

  • In the first of these cases, the function must list the contained directories. Tcl uses this to implement recursive globbing, so it is critical that filesystems implement directory matching correctly. 🚨
  • In the second of these cases, with TCL_GLOB_TYPE_MOUNT, the filesystem must list the mount points which lie within the given pathPtr (and in this case, pathPtr need not lie within the same filesystem β€” different to all other cases in which this function is called). Support for this is critical if Tcl is to have seamless transitions from one filesystem to another. 🌈

πŸ•°οΈ UTIMEPROC

Function to process a Tcl_FSUtime call. Required to allow setting (not reading, bestie, just setting) of times with file mtime, file atime and the open-r/open-w/fcopy implementation of file copy.

typedef int Tcl_FSUtimeProc(
        Tcl_Obj *pathPtr,
        struct utimbuf *tval);

The access and modification times of the file specified by pathPtr should be changed to the values given in the tval structure.

The return value should be 0 on success and -1 on an error, as with the system utime. πŸ’€

πŸ”— LINKPROC

Function to process a Tcl_FSLink call. Should be implemented only if the filesystem supports links, and may otherwise be NULL.

typedef Tcl_Obj *Tcl_FSLinkProc(
        Tcl_Obj *linkNamePtr,
        Tcl_Obj *toPtr,
        int linkAction);

If toPtr is NULL, the function is being asked to read the contents of a link. The result is a Tcl_Obj specifying the contents of the link given by linkNamePtr, or NULL if the link could not be read. The result is owned by the caller (and should therefore have its ref count incremented before being returned). Any callers should call Tcl_DecrRefCount on this result when it is no longer needed.

If toPtr is not NULL, the function should attempt to create a link. The result in this case should be toPtr if the link was successful and NULL otherwise. In this case the result is not owned by the caller (i.e. no reference count manipulations on either end are needed). See the documentation for Tcl_FSLink for the correct interpretation of the linkAction flags. ✨

πŸ“¦ LISTVOLUMESPROC

Function to list any filesystem volumes added by this filesystem. Should be implemented only if the filesystem adds volumes at the head of the filesystem, so that they can be returned by file volumes.

typedef Tcl_Obj *Tcl_FSListVolumesProc(void);

The result should be a list of volumes added by this filesystem, or NULL (or an empty list) if no volumes are provided. The result value is considered to be owned by the filesystem (not by Tcl's core), but should be given a reference count for Tcl. Tcl will use the contents of the list and then decrement that reference count. This allows filesystems to choose whether they actually want to retain a "global list" of volumes or not:

  • if not, they generate the list on the fly and pass it to Tcl with a reference count of 1 and then forget about the list πŸ™ˆ
  • if yes, then they simply increment the reference count of their global list and pass it to Tcl which will copy the contents and then decrement the count back to where it was πŸŽ€

Therefore, Tcl considers return values from this proc to be read-only. periodt.

🏷️ FILEATTRSTRINGSPROC

Function to list all attribute strings which are valid for this filesystem. If not implemented the filesystem will not support the file attributes command. This allows arbitrary additional information to be attached to files in the filesystem. If it is not implemented, there is no need to implement the get and set methods.

typedef const char *const *Tcl_FSFileAttrStringsProc(
        Tcl_Obj *pathPtr,
        Tcl_Obj **objPtrRef);

The called function may either return an array of strings, or may instead return NULL and place a Tcl list into the given objPtrRef. Tcl will take that list and first increment its reference count before using it. On completion of that use, Tcl will decrement its reference count. Hence:

  • if the list should be disposed of by Tcl when done, it should have a reference count of zero 🫢
  • if the list should not be disposed of, the filesystem should ensure it returns a value with a reference count of at least one ✨

πŸ“– FILEATTRSGETPROC

Function to process a Tcl_FSFileAttrsGet call, used by file attributes.

typedef int Tcl_FSFileAttrsGetProc(
        Tcl_Interp *interp,
        int index,
        Tcl_Obj *pathPtr,
        Tcl_Obj **objPtrRef);

Returns a standard Tcl return code. The attribute value retrieved, which corresponds to the index'th element in the list returned by the Tcl_FSFileAttrStringsProc, is a Tcl_Obj placed in objPtrRef (if TCL_OK was returned) and is likely to have a reference count of zero. Either way we must either store it somewhere (e.g. the Tcl result), or Incr/Decr its reference count to ensure it is properly freed. 😀

✍️ FILEATTRSSETPROC

Function to process a Tcl_FSFileAttrsSet call, used by file attributes. If the filesystem is read-only, there is no need to implement this.

typedef int Tcl_FSFileAttrsSetProc(
        Tcl_Interp *interp,
        int index,
        Tcl_Obj *pathPtr,
        Tcl_Obj *objPtr);

The attribute value of the index'th element in the list returned by the Tcl_FSFileAttrStringsProc should be set to the objPtr given. πŸ’…

πŸ“ CREATEDIRECTORYPROC

Function to process a Tcl_FSCreateDirectory call. Should be implemented unless the FS is read-only.

typedef int Tcl_FSCreateDirectoryProc(
        Tcl_Obj *pathPtr);

The return value is a standard Tcl result indicating whether an error occurred in the process. If successful, a new directory should have been added to the filesystem in the location specified by pathPtr. new folder just dropped πŸ‘‘

πŸ—‘οΈ REMOVEDIRECTORYPROC

Function to process a Tcl_FSRemoveDirectory call. Should be implemented unless the FS is read-only.

typedef int Tcl_FSRemoveDirectoryProc(
        Tcl_Obj *pathPtr,
        int recursive,
        Tcl_Obj **errorPtr);

The return value is a standard Tcl result indicating whether an error occurred in the process. If successful, the directory specified by pathPtr should have been removed from the filesystem. Here's the tea on the recursive flag:

  • If the recursive flag is given, then a non-empty directory should be deleted without error. βœ…
  • If this flag is not given, and the directory is non-empty, a POSIX <red>EEXIST</red> error should be signaled. 🚨

If an error does occur, the name of the file or directory which caused the error should be placed in errorPtr.

πŸ’€ DELETEFILEPROC

Function to process a Tcl_FSDeleteFile call. Should be implemented unless the FS is read-only.

typedef int Tcl_FSDeleteFileProc(
        Tcl_Obj *pathPtr);

The return value is a standard Tcl result indicating whether an error occurred in the process. If successful, the file specified by pathPtr should have been removed from the filesystem.

Heads up bestie: if the filesystem supports symbolic links, Tcl will always call this function and not Tcl_FSRemoveDirectoryProc when needed to delete them (even if they are symbolic links to directories). don't forget it fr fr πŸ’–

🌈 FILESYSTEM EFFICIENCY 🌈

Okay bestie, listen β€” these ones are optional besties. The core has a fallback implementation for literally all of them, so you do not have to implement any of these for your particular filesystem. But if you're curious what happens when you leave the field NULL, that's spelled out in each lil description below. 🫢


πŸ¦‹ LSTATPROC

Function to process a Tcl_FSLstat call. If you don't implement it, Tcl will just try to use the statProc from above instead. So basically you only need this if your filesystem can actually tell the difference between stat and lstat calls. iykyk ✨

typedef int Tcl_FSLstatProc(
        Tcl_Obj *pathPtr,
        Tcl_StatBuf *statPtr);

The behavior here is very similar to the Tcl_FSStatProc we already talked about, with one teeny tiny exception: if you point it at a symbolic link, it gives you the tea about the link itself, not the target file. πŸ’…


πŸŽ€ COPYFILEPROC

Function to process a Tcl_FSCopyFile call. Not implemented? No stress β€” Tcl falls back on open-r, open-w and fcopy as its copying mechanism. So like... only bother implementing this if your filesystem can do that action more efficiently. Periodt. πŸ”₯

typedef int Tcl_FSCopyFileProc(
        Tcl_Obj *srcPathPtr,
        Tcl_Obj *destPathPtr);

The return value is a standard Tcl result β€” it tells Tcl whether an error happened during the copying process. Now pay attention bestie, this part is important:

  • destPathPtr is the name of the file which should become the copy of srcPathPtr. It is never the name of a directory into which srcPathPtr could be copied. So this function is honestly way simpler than the Tcl level file copy subcommand, no cap. 😌
  • If the filesystem supports symbolic links, Tcl will always call this function and not copyDirectoryProc when it needs to copy them β€” even if they're symbolic links to directories. The way that it just... does that. 🌸
  • And finally: if your filesystem decides nah, it cannot support the file copy action, then call Tcl_SetErrno(EXDEV) and return a non-TCL_OK result, and that tells Tcl to go use its standard fallback mechanisms. Iconic escape hatch behavior. πŸšͺ✨

🧚 RENAMEFILEPROC

Function to process a Tcl_FSRenameFile call. If you don't implement it, Tcl falls back on a copy-and-delete situation. So again β€” only implement if your filesystem can do that action more efficiently. πŸ’–

typedef int Tcl_FSRenameFileProc(
        Tcl_Obj *srcPathPtr,
        Tcl_Obj *destPathPtr);

The return value is a standard Tcl result telling Tcl whether an error happened during the renaming process. And if the filesystem decides it can't support the file rename action at all, call Tcl_SetErrno(EXDEV) and return a non-TCL_OK result β€” that signals Tcl to use its standard fallback mechanisms. Easy. πŸ¦‹


🌸 COPYDIRECTORYPROC

Function to process a Tcl_FSCopyDirectory call. If you leave it out, Tcl falls back on a recursive file mkdir, file copy mechanism. So, yeah β€” only implement this if your filesystem can do that action more efficiently. (Sensing a pattern here? It's giving very consistent design, honestly.) πŸ‘‘

typedef int Tcl_FSCopyDirectoryProc(
        Tcl_Obj *srcPathPtr,
        Tcl_Obj *destPathPtr,
        Tcl_Obj **errorPtr);

The return value is a standard Tcl result indicating whether an error occurred in the copying process. And if an error does occur, then the name of the file or directory which caused that error should be placed in errorPtr. Helpful, right? πŸŽ€

  • Note that destPathPtr is the name of the directory-name which should become the mirror-image of srcPathPtr. It is not the name of a directory into which srcPathPtr should be copied β€” so once again, much simpler than the Tcl level file copy subcommand. Lowkey a whole mood. 😭
  • And finally, if the filesystem determines it cannot support the directory copy action, calling Tcl_SetErrno(EXDEV) and returning a non-TCL_OK result will tell Tcl to use its standard fallback mechanisms. 🫢

πŸ’œ LOADFILEPROC

Function to process a Tcl_FSLoadFile call. If not implemented, Tcl will fall back on a copy to native-temp followed by a Tcl_FSLoadFile on that temporary copy. So you only need this if your filesystem can load code directly β€” OR, plot twist, you can implement it simply to return TCL_ERROR to disable load functionality in this filesystem entirely. Chaotic but valid. πŸ’€

typedef int Tcl_FSLoadFileProc(
        Tcl_Interp *interp,
        Tcl_Obj *pathPtr,
        Tcl_LoadHandle *handlePtr,
        Tcl_FSUnloadFileProc *unloadProcPtr);

Returns a standard Tcl completion code. If an error occurs, an error message gets left in the interp's result. This function dynamically loads a binary code file into memory. πŸ“¦

On a successful load:

  • handlePtr should be filled with a token for the dynamically loaded file
  • unloadProcPtr should be filled in with the address of a procedure

That unload procedure will be called with the given Tcl_LoadHandle as its only parameter when Tcl needs to unload the file. For example, for the native filesystem, the Tcl_LoadHandle returned is currently a token which can be used in the private TclpFindSymbol to access functions in the new code. Each filesystem is free to define the Tcl_LoadHandle as it requires β€” main character energy, define it however you want. ✨

And finally, if the filesystem determines it cannot support the file load action, calling Tcl_SetErrno(EXDEV) and returning a non-TCL_OK result will tell Tcl to use its standard fallback mechanisms. 🌈


⭐ UNLOADFILEPROC

Function to unload a previously successfully loaded file. If load was implemented, then this should also be implemented β€” that is, if there's any cleanup action required. Responsible queen behavior. πŸ‘‘

typedef void Tcl_FSUnloadFileProc(
        Tcl_LoadHandle loadHandle);

πŸ’… GETCWDPROC

Function to process a Tcl_FSGetCwd call. Most filesystems need not implement this. It will usually only be called once, if getcwd is called before chdir. May be NULL. πŸ“

typedef Tcl_Obj *Tcl_FSGetCwdProc(
        Tcl_Interp *interp);

If the filesystem supports a native notion of a current working directory (which might perhaps change independent of Tcl β€” scandalous, honestly), this function should return that cwd as the result. Or NULL if the current directory could not be determined β€” e.g. the user does not have appropriate permissions on the cwd directory. And if NULL is returned, an error message is left in the interp's result. 😭


🌺 CHDIRPROC

Function to process a Tcl_FSChdir call. If filesystems do not implement this, it will be emulated by a series of directory access checks. Otherwise, virtual filesystems which do implement it need only respond with a positive return result if the pathPtr is a valid, accessible directory in their filesystem. πŸ¦‹

They need not remember the result, since that will be automatically remembered for use by Tcl_FSGetCwd. The core's got your back on the memory stuff, bestie. πŸ’–

Real filesystems, though, should carry out the correct action (i.e. call the correct system chdir API). Actually do the thing. 🫢

typedef int Tcl_FSChdirProc(
        Tcl_Obj *pathPtr);

The Tcl_FSChdirProc changes the application's current working directory to the value specified in pathPtr. The function returns -1 on error or 0 on success. That's it, that's the whole vibe check. βœ…


πŸ’– REFERENCE COUNT MANAGEMENT πŸ’–

Okay bestie, gather round, because this is the reference-counting section and it is not the time to freestyle. Get this wrong and you either leak memory or crash the interpreter, and neither is slay. πŸ™ˆ


🫢 PUBLIC API CALLS 🫢

For all of these functions, pathPtr (and yes, that means srcPathPtr and destPathPtr in Tcl_FSCopyFile, Tcl_FSCopyDirectory, and Tcl_FSRenameFile, plus firstPtr and secondPtr in Tcl_FSEqualPaths, and linkNamePtr and toPtr in Tcl_FSLink) must not be a zero reference count value. References may be retained in internal caches even for theoretically read-only operations. These functions may also manipulate the interpreter result if they take and are given a non-NULL interp argument; you must not count on the interpreter result to hold the reference count of any argument value over these calls, and you should manage your own references there. However, references held by the arguments to a Tcl command are suitable for reference count management purposes for the duration of the implementation of that command. Periodt. πŸ’…

  • The errorPtr argument to Tcl_FSCopyDirectory and Tcl_FSRemoveDirectory is, when an object is set into it at all, set to an object with a non-zero reference count that should be passed to Tcl_DecrRefCount when no longer needed. So if you get one, you own a ref and you better give it back. 🫢
  • Tcl_FSListVolumes always returns a zero-reference object, much like Tcl_NewObj. Don't decref what you don't own, bestie.
  • Tcl_FSLink always returns a non-zero-reference object when it is asked to read; you must call Tcl_DecrRefCount on the object once you no longer need it. Read mode means you're holding a ref, so clean up. 🧹
  • Tcl_FSGetCwd always returns a non-zero-reference object; you must call Tcl_DecrRefCount on the object once you no longer need it.
  • Tcl_FSPathSeparator always returns a zero-reference object, much like Tcl_NewObj.
  • Tcl_FSJoinPath always returns a zero-reference object, much like Tcl_NewObj. Its listObj argument can have any reference count; it is only read by this function.
  • Tcl_FSSplitPath always returns a zero-reference object, much like Tcl_NewObj.
  • Tcl_FSGetNormalizedPath returns an object with a non-zero reference count where Tcl is the owner. You should increment its reference count if you want to retain it, but do not need to if you are just using the value immediately. It's giving "borrowed until you make it official" energy. ✨
  • Tcl_FSJoinToPath always returns a zero-reference object, much like Tcl_NewObj. Its basePtr argument follows the rules above for pathPtr, as do the values in the objv argument.
  • Tcl_FSGetTranslatedPath returns a non-zero-reference object (or NULL in the error case); you must call Tcl_DecrRefCount on the object once you no longer need it.
  • Tcl_FSNewNativePath always returns a zero-reference object (or NULL), much like Tcl_NewObj.
  • Tcl_FSFileSystemInfo always returns a zero-reference object (or NULL), much like Tcl_NewObj.
  • The objPtr and objPtrRef arguments to Tcl_FSFileAttrsGet, Tcl_FSFileAttrsSet and Tcl_FSFileAttrStrings are conventional Tcl values; the objPtr argument will be read but not retained, and the objPtrRef argument will have (on success) a zero-reference value written into it (as with Tcl_NewObj). Tcl_FSFileAttrsGet and Tcl_FSFileAttrsSet may also manipulate the interpreter result.
  • The resultPtr argument to Tcl_FSMatchInDirectory will not have its reference count manipulated, but it should have a reference count of no more than 1, and should not be the current interpreter result (as the function may overwrite that on error). Lowkey important, don't skip. 🚨

🧚 VIRTUAL FILESYSTEM INTERFACE 🧚

For all virtual filesystem implementation functions, any pathPtr arguments should not have their reference counts manipulated. If they take an interp argument, they may set an error message in that, but must not manipulate the pathPtr afterwards. Aside from that:

  • internalToNormalizedProc β€” This should return a zero-reference count value, as if allocated with Tcl_NewObj.
  • normalizePathProc β€” Unlike with other API implementation functions, the pathPtr argument here is guaranteed to be an unshared object that should be updated. Its reference count should not be modified.
  • filesystemPathTypeProc β€” The return value (if non-NULL) either has a reference count of zero or needs to be maintained (on a per-thread basis) by the filesystem. Tcl will increment the reference count of the value if it wishes to retain it.
  • filesystemSeparatorProc β€” The return value should be a value with reference count of zero.
  • matchInDirectoryProc β€” The resultPtr argument should be assumed to hold a list that can be appended to (i.e., that has a reference count no greater than 1). No reference to it should be retained.
  • linkProc β€” If toPtr is NULL, this should return a value with reference count 1 that has just been allocated and passed to Tcl_IncrRefCount. If toPtr is not NULL, it should be returned on success.
  • listVolumesProc β€” The result value should be a list (if non-NULL); it will have its reference count decremented once (with Tcl_DecrRefCount) by Tcl once done.
  • fileAttrStringsProc β€” If the result is NULL, the objPtrRef should have a list value written to it; that list will have its reference count both incremented (with Tcl_IncrRefCount) and decremented (with Tcl_DecrRefCount).
  • fileAttrsGetProc β€” The objPtrRef argument should have (on non-error return) a zero reference count value written to it (allocated as if with Tcl_NewObj).
  • fileAttrsSetProc β€” The objPtr argument should either just be read or its reference count incremented to retain it.
  • removeDirectoryProc β€” If an error is being reported, the problem filename reported via errorPtr should be newly allocated (as if with Tcl_NewObj) and have a reference count of 1 (i.e., have been passed to Tcl_IncrRefCount).
  • copyDirectoryProc β€” If an error is being reported, the problem filename reported via errorPtr should be newly allocated (as if with Tcl_NewObj) and have a reference count of 1 (i.e., have been passed to Tcl_IncrRefCount).
  • getCwdProc β€” The result will be passed to Tcl_DecrRefCount by the implementation of Tcl_FSGetCwd after it has been normalized.

🌈 SEE ALSO 🌈

Go read these next, bestie: cd(3tcl), file(3tcl), filename(3tcl), load(3tcl), open(3tcl), pwd(3tcl), source(3tcl), unload(3tcl). It's giving required reading. πŸ“š


πŸ‘‘ KEYWORDS πŸ‘‘

stat, access, filesystem, vfs, virtual filesystem


That's the whole page, and the footer says Tcl 8.4 / Filesystem(3tcl). Mwah, go write some absolutely iconic filesystem code. πŸ’‹βœ¨

πŸ“¦ where this page comes from