Common File System : Using low-level file operations : Opening files

Opening files
A file is opened by sending the open:oflag: message to the CfsFileDescriptor class, which answers a new CfsFileDescriptor instance to act as the receiver for I/O operations to that file. The first argument is a String or DBString specifying the path of the file to be opened; the second is an integer specifying the inclusive-OR of exactly one access mode and zero or more open flags. Access modes and flags are defined in the CfsConstants pool dictionary.
The following table describes open access modes and flags.
Table 10. Open access modes and flags (OR together)
Some examples of the use of access modes and open flags follow:
| fd |
"Opens an existing file for reading, fails if it does not exist"
(fd := CfsFileDescriptor
open: 'exists.txt'
oflag: ORDONLY) isCfsError
ifTrue: [^self error: fd message].
fd close.
| fd |
"Opens a new file for writing, fails if it exists"
(fd := CfsFileDescriptor
open: 'new.txt'
oflag: OWRONLY | OCREAT | OEXCL) isCfsError
ifTrue: [^self error: fd message].
fd close.
| fd |
"Opens an existing file for reading and writing,
or creates it if it does not exist"
(fd := CfsFileDescriptor
open: 'log.txt'
oflag: ORDWR | OCREAT) isCfsError
ifTrue: [^self error: fd message].
fd close.
| fd |
"Ensures that the opened file is empty: creates if it does not exist,
truncate to size 0 if it does."
(fd := CfsFileDescriptor
open: 'empty.txt'
oflag: ORDWR | OCREAT | OTRUNC) isCfsError
ifTrue: [^self error: fd message].
fd close.