Obtaining a CfsStat instance
Sending the stat: message to the CfsStat class returns a CfsStat instance containing the statistics for the file specified as its argument. These statistics can be accessed by sending messages, that match POSIX.1 stat structure names, to the instance. An example follows:
"Answer the file size"
^(CfsStat stat: 'bigfile.txt') stSize
 
Tip:
To obtain the exact size of a file, send size to an open file descriptor rather than using stat:, because CfsFileDescriptor>>#size reflects the true size of the file based on the end-of-file position as of last write, while CfsStat>>#size obtains the amount of storage space allocated to the file by reading the file's directory entry. For this reason, the size obtained by sending size to a CfsState or CfsDirectoryEntry might not match the logical size of the file, and might not be up-to-date if the file is currently open, even if a flush was just performed on the file descriptor.
The following messages can be sent to a CfsStat instance. The messages always answer a meaningful value, because all platforms support the information associated with them.
isBlk
Answers true if the receiver is reporting statistics for a block special file. Otherwise answers false.
isChr
Answers true if the receiver is reporting statistics for a character special file. Otherwise answers false.
isDir
Answers true if the receiver is reporting statistics for a directory. Otherwise answers false.
isFifo
Answers true if the receiver is reporting statistics for a pipe or for a FIFO special file. Otherwise answers false.
isReg
Answers true if the receiver is reporting statistics for a regular file. Otherwise answers false.
isSpecial
Answers true if the receiver is reporting statistics for a special file. Otherwise answers false. A special file is any file that is neither a regular file nor a directory.
stat:
Obtains statistics for the specified file and stores them in the receiver.
stMode
Answers the platform-specific file mode, which indicates the type of file and other attributes.
stMtime
Answers an array containing the date and time that the file was last modified.
stSize
Answers an integer that is the size in bytes of the regular file. If the receiver is not reporting statistics for a regular file, the value is unspecified.
The following messages in provide access to additional information that is not supported uniformly by all platforms. These messages can answer nil if a particular platform does not support the associated type of information.
stAtime
Answers an array containing the date and time that the file was last accessed.
stCTime
Answers an array containing the date and time that the file's status was last changed.
stDev
Answers the device ID of the device containing the file.
stFtime
Answers an array containing the creation date and time.
stGid
Answers the group ID for the file.
stIno
Answers the serial number of the file.
stNlink
Answers the number of links for the file.
stUid
Answers the user ID for the file.
If repeated stat operations are to be performed, garbage creation can be minimized by creating an empty CfsStat instance and then reusing it by sending the stat: message to the instance. An example follows:
| statBuf |
"Create CfsStat instance for buffer"
statBuf := CfsStat new.
"Answer the sum of the sizes of three files"
^(statBuf stat: 'one.fil') stSize + (statBuf stat: 'two.fil')
stSize + (statBuf stat: 'three.fil') stSize
By combining the stat: message with error checking, it is possible to test for file existence and other special properties. Some examples follow.
"Check file existence: Answer true if path specifies an existing file of any type"
^(CfsStat stat: path) isCfsError not
"Check if a path name is already being used"
"Answer true if path specifies an existing file of any type"
"Customized to consider inaccessible entries to be existing files"
| result |
result := CfsStat stat: path.
result isCfsError
ifTrue: [
result errno = EACCES ifTrue: ^true].
^false]
ifFalse: [^true].
"Answer true if path specifies an existing directory"
| result |
result := CfsStat stat: 'aDir'.
result isCfsError
ifTrue: [^false]
ifFalse: [^result isDir].
 
Tip:
Do not use stat: to try to prevent errors by testing whether a file exists before opening it. It is better to catch the fail case from the open operation, because the file can be deleted in the time between the existence test and opening the file.
Last modified date: 04/20/2020