Open (system call)
Encyclopedia
For most file system
File system
A file system is a means to organize data expected to be retained after a program terminates by providing procedures to store, retrieve and update data, as well as manage the available space on the device which contain it. A file system organizes data in an efficient manner and is tuned to the...

s, a program initializes access to a file
Computer file
A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished...

 in a filesystem using the open system call
System call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...

. This allocates resources associated to the file (the file descriptor
File descriptor
In computer programming, a file descriptor is an abstract indicator for accessing a file. The term is generally used in POSIX operating systems...

), and returns a handle that the process will use to refer to that file. In some cases the open is performed by the first access.

The same file may be opened simultaneously by several processes, and even by the same process (resulting in several file descriptors for the same file) depending on the file organization and filesystem. Operations on the descriptors like moving the file pointer, or closing it are independent (they do not affect other descriptors for the same file). Operations of the file (like a write) can be seen by operations on the other descriptors (a posterior read can read the written data).

During the open, the filessytem may allocate memory for buffers (or it may wait until the first operation).

The absolute filename is resolved. This may include connecting to a remote host and notifying an operator that a removable media is required. It may include the initialization of a communication device. At this point an error may be returned if the host or media is not available. The first access to at least the directory within the filesystem is performed. An error will usually be returned if the higher level components of the path (directories) cannot be located or accessed. An error will be returned if the file is expected to exist and it does not or if the file should not already exist and it does.

If the file is expected to exist and it does, the file access, as restricted by permission flags within the file meta data or access control list, are validated against the requested type of operations. This usually requires an additional filesystem access although in some filesystems meta flags may be part of the directory structure.

If the file is being created the filesystem may allocate the default initial amount of storage or a specified amount depending on the file system capabilities. If this fails an error will be returned. Updating the directory with the new entry may be performed or it maybe delayed until the close
Close
Close may refer to:*Close In music:*"Close", a song by Rascal Flatts from Unstoppable*"Close", a song by Soul Asylum from Candy from a Stranger*"Close", a song by Westlife from Coast to Coast...

 is performed

Various other errors which may occur during the open include directory update failures, un-permitted multiple connections, media failures, communication link failures and device failures.

The return value must always be examined and an error specific action taken.

In many cases programing language specific run-time library opens may perform additional actions including initializing a run-time library structure related to the file.

As soon as an file is no longer needed, the program should close
Close
Close may refer to:*Close In music:*"Close", a song by Rascal Flatts from Unstoppable*"Close", a song by Soul Asylum from Candy from a Stranger*"Close", a song by Westlife from Coast to Coast...

 it. This will cause run-time library and filesystem buffers to be updated to the physical media and permit other processes to access the data if exclusive use had been required. Some run-time libraries may close
Close
Close may refer to:*Close In music:*"Close", a song by Rascal Flatts from Unstoppable*"Close", a song by Soul Asylum from Candy from a Stranger*"Close", a song by Westlife from Coast to Coast...

 a file if the program calls the run-time exit. Some filesystems may perform the necessary operations if the program terminates. Neither of these is likely to take place in the event of a kernel or power failure. This can cause damaged filessytem structures requiring the running of privileged and lengthy filesystem utilities during which the entire file system may be inaccessible.

open call arguments

  1. The pathname to the file,
  2. The kind of access requested on the file (read, write, append etc.),
  3. The initial file permission is requested using the third argument called mode. This argument is relevant only when a new file is being created.


After using the file, the process should close the file using close
Close (system call)
For most file systems, a program terminates access to a file in a filesystem using the close system call. This flushes buffers, updates file metadata , de-allocates resources associated with the file and updates the system wide table of files in use...

 call, which takes the file descriptor of the file to be closed. Some filessytems include a disposition to permit releasing the file.

Some computer languages include run-time libraries which include additional functionality for particular filesystems. The open (or some auxiliary routine) may include specifications for key size, record size, connection speed. Some open routines include specification of the program code to be executed in the event of an error.

perl language form

open FILEHANDLE,MODE[,EXPR]

for example:

open(my $fh, ">", "output.txt")

Perl also uses the tie function of the Tie::File module to associate an array with a
file. The tie::AnyDBM_File function associates a hash with a file.

C library POSIX definition

The open call is standardized by the POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

 specification

int open (const char *path, int oflag, .../*,mode_t mode */);
int openat ...
int creat(const char *path, mode_t mode)
FILE *fopen(const char *restrict filename, const char *restrict mode);

The value returned is a file descriptor which is a reference to a process specific structure which contains, among other things, a position pointer that indicates which place in the file will be acted upon by the next operation.

Open may return a -1 indicating a failure with errno detailing the error.

The file system also updates a global table of all open files which is used for determining if a file is currently in use by any process.
path

The name of the file to open. It includes the file path defining where, in which file system, the file is found (or should be created).

openat expects a relative path.
oflag

This argument formed by OR'ing together optional parameters and (from <fcntl.h
Fcntl.h
fcntl.h is the header in the C POSIX library for the C programming language that contains constructs that refer to file control, e.g. opening a file, retrieving and changing the permissions of file, locking a file for edit, etc.- Member constants :...

>) one of:
O_RDONLY, O_RDWR and O_WRONLY


Option parameters include:
O_APPEND data written will be appended to the end of the file. The file operations will always adjust the position pointer to the end of the file.

O_CREAT Create the file if it does not exist; otherwise the open fails setting errno
Errno.h
Errno.h is a header file in the standard library of C programming language. It defines macros to report error conditions through error codes stored in a static location called errno....

 to ENOENT.

O_EXCL Used with O_CREAT if the file already exists, then fail, setting errno to EEXIST.

O_TRUNC If the file already exists then discard its previous contents, reducing it to an empty file. Not applicable for a device or named pipe.


Additional flags and errors are defined in open call.

creat is implemented as:

int creat(const char *path, mode_t mode)
{ return open(path, O_WRONLY|O_CREAT|O_TRUNC, mode); }
fopen uses string flags such as r, w, a and + and returns a file pointer used with fgets, fputs and fclose.
mode_t

Optional and relevant only when creating a new file, defines the
file permissions. These include read, write or execute the file by the owner, group or all users. The mode is masked by the calling process's umask
Umask
umask is a command and a function in POSIX environments that sets the file mode creation mask of the current process which limits the permission modes for files and directories created by the process...

: bits set in the umask are cleared in the mode.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK