Sunday, October 5, 2014

Flags in Linux OPEN call

O_APPEND
Before each write, the file posiiton will be updated to point to the end of the file.  This happens even a second program writes to the same file since the last write by the first program.  The write by the first program will started from the new end of file position.

O_ASYNC
A signal (SIGIO) will be generated when the file becomes readable or writable.  This flag is used for pipes, sockets or terminal and not for regular file.

O_CLOEXEC (close on exec)
Upon executing a new process, the file will automatically closed.  This saved the call to fcntl and eliminates possible race condition

O_CREAT
Create the file if it does not exist.  If the file exist, this flag has no action unless O_EXCL is used

O_EXCL
When used with O_CREAT, the open call will fail if the file already exists.  This is to prevent race condition. This flag has no meaning if not used with O_CREAT

O_DIRECT
Open the file for direct I/O (i,e, no system buffering)

O_DIRECTORY
If the file is not a directory, open will fail.  This flag is used internally by the opendir() call

O_LARGEFILE
Use a 64-bit offset for the file.  This is to break the 32-bit (2G) size barrier.

O_NOATIME+
The file last access time is not to be updated upon opening.  This is mainly used for performance purpose for backup and indexing programs that need to open and inspect a large number of file constantly.

O_NOCTTY
Rarely used.  The file refer to a terminal device.  This flag indicate the terminal will not become the controlling terminal even if there is no terminal currently.

O_NOFOLLOW
If the file is a symbolic link, fail the open call.  For example, opening /a/b/c, the path entries (a and b) can contain symbolic link.  Only the last file name (c)  must not be a symbolic link

C_NONBLOCK
Non-blocking open call.  This flag is only used for FIFO.

O_SYNC
Synchronous I/O - WRITE only return after data has externalized to disk.  As READ is always synchronous, this flag has no effect for READ only file.

O_TRUNC
If the file exist, a regular file and opened for WRITE, the file length will be reset to zero.  This flag is ignored for FIFO or terminal.  Used for other file type is undefined.  Use of this flag for file opened for O_RDONLY  is also undefined

No comments: