fileoperating-systemimplementation

typical implementation of read/write permissions in file reading code


Every language I've done some form of file handling in has some form of read/write specification when interacting with a file, something like

open_file(file_name, read=true) 

Is this typically implemented in the library? Or is there some simple operating system support that library devs can leverage?

I looked around the python file handling docs but I didn't see any clear specification of how read/write worked.


Solution

  • In order to be portable, languages would usually not specify how things are implemented.

    However, almost always file permissions they are implemented by the operating system. For example, most operating systems implement some subset of POSIX. A POSIX operating system allows you to open a file as follows:

    int open(const char *path, int oflag, ... );
    

    And the documentation of oflag includes:

    Applications shall specify exactly one of the first three values (file access modes) below in the value of oflag:
    O_RDONLY
        Open for reading only.
    O_WRONLY
        Open for writing only.
    O_RDWR
        Open for reading and writing. The result is undefined if this flag is applied to a FIFO. 
    

    Language implementations simply translate calls such as open_file(file_name, read=true) to the operating system's equivalent of open().