Filesystem(3)
โจ NAME โจ
okay so bestie, this is the Filesystem(3) page from the Tcl Library Procedures manual, and it is literally the whole C-level filesystem plug-in situation. it's procedures to interact with any filesystem โ yes, any. iconic behavior tbh. here's the full roster of functions this page covers:
Tcl_FSRegisterTcl_FSUnregisterTcl_FSDataTcl_FSMountsChangedTcl_FSGetFileSystemForPathTcl_FSGetPathTypeTcl_FSCopyFileTcl_FSCopyDirectoryTcl_FSCreateDirectoryTcl_FSDeleteFileTcl_FSRemoveDirectoryTcl_FSRenameFileTcl_FSListVolumesTcl_FSEvalFileTcl_FSEvalFileExTcl_FSLoadFileTcl_FSUnloadFileTcl_FSMatchInDirectoryTcl_FSLinkTcl_FSLstatTcl_FSUtimeTcl_FSFileAttrsGetTcl_FSFileAttrsSetTcl_FSFileAttrStringsTcl_FSStatTcl_FSAccessTcl_FSOpenFileChannelTcl_FSGetCwdTcl_FSChdirTcl_FSPathSeparatorTcl_FSJoinPathTcl_FSSplitPathTcl_FSEqualPathsTcl_FSGetNormalizedPathTcl_FSJoinToPathTcl_FSConvertToPathTypeTcl_FSGetInternalRepTcl_FSGetTranslatedPathTcl_FSGetTranslatedStringPathTcl_FSNewNativePathTcl_FSGetNativePathTcl_FSFileSystemInfoTcl_GetAccessTimeFromStatTcl_GetBlockSizeFromStatTcl_GetBlocksFromStatTcl_GetChangeTimeFromStatTcl_GetDeviceTypeFromStatTcl_GetFSDeviceFromStatTcl_GetFSInodeFromStatTcl_GetGroupIdFromStatTcl_GetLinkCountFromStatTcl_GetModeFromStatTcl_GetModificationTimeFromStatTcl_GetSizeFromStatTcl_GetUserIdFromStatTcl_AllocStatBufTcl_FSTildeExpand
that's the whole squad. they handle paths, dirs, files, stats, symlinks, mounts, eval, load/unload, and every little filesystem vibe you can think of. periodt. ๐
โจ SYNOPSIS โจ
so if you're in C and you wanna call these, you start with:
#include <tcl.h>
then the prototypes are exactly this, bestie โ no cap, don't change a character:
#include <tcl.h>
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)
โจ ARGUMENTS โจ
here's the tea on every argument, bestie. (in) means you pass it in, (out) means Tcl fills it for you. don't mix them up, okay? ๐
const Tcl_Filesystem *fsPtr (in)โ points to a struct packed with addresses of procedures that do the various filesystem operations. basically your custom filesystem's control panel.const char *pathStr (in)โ a pointer to a NUL-terminated string that represents a file system path.Tcl_Obj *pathPtr (in)โ the path this value holds is what gets used for the operation. if it doesn't already have an internal path representation, Tcl converts it so it does. lowkey magic.Tcl_Obj *srcPathPtr (in)โ same energy aspathPtr, but it's the source file for a copy or rename.Tcl_Obj *destPathPtr (in)โ same energy aspathPtr, but it's the destination filename for a copy or rename.int recursive (in)โ whether to remove subdirectories and all their contents too.const char *encodingName (in)โ the encoding of the data stored in the file identified bypathPtrthat's about to be 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 struct get returned. this parameter can beNULL.Tcl_Interp *interp (in)โ the interpreter used for results, evaluation, or reporting error messages.void *clientData (in)โ the native description of the path value to create.Tcl_Obj *firstPtr (in)โ the first of two path values to compare. the value may be converted to path type.Tcl_Obj *secondPtr (in)โ the second of two path values to compare. 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)โ the number of elements inlistObjthat should be joined together. if negative, then all elements are joined.Tcl_Obj **errorPtr (out)โ if there's an error, 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 (usingTcl_ListObjAppendElement) the list of files or directories that were successfully matched.int mode (in)โ a mask made of one or more ofR_OK,W_OK,X_OK, andF_OK.R_OK,W_OK, andX_OKrequest checking whether the file exists and has read, write, and execute permissions, respectively.F_OKjust requests checking for the existence of the file.Tcl_StatBuf *statPtr (out)โ the structure that contains the result of astatorlstatoperation.const char *sym1(in) โ the name of a procedure we're gonna go look up in the file's symbol table ๐const char *sym2(in) โ also the name of a procedure to look up in the file's symbol table (yeah bestie, twin behavior, both of them do the exact same job)Tcl_LibraryInitProc **proc1Ptr(out) โ gets filled in with the init function for this code โจTcl_LibraryInitProc **proc2Ptr(out) โ gets filled in with the safe-init function for this code ๐void **clientDataPtr(out) โ gets filled in with theclientDatavalue that gets handed to this code's unload function whenever it's calledTcl_LoadHandle *loadHandlePtr(out) โ gets filled in with an abstract token that represents the loaded fileTcl_FSUnloadFileProc **unloadProcPtr(out) โ gets filled in with the function you use to unload this piece of codeTcl_LoadHandle loadHandle(in) โ a handle to the loaded library that's about to be unloaded, bye bb ๐utimbuf *tval(in) โ the access and modification times sitting in this struct get read, and then used to set those exact values on a given file โฐconst char *modeString(in) โ says how the file is supposed to be accessed. It can have any of the values that are allowed for the mode argument to the Tclopencommand ๐int permissions(in) โ POSIX-style permission flags like0644. If a brand new file gets created, these permissions are what get set on it ๐Tcl_Size | int *lenPtr(out) โ gets filled in with how many elements are in the split path. Can be(Tcl_Size *)NULLwhen it's not being used. If it points at a variable whose type isn'tTcl_Size, a compiler warning is gonna pop off. If your extension is compiled with-DTCL_8_API, this function returnsNULLfor paths that have more thanINT_MAXelements (which should trigger proper error-handling), otherwise bestieโฆ expect it to crash ๐Tcl_Obj *basePtr(in) โ the base path that we're joining the given elements onto. May beNULL.Tcl_Size objc(in) โ how many elements are inobjvTcl_Obj *const objv[](in) โ the elements that get joined onto the given base path ๐งฉTcl_Obj *linkNamePtr(in) โ the name of the link that's getting created or readTcl_Obj *toPtr(in) โ what the link calledlinkNamePtrshould be linked to, orNULLif the symbolic link specified bylinkNamePtris the thing being readint linkAction(in) โ an OR-ed combination of flags saying what kind of link should get created (this gets ignored iftoPtrisNULL). The valid bits you can set areTCL_CREATE_SYMBOLIC_LINKandTCL_CREATE_HARD_LINK. When both flags are set and the underlying filesystem can do either one, symbolic links are the preferred pick ๐Tcl_DString *dsPtr(out) โ a pointer to aTcl_DStringthat holds an output string result
๐ธ DESCRIPTION ๐ธ
okay so, why would you even call the Tcl_FS API functions (like Tcl_FSAccess and Tcl_FSStat) instead of just calling system level functions like access and stat directly? bestie there are several reasons and honestly they all eat ๐
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 that exist in the system level calls. Third, these function calls deal with any โUtf to platform-nativeโ path conversions that might be required (and they may cache the results of those conversions for greater efficiency on subsequent calls). Fourth, and maybe most importantly of all, all of these functions are โvirtual filesystem awareโ. Any virtual filesystem (VFS for short) that's been registered (through Tcl_FSRegister) may reroute file access to alternative media or access methods. That means 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. It also means any Tcl extension that accesses the filesystem (FS for short) through this API is automatically โvirtual filesystem awareโ. And obviously, if an extension goes and accesses the native filesystem directly (through platform-specific APIs, for example), then Tcl cannot intercept those calls. no cap ๐ซ
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 that implement all or some of the functionality listed here. And finally, the Tcl_FSStat and Tcl_FSLstat calls abstract away from what the โstruct statโ buffer is actually declared to be, which lets the same code 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 you 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 that's required to ensure 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. periodt.
๐จ tl;dr: refcount of zero = do not pass go, do not collect $200, nothing happens and you might just crash.
๐ฆ 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โ).
Tcl_FSCreateDirectory attempts to create the directory given by pathPtr by calling the owning filesystem's โcreate directoryโ function ๐
Tcl_FSDeleteFile attempts to delete the file given by pathPtr by calling the owning filesystem's โdelete fileโ function ๐๏ธ
Tcl_FSRemoveDirectory attempts to remove the directory given by pathPtr by calling the owning filesystem's โremove directoryโ function
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โ).
Tcl_FSListVolumes calls each filesystem that 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). โญ
๐ฆ Tcl_FSEvalFileEx ๐ฆ
okay so bestie, Tcl_FSEvalFileEx reads the file given by pathPtr using the encoding identified by encodingName, and then evaluates its contents as a Tcl script. It hands you back the same information as Tcl_EvalObjEx. If encodingName is NULL, then utf-8 is the encoding used for reading the file contents. And if the file could not be read, a Tcl error gets returned to describe why the file could not be read. No mystery, no silence, just receipts. ๐
The eofchar for files is "\x1A" (^Z) for all platforms. So if you require a "^Z" in code for string comparison, you can use "\x1A", which will be safely substituted by the Tcl interpreter into "^Z". iykyk โจ
Tcl_FSEvalFile is the simpler version of Tcl_FSEvalFileEx โ it always uses the utf-8 encoding when reading the file. lowkey the chill one.
๐ Tcl_FSLoadFile & Tcl_FSUnloadFile ๐
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 to which pathPtr belongs will be called. And if that filesystem does not implement this function (which, spoiler alert, most virtual filesystems will not, because of OS limitations in dynamically loading binary code), Tcl will attempt to copy the file to a temporary directory and load that temporary file instead. adaptive queen behavior. ๐ซถ
Tcl_FSUnloadFile reverses the operation, asking for the library indicated by the loadHandle to be removed from the process. But heads up bestie: note that, unlike with the unload command, this does not give the library any opportunity to clean up. No goodbye party, no last words. ๐ญ
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. handy.
๐ Tcl_FSMatchInDirectory ๐
Tcl_FSMatchInDirectory is what the globbing code uses to search a directory for all files which match a given pattern. The appropriate function for the filesystem to which pathPtr belongs will be called. ๐ฆ
The return value is a standard Tcl result indicating whether an error occurred in globbing. Error messages are placed in interp (unless interp is NULL, which is allowed), but good results are placed in the resultPtr given. errors go left, wins go right. โจ
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. A special case of being called with a NULL pattern indicates that the path needs to be checked only for the correct type. type check era.
๐ Tcl_FSLink ๐
Tcl_FSLink replaces the library version of readlink, and extends it to support the creation of links. The appropriate function for the filesystem to which linkNamePtr belongs will be called.
If the 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. The result is owned by the caller, which should call Tcl_DecrRefCount when the result is no longer needed. clean up after yourself bestie, it's giving responsible. ๐
If the toPtr is not NULL, Tcl should create a link of one of the types passed in in the linkAction flag. This 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. symbolic 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. simple as.
๐ธ Tcl_FSLstat ๐ธ
Tcl_FSLstat 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 do need search rights to all directories named in the path leading to the file. permission check, but make it specific.
The Tcl_StatBuf structure includes info regarding:
- device
- inode (always
0on Windows) - privilege mode
- nlink (always
1on Windows) - user id (always
0on Windows) - group id (always
0on Windows) - rdev (same as device on Windows)
- size
- last access time
- last modification time
- 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_FSLstat returns 0 and the stat structure is filled with data. Otherwise, -1 is returned, and no stat info is given. nothing, zero, nada. ๐
๐ง Tcl_FSUtime ๐ง
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. time travel but make it metadata. โญ
๐ Tcl_FSFileAttrsGet ๐
Tcl_FSFileAttrsGet implements read access for the hookable file attributes subcommand. The appropriate function for the filesystem to which pathPtr belongs 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, so don't get attached bestie. ๐
๐ Tcl_FSFileAttrsSet ๐
Tcl_FSFileAttrsSet implements write access for the hookable file attributes subcommand. The appropriate function for the filesystem to which pathPtr belongs will be called. the write girlie of the duo. ๐
๐ Tcl_FSFileAttrStrings ๐
Tcl_FSFileAttrStrings implements part of the hookable file attributes subcommand. The appropriate function for the filesystem to which pathPtr belongs 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
- if the list should not be disposed of, the filesystem should ensure it retains a reference count to the value
refcounting is a whole personality trait and honestly? respect. โจ
๐ Tcl_FSAccess ๐
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. If pathname is a symbolic link on Unix, then permissions of the file referred by this symbolic link are tested. follows the link, doesn't judge the link itself.
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. denied is denied bestie. ๐ญ
๐ธ Tcl_FSStat ๐ธ
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 do need search rights to all directories named in the path leading to the file. same rule as before, no shortcuts.
The Tcl_StatBuf structure includes info regarding:
- device
- inode (always
0on Windows) - privilege mode
- nlink (always
1on Windows) - user id (always
0on Windows) - group id (always
0on Windows) - rdev (same as device on Windows)
- size
- last access time
- last modification time
- 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. iconic mirror of Tcl_FSLstat, we love consistency.
๐ฆ Tcl_FSOpenFileChannel ๐ฆ
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. The syntax and meaning of all arguments is similar to those given in the Tcl open command when opening a file. same vibes, different entry point.
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. double the error reporting, double the clarity. โจ
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. glow up: you're the new stdout now. ๐
๐ Tcl_FSGetCwd ๐
Tcl_FSGetCwd replaces the library version of getcwd.
It returns the Tcl library's current working directory. This may be different to the native platform's working directory, which happens when the current working directory is not in the native filesystem. plot twist, but a legal one.
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 unbothered, as she should be. ๐
๐ Tcl_FSChdir ๐
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 in the bag. ๐ซถ
For results, see chdir documentation. If successful, we keep a record of the successful path in cwdPathPtr for subsequent calls to Tcl_FSGetCwd. remembering your location for later, very main character of her. โญ
๐ง Tcl_FSPathSeparator ๐ง
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. invalid path, invalid vibes, no separator for you. ๐
๐ Tcl_FSJoinPath ๐
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. If elements is less than 0, we use the entire list. negative number = "do the whole thing", noted. โจ
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. bestie please, the refcount, it matters. ๐ญ
The returned value, typically with a reference count of zero (but it could be shared under some conditions), 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. If the number of elements is zero, then the returned value will be an empty-string Tcl_Obj. zero elements, zero string, zero drama. ๐
๐ Tcl_FSSplitPath ๐
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. split it, count it, slay it โจ ๐
okay bestie, buckle up, we're deep in the filesystem lore now โ this is the part where Tcl hands you a whole โจtoolkitโจ of path-wrangling functions. let's go ๐
Tcl_FSEqualPaths is literally just doing a vibe check on two paths: do they point at the same filesystem object or not? It returns 1 if the paths are equal, and 0 if they're different. And if either path is NULL? Yeah no, 0 always comes back. Bye. ๐
Tcl_FSGetNormalizedPath is out here trying to extract from the given Tcl_Obj a unique normalized path representation, whose string value can be used as a unique identifier for the file. iconic
It returns the normalized path value, owned by Tcl, or NULL if the path was invalid or could otherwise not be successfully converted. Extraction of absolute, normalized paths is very efficient (because the filesystem operates on these representations internally), although the result when the filesystem contains numerous symbolic links may not be the most user-friendly version of a path. So like, it's fast but sometimes ugly, we don't judge. ๐ 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 normalized path value may be freed any time the cwd changes) โ the caller can of course increment the reference count if it wishes to maintain a copy for longer. reference counting girlies rise ๐ซถ
Tcl_FSJoinToPath takes the given value, which should usually be a valid path or NULL, and joins onto it the array of paths segments given. Slay, concatenation era. โจ
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. 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. So like, don't be caught lacking, add that refcount. ๐
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. cwd really said "plot twist" ๐ญ
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. making it work, making it cute ๐
Returns NULL or a valid internal path representation. This internal representation is cached, so that repeated calls to this function will not require additional conversions. Caching? Obsessed. ๐ฅ
Tcl_FSGetTranslatedPath attempts to extract the translated path from the given Tcl_Obj.
If the translation succeeds (i.e. the value is a valid path), then it is 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. do NOT forget the decr ref count, bestie, that's a memory leak and we are not doing that today ๐จ
Real talk though: this function is of little practical use, andTcl_FSGetNormalizedPathorTcl_FSGetNativePathare usually better functions to use for most purposes. Like, it's giving deprecated-adjacent energy. ๐
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. Same warning, same vibes. ๐
Tcl_FSNewNativePath performs 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. glow up, native path โ Tcl path ๐ฆ
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. So it's lowkey mysterious until someone asks. ๐
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. future goals, manifesting โจ 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). cwd strikes again, that little menace ๐ค
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. she's giving mysterious, she's giving minimal ๐
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. we love a match ๐
If no filesystem will accept the path, NULL is returned. Rejection era. ๐
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. Three options, pick your fighter. ๐
Tcl_FSTildeExpand performs tilde substitution on the input path passed via pathStr as described in the documentation for the file tildeexpand Tcl command. yes bestie, ~ expansion, the people's favourite ๐ก 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. if you don't care about the drama, pass NULL and move on ๐
๐ PORTABLE STAT RESULT API ๐
this is the part where we get our stats read, and no we're not talking about follower count ๐
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. portable queen behaviour ๐ฆ
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 below. 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โ returnsst_dev๐Tcl_GetFSInodeFromStatโ returnsst_inoโจTcl_GetModeFromStatโ returnsst_mode๐Tcl_GetLinkCountFromStatโ returnsst_nlink๐ฆTcl_GetUserIdFromStatโ returnsst_uid๐ธTcl_GetGroupIdFromStatโ returnsst_gid๐Tcl_GetDeviceTypeFromStatโ returnsst_rdev๐ซถTcl_GetAccessTimeFromStatโ returnsst_atime๐ญTcl_GetModificationTimeFromStatโ returnsst_mtime๐ฅTcl_GetChangeTimeFromStatโ returnsst_ctime๐Tcl_GetSizeFromStatโ returnsst_size๐Tcl_GetBlocksFromStatโ returnsst_blocksโญTcl_GetBlockSizeFromStatโ returnsst_blksize๐
thirteen functions, thirteen fields, zero notes, we ate and left no crumbs ๐
โจ THE VIRTUAL FILESYSTEM API โจ
okay bestie, so a filesystem provides a Tcl_Filesystem structure. That struct is basically a glow-up bag of function pointers ๐
โ pointers to functions that implement the various operations on a filesystem. Those operations get invoked as needed by the generic layer, which generally occurs through the functions listed above. no cap.
The Tcl_Filesystem structures are manipulated using the following methods. periodt.
Tcl_FSRegisterโ takes a pointer to a filesystem structure and an optional piece of data to associated with that filesystem. When you call this function, Tcl will attach the filesystem to the list of known filesystems, and it will become fully functional immediately. Tcl does not check if the same filesystem is registered multiple times (and in general that is not a good thing to do ๐).TCL_OKwill be returned. slay.Tcl_FSUnregisterโ removes the given filesystem structure from the list of known filesystems, if it is known, and returnsTCL_OK. If the filesystem is not currently registered,TCL_ERRORis returned. rude but valid ๐.Tcl_FSDataโ will return theclientDataassociated with the given filesystem, if that filesystem is registered. Otherwise it will returnNULL. iconic.Tcl_FSMountsChangedโ is used to inform the Tcl's core that the set of mount points for the given (already registered) filesystem have changed, and that cached file representations may therefore no longer be correct. so if mounts move, cached reps might be lying bestie ๐ญ.
๐ง THE TCL_FILESYSTEM STRUCTURE ๐ง
The Tcl_Filesystem structure contains the following fields:
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;
Except for the first three fields in this structure โ which contain simple data elements โ all entries contain addresses of functions called by the generic filesystem layer to perform the complete range of filesystem related actions. literally the whole menu ๐
The many functions in this structure are broken down into three categories:
- infrastructure functions โ almost all of which must be implemented. mandatory bestie.
- operational functions โ which must be implemented if a complete filesystem is provided.
- efficiency functions โ which need only be implemented if they can be done so efficiently, or if they have side-effects which are required by the filesystem. Tcl has less efficient emulations it can fall back on. so if you're not fast, Tcl got a backup dancer ๐.
It is important to note that, in the current version of Tcl, most of these fallbacks are only used to handle commands initiated in Tcl, not in C.
What this means is, that if a file rename command is issued in Tcl, and the relevant filesystem(s) do not implement their Tcl_FSRenameFileProc, Tcl's core will instead fallback on a combination of other filesystem functions (it will use Tcl_FSCopyFileProc followed by Tcl_FSDeleteFileProc, and if Tcl_FSCopyFileProc is not implemented there is a further fallback). However, if a Tcl_FSRenameFileProc command is issued at the C level, no such fallbacks occur. 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 C stays strict, except those four got exceptions, iykyk ๐ซถ.
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. UTF-8 to native rep pipeline, ate โจ.
๐ EXAMPLE FILESYSTEM DEFINITION ๐
Here is the filesystem lookup table used by the โvfsโ extension which allows filesystem actions to be implemented in Tcl.
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
};
๐๏ธ FILESYSTEM INFRASTRUCTURE ๐๏ธ
okay so these fields are the โจplumbingโจ โ basic info about how your filesystem is built, plus the addresses of the functions that glue a particular filesystem to a file path and handle all the internal path-representation business (copying them, freeing them, the whole backstage situation). lowkey essential.
TYPENAME
The typeName field is a null-terminated string that tells the world what kind of filesystem you implemented. Like, its whole personality. Classic examples: "native", "zip", or "vfs". ๐
STRUCTURE LENGTH
The structureLength field is, like, basically always implemented as sizeof(Tcl_Filesystem), and it exists so that binary backwards compatibility stays cute and easy if the size of the structure ever changes in a future Tcl release. future-proofing queen ๐
VERSION
Set the version field to TCL_FILESYSTEM_VERSION_1. That's it. That's the whole assignment. periodt โจ
PATHINFILESYSTEMPROC
The pathInFilesystemProc field holds the address of the function that decides whether a given path value belongs to this filesystem or not. Bestie, this is the bouncer at the club ๐ช โ Tcl will only call the rest of the filesystem functions with a path that this function already returned TCL_OK for.
- If the path does not belong here, return
-1. (And heads up: the behavior of Tcl for any other return value is not defined, so don't get creative. ๐) - If you return
TCL_OK, then that optionalclientDataPtroutput parameter can be used to return an internal, filesystem-specific representation of the path. Tcl caches it inside the path value, and the other filesystem functions can grab it back efficiently later. Iconic.
Tcl will also cache the fact that this path belongs to this filesystem. Those caches get invalidated when filesystem structures are added to or removed from Tcl's internal list of known filesystems. vibe check on the registry, fr fr.
typedef int Tcl_FSPathInFilesystemProc(
Tcl_Obj *pathPtr,
void **clientDataPtr);
DUPINTERNALREPPROC
This one makes a copy of a path's internal representation, and Tcl calls it whenever it needs to duplicate a path value. ๐ฆ If it's NULL, Tcl just won't copy the internal representation at all โ which means it might need to be regenerated later. no copy, no drama, but alsoโฆ more work later.
typedef void *Tcl_FSDupInternalRepProc(
void *clientData);
FREEINTERNALREPPROC
Free the internal representation. You must implement this if your internal representations need freeing (i.e. if some memory gets allocated when an internal representation is generated) โ otherwise it can be NULL and you're chilling. ๐ง
typedef void Tcl_FSFreeInternalRepProc(
void *clientData);
INTERNALTONORMALIZEDPROC
Function to convert 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. slay โจ
typedef Tcl_Obj *Tcl_FSInternalToNormalizedProc(
void *clientData);
CREATEINTERNALREPPROC
Function that takes a path value, calculates an internal representation for it, and stores that native representation in the value. May be NULL if paths have no internal representation, or if the Tcl_FSPathInFilesystemProc for this filesystem always immediately creates an internal representation for the paths it accepts. either way, someone's doing the work.
typedef void *Tcl_FSCreateInternalRepProc(
Tcl_Obj *pathPtr);
NORMALIZEPATHPROC
Function to normalize a path. You should implement this for every filesystem that can have multiple string representations for the same path value.
Here's the tea โ: in Tcl, every "path" must have a single unique "normalized" string representation. But depending on your filesystem, there may be more than one unnormalized string representation pointing at that same path โ like a relative path, or a path with different character case if the filesystem is case insensitive, or a path containing symbolic links, etc.
Rules of the game:
- 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).
- All other path components should be converted from symbolic links.
- That one exception is required so everything agrees with Tcl's semantics for
file delete,file rename, andfile copywhen they operate on symbolic links. don't break the link, bestie ๐
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. In that last of the three valid cases, the implementation can assume that the path up to and including the file separator is already known and normalized. so you can just trust it, fr fr ๐
typedef int Tcl_FSNormalizePathProc(
Tcl_Interp *interp,
Tcl_Obj *pathPtr,
int nextCheckpoint);
โ๏ธ FILESYSTEM OPERATIONS โ๏ธ
The fields in this part of the structure hold the addresses of the functions that actually do the basic filesystem operations. the main characters, honestly.
A filesystem that wants to work with the complete standard Tcl command set must implement all of these. If some of them aren't implemented, certain Tcl commands may just fail when operating on paths inside that filesystem. And sometimes that's literally the point:
- a read-only filesystem should not implement the last four functions ๐ซ
- a filesystem that doesn't support symbolic links need not implement the
readlinkfunction ๐โ
The Tcl core expects filesystems to behave exactly this way. intentional gaps are still valid vibes.
FILESYSTEMPATHTYPEPROC
Function to determine the type of a path in this filesystem. May be NULL, in which case no type info is available to users of the filesystem. The "type" is purely for informational purposes, and you return it as the string representation of the Tcl_Obj you hand back. Typical return values might be "networked", "zip", or "ftp". ๐ก
The Tcl_Obj result is owned by the filesystem, so if Tcl wants to hold onto a reference to it, Tcl itself will increment that value's reference count. not your problem, bestie.
typedef Tcl_Obj *Tcl_FSFilesystemPathTypeProc(
Tcl_Obj *pathPtr);
FILESYSTEMSEPARATORPROC
Function that returns the separator character(s) for this filesystem. You only need to implement this if your filesystem wants a separator different from the standard "/" string. Amongst other things, it's what the file separator command returns. The return value should be a value with a reference count of zero. ๐
typedef Tcl_Obj *Tcl_FSFilesystemSeparatorProc(
Tcl_Obj *pathPtr);
STATPROC
Function to process a Tcl_FSStat call. Must be implemented for any reasonable filesystem, because a ton of Tcl-level commands depend crucially on it โ think file atime, file isdirectory, file size, glob. no stat, no slay ๐ฅ
typedef int Tcl_FSStatProc(
Tcl_Obj *pathPtr,
Tcl_StatBuf *statPtr);
The Tcl_FSStatProc fills the stat structure statPtr with info about the specified file. You don't need any access rights to the file to get this info, but you do need search rights to every directory named in the path leading to the file. The stat structure includes info about:
- device
- inode (always
0on Windows) - privilege mode
nlink(always1on Windows)- user id (always
0on Windows) - group id (always
0on Windows) rdev(same as device on Windows)- size
- last access time
- last modification time
- last metadata change time
If the file represented by pathPtr exists, Tcl_FSStatProc returns 0 and the stat structure gets filled with data. Otherwise, -1 is returned and no stat info is given. that's the whole deal ๐ซถ
ACCESSPROC
Function to process a Tcl_FSAccess call. Must be implemented for any reasonable filesystem, because lots of Tcl-level commands depend crucially on it โ like file exists and file readable. ๐
typedef int Tcl_FSAccessProc(
Tcl_Obj *pathPtr,
int mode);
The 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. If the pathname refers to a symbolic link, then the permissions of the file that the symbolic link refers to should be tested. ๐โก๏ธ
- On success (all requested permissions granted): return
0. โ - On error (at least one bit in
modeasked for a permission that's denied, or some other error happened): return-1. โ
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 โ like open, encoding, and a whole bunch of Tk commands. ๐
typedef Tcl_Channel Tcl_FSOpenFileChannelProc(
Tcl_Interp *interp,
Tcl_Obj *pathPtr,
int mode,
int permissions);
The Tcl_FSOpenFileChannelProc opens the file specified by pathPtr and returns a channel handle you can use to do input and output on that file. This API is modeled after the fopen procedure from the Unix standard I/O library. The syntax and meaning of all the arguments is similar to what you'd pass to 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 happens while opening the channel,
Tcl_FSOpenFileChannelProcreturnsNULLand records a POSIX error code that you can retrieve withTcl_GetErrno. - Also, if
interpis non-NULL,Tcl_FSOpenFileChannelProcleaves an error message ininterp's result after any error. ๐
The newly created channel must not be registered in the supplied interpreter by aTcl_FSOpenFileChannelProcโ that task is up to the caller ofTcl_FSOpenFileChannel(if it's needed at all). And if one of the standard channels,stdin,stdout, orstderr, was previously closed, then creating the new channel also assigns it as the replacement for that standard channel. surprise promotion fr ๐
MATCHINDIRECTORYPROC
Function to process a Tcl_FSMatchInDirectory call. If you don't implement it, then glob and recursive copy functionality will be lacking in the filesystem โ and that can even impact commands like encoding names, which use glob functionality internally. the ripple effects are real ๐ญ
typedef int Tcl_FSMatchInDirectoryProc(
Tcl_Interp *interp,
Tcl_Obj *resultPtr,
Tcl_Obj *pathPtr,
const char *pattern,
Tcl_GlobTypeData *types);
So like, the function is supposed to hand back all the files or directories (or other filesystem objects, bestie, we're inclusive) that match the pattern and line up with the types spec you gave it. โจ
Now here's the tea: there are two ways this function can get called. ๐
- If
patternisNULL, thenpathPtris a full path spec for a single file or directory, and all you're doing is checking that it exists and is the correct type. Just a vibe check, basically. ๐ - Otherwise,
pathPtris a directory, and the function needs to search through its contents looking for files or directories with the correct type.
Either way, you can safely assume pathPtr is both non-NULL and non-empty. ๐ But heads up โ it is not currently documented whether pathPtr will have a file separator at the end or not, so your code needs to be flexible to both possibilities. Yes, really. Write it for both. No cap.
The return value is a standard Tcl result saying whether an error happened during the matching process. Error messages go in interp โ unless interp is NULL, in which case you don't need to generate one at all. ๐ซถ On a TCL_OK result, results should be added to the resultPtr value you were given (and you can assume that's a valid, unshared Tcl list). The matches you add to resultPtr should include any path prefix that was given in pathPtr โ which usually means they'll be absolute path specifications. ๐ฃ๏ธ
Note that if no matches are found, that just leads to an empty result. Errors are only signaled for actual file or filesystem problems that might occur during the matching process. So an empty list is not a crisis, it's just a quiet day. ๐
The Tcl_GlobTypeData structure passed in the types parameter has these 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 you have to handle correctly, and both of them happen when types is non-NULL. They are when types->types & TCL_GLOB_TYPE_DIR or types->types & TCL_GLOB_TYPE_MOUNT are true โ and especially when the other flags are false. ๐
- In the first case, the function must list the contained directories. Tcl uses this to implement recursive globbing, so it is critical that filesystems implement directory matching correctly. Do not skip this one, I'm begging. ๐ญ
- In the second case, with
TCL_GLOB_TYPE_MOUNT, the filesystem must list the mount points that lie within the givenpathPtr. And here's the wild part โ in this case,pathPtrneed not lie within the same filesystem, which is different from all other cases where this function gets called. ๐ Support for this is critical if Tcl is going to have seamless transitions from one filesystem to another.
๐ซ UTIMEPROC ๐ซ
Function to process a Tcl_FSUtime call. Required to allow setting (not reading, bestie โ 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, same as 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. And listen โ the result is owned by the caller, so it should therefore have its ref count incremented before being returned. Any callers should call Tcl_DecrRefCount on this result when it's 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 lets filesystems choose whether they actually want to retain a โglobal listโ of volumes or not:
- If not: they generate the list on the fly, pass it to Tcl with a reference count of
1, and then forget about the list entirely. ๐ - If yes: 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. Do not touch. ๐ซ
๐ FILEATTRSTRINGSPROC ๐
Function to list all attribute strings that are valid for this filesystem. If it's not implemented, the filesystem won't support the file attributes command. This is how you attach arbitrary extra info to files in the filesystem โ cute, right? ๐ And if it's not implemented, there's no need to implement the get and set methods either.
typedef const char *const *Tcl_FSFileAttrStringsProc(
Tcl_Obj *pathPtr,
Tcl_Obj **objPtrRef);
The called function may either return an array of strings, or it 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. ๐
So, to be clear:
- 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 make sure 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 make sure it gets properly freed. Don't leave it floating, bestie. ๐ฆ
โ๏ธ FILEATTRSSETPROC โ๏ธ
Function to process a Tcl_FSFileAttrsSet call, used by file attributes. If the filesystem is read-only, there's 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 it was 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.
Now the flags, pay attention bestie:
- If the
recursiveflag is given, then a non-empty directory should be deleted without error. Just yeet the whole thing. ๐งน - If this flag is not given, and the directory is non-empty, a POSIX โ
EEXISTโ 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. That's your receipt, keep it. ๐งพ
๐ 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. Gone. Poof. ๐
Note that, if the filesystem supports symbolic links, Tcl will always call this function and not Tcl_FSRemoveDirectoryProc when it needs to delete them โ even if they are symbolic links to directories. So like, don't get clever about it. ๐
โจ FILESYSTEM EFFICIENCY โจ
okay bestie, listen โ these ones are the optional glow-up functions ๐
. You do not have to implement them for your filesystem, because the core already has a fallback implementation sitting right there ready to go. But every single one of them has its own lil "what happens if you leave the field NULL" note, so read each description, mkay? No skipping. ๐
LSTATPROC ๐ธ
This is the function that handles a Tcl_FSLstat call. If you don't implement it, Tcl will just try to use the statProc defined above instead. So literally the only reason to implement it is 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 vibe of this function is super similar to the Tcl_FSStatProc defined above โ one tiny difference though: if you point it at a symbolic link, it gives you the deets about the link itself, not about the target file. ๐ฆ
COPYFILEPROC ๐
Handles a Tcl_FSCopyFile call. Skip it and Tcl falls back on open-r, open-w and fcopy as its copying mechanism. So implement it only if your filesystem can do that copy more efficiently. Efficient queen behavior, fr fr.
typedef int Tcl_FSCopyFileProc(
Tcl_Obj *srcPathPtr,
Tcl_Obj *destPathPtr);
The return value is a standard Tcl result telling you whether an error happened during the copying. Heads up: destPathPtr is the name of the file that should become the copy of srcPathPtr. It is never the name of a directory into which srcPathPtr could be copied โ like, the function is way simpler than the Tcl-level file copy subcommand. Also note: if your 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 pointing at directories. And finally โ the escape hatch ๐ช โ if your filesystem figures out it just can't support the file copy action, call Tcl_SetErrno(EXDEV) and return a non-TCL_OK result, and that tells Tcl to use its standard fallback mechanisms. no cap.
RENAMEFILEPROC ๐
Handles a Tcl_FSRenameFile call. Don't implement it and 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);
Return value is a standard Tcl result saying whether an error occurred during the renaming. And same escape hatch as before: if your filesystem determines it cannot support the file rename action, call Tcl_SetErrno(EXDEV) and return a non-TCL_OK result, and Tcl will go use its standard fallback mechanisms. ๐ซถ
COPYDIRECTORYPROC ๐
Handles a Tcl_FSCopyDirectory call. No implementation? Tcl falls back on a recursive file mkdir, file copy mechanism. So โ you already know the drill โ implement it only if your filesystem can do it more efficiently.
typedef int Tcl_FSCopyDirectoryProc(
Tcl_Obj *srcPathPtr,
Tcl_Obj *destPathPtr,
Tcl_Obj **errorPtr);
Return value = standard Tcl result, tells you if an error occurred during copying. If an error does happen, the name of the file or directory that caused it should be dropped into errorPtr. Note: 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 โ again, much simpler than the Tcl-level file copy subcommand. And finally, the classic: if your filesystem can't support the directory copy action, call Tcl_SetErrno(EXDEV) and return a non-TCL_OK result to tell Tcl to use its standard fallback mechanisms. โญ
LOADFILEPROC ๐ฅ
Handles a Tcl_FSLoadFile call. If you don't implement it, Tcl falls back on a copy to native-temp followed by a Tcl_FSLoadFile on that temporary copy. So implement it if your filesystem can load code directly โ or implement it just to return TCL_ERROR and disable load functionality in this filesystem entirely. iconically petty, we love it.
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. What this does: dynamically loads a binary code file into memory. On a successful load, handlePtr should be filled with a token for the dynamically loaded file, and unloadProcPtr should be filled in with the address of a procedure. That unload procedure gets called with the given Tcl_LoadHandle as its only parameter whenever 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. Every filesystem is free to define the Tcl_LoadHandle however it requires. โจ And yep, you guessed it: if the filesystem determines it cannot support the file load action, calling Tcl_SetErrno(EXDEV) and returning a non-TCL_OK result tells Tcl to use its standard fallback mechanisms.
UNLOADFILEPROC ๐ซ
This one unloads a previously successfully loaded file. If load was implemented, then this should also be implemented โ but only if there's any cleanup action required.
typedef void Tcl_FSUnloadFileProc(
Tcl_LoadHandle loadHandle);
GETCWDPROC ๐ฆ
Handles a Tcl_FSGetCwd call. Most filesystems do not need to implement this. It will usually only be called once, and only if getcwd is called before chdir. It may be NULL.
typedef Tcl_Obj *Tcl_FSGetCwdProc(
Tcl_Interp *interp);
If your filesystem supports a native notion of a current working directory โ which might perhaps change independently of Tcl โ this function should return that cwd as the result, or NULL if the current directory couldn't be determined (e.g. the user doesn't have the appropriate permissions on the cwd directory). And if NULL is returned? An error message is left in the interp's result. ๐
CHDIRPROC ๐
Handles a Tcl_FSChdir call. If filesystems don't implement this, it gets emulated by a series of directory access checks. Otherwise, virtual filesystems that do implement it only need to respond with a positive return result if pathPtr is a valid, accessible directory in their filesystem. They don't even need to remember the result, because that gets automatically remembered for use by Tcl_FSGetCwd. Real filesystems, though? They should carry out the correct action, i.e. call the correct system chdir API. Do it properly, bestie. ๐ธ
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 thing. ๐
๐ REFERENCE COUNT MANAGEMENT ๐
ok bestie, this is the part where we talk about who owns what and who has to clean up. yes, it's giving homework. but it's also giving "if you get this wrong your program crashes in ways that will haunt you" ๐ so let's lock in.
โจ PUBLIC API CALLS โจ
For all of these functions, pathPtr (including the srcPathPtr and destPathPtr arguments to Tcl_FSCopyFile, Tcl_FSCopyDirectory, and Tcl_FSRenameFile, the firstPtr and secondPtr arguments to Tcl_FSEqualPaths, and the linkNamePtr and toPtr arguments to 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 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.
- The
errorPtrargument toTcl_FSCopyDirectoryandTcl_FSRemoveDirectoryis, when an object is set into it at all, set to an object with a non-zero reference count that should be passed toTcl_DecrRefCountwhen no longer needed. ๐งน Tcl_FSListVolumesโ always returns a zero-reference object, much likeTcl_NewObj. โจTcl_FSLinkโ always returns a non-zero-reference object when it is asked to read; you must callTcl_DecrRefCounton the object once you no longer need it. ๐ฅTcl_FSGetCwdโ always returns a non-zero-reference object; you must callTcl_DecrRefCounton the object once you no longer need it. ๐ฅTcl_FSPathSeparatorโ always returns a zero-reference object, much likeTcl_NewObj. โจTcl_FSJoinPathโ always returns a zero-reference object, much likeTcl_NewObj. ItslistObjargument can have any reference count; it is only read by this function. ๐Tcl_FSSplitPathโ always returns a zero-reference object, much likeTcl_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. โกTcl_FSJoinToPathโ always returns a zero-reference object, much likeTcl_NewObj. ItsbasePtrargument follows the rules above forpathPtr, as do the values in theobjvargument. ๐Tcl_FSGetTranslatedPathโ returns a non-zero-reference object (orNULLin the error case); you must callTcl_DecrRefCounton the object once you no longer need it. ๐ฅTcl_FSNewNativePathโ always returns a zero-reference object (orNULL), much likeTcl_NewObj. โจTcl_FSFileSystemInfoโ always returns a zero-reference object (orNULL), much likeTcl_NewObj. โจ- The
objPtrandobjPtrRefarguments toTcl_FSFileAttrsGet,Tcl_FSFileAttrsSetandTcl_FSFileAttrStringsare conventional Tcl values; theobjPtrargument will be read but not retained, and theobjPtrRefargument will have (on success) a zero-reference value written into it (as withTcl_NewObj).Tcl_FSFileAttrsGetandTcl_FSFileAttrsSetmay also manipulate the interpreter result. ๐ - The
resultPtrargument toTcl_FSMatchInDirectorywill 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). โ ๏ธ
๐ธ 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 withTcl_NewObj. โจnormalizePathProcโ unlike with other API implementation functions, thepathPtrargument 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โ theresultPtrargument 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โ iftoPtrisNULL, this should return a value with reference count 1 that has just been allocated and passed toTcl_IncrRefCount. IftoPtris notNULL, 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 (withTcl_DecrRefCount) by Tcl once done. ๐งนfileAttrStringsProcโ if the result isNULL, theobjPtrRefshould have a list value written to it; that list will have its reference count both incremented (withTcl_IncrRefCount) and decremented (withTcl_DecrRefCount). ๐fileAttrsGetProcโ theobjPtrRefargument should have (on non-error return) a zero reference count value written to it (allocated as if withTcl_NewObj). โจfileAttrsSetProcโ theobjPtrargument should either just be read or its reference count incremented to retain it. ๐removeDirectoryProcโ if an error is being reported, the problem filename reported viaerrorPtrshould be newly allocated (as if withTcl_NewObj) and have a reference count of 1 (i.e., have been passed toTcl_IncrRefCount). ๐๏ธcopyDirectoryProcโ if an error is being reported, the problem filename reported viaerrorPtrshould be newly allocated (as if withTcl_NewObj) and have a reference count of 1 (i.e., have been passed toTcl_IncrRefCount). ๐getCwdProcโ the result will be passed toTcl_DecrRefCountby the implementation ofTcl_FSGetCwdafter it has been normalized. ๐งญ
๐ SEE ALSO ๐
go read those too bestie, they're the whole friend group ๐ซถ
๐ KEYWORDS ๐
stat, access, filesystem, vfs, virtual filesystem
and that's a wrap on the whole page, bestie!! ๐ this one came from Tcl 8.4 and it's the Filesystem(3) page โ thank you for reading all nine parts, you're literally the main character of reference-count management now, no cap ๐ โจ
๐ฆ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 ยท TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 ยท TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 ยท TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 ยท TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 2001 Vincent Darley
- Copyright (c) 2008-2010 Donal K. Fellows
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms