Share modes
The Common File System supports four share modes for opening files. The constants used to specify these modes appear in the following table. These constants are defined in the CfsConstants pool dictionary.
Table 13. Share mode constants
Pool variable
Description
ODENYNONE
Other processes can open the file for any access: read-only, write-only, or read-write.
ODENYRD
Other processes can open the file for write-only access, but they cannot open it for read-only or read-write access.
ODENYWR
Other processes can open the file for read-only access, but they cannot open it for write-only or read-write access.
ODENYRDWR
The current process has exclusive access to the file. Other processes cannot open the file. It is unspecified whether the file can be opened by the current process.
Selecting valid share modes
Like file locking types, share modes are not uniformly supported by all platforms. Some platforms can have default share modes that are associated with access modes, and cannot be changed. For example, opening a file with ORDONLY access might have the same effect as specifying the ODENYWR share mode, and opening a file with OWRONLY access might have the same effect as specifying ODENYRDWR. On platforms that do not support specifiable share modes, share modes are ignored.
To determine whether a particular share mode is supported, send the supportsShareMode: message to the CfsFileDescriptor class, with the desired share mode constant as an argument. If the specified share mode is supported, true is answered. Otherwise, false is answered. The following code fragment determines if the ODENYRDWR share mode is supported:
"Is ODENYRDWR supported?"
^CfsFileDescriptor supportsShareMode: ODENYRDWR.
Tip:
Like locking, share modes might have no effect unless the file is being accessed by what the operating system considers to be a distinct process. With some network software, it can only be effective if the file is being accessed from a different machine.
Opening files using share modes
To open a file descriptor with a specific share mode, send the open:oflag:share: message to the CfsFileDescriptor class instead of sending open:oflag:. The third argument specifies the share mode to use. If share modes are not supported, it has no effect. Here are some examples:
"Open a file read/write with exclusive access"
| fd |
fd := CfsFileDescriptor
open: 'exclusiv.fil'
oflag: ORDWR | OCREAT
share: ODENYRDWR.
fd isCfsError
ifTrue: [^self error: fd printString].
fd close.
"Open a file denying write access to others"
| fd |
fd := CfsFileDescriptor
open: 'readonly.str'
oflag: ORDONLY
share: ODENYWR.
fd isCfsError
ifTrue: [^self error: fd printString].
fd close.
Last modified date: 05/12/2020