Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/FileSystem.3 @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 63.9 KB
Line 
1'\"
2'\" Copyright (c) 2001 Vincent Darley
3'\"
4'\" See the file "license.terms" for information on usage and redistribution
5'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6'\"
7'\" RCS: @(#) $Id: FileSystem.3,v 1.62 2007/12/13 15:22:31 dgp Exp $
8'\"
9.so man.macros
10.TH Filesystem 3 8.4 Tcl "Tcl Library Procedures"
11.BS
12.SH NAME
13Tcl_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_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_AllocStatBuf \- procedures to interact with any filesystem
14.SH SYNOPSIS
15.nf
16\fB#include <tcl.h>\fR
17.sp
18int
19\fBTcl_FSRegister\fR(\fIclientData, fsPtr\fR)
20.sp
21int
22\fBTcl_FSUnregister\fR(\fIfsPtr\fR)
23.sp
24ClientData
25\fBTcl_FSData\fR(\fIfsPtr\fR)
26.sp
27void
28\fBTcl_FSMountsChanged\fR(\fIfsPtr\fR)
29.sp
30Tcl_Filesystem*
31\fBTcl_FSGetFileSystemForPath\fR(\fIpathPtr\fR)
32.sp
33Tcl_PathType
34\fBTcl_FSGetPathType\fR(\fIpathPtr\fR)
35.sp
36int
37\fBTcl_FSCopyFile\fR(\fIsrcPathPtr, destPathPtr\fR)
38.sp
39int
40\fBTcl_FSCopyDirectory\fR(\fIsrcPathPtr, destPathPtr, errorPtr\fR)
41.sp
42int
43\fBTcl_FSCreateDirectory\fR(\fIpathPtr\fR)
44.sp
45int
46\fBTcl_FSDeleteFile\fR(\fIpathPtr\fR)
47.sp
48int
49\fBTcl_FSRemoveDirectory\fR(\fIpathPtr, int recursive, errorPtr\fR)
50.sp
51int
52\fBTcl_FSRenameFile\fR(\fIsrcPathPtr, destPathPtr\fR)
53.sp
54Tcl_Obj*
55\fBTcl_FSListVolumes\fR(\fIvoid\fR)
56.sp
57.VS 8.5
58int
59\fBTcl_FSEvalFileEx\fR(\fIinterp, pathPtr, encodingName\fR)
60.VE 8.5
61.sp
62int
63\fBTcl_FSEvalFile\fR(\fIinterp, pathPtr\fR)
64.sp
65int
66\fBTcl_FSLoadFile\fR(\fIinterp, pathPtr, sym1, sym2, proc1Ptr, proc2Ptr,
67               handlePtr, unloadProcPtr\fR)
68.sp
69int
70\fBTcl_FSMatchInDirectory\fR(\fIinterp, resultPtr, pathPtr, pattern, types\fR)
71.sp
72Tcl_Obj*
73\fBTcl_FSLink\fR(\fIlinkNamePtr, toPtr, linkAction\fR)
74.sp
75int
76\fBTcl_FSLstat\fR(\fIpathPtr, statPtr\fR)
77.sp
78int
79\fBTcl_FSUtime\fR(\fIpathPtr, tval\fR)
80.sp
81int
82\fBTcl_FSFileAttrsGet\fR(\fIinterp, int index, pathPtr, objPtrRef\fR)
83.sp
84int
85\fBTcl_FSFileAttrsSet\fR(\fIinterp, int index, pathPtr, Tcl_Obj *objPtr\fR)
86.sp
87const char**
88\fBTcl_FSFileAttrStrings\fR(\fIpathPtr, objPtrRef\fR)
89.sp
90int
91\fBTcl_FSStat\fR(\fIpathPtr, statPtr\fR)
92.sp
93int
94\fBTcl_FSAccess\fR(\fIpathPtr, mode\fR)
95.sp
96Tcl_Channel
97\fBTcl_FSOpenFileChannel\fR(\fIinterp, pathPtr, modeString, permissions\fR)
98.sp
99Tcl_Obj*
100\fBTcl_FSGetCwd\fR(\fIinterp\fR)
101.sp
102int
103\fBTcl_FSChdir\fR(\fIpathPtr\fR)
104.sp
105Tcl_Obj*
106\fBTcl_FSPathSeparator\fR(\fIpathPtr\fR)
107.sp
108Tcl_Obj*
109\fBTcl_FSJoinPath\fR(\fIlistObj, elements\fR)
110.sp
111Tcl_Obj*
112\fBTcl_FSSplitPath\fR(\fIpathPtr, lenPtr\fR)
113.sp
114int
115\fBTcl_FSEqualPaths\fR(\fIfirstPtr, secondPtr\fR)
116.sp
117Tcl_Obj*
118\fBTcl_FSGetNormalizedPath\fR(\fIinterp, pathPtr\fR)
119.sp
120Tcl_Obj*
121\fBTcl_FSJoinToPath\fR(\fIbasePtr, objc, objv\fR)
122.sp
123int
124\fBTcl_FSConvertToPathType\fR(\fIinterp, pathPtr\fR)
125.sp
126ClientData
127\fBTcl_FSGetInternalRep\fR(\fIpathPtr, fsPtr\fR)
128.sp
129Tcl_Obj *
130\fBTcl_FSGetTranslatedPath\fR(\fIinterp, pathPtr\fR)
131.sp
132const char *
133\fBTcl_FSGetTranslatedStringPath\fR(\fIinterp, pathPtr\fR)
134.sp
135Tcl_Obj*
136\fBTcl_FSNewNativePath\fR(\fIfsPtr, clientData\fR)
137.sp
138const char *
139\fBTcl_FSGetNativePath\fR(\fIpathPtr\fR)
140.sp
141Tcl_Obj*
142\fBTcl_FSFileSystemInfo\fR(\fIpathPtr\fR)
143.sp
144Tcl_StatBuf*
145\fBTcl_AllocStatBuf\fR()
146.SH ARGUMENTS
147.AS Tcl_FSUnloadFileProc **unloadProcPtr out
148.AP Tcl_Filesystem *fsPtr in
149Points to a structure containing the addresses of procedures that
150can be called to perform the various filesystem operations.
151.AP Tcl_Obj *pathPtr in
152The path represented by this object is used for the operation in
153question.  If the object does not already have an internal \fBpath\fR
154representation, it will be converted to have one.
155.AP Tcl_Obj *srcPathPtr in
156As for \fIpathPtr\fR, but used for the source file for a copy or
157rename operation.
158.AP Tcl_Obj *destPathPtr in
159As for \fIpathPtr\fR, but used for the destination filename for a copy or
160rename operation.
161.AP "const char" *encodingName in
162The encoding of the data stored in the
163file identified by \fIpathPtr\fR and to be evaluted.
164.AP "const char" *pattern in
165Only files or directories matching this pattern will be returned.
166.AP Tcl_GlobTypeData *types in
167Only files or directories matching the type descriptions contained in
168this structure will be returned.  This parameter may be NULL.
169.AP Tcl_Interp *interp in
170Interpreter to use either for results, evaluation, or reporting error
171messages.
172.AP ClientData clientData in
173The native description of the path object to create.
174.AP Tcl_Obj *firstPtr in
175The first of two path objects to compare.  The object may be converted
176to \fBpath\fR type.
177.AP Tcl_Obj *secondPtr in
178The second of two path objects to compare.  The object may be converted
179to \fBpath\fR type.
180.AP Tcl_Obj *listObj in
181The list of path elements to operate on with a \fBjoin\fR operation.
182.AP int elements in
183If non-negative, the number of elements in the \fIlistObj\fR which should
184be joined together.  If negative, then all elements are joined.
185.AP Tcl_Obj **errorPtr out
186In the case of an error, filled with an object containing the name of
187the file which caused an error in the various copy/rename operations.
188.AP Tcl_Obj **objPtrRef out
189Filled with an object containing the result of the operation.
190.AP Tcl_Obj *resultPtr out
191Pre-allocated object in which to store (using
192\fBTcl_ListObjAppendElement\fR) the list of
193files or directories which are successfully matched.
194.AP int mode in
195Mask consisting of one or more of R_OK, W_OK, X_OK and F_OK.  R_OK,
196W_OK and X_OK request checking whether the file exists and  has  read,
197write and  execute  permissions, respectively.  F_OK just requests
198checking for the existence of the file.
199.AP Tcl_StatBuf *statPtr out
200The structure that contains the result of a stat or lstat operation.
201.AP "const char" *sym1 in
202Name of a procedure to look up in the file's symbol table
203.AP "const char" *sym2 in
204Name of a procedure to look up in the file's symbol table
205.AP Tcl_PackageInitProc **proc1Ptr out
206Filled with the init function for this code.
207.AP Tcl_PackageInitProc **proc2Ptr out
208Filled with the safe-init function for this code.
209.AP ClientData *clientDataPtr out
210Filled with the clientData value to pass to this code's unload
211function when it is called.
212.AP Tcl_LoadHandle *handlePtr out
213Filled with an abstract token representing the loaded file.
214.AP Tcl_FSUnloadFileProc **unloadProcPtr out
215Filled with the function to use to unload this piece of code.
216.AP utimbuf *tval in
217The access and modification times in this structure are read and
218used to set those values for a given file.
219.AP "const char" *modeString in
220Specifies how the file is to be accessed.  May have any of the values
221allowed for the \fImode\fR argument to the Tcl \fBopen\fR command.
222.AP int permissions in
223POSIX-style permission flags such as 0644.  If a new file is created, these
224permissions will be set on the created file.
225.AP int *lenPtr out
226If non-NULL, filled with the number of elements in the split path.
227.AP Tcl_Obj *basePtr in
228The base path on to which to join the given elements.  May be NULL.
229.AP int objc in
230The number of elements in \fIobjv\fR.
231.AP "Tcl_Obj *const" objv[] in
232The elements to join to the given base path.
233.AP Tcl_Obj *linkNamePtr in
234The name of the link to be created or read.
235.AP Tcl_Obj *toPtr in
236What the link called \fIlinkNamePtr\fR should be linked to, or NULL if
237the symbolic link specified by \fIlinkNamePtr\fR is to be read.
238.AP int linkAction in
239OR-ed combination of flags indicating what kind of link should be
240created (will be ignored if \fItoPtr\fR is NULL). Valid bits to set
241are \fBTCL_CREATE_SYMBOLIC_LINK\fR and \fBTCL_CREATE_HARD_LINK\fR.
242When both flags are set and the underlying filesystem can do either,
243symbolic links are preferred.
244.BE
245
246.SH DESCRIPTION
247.PP
248There are several reasons for calling the \fBTcl_FS\fR API functions
249(e.g. \fBTcl_FSAccess\fR and \fBTcl_FSStat\fR)
250rather than calling system level functions like \fBaccess\fR and
251\fBstat\fR directly.  First, they will work cross-platform, so an
252extension which calls them should work unmodified on Unix and
253Windows.  Second, the Windows implementation of some of these functions
254fixes some bugs in the system level calls.  Third, these function calls
255deal with any
256.QW "Utf to platform-native"
257path conversions which may be
258required (and may cache the results of such conversions for greater
259efficiency on subsequent calls).  Fourth, and perhaps most importantly,
260all of these functions are
261.QW "virtual filesystem aware" .
262Any virtual filesystem (VFS for short) which has been registered (through
263\fBTcl_FSRegister\fR) may reroute file access to alternative
264media or access methods.  This means that all of these functions (and
265therefore the corresponding \fBfile\fR, \fBglob\fR, \fBpwd\fR, \fBcd\fR,
266\fBopen\fR, etc.  Tcl commands) may be operate on
267.QW files
268which are not
269native files in the native filesystem.  This also means that any Tcl
270extension which accesses the filesystem (FS for short) through this API is
271automatically
272.QW "virtual filesystem aware" .
273Of course, if an extension
274accesses the native filesystem directly (through platform-specific
275APIs, for example), then Tcl cannot intercept such calls.
276.PP
277If appropriate VFSes have been registered, the
278.QW files
279may, to give two
280examples, be remote (e.g. situated on a remote ftp server) or archived
281(e.g. lying inside a .zip archive).  Such registered filesystems provide
282a lookup table of functions to implement all or some of the functionality
283listed here.  Finally, the \fBTcl_FSStat\fR and \fBTcl_FSLstat\fR calls
284abstract away from what the
285.QW "struct stat"
286buffer is actually
287declared to be, allowing the same code to be used both on systems with
288and systems without support for files larger than 2GB in size.
289.PP
290The \fBTcl_FS\fR API is objectified and may cache internal
291representations and other path-related strings (e.g. the current working
292directory).  One side-effect of this is that one must not pass in objects
293with a reference count of zero to any of these functions.  If such calls were
294handled, they might result
295in memory leaks (under some circumstances, the filesystem code may wish
296to retain a reference to the passed in object, and so one must not assume
297that after any of these calls return, the object still has a reference count of
298zero - it may have been incremented) or in a direct segmentation fault
299(or other memory access error)
300due to the object being freed part way through the complex object
301manipulation required to ensure that the path is fully normalized and
302absolute for filesystem determination.  The practical lesson to learn
303from this is that
304.CS
305Tcl_Obj *path = Tcl_NewStringObj(...);
306Tcl_FS\fIWhatever\fR(path);
307Tcl_DecrRefCount(path);
308.CE
309is wrong, and may cause memory errors. The \fIpath\fR must have its
310reference count incremented before passing it in, or
311decrementing it.  For this reason, objects with a reference count of zero are
312considered not to be valid filesystem paths and calling any Tcl_FS API
313function with such an object will result in no action being taken.
314.SS "FS API FUNCTIONS"
315\fBTcl_FSCopyFile\fR attempts to copy the file given by \fIsrcPathPtr\fR to the
316path name given by \fIdestPathPtr\fR.  If the two paths given lie in the same
317filesystem (according to \fBTcl_FSGetFileSystemForPath\fR) then that
318filesystem's
319.QW "copy file"
320function is called (if it is non-NULL).
321Otherwise the function returns -1 and sets the \fBerrno\fR global C
322variable to the
323.QW EXDEV
324POSIX error code (which signifies a
325.QW "cross-domain link" ).
326.PP
327\fBTcl_FSCopyDirectory\fR attempts to copy the directory given by \fIsrcPathPtr\fR to the
328path name given by \fIdestPathPtr\fR.  If the two paths given lie in the same
329filesystem (according to \fBTcl_FSGetFileSystemForPath\fR) then that
330filesystem's
331.QW "copy file"
332function is called (if it is non-NULL).
333Otherwise the function returns -1 and sets the \fBerrno\fR global C
334variable to the
335.QW EXDEV
336POSIX error code (which signifies a
337.QW "cross-domain link" ).
338.PP
339\fBTcl_FSCreateDirectory\fR attempts to create the directory given by
340\fIpathPtr\fR by calling the owning filesystem's
341.QW "create directory"
342function.
343.PP
344\fBTcl_FSDeleteFile\fR attempts to delete the file given by
345\fIpathPtr\fR by calling the owning filesystem's
346.QW "delete file"
347function.
348.PP
349\fBTcl_FSRemoveDirectory\fR attempts to remove the directory given by
350\fIpathPtr\fR by calling the owning filesystem's
351.QW "remove directory"
352function.
353.PP
354\fBTcl_FSRenameFile\fR attempts to rename the file or directory given by
355\fIsrcPathPtr\fR to the path name given by \fIdestPathPtr\fR.  If the two paths
356given lie in the same filesystem (according to
357\fBTcl_FSGetFileSystemForPath\fR) then that filesystem's
358.QW "rename file"
359function is called (if it is non-NULL).  Otherwise the function returns -1
360and sets the \fBerrno\fR global C variable to the
361.QW EXDEV
362POSIX error code (which signifies a
363.QW "cross-domain link" ).
364.PP
365\fBTcl_FSListVolumes\fR calls each filesystem which has a non-NULL
366.QW "list volumes"
367function and asks them to return their list of root volumes.  It
368accumulates the return values in a list which is returned to the
369caller (with a reference count of 0).
370.PP
371.VS 8.5
372\fBTcl_FSEvalFileEx\fR reads the file given by \fIpathPtr\fR using
373the encoding identified by \fIencodingName\fR and evaluates
374its contents as a Tcl script.  It returns the same information as
375\fBTcl_EvalObjEx\fR.
376If \fIencodingName\fR is NULL, the system encoding is used for
377reading the file contents.
378If the file could not be read then a Tcl error is returned to describe
379why the file could not be read.
380The eofchar for files is
381.QW \e32
382(^Z) for all platforms.
383If you require a
384.QW ^Z
385in code for string comparison, you can use
386.QW \e032
387or
388.QW \eu001a ,
389which will be safely substituted by the Tcl interpreter into
390.QW ^Z .
391\fBTcl_FSEvalFile\fR is a simpler version of
392\fBTcl_FSEvalFileEx\fR that always uses the system encoding
393when reading the file.
394.VE 8.5
395.PP
396\fBTcl_FSLoadFile\fR dynamically loads a binary code file into memory and
397returns the addresses of two procedures within that file, if they are
398defined.  The appropriate function for the filesystem to which \fIpathPtr\fR
399belongs will be called.  If that filesystem does not implement this
400function (most virtual filesystems will not, because of OS limitations
401in dynamically loading binary code), Tcl will attempt to copy the file
402to a temporary directory and load that temporary file.
403.PP
404Returns a standard Tcl completion code.  If an error occurs, an error
405message is left in the \fIinterp\fR's result.
406.PP
407\fBTcl_FSMatchInDirectory\fR is used by the globbing code to search a
408directory for all files which match a given pattern.  The appropriate
409function for the filesystem to which \fIpathPtr\fR belongs will be called.
410.PP
411The return value is a standard Tcl result indicating whether an error
412occurred in globbing.  Error messages are placed in interp (unless
413interp is NULL, which is allowed), but good results are placed in the
414resultPtr given.
415.PP
416Note that the \fBglob\fR code implements recursive patterns internally, so
417this function will only ever be passed simple patterns, which can be
418matched using the logic of \fBstring match\fR.  To handle recursion, Tcl
419will call this function frequently asking only for directories to be
420returned.  A special case of being called with a NULL pattern indicates
421that the path needs to be checked only for the correct type.
422.PP
423\fBTcl_FSLink\fR replaces the library version of \fBreadlink\fR, and
424extends it to support the creation of links.  The appropriate function
425for the filesystem to which \fIlinkNamePtr\fR belongs will be called.
426.PP
427If the \fItoPtr\fR is NULL, a
428.QW "read link"
429action is performed.  The result
430is a Tcl_Obj specifying the contents of the symbolic link given by
431\fIlinkNamePtr\fR, or NULL if the link could not be read.  The result is owned
432by the caller, which should call Tcl_DecrRefCount when the result is no
433longer needed.  If the \fItoPtr\fR is not NULL, Tcl should create a link
434of one of the types passed in in the \fIlinkAction\fR flag.  This flag is
435an ORed combination of \fBTCL_CREATE_SYMBOLIC_LINK\fR and \fBTCL_CREATE_HARD_LINK\fR.
436Where a choice exists (i.e. more than one flag is passed in), the Tcl
437convention is to prefer symbolic links.  When a link is successfully
438created, the return value should be \fItoPtr\fR (which is therefore
439already owned by the caller).  If unsuccessful, NULL is returned.
440.PP
441\fBTcl_FSLstat\fR fills the stat structure \fIstatPtr\fR with information
442about the specified file.  You do not need any access rights to the
443file to get this information but you need search rights to all
444directories named in the path leading to the file.  The stat structure
445includes info regarding device, inode (always 0 on Windows),
446privilege mode, nlink (always 1 on Windows), user id (always 0 on
447Windows), group id (always 0 on Windows), rdev (same as device on
448Windows), size, last access time, last modification time, and creation
449time.
450.PP
451If \fIpath\fR exists, \fBTcl_FSLstat\fR returns 0 and the stat structure
452is filled with data.  Otherwise, -1 is returned, and no stat info is
453given.
454.PP
455\fBTcl_FSUtime\fR replaces the library version of utime.
456.PP
457This returns 0 on success and -1 on error (as per the \fButime\fR
458documentation).  If successful, the function
459will update the
460.QW atime
461and
462.QW mtime
463values of the file given.
464.PP
465\fBTcl_FSFileAttrsGet\fR implements read access for the hookable \fBfile
466attributes\fR subcommand.  The appropriate function for the filesystem to
467which \fIpathPtr\fR belongs will be called.
468.PP
469If the result is \fBTCL_OK\fR, then an object was placed in
470\fIobjPtrRef\fR, which
471will only be temporarily valid (unless \fBTcl_IncrRefCount\fR is called).
472.PP
473\fBTcl_FSFileAttrsSet\fR implements write access for the hookable \fBfile
474attributes\fR subcommand.  The appropriate function for the filesystem to
475which \fIpathPtr\fR belongs will be called.
476.PP
477\fBTcl_FSFileAttrStrings\fR implements part of the hookable \fBfile
478attributes\fR subcommand.  The appropriate function for the filesystem
479to which \fIpathPtr\fR belongs will be called.
480.PP
481The called procedure may either return an array of strings, or may
482instead return NULL and place a Tcl list into the given \fIobjPtrRef\fR.  Tcl
483will take that list and first increment its reference count before using it.
484On completion of that use, Tcl will decrement its reference count.  Hence if
485the list should be disposed of by Tcl when done, it should have a
486reference count of zero, and if the list should not be disposed of, the
487filesystem should ensure it retains a reference count to the object.
488.PP
489\fBTcl_FSAccess\fR checks whether the process would be allowed to read,
490write or test for existence of the file (or other filesystem object)
491whose name is \fIpathname\fR.   If \fIpathname\fR is a symbolic link on Unix,
492then permissions of the file referred by this symbolic link are
493tested.
494.PP
495On success (all requested permissions granted), zero is returned.  On
496error (at least one bit in mode asked for a permission that is denied,
497or some other error occurred), -1 is returned.
498.PP
499\fBTcl_FSStat\fR fills the stat structure \fIstatPtr\fR with information
500about the specified file.  You do not need any access rights to the
501file to get this information but you need search rights to all
502directories named in the path leading to the file.  The stat structure
503includes info regarding device, inode (always 0 on Windows),
504privilege mode, nlink (always 1 on Windows), user id (always 0 on
505Windows), group id (always 0 on Windows), rdev (same as device on
506Windows), size, last access time, last modification time, and creation
507time.
508.PP
509If \fIpath\fR exists, \fBTcl_FSStat\fR returns 0 and the stat structure
510is filled with data.  Otherwise, -1 is returned, and no stat info is
511given.
512.PP
513\fBTcl_FSOpenFileChannel\fR opens a file specified by \fIpathPtr\fR and
514returns a channel handle that can be used to perform input and output on
515the file. This API is modeled after the \fBfopen\fR procedure of
516the Unix standard I/O library.
517The syntax and meaning of all arguments is similar to those
518given in the Tcl \fBopen\fR command when opening a file.
519If an error occurs while opening the channel, \fBTcl_FSOpenFileChannel\fR
520returns NULL and records a POSIX error code that can be
521retrieved with \fBTcl_GetErrno\fR.
522In addition, if \fIinterp\fR is non-NULL, \fBTcl_FSOpenFileChannel\fR
523leaves an error message in \fIinterp\fR's result after any error.
524.PP
525The newly created channel is not registered in the supplied interpreter; to
526register it, use \fBTcl_RegisterChannel\fR, described below.
527If one of the standard channels, \fBstdin, stdout\fR or \fBstderr\fR was
528previously closed, the act of creating the new channel also assigns it as a
529replacement for the standard channel.
530.PP
531\fBTcl_FSGetCwd\fR replaces the library version of \fBgetcwd\fR.
532.PP
533It returns the Tcl library's current working directory.  This may be
534different to the native platform's working directory, which happens when
535the current working directory is not in the native filesystem.
536.PP
537The result is a pointer to a Tcl_Obj specifying the current directory,
538or NULL if the current directory could not be determined.  If NULL is
539returned, an error message is left in the \fIinterp\fR's result.
540.PP
541The result already has its reference count incremented for the caller.  When
542it is no longer needed, that reference count should be decremented.  This is
543needed for thread-safety purposes, to allow multiple threads to access
544this and related functions, while ensuring the results are always
545valid.
546.PP
547\fBTcl_FSChdir\fR replaces the library version of \fBchdir\fR.  The path is
548normalized and then passed to the filesystem which claims it.  If that
549filesystem does not implement this function, Tcl will fallback to a
550combination of \fBstat\fR and \fBaccess\fR to check whether the directory
551exists and has appropriate permissions.
552.PP
553For results, see \fBchdir\fR documentation.  If successful, we keep a
554record of the successful path in \fIcwdPathPtr\fR for subsequent calls to
555\fBTcl_FSGetCwd\fR.
556.PP
557\fBTcl_FSPathSeparator\fR returns the separator character to be used for
558most specific element of the path specified by \fIpathPtr\fR (i.e. the last
559part of the path).
560.PP
561The separator is returned as a Tcl_Obj containing a string of length
5621.  If the path is invalid, NULL is returned.
563.PP
564\fBTcl_FSJoinPath\fR takes the given Tcl_Obj, which must be a valid
565list (which is allowed to have a reference count of zero), and returns the path
566object given by considering the first \fIelements\fR elements as valid path
567segments (each path segment may be a complete path, a partial path or
568just a single possible directory or file name).  If any path segment is
569actually an absolute path, then all prior path segments are discarded.
570If \fIelements\fR is less than 0, we use the entire list.
571.PP
572It is possible that the returned object is actually an element
573of the given list, so the caller should be careful to increment the
574reference count of the result before freeing the list.
575.PP
576The returned object, typically with a reference count of zero (but it
577could be shared
578under some conditions), contains the joined path.  The caller must
579add a reference count to the object before using it.  In particular, the
580returned object could be an element of the given list, so freeing the
581list might free the object prematurely if no reference count has been taken.
582If the number of elements is zero, then the returned object will be
583an empty-string Tcl_Obj.
584.PP
585\fBTcl_FSSplitPath\fR takes the given Tcl_Obj, which should be a valid path,
586and returns a Tcl list object containing each segment of that path as
587an element.
588It returns a list object with a reference count of zero.  If the
589passed in \fIlenPtr\fR is non-NULL, the variable it points to will be
590updated to contain the number of elements in the returned list.
591.PP
592\fBTcl_FSEqualPaths\fR tests whether the two paths given represent the same
593filesystem object
594.PP
595It returns 1 if the paths are equal, and 0 if they are different.  If
596either path is NULL, 0 is always returned.
597.PP
598\fBTcl_FSGetNormalizedPath\fR this important function attempts to extract
599from the given Tcl_Obj a unique normalized path representation, whose
600string value can be used as a unique identifier for the file.
601.PP
602It returns the normalized path object, owned by Tcl, or NULL if the path
603was invalid or could otherwise not be successfully converted.
604Extraction of absolute, normalized paths is very efficient (because the
605filesystem operates on these representations internally), although the
606result when the filesystem contains numerous symbolic links may not be
607the most user-friendly version of a path.  The return value is owned by
608Tcl and has a lifetime equivalent to that of the \fIpathPtr\fR passed in
609(unless that is a relative path, in which case the normalized path
610object may be freed any time the cwd changes) - the caller can of
611course increment the refCount if it wishes to maintain a copy for longer.
612.PP
613\fBTcl_FSJoinToPath\fR takes the given object, which should usually be a
614valid path or NULL, and joins onto it the array of paths segments
615given.
616.PP
617Returns object, typically with refCount of zero (but it could be shared
618under some conditions), containing the joined path.  The caller must
619add a refCount to the object before using it.  If any of the objects
620passed into this function (pathPtr or path elements) have a refCount
621of zero, they will be freed when this function returns.
622.PP
623\fBTcl_FSConvertToPathType\fR tries to convert the given Tcl_Obj to a valid
624Tcl path type, taking account of the fact that the cwd may have changed
625even if this object is already supposedly of the correct type.
626The filename may begin with
627.QW ~
628(to indicate current user's home directory) or
629.QW ~<user>
630(to indicate any user's home directory).
631.PP
632If the conversion succeeds (i.e. the object is a valid path in one of
633the current filesystems), then \fBTCL_OK\fR is returned.  Otherwise
634\fBTCL_ERROR\fR is returned, and an error message may
635be left in the interpreter.
636.PP
637\fBTcl_FSGetInternalRep\fR extracts the internal representation of a given
638path object, in the given filesystem.  If the path object belongs to a
639different filesystem, we return NULL. If the internal representation is
640currently NULL, we attempt to generate it, by calling the filesystem's
641\fBTcl_FSCreateInternalRepProc\fR.
642.PP
643Returns NULL or a valid internal path representation.  This internal
644representation is cached, so that repeated calls to this function will
645not require additional conversions.
646.PP
647\fBTcl_FSGetTranslatedPath\fR attempts to extract the translated path
648from the given Tcl_Obj.
649.PP
650If the translation succeeds (i.e. the object is a valid path), then it is
651returned.  Otherwise NULL will be returned, and an error message may be
652left in the interpreter.  A
653.QW translated
654path is one which contains no
655.QW ~
656or
657.QW ~user
658sequences (these have been expanded to their current
659representation in the filesystem).  The object returned is owned by the
660caller, which must store it or call Tcl_DecrRefCount to ensure memory is
661freed.  This function is of little practical use, and
662\fBTcl_FSGetNormalizedPath\fR or \fBTcl_GetNativePath\fR are usually
663better functions to use for most purposes.
664.PP
665\fBTcl_FSGetTranslatedStringPath\fR does the same as
666\fBTcl_FSGetTranslatedPath\fR, but returns a character string or NULL.
667The string returned is dynamically allocated and owned by the caller,
668which must store it or call \fBckfree\fR to ensure it is freed.  Again,
669\fBTcl_FSGetNormalizedPath\fR or \fBTcl_GetNativePath\fR are usually
670better functions to use for most purposes.
671.PP
672\fBTcl_FSNewNativePath\fR performs something like the reverse of the
673usual obj->path->nativerep conversions.  If some code retrieves a path
674in native form (from, e.g. \fBreadlink\fR or a native dialog), and that path
675is to be used at the Tcl level, then calling this function is an
676efficient way of creating the appropriate path object type.
677.PP
678The resulting object is a pure
679.QW path
680object, which will only receive
681a UTF-8 string representation if that is required by some Tcl code.
682.PP
683\fBTcl_FSGetNativePath\fR is for use by the Win/Unix native
684filesystems, so that they can easily retrieve the native (char* or
685TCHAR*) representation of a path.  This function is a convenience
686wrapper around \fBTcl_FSGetInternalRep\fR, and assumes the native
687representation is string-based.  It may be desirable in the future to
688have non-string-based native representations (for example, on MacOSX, a
689representation using a fileSpec of FSRef structure would probably be
690more efficient).  On Windows a full Unicode representation would allow
691for paths of unlimited length.  Currently the representation is simply a
692character string which may contain either the relative path or a
693complete, absolute normalized path in the native encoding (complex
694conditions dictate which of these will be provided, so neither can be
695relied upon, unless the path is known to be absolute).  If you need a
696native path which must be absolute, then you should ask for the native
697version of a normalized path.  If for some reason a non-absolute,
698non-normalized version of the path is needed, that must be constructed
699separately (e.g. using \fBTcl_FSGetTranslatedPath\fR).
700.PP
701The native representation is cached so that repeated calls to this
702function will not require additional conversions.  The return value is
703owned by Tcl and has a lifetime equivalent to that of the \fIpathPtr\fR
704passed in (unless that is a relative path, in which case the native
705representation may be freed any time the cwd changes).
706.PP
707\fBTcl_FSFileSystemInfo\fR returns a list of two elements.  The first
708element is the name of the filesystem (e.g.
709.QW native ,
710.QW vfs ,
711.QW zip ,
712or
713.QW prowrap ,
714perhaps), and the second is the particular type of the
715given path within that filesystem (which is filesystem dependent).  The
716second element may be empty if the filesystem does not provide a
717further categorization of files.
718.PP
719A valid list object is returned, unless the path object is not
720recognized, when NULL will be returned.
721.PP
722\fBTcl_FSGetFileSystemForPath\fR returns the a pointer to the
723\fBTcl_Filesystem\fR which accepts this path as valid.
724.PP
725If no filesystem will accept the path, NULL is returned.
726.PP
727\fBTcl_FSGetPathType\fR determines whether the given path is relative
728to the current directory, relative to the current volume, or
729absolute.
730.PP
731It returns one of \fBTCL_PATH_ABSOLUTE\fR, \fBTCL_PATH_RELATIVE\fR, or
732\fBTCL_PATH_VOLUME_RELATIVE\fR
733.PP
734\fBTcl_AllocStatBuf\fR allocates a \fITcl_StatBuf\fR on the system
735heap (which may be deallocated by being passed to \fBckfree\fR.)  This
736allows extensions to invoke \fBTcl_FSStat\fR and \fBTcl_FSLStat\fR
737without being dependent on the size of the buffer.  That in turn
738depends on the flags used to build Tcl.
739.SH "THE VIRTUAL FILESYSTEM API"
740.PP
741A filesystem provides a \fBTcl_Filesystem\fR structure that contains
742pointers to functions that implement the various operations on a
743filesystem; these operations are invoked as needed by the generic
744layer, which generally occurs through the functions listed above.
745.PP
746The \fBTcl_Filesystem\fR structures are manipulated using the following
747methods.
748.PP
749\fBTcl_FSRegister\fR takes a pointer to a filesystem structure and an
750optional piece of data to associated with that filesystem.  On calling
751this function, Tcl will attach the filesystem to the list of known
752filesystems, and it will become fully functional immediately.  Tcl does
753not check if the same filesystem is registered multiple times (and in
754general that is not a good thing to do).  \fBTCL_OK\fR will be returned.
755.PP
756\fBTcl_FSUnregister\fR removes the given filesystem structure from
757the list of known filesystems, if it is known, and returns \fBTCL_OK\fR.  If
758the filesystem is not currently registered, \fBTCL_ERROR\fR is returned.
759.PP
760\fBTcl_FSData\fR will return the ClientData associated with the given
761filesystem, if that filesystem is registered.  Otherwise it will
762return NULL.
763.PP
764\fBTcl_FSMountsChanged\fR is used to inform the Tcl's core that
765the set of mount points for the given (already registered) filesystem
766have changed, and that cached file representations may therefore no
767longer be correct.
768.SS "THE TCL_FILESYSTEM STRUCTURE"
769.PP
770The \fBTcl_Filesystem\fR structure contains the following fields:
771.CS
772typedef struct Tcl_Filesystem {
773    const char *\fItypeName\fR;
774    int \fIstructureLength\fR;
775    Tcl_FSVersion \fIversion\fR;
776    Tcl_FSPathInFilesystemProc *\fIpathInFilesystemProc\fR;
777    Tcl_FSDupInternalRepProc *\fIdupInternalRepProc\fR;
778    Tcl_FSFreeInternalRepProc *\fIfreeInternalRepProc\fR;
779    Tcl_FSInternalToNormalizedProc *\fIinternalToNormalizedProc\fR;
780    Tcl_FSCreateInternalRepProc *\fIcreateInternalRepProc\fR;
781    Tcl_FSNormalizePathProc *\fInormalizePathProc\fR;
782    Tcl_FSFilesystemPathTypeProc *\fIfilesystemPathTypeProc\fR;
783    Tcl_FSFilesystemSeparatorProc *\fIfilesystemSeparatorProc\fR;
784    Tcl_FSStatProc *\fIstatProc\fR;
785    Tcl_FSAccessProc *\fIaccessProc\fR;
786    Tcl_FSOpenFileChannelProc *\fIopenFileChannelProc\fR;
787    Tcl_FSMatchInDirectoryProc *\fImatchInDirectoryProc\fR;
788    Tcl_FSUtimeProc *\fIutimeProc\fR;
789    Tcl_FSLinkProc *\fIlinkProc\fR;
790    Tcl_FSListVolumesProc *\fIlistVolumesProc\fR;
791    Tcl_FSFileAttrStringsProc *\fIfileAttrStringsProc\fR;
792    Tcl_FSFileAttrsGetProc *\fIfileAttrsGetProc\fR;
793    Tcl_FSFileAttrsSetProc *\fIfileAttrsSetProc\fR;
794    Tcl_FSCreateDirectoryProc *\fIcreateDirectoryProc\fR;
795    Tcl_FSRemoveDirectoryProc *\fIremoveDirectoryProc\fR;
796    Tcl_FSDeleteFileProc *\fIdeleteFileProc\fR;
797    Tcl_FSCopyFileProc *\fIcopyFileProc\fR;
798    Tcl_FSRenameFileProc *\fIrenameFileProc\fR;
799    Tcl_FSCopyDirectoryProc *\fIcopyDirectoryProc\fR;
800    Tcl_FSLstatProc *\fIlstatProc\fR;
801    Tcl_FSLoadFileProc *\fIloadFileProc\fR;
802    Tcl_FSGetCwdProc *\fIgetCwdProc\fR;
803    Tcl_FSChdirProc *\fIchdirProc\fR;
804} Tcl_Filesystem;
805.CE
806.PP
807Except for the first three fields in this structure which contain
808simple data elements, all entries contain addresses of functions called
809by the generic filesystem layer to perform the complete range of
810filesystem related actions.
811.PP
812The many functions in this structure are broken down into three
813categories: infrastructure functions (almost all of which must be
814implemented), operational functions (which must be implemented if a
815complete filesystem is provided), and efficiency functions (which need
816only be implemented if they can be done so efficiently, or if they have
817side-effects which are required by the filesystem; Tcl has less
818efficient emulations it can fall back on).  It is important to note
819that, in the current version of Tcl, most of these fallbacks are only
820used to handle commands initiated in Tcl, not in C. What this means is,
821that if a \fBfile rename\fR command is issued in Tcl, and the relevant
822filesystem(s) do not implement their \fITcl_FSRenameFileProc\fR, Tcl's
823core will instead fallback on a combination of other filesystem
824functions (it will use \fITcl_FSCopyFileProc\fR followed by
825\fITcl_FSDeleteFileProc\fR, and if \fITcl_FSCopyFileProc\fR is not
826implemented there is a further fallback).  However, if a
827\fITcl_FSRenameFileProc\fR command is issued at the C level, no such
828fallbacks occur.  This is true except for the last four entries in the
829filesystem table (\fBlstat\fR, \fBload\fR, \fBgetcwd\fR and \fBchdir\fR)
830for which fallbacks do in fact occur at the C level.
831.PP
832Any functions which take path names in Tcl_Obj form take
833those names in UTF\-8 form.  The filesystem infrastructure API is
834designed to support efficient, cached conversion of these UTF\-8 paths
835to other native representations.
836.SS "EXAMPLE FILESYSTEM DEFINITION"
837.PP
838Here is the filesystem lookup table used by the
839.QW vfs
840extension which allows filesystem actions to be implemented in Tcl.
841.CS
842static Tcl_Filesystem vfsFilesystem = {
843    "tclvfs",
844    sizeof(Tcl_Filesystem),
845    TCL_FILESYSTEM_VERSION_1,
846    &VfsPathInFilesystem,
847    &VfsDupInternalRep,
848    &VfsFreeInternalRep,
849    /* No internal to normalized, since we don't create
850     * any pure 'internal' Tcl_Obj path representations */
851    NULL,
852    /* No create native rep function, since we don't use
853     * it and don't choose to support uses of
854     * Tcl_FSNewNativePath */
855    NULL,
856    /* Normalize path isn't needed - we assume paths only
857     * have one representation */
858    NULL,
859    &VfsFilesystemPathType,
860    &VfsFilesystemSeparator,
861    &VfsStat,
862    &VfsAccess,
863    &VfsOpenFileChannel,
864    &VfsMatchInDirectory,
865    &VfsUtime,
866    /* We choose not to support symbolic links inside our
867     * VFS's */
868    NULL,
869    &VfsListVolumes,
870    &VfsFileAttrStrings,
871    &VfsFileAttrsGet,
872    &VfsFileAttrsSet,
873    &VfsCreateDirectory,
874    &VfsRemoveDirectory,
875    &VfsDeleteFile,
876    /* No copy file; use the core fallback mechanism */
877    NULL,
878    /* No rename file; use the core fallback mechanism */
879    NULL,
880    /* No copy directory; use the core fallback mechanism */
881    NULL,
882    /* Core will use stat for lstat */
883    NULL,
884    /* No load; use the core fallback mechansism */
885    NULL,
886    /* We don't need a getcwd or chdir; the core's own
887     * internal value is suitable */
888    NULL,
889    NULL
890};
891.CE
892.SH "FILESYSTEM INFRASTRUCTURE"
893.PP
894These fields contain basic information about the filesystem structure
895and addresses of functions which are used to associate
896a particular filesystem with a file path, and deal with the internal
897handling of path representations, for example copying and freeing such
898representations.
899.SS TYPENAME
900.PP
901The \fItypeName\fR field contains a null-terminated string that
902identifies the type of the filesystem implemented, e.g.
903.QW native ,
904.QW zip
905or
906.QW vfs .
907.SS "STRUCTURE LENGTH"
908.PP
909The \fIstructureLength\fR field is generally implemented as
910\fIsizeof(Tcl_Filesystem)\fR, and is there to allow easier
911binary backwards compatibility if the size of the structure
912changes in a future Tcl release.
913.SS VERSION
914.PP
915The \fIversion\fR field should be set to \fBTCL_FILESYSTEM_VERSION_1\fR.
916.SS PATHINFILESYSTEMPROC
917.PP
918The \fIpathInFilesystemProc\fR field contains the address of a function
919which is called to determine whether a given path object belongs to this
920filesystem or not.  Tcl will only call the rest of the filesystem
921functions with a path for which this function has returned \fBTCL_OK\fR.
922If the path does not belong, -1 should be returned (the behaviour of Tcl
923for any other return value is not defined).  If \fBTCL_OK\fR is returned,
924then the optional \fIclientDataPtr\fR output parameter can be used to
925return an internal (filesystem specific) representation of the path,
926which will be cached inside the path object, and may be retrieved
927efficiently by the other filesystem functions.  Tcl will simultaneously
928cache the fact that this path belongs to this filesystem.  Such caches
929are invalidated when filesystem structures are added or removed from
930Tcl's internal list of known filesystems.
931.PP
932.CS
933typedef int Tcl_FSPathInFilesystemProc(
934        Tcl_Obj *\fIpathPtr\fR,
935        ClientData *\fIclientDataPtr\fR);
936.CE
937.SS DUPINTERNALREPPROC
938.PP
939This function makes a copy of a path's internal representation, and is
940called when Tcl needs to duplicate a path object.  If NULL, Tcl will
941simply not copy the internal representation, which may then need to be
942regenerated later.
943.PP
944.CS
945typedef ClientData Tcl_FSDupInternalRepProc(
946        ClientData \fIclientData\fR);
947.CE
948.SS FREEINTERNALREPPROC
949Free the internal representation.  This must be implemented if internal
950representations need freeing (i.e. if some memory is allocated when an
951internal representation is generated), but may otherwise be NULL.
952.PP
953.CS
954typedef void Tcl_FSFreeInternalRepProc(
955        ClientData \fIclientData\fR);
956.CE
957.SS INTERNALTONORMALIZEDPROC
958.PP
959Function to convert internal representation to a normalized path.  Only
960required if the filesystem creates pure path objects with no string/path
961representation.  The return value is a Tcl object whose string
962representation is the normalized path.
963.PP
964.CS
965typedef Tcl_Obj* Tcl_FSInternalToNormalizedProc(
966        ClientData \fIclientData\fR);
967.CE
968.SS CREATEINTERNALREPPROC
969.PP
970Function to take a path object, and calculate an internal
971representation for it, and store that native representation in the
972object.  May be NULL if paths have no internal representation, or if
973the \fITcl_FSPathInFilesystemProc\fR for this filesystem always
974immediately creates an internal representation for paths it accepts.
975.PP
976.CS
977typedef ClientData Tcl_FSCreateInternalRepProc(
978        Tcl_Obj *\fIpathPtr\fR);
979.CE
980.SS NORMALIZEPATHPROC
981.PP
982Function to normalize a path.  Should be implemented for all
983filesystems which can have multiple string representations for the same
984path object.  In Tcl, every
985.QW path
986must have a single unique
987.QW normalized
988string representation.  Depending on the filesystem,
989there may be more than one unnormalized string representation which
990refers to that path (e.g. a relative path, a path with different
991character case if the filesystem is case insensitive, a path contain a
992reference to a home directory such as
993.QW ~ ,
994a path containing symbolic
995links, etc).  If the very last component in the path is a symbolic
996link, it should not be converted into the object it points to (but
997its case or other aspects should be made unique).  All other path
998components should be converted from symbolic links.  This one
999exception is required to agree with Tcl's semantics with \fBfile
1000delete\fR, \fBfile rename\fR, \fBfile copy\fR operating on symbolic links.
1001This function may be called with \fInextCheckpoint\fR either
1002at the beginning of the path (i.e. zero), at the end of the path, or
1003at any intermediate file separator in the path.  It will never
1004point to any other arbitrary position in the path. In the last of
1005the three valid cases, the implementation can assume that the path
1006up to and including the file separator is known and normalized.
1007.PP
1008.CS
1009typedef int Tcl_FSNormalizePathProc(
1010        Tcl_Interp *\fIinterp\fR,
1011        Tcl_Obj *\fIpathPtr\fR,
1012        int \fInextCheckpoint\fR);
1013.CE
1014.SH "FILESYSTEM OPERATIONS"
1015.PP
1016The fields in this section of the structure contain addresses of
1017functions which are called to carry out the basic filesystem
1018operations.  A filesystem which expects to be used with the complete
1019standard Tcl command set must implement all of these.  If some of
1020them are not implemented, then certain Tcl commands may fail when
1021operating on paths within that filesystem.  However, in some instances
1022this may be desirable (for example, a read-only filesystem should not
1023implement the last four functions, and a filesystem which does not
1024support symbolic links need not implement the \fBreadlink\fR function,
1025etc.  The Tcl core expects filesystems to behave in this way).
1026.SS FILESYSTEMPATHTYPEPROC
1027.PP
1028Function to determine the type of a path in this filesystem.  May be
1029NULL, in which case no type information will be available to users of
1030the filesystem.  The
1031.QW type
1032is used only for informational purposes,
1033and should be returned as the string representation of the Tcl_Obj
1034which is returned.  A typical return value might be
1035.QW networked ,
1036.QW zip
1037or
1038.QW ftp .
1039The Tcl_Obj result is owned by the filesystem and so Tcl will
1040increment the refCount of that object if it wishes to retain a reference
1041to it.
1042.PP
1043.CS
1044typedef Tcl_Obj* Tcl_FSFilesystemPathTypeProc(
1045        Tcl_Obj *\fIpathPtr\fR);
1046.CE
1047.SS FILESYSTEMSEPARATORPROC
1048.PP
1049Function to return the separator character(s) for this filesystem.
1050This need only be implemented if the filesystem wishes to use a
1051different separator than the standard string
1052.QW / .
1053Amongst other
1054uses, it is returned by the \fBfile separator\fR command.  The
1055return value should be an object with refCount of zero.
1056.PP
1057.CS
1058typedef Tcl_Obj* Tcl_FSFilesystemSeparatorProc(
1059        Tcl_Obj *\fIpathPtr\fR);
1060.CE
1061.SS STATPROC
1062.PP
1063Function to process a \fBTcl_FSStat\fR call.  Must be implemented for any
1064reasonable filesystem, since many Tcl level commands depend crucially
1065upon it (e.g. \fBfile atime\fR, \fBfile isdirectory\fR, \fBfile size\fR,
1066\fBglob\fR).
1067.PP
1068.CS
1069typedef int Tcl_FSStatProc(
1070        Tcl_Obj *\fIpathPtr\fR,
1071        Tcl_StatBuf *\fIstatPtr\fR);
1072.CE
1073.PP
1074The \fBTcl_FSStatProc\fR fills the stat structure \fIstatPtr\fR with
1075information about the specified file.  You do not need any access
1076rights to the file to get this information but you need search rights
1077to all directories named in the path leading to the file.  The stat
1078structure includes info regarding device, inode (always 0 on Windows),
1079privilege mode, nlink (always 1 on Windows), user id (always 0 on
1080Windows), group id (always 0 on Windows), rdev (same as device on
1081Windows), size, last access time, last modification time, and creation
1082time.
1083.PP
1084If the file represented by \fIpathPtr\fR exists, the
1085\fBTcl_FSStatProc\fR returns 0 and the stat structure is filled with
1086data.  Otherwise, -1 is returned, and no stat info is given.
1087.SS ACCESSPROC
1088.PP
1089Function to process a \fBTcl_FSAccess\fR call.  Must be implemented for
1090any reasonable filesystem, since many Tcl level commands depend crucially
1091upon it (e.g. \fBfile exists\fR, \fBfile readable\fR).
1092.PP
1093.CS
1094typedef int Tcl_FSAccessProc(
1095        Tcl_Obj *\fIpathPtr\fR,
1096        int \fImode\fR);
1097.CE
1098.PP
1099The \fBTcl_FSAccessProc\fR checks whether the process would be allowed
1100to read, write or test for existence of the file (or other filesystem
1101object) whose name is in \fIpathPtr\fR.  If the pathname refers to a
1102symbolic link, then the
1103permissions of the file referred by this symbolic link should be tested.
1104.PP
1105On success (all requested permissions granted), zero is returned.  On
1106error (at least one bit in mode asked for a permission that is denied,
1107or some other  error occurred), -1 is returned.
1108.SS OPENFILECHANNELPROC
1109.PP
1110Function to process a \fBTcl_FSOpenFileChannel\fR call.  Must be
1111implemented for any reasonable filesystem, since any operations
1112which require open or accessing a file's contents will use it
1113(e.g. \fBopen\fR, \fBencoding\fR, and many Tk commands).
1114.PP
1115.CS
1116typedef Tcl_Channel Tcl_FSOpenFileChannelProc(
1117        Tcl_Interp *\fIinterp\fR,
1118        Tcl_Obj *\fIpathPtr\fR,
1119        int \fImode\fR,
1120        int \fIpermissions\fR);
1121.CE
1122.PP
1123The \fBTcl_FSOpenFileChannelProc\fR opens a file specified by
1124\fIpathPtr\fR and returns a channel handle that can be used to perform
1125input and output on the file.  This API is modeled after the \fBfopen\fR
1126procedure of the Unix standard I/O library.  The syntax and meaning of
1127all arguments is similar to those given in the Tcl \fBopen\fR command
1128when opening a file, where the \fImode\fR argument is a combination of
1129the POSIX flags O_RDONLY, O_WRONLY, etc.  If an error occurs while
1130opening the channel, the \fBTcl_FSOpenFileChannelProc\fR returns NULL and
1131records a POSIX error code that can be retrieved with \fBTcl_GetErrno\fR.
1132In addition, if \fIinterp\fR is non-NULL, the
1133\fBTcl_FSOpenFileChannelProc\fR leaves an error message in \fIinterp\fR's
1134result after any error.
1135.PP
1136The newly created channel is not registered in the supplied
1137interpreter; to register it, use \fBTcl_RegisterChannel\fR. If one of
1138the standard channels, \fBstdin, stdout\fR or \fBstderr\fR was
1139previously closed, the act of creating the new channel also assigns it
1140as a replacement for the standard channel.
1141.SS MATCHINDIRECTORYPROC
1142.PP
1143Function to process a \fBTcl_FSMatchInDirectory\fR call.  If not
1144implemented, then glob and recursive copy functionality will be lacking
1145in the filesystem (and this may impact commands like \fBencoding names\fR
1146which use glob functionality internally).
1147.PP
1148.CS
1149typedef int Tcl_FSMatchInDirectoryProc(
1150        Tcl_Interp* \fIinterp\fR,
1151        Tcl_Obj *\fIresultPtr\fR,
1152        Tcl_Obj *\fIpathPtr\fR,
1153        const char *\fIpattern\fR,
1154        Tcl_GlobTypeData *\fItypes\fR);
1155.CE
1156.PP
1157The function should return all files or directories (or other filesystem
1158objects) which match the given pattern and accord with the \fItypes\fR
1159specification given.  There are two ways in which this function may be
1160called.  If \fIpattern\fR is NULL, then \fIpathPtr\fR is a full path
1161specification of a single file or directory which should be checked for
1162existence and correct type.  Otherwise, \fIpathPtr\fR is a directory, the
1163contents of which the function should search for files or directories
1164which have the correct type.  In either case, \fIpathPtr\fR can be
1165assumed to be both non-NULL and non-empty.  It is not currently
1166documented whether \fIpathPtr\fR will have a file separator at its end of
1167not, so code should be flexible to both possibilities.
1168.PP
1169The return value is a standard Tcl result indicating whether an error
1170occurred in the matching process.  Error messages are placed in
1171\fIinterp\fR, unless \fIinterp\fR in NULL in which case no error
1172message need be generated; on a \fBTCL_OK\fR result, results should be
1173added to the \fIresultPtr\fR object given (which can be assumed to be a
1174valid unshared Tcl list).  The matches added
1175to \fIresultPtr\fR should include any path prefix given in \fIpathPtr\fR
1176(this usually means they will be absolute path specifications).
1177Note that if no matches are found, that simply leads to an empty
1178result; errors are only signaled for actual file or filesystem
1179problems which may occur during the matching process.
1180.PP
1181The \fBTcl_GlobTypeData\fR structure passed in the \fItypes\fR
1182parameter contains the following fields:
1183.CS
1184typedef struct Tcl_GlobTypeData {
1185        /* Corresponds to bcdpfls as in 'find -t' */
1186        int \fItype\fR;
1187        /* Corresponds to file permissions */
1188        int \fIperm\fR;
1189        /* Acceptable mac type */
1190        Tcl_Obj *\fImacType\fR;
1191        /* Acceptable mac creator */
1192        Tcl_Obj *\fImacCreator\fR;
1193} Tcl_GlobTypeData;
1194.CE
1195.PP
1196There are two specific cases which it is important to handle correctly,
1197both when \fItypes\fR is non-NULL. The two cases are when \fItypes->types
1198& TCL_GLOB_TYPE_DIR\fR or \fItypes->types & TCL_GLOB_TYPE_MOUNT\fR are
1199true (and in particular when the other flags are false).  In the first of
1200these cases, the function must list the contained directories.  Tcl uses
1201this to implement recursive globbing, so it is critical that filesystems
1202implement directory matching correctly.  In the second of these cases,
1203with \fBTCL_GLOB_TYPE_MOUNT\fR, the filesystem must list the mount points
1204which lie within the given \fIpathPtr\fR (and in this case, \fIpathPtr\fR
1205need not lie within the same filesystem - different to all other cases in
1206which this function is called).  Support for this is critical if Tcl is
1207to have seamless transitions between from one filesystem to another.
1208.SS UTIMEPROC
1209.PP
1210Function to process a \fBTcl_FSUtime\fR call.  Required to allow setting
1211(not reading) of times with \fBfile mtime\fR, \fBfile atime\fR and the
1212open-r/open-w/fcopy implementation of \fBfile copy\fR.
1213.PP
1214.CS
1215typedef int Tcl_FSUtimeProc(
1216        Tcl_Obj *\fIpathPtr\fR,
1217        struct utimbuf *\fItval\fR);
1218.CE
1219.PP
1220The access and modification times of the file specified by \fIpathPtr\fR
1221should be changed to the values given in the \fItval\fR structure.
1222.PP
1223The return value should be 0 on success and -1 on an error, as
1224with the system \fButime\fR.
1225.SS LINKPROC
1226.PP
1227Function to process a \fBTcl_FSLink\fR call.  Should be implemented
1228only if the filesystem supports links, and may otherwise be NULL.
1229.PP
1230.CS
1231typedef Tcl_Obj* Tcl_FSLinkProc(
1232        Tcl_Obj *\fIlinkNamePtr\fR,
1233        Tcl_Obj *\fItoPtr\fR,
1234        int \fIlinkAction\fR);
1235.CE
1236.PP
1237If \fItoPtr\fR is NULL, the function is being asked to read the
1238contents of a link.  The result is a Tcl_Obj specifying the contents of
1239the link given by \fIlinkNamePtr\fR, or NULL if the link could
1240not be read.  The result is owned by the caller (and should therefore
1241have its ref count incremented before being returned).  Any callers
1242should call Tcl_DecrRefCount on this result when it is no longer needed.
1243If \fItoPtr\fR is not NULL, the function should attempt to create a link.
1244The result in this case should be \fItoPtr\fR if the link was successful
1245and NULL otherwise.  In this case the result is not owned by the caller
1246(i.e. no ref count manipulation on either end is needed). See
1247the documentation for \fBTcl_FSLink\fR for the correct interpretation
1248of the \fIlinkAction\fR flags.
1249.SS LISTVOLUMESPROC
1250.PP
1251Function to list any filesystem volumes added by this filesystem.
1252Should be implemented only if the filesystem adds volumes at the head
1253of the filesystem, so that they can be returned by \fBfile volumes\fR.
1254.PP
1255.CS
1256typedef Tcl_Obj* Tcl_FSListVolumesProc(void);
1257.CE
1258.PP
1259The result should be a list of volumes added by this filesystem, or
1260NULL (or an empty list) if no volumes are provided.  The result object
1261is considered to be owned by the filesystem (not by Tcl's core), but
1262should be given a refCount for Tcl.  Tcl will use the contents of the
1263list and then decrement that refCount.  This allows filesystems to
1264choose whether they actually want to retain a
1265.QW "master list"
1266of volumes
1267or not (if not, they generate the list on the fly and pass it to Tcl
1268with a refCount of 1 and then forget about the list, if yes, then
1269they simply increment the refCount of their master list and pass it
1270to Tcl which will copy the contents and then decrement the count back
1271to where it was).
1272.PP
1273Therefore, Tcl considers return values from this proc to be read-only.
1274.SS FILEATTRSTRINGSPROC
1275.PP
1276Function to list all attribute strings which are valid for this
1277filesystem.  If not implemented the filesystem will not support
1278the \fBfile attributes\fR command.  This allows arbitrary additional
1279information to be attached to files in the filesystem.  If it is
1280not implemented, there is no need to implement the \fBget\fR and \fBset\fR
1281methods.
1282.PP
1283.CS
1284typedef const char** Tcl_FSFileAttrStringsProc(
1285        Tcl_Obj *\fIpathPtr\fR,
1286        Tcl_Obj** \fIobjPtrRef\fR);
1287.CE
1288.PP
1289The called function may either return an array of strings, or may
1290instead return NULL and place a Tcl list into the given \fIobjPtrRef\fR.  Tcl
1291will take that list and first increment its reference count before using it.
1292On completion of that use, Tcl will decrement its reference count.  Hence if
1293the list should be disposed of by Tcl when done, it should have a
1294reference count of zero, and if the list should not be disposed of, the
1295filesystem should ensure it returns an object with a refererence count
1296of at least one.
1297.SS FILEATTRSGETPROC
1298.PP
1299Function to process a \fBTcl_FSFileAttrsGet\fR call, used by \fBfile
1300attributes\fR.
1301.PP
1302.CS
1303typedef int Tcl_FSFileAttrsGetProc(
1304        Tcl_Interp *\fIinterp\fR,
1305        int \fIindex\fR,
1306        Tcl_Obj *\fIpathPtr\fR,
1307        Tcl_Obj **\fIobjPtrRef\fR);
1308.CE
1309.PP
1310Returns a standard Tcl return code.  The attribute value retrieved,
1311which corresponds to the \fIindex\fR'th element in the list returned by
1312the \fBTcl_FSFileAttrStringsProc\fR, is a Tcl_Obj placed in \fIobjPtrRef\fR (if
1313\fBTCL_OK\fR was returned) and is likely to have a reference count of zero.  Either
1314way we must either store it somewhere (e.g. the Tcl result), or
1315Incr/Decr its reference count to ensure it is properly freed.
1316.SS FILEATTRSSETPROC
1317.PP
1318Function to process a \fBTcl_FSFileAttrsSet\fR call, used by \fBfile
1319attributes\fR.  If the filesystem is read-only, there is no need
1320to implement this.
1321.PP
1322.CS
1323typedef int Tcl_FSFileAttrsSetProc(
1324        Tcl_Interp *\fIinterp\fR,
1325        int \fIindex\fR,
1326        Tcl_Obj *\fIpathPtr\fR,
1327        Tcl_Obj *\fIobjPtr\fR);
1328.CE
1329.PP
1330The attribute value of the \fIindex\fR'th element in the list returned by
1331the Tcl_FSFileAttrStringsProc should be set to the \fIobjPtr\fR given.
1332.SS CREATEDIRECTORYPROC
1333.PP
1334Function to process a \fBTcl_FSCreateDirectory\fR call.  Should be
1335implemented unless the FS is read-only.
1336.PP
1337.CS
1338typedef int Tcl_FSCreateDirectoryProc(
1339        Tcl_Obj *\fIpathPtr\fR);
1340.CE
1341.PP
1342The return value is a standard Tcl result indicating whether an error
1343occurred in the process.  If successful, a new directory should have
1344been added to the filesystem in the location specified by
1345\fIpathPtr\fR.
1346.SS REMOVEDIRECTORYPROC
1347.PP
1348Function to process a \fBTcl_FSRemoveDirectory\fR call.  Should be
1349implemented unless the FS is read-only.
1350.PP
1351.CS
1352typedef int Tcl_FSRemoveDirectoryProc(
1353        Tcl_Obj *\fIpathPtr\fR,
1354        int \fIrecursive\fR,
1355        Tcl_Obj **\fIerrorPtr\fR);
1356.CE
1357.PP
1358The return value is a standard Tcl result indicating whether an error
1359occurred in the process.  If successful, the directory specified by
1360\fIpathPtr\fR should have been removed from the filesystem.  If the
1361\fIrecursive\fR flag is given, then a non-empty directory should be
1362deleted without error.  If this flag is not given, then and the
1363directory is non-empty a POSIX
1364.QW EEXIST
1365error should be signalled.  If an
1366error does occur, the name of the file or directory which caused the
1367error should be placed in \fIerrorPtr\fR.
1368.SS DELETEFILEPROC
1369.PP
1370Function to process a \fBTcl_FSDeleteFile\fR call.  Should be implemented
1371unless the FS is read-only.
1372.PP
1373.CS
1374typedef int Tcl_FSDeleteFileProc(
1375        Tcl_Obj *\fIpathPtr\fR);
1376.CE
1377.PP
1378The return value is a standard Tcl result indicating whether an error
1379occurred in the process.  If successful, the file specified by
1380\fIpathPtr\fR should have been removed from the filesystem.  Note that,
1381if the filesystem supports symbolic links, Tcl will always call this
1382function and not Tcl_FSRemoveDirectoryProc when needed to delete them
1383(even if they are symbolic links to directories).
1384.SH "FILESYSTEM EFFICIENCY"
1385.PP
1386These functions need not be implemented for a particular filesystem
1387because the core has a fallback implementation available. See each
1388individual description for the consequences of leaving the field NULL.
1389.SS LSTATPROC
1390.PP
1391Function to process a \fBTcl_FSLstat\fR call.  If not implemented, Tcl
1392will attempt to use the \fIstatProc\fR defined above instead.  Therefore
1393it need only be implemented if a filesystem can differentiate between
1394\fBstat\fR and \fBlstat\fR calls.
1395.PP
1396.CS
1397typedef int Tcl_FSLstatProc(
1398        Tcl_Obj *\fIpathPtr\fR,
1399        Tcl_StatBuf *\fIstatPtr\fR);
1400.CE
1401.PP
1402The behavior of this function is very similar to that of the
1403\fBTcl_FSStatProc\fR defined above, except that if it is applied
1404to a symbolic link, it returns information about the link, not
1405about the target file.
1406.SS COPYFILEPROC
1407.PP
1408Function to process a \fBTcl_FSCopyFile\fR call.  If not implemented Tcl
1409will fall back on \fBopen\fR-r, \fBopen\fR-w and \fBfcopy\fR as a
1410copying mechanism.
1411Therefore it need only be implemented if the filesystem can perform
1412that action more efficiently.
1413.PP
1414.CS
1415typedef int Tcl_FSCopyFileProc(
1416        Tcl_Obj *\fIsrcPathPtr\fR,
1417        Tcl_Obj *\fIdestPathPtr\fR);
1418.CE
1419.PP
1420The return value is a standard Tcl result indicating whether an error
1421occurred in the copying process.  Note that, \fIdestPathPtr\fR is the
1422name of the file which should become the copy of \fIsrcPathPtr\fR. It
1423is never the name of a directory into which \fIsrcPathPtr\fR could be
1424copied (i.e. the function is much simpler than the Tcl level \fBfile
1425copy\fR subcommand).  Note that,
1426if the filesystem supports symbolic links, Tcl will always call this
1427function and not \fIcopyDirectoryProc\fR when needed to copy them
1428(even if they are symbolic links to directories).  Finally, if the
1429filesystem determines it cannot support the \fBfile copy\fR action,
1430calling \fBTcl_SetErrno(EXDEV)\fR and returning a non-\fBTCL_OK\fR
1431result will tell Tcl to use its standard fallback mechanisms.
1432.SS RENAMEFILEPROC
1433.PP
1434Function to process a \fBTcl_FSRenameFile\fR call.  If not implemented,
1435Tcl will fall back on a copy and delete mechanism.  Therefore it need
1436only be implemented if the filesystem can perform that action more
1437efficiently.
1438.PP
1439.CS
1440typedef int Tcl_FSRenameFileProc(
1441        Tcl_Obj *\fIsrcPathPtr\fR,
1442        Tcl_Obj *\fIdestPathPtr\fR);
1443.CE
1444.PP
1445The return value is a standard Tcl result indicating whether an error
1446occurred in the renaming process.  If the
1447filesystem determines it cannot support the \fBfile rename\fR action,
1448calling \fBTcl_SetErrno(EXDEV)\fR and returning a non-\fBTCL_OK\fR
1449result will tell Tcl to use its standard fallback mechanisms.
1450.SS COPYDIRECTORYPROC
1451.PP
1452Function to process a \fBTcl_FSCopyDirectory\fR call.  If not
1453implemented, Tcl will fall back on a recursive \fBfile mkdir\fR, \fBfile copy\fR
1454mechanism.  Therefore it need only be implemented if the filesystem can
1455perform that action more efficiently.
1456.PP
1457.CS
1458typedef int Tcl_FSCopyDirectoryProc(
1459        Tcl_Obj *\fIsrcPathPtr\fR,
1460        Tcl_Obj *\fIdestPathPtr\fR,
1461        Tcl_Obj **\fIerrorPtr\fR);
1462.CE
1463.PP
1464The return value is a standard Tcl result indicating whether an error
1465occurred in the copying process.  If an error does occur, the name of
1466the file or directory which caused the error should be placed in
1467\fIerrorPtr\fR. Note that, \fIdestPathPtr\fR is the name of the
1468directory-name which should become the mirror-image of
1469\fIsrcPathPtr\fR. It is not the name of a directory into which
1470\fIsrcPathPtr\fR should be copied (i.e. the function is much simpler
1471than the Tcl level \fBfile copy\fR subcommand).  Finally, if the
1472filesystem determines it cannot support the directory copy action,
1473calling \fBTcl_SetErrno(EXDEV)\fR and returning a non-\fBTCL_OK\fR
1474result will tell Tcl to use its standard fallback mechanisms.
1475.SS LOADFILEPROC
1476.PP
1477Function to process a \fBTcl_FSLoadFile\fR call.  If not implemented, Tcl
1478will fall back on a copy to native-temp followed by a \fBTcl_FSLoadFile\fR on
1479that temporary copy.  Therefore it need only be implemented if the
1480filesystem can load code directly, or it can be implemented simply to
1481return \fBTCL_ERROR\fR to disable load functionality in this filesystem
1482entirely.
1483.PP
1484.CS
1485typedef int Tcl_FSLoadFileProc(
1486        Tcl_Interp *\fIinterp\fR,
1487        Tcl_Obj *\fIpathPtr\fR,
1488        Tcl_LoadHandle *\fIhandlePtr\fR,
1489        Tcl_FSUnloadFileProc *\fIunloadProcPtr\fR);
1490.CE
1491.PP
1492Returns a standard Tcl completion code.  If an error occurs, an error
1493message is left in the \fIinterp\fR's result.  The function dynamically loads a
1494binary code file into memory.  On a successful load, the \fIhandlePtr\fR
1495should be filled with a token for the dynamically loaded file, and the
1496\fIunloadProcPtr\fR should be filled in with the address of a procedure.
1497The unload procedure will be called with the given \fBTcl_LoadHandle\fR as its
1498only parameter when Tcl needs to unload the file.  For example, for the
1499native filesystem, the \fBTcl_LoadHandle\fR returned is currently a token
1500which can be used in the private \fBTclpFindSymbol\fR to access functions
1501in the new code.  Each filesystem is free to define the
1502\fBTcl_LoadHandle\fR as it requires.  Finally, if the
1503filesystem determines it cannot support the file load action,
1504calling \fBTcl_SetErrno(EXDEV)\fR and returning a non-\fBTCL_OK\fR
1505result will tell Tcl to use its standard fallback mechanisms.
1506.SS UNLOADFILEPROC
1507.PP
1508Function to unload a previously successfully loaded file.  If load was
1509implemented, then this should also be implemented, if there is any
1510cleanup action required.
1511.PP
1512.CS
1513typedef void Tcl_FSUnloadFileProc(
1514        Tcl_LoadHandle \fIloadHandle\fR);
1515.CE
1516.SS GETCWDPROC     
1517.PP
1518Function to process a \fBTcl_FSGetCwd\fR call.  Most filesystems need not
1519implement this.  It will usually only be called once, if \fBgetcwd\fR is
1520called before \fBchdir\fR.  May be NULL.
1521.PP
1522.CS
1523typedef Tcl_Obj* Tcl_FSGetCwdProc(
1524        Tcl_Interp *\fIinterp\fR);
1525.CE
1526.PP
1527If the filesystem supports a native notion of a current working
1528directory (which might perhaps change independent of Tcl), this
1529function should return that cwd as the result, or NULL if the current
1530directory could not be determined (e.g. the user does not have
1531appropriate permissions on the cwd directory).  If NULL is returned, an
1532error message is left in the \fIinterp\fR's result.
1533.SS CHDIRPROC
1534.PP
1535Function to process a \fBTcl_FSChdir\fR call.  If filesystems do not
1536implement this, it will be emulated by a series of directory access
1537checks.  Otherwise, virtual filesystems which do implement it need only
1538respond with a positive return result if the \fIpathPtr\fR is a valid,
1539accessible directory in their filesystem.  They need not remember the
1540result, since that will be automatically remembered for use by
1541\fBTcl_FSGetCwd\fR.
1542Real filesystems should carry out the correct action (i.e. call the
1543correct system \fBchdir\fR API).
1544.PP
1545.CS
1546typedef int Tcl_FSChdirProc(
1547        Tcl_Obj *\fIpathPtr\fR);
1548.CE
1549.PP
1550The \fBTcl_FSChdirProc\fR changes the applications current working
1551directory to the value specified in \fIpathPtr\fR. The function returns
1552-1 on error or 0 on success.
1553.SH "SEE ALSO"
1554cd(n), file(n), load(n), open(n), pwd(n), unload(n)
1555.SH KEYWORDS
1556stat, access, filesystem, vfs, virtual
Note: See TracBrowser for help on using the repository browser.