70 lines · 3.0 KB · Text
RawDownload
= open,  create :  open  a file for reading or writing, create file     include "sys.m";    sys := load Sys Sys->PATH;     create: fn(file: string, omode, perm: int): ref FD;    open:   fn(file: string, omode: int):       ref FD; Open opens the file for I/O and returns  an  associated  filedescriptor.   Omode  is  one  of  Sys->OREAD, Sys->OWRITE, orSys->ORDWR, asking for permission to read, write, or read andwrite,  respectively.   There are two values that can be OR'dwith those to form omode: Sys->OTRUNC says  to  truncate  thefile  before opening it, which requires write permission evenif omode is Sys->OREAD; and Sys->ORCLOSE says to  remove  thefile  when  it is closed (ie, when the last reference to thisfile descriptor goes away). Open returns nil if the file does not exist, if the file nameis unacceptable, if the user does not have permission to openit as requested (see sys-stat(2) for a description of permis‐sions), or if any other error occurs. Create  creates a new file or prepares to rewrite an existingfile, opens it according to omode (as  described  for  open),and returns an associated file descriptor. If  the  file  is new, the owner is set to the user id of thecreating process group, the group to that of  the  containingdirectory, and the permissions to perm ANDed with the permis‐sions of the containing directory.  The bits in perm are  thesame as those in the file mode returned by sys-stat(2). If  the file already exists, it is truncated to 0 length, butthe permissions, owner, and group remain unchanged. The created file will be a directory if the Sys->DMDIR bit isset  in  perm,  and  omode  is  Sys->OREAD.  The file will beexclusive-use if the Sys->DMEXCL bit is set in perm  and  theunderlying  file server supports it; see open(5) for details.It will be append-only if the Sys->DMAPPEND bit is  set,  andthe underlying file server supports it. Create returns nil if the path up to the last element of filecannot be evaluated, if the file name is unacceptable, if theuser  does  not have write permission in the final directory,if the file already exists and does  not  permit  the  accessdefined by omode, or if any other error occurs. If  the  file is new and the directory in which it is createdis a union directory (see sys-intro(2)) then the  constituentdirectory  where the file is created depends on the structureof the union: see sys-bind(2). Since create may succeed even if the file exists,  a  specialmechanism  is  necessary  for  applications  that  require anatomic create operation.  If the Sys->OEXCL bit is set in themode  for  a  create, the call succeeds only if the file doesnot already exist; see open(5) for details. There is no explicit ``close'' routine: when the last  refer‐ence  to  the  file descriptor is released, the system closesthe associated file.  For devices and network protocols whereshutdown  must  be  guaranteed, write a hangup message to theassociated control file and use the return value of the writeto verify closure. == see ALSOsys-intro(2), sys-bind(2), sys-stat(2)