fs.texi 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. @node POSIX file system access
  2. @section File system access
  3. @stindex posix-files
  4. These procedures operate on the file system via the facilities defined
  5. by POSIX and offer more than standard & portable R5RS operations. All
  6. of these names are exported by the structures @code{posix-files} and
  7. @code{posix}.
  8. @cindex POSIX directory access
  9. @cindex directory streams
  10. @deffn procedure open-directory-stream filename @returns{} dir-stream
  11. @deffnx procedure directory-stream? object @returns{} boolean
  12. @deffnx procedure read-directory-stream dir-stream @returns{} filename or @code{#f}
  13. @deffnx procedure close-directory-stream dir-stream @returns{} unspecified
  14. Directory streams are the low-level interface provided by POSIX to
  15. enumerate the contents of a directory. @code{Open-directory-stream}
  16. opens a new directory stream that will enumerate all of the files
  17. within the directory named by @var{filename}. @code{Directory-stream?}
  18. is the disjoint type predicate for directory streams.
  19. @code{Read-directory-stream} consumes the next filename from
  20. @var{dir-stream} and returns it, or returns @code{#f} if the stream has
  21. finished. Note that @code{read-directory-stream} will return only
  22. simple filenames, not full pathnames. @code{Close-directory-stream}
  23. closes @var{dir-stream}, removing any storage it required in the
  24. operating system. Closing an already closed directory stream has no
  25. effect.
  26. @end deffn
  27. @cindex directory listing
  28. @cindex listing directories
  29. @deffn procedure list-directory filename @returns{} string list
  30. Returns the list of filenames in the directory named by @var{filename}.
  31. This is equivalent to opening a directory stream, repeatedly reading
  32. from it & accumulating the list of filenames, and closing the stream.
  33. @end deffn
  34. @cindex POSIX working directory
  35. @cindex working directory
  36. @deffn procedure working-directory @returns{} string
  37. @deffnx procedure set-working-directory! string @returns{} unspecified
  38. These access the working directory's filename of the current process.
  39. @end deffn
  40. @cindex POSIX file opening
  41. @deffn procedure open-file pathname file-options [file-mode] @returns{} port
  42. Opens a port to the file named by the string @var{pathname}.
  43. @var{File-options} specifies various aspects of the port. The optional
  44. @var{file-mode} argument is used only if the file to be opened does not
  45. already exist; it specifies the permissions to be assigned to the file
  46. if it is created. The returned port is an input port if the given
  47. options include @code{read-only}; otherwise @code{open-file} returns an
  48. output port. Because Scheme48 does not support combined input/output
  49. ports, @code{dup-switching-mode} can be used to open an input port for
  50. output ports opened with the @code{read-write} option.
  51. @end deffn
  52. File options are stored in a boxed mask representation. File option
  53. sets are created with @code{file-options} and tested with
  54. @code{file-options-on?}.
  55. @deffn syntax file-options name @dots{} @returns{} file-options
  56. @deffnx procedure file-options-on? options@suba{a} options@suba{b} @returns{} boolean
  57. @code{File-options} evaluates to a file option set, suitable for passing
  58. to @code{open-file}, that includes all of the given named options.
  59. @code{File-options-on?} returns true if @var{options@suba{a}} includes
  60. all of the options in @var{options@suba{b}}, or false if otherwise.
  61. The following file option names are supported as arguments to the
  62. @code{file-options} syntax:
  63. @table @code
  64. @item create
  65. create file if it does not already exist; a @var{file-mode} argument is
  66. required to be passed to @code{open-file} if the @code{create} option
  67. is specified
  68. @item exclusive
  69. an error will be signalled if this option & @code{create} are both set
  70. and the file already exists
  71. @item no-controlling-tty
  72. if the pathname being opened is a terminal device, the terminal will
  73. not become the controlling terminal of the process
  74. @item truncate
  75. file is truncated
  76. @item append
  77. written data to the newly opened file will be appended to the existing
  78. contents
  79. @item nonblocking
  80. read & write operations will not block
  81. @item read-only
  82. file may not be written to, only read from
  83. @item read-write
  84. file may be both read from & written to
  85. @item write-only
  86. file may not be read from, only written to
  87. @end table
  88. The last three are all mutually exclusive.
  89. @end deffn
  90. Examples:
  91. @lisp
  92. (open-file "some-file.txt"
  93. (file-options create write-only)
  94. (file-mode read owner-write))@end lisp
  95. @noindent
  96. returns an output port that writes to a newly-created file that can be
  97. read from by anyone but written to only by the owner. Once the file
  98. @file{some-file.txt} exists,
  99. @lisp
  100. (open-file "some-file.txt"
  101. (file-options append write-only))@end lisp
  102. @noindent
  103. will open an output port that appends to the file.
  104. @embedref{POSIX I/O utilities,@code{I/o-flags} & @code{set-i/o-flags!}}
  105. can be used to access the @code{append}, @code{nonblocking}, and
  106. read/write file options of ports, as well as modify the @code{append} &
  107. @code{nonblocking} options.
  108. @cindex nonblocking I/O
  109. @cindex blocking I/O
  110. To keep port operations from blocking in the Scheme48 process, output
  111. ports are set to be nonblocking at the time of creation. (Input ports
  112. are managed using @code{select(2)}.) @code{Set-i/o-flags!} can be used
  113. to make an output port blocking, for example directly before forking,
  114. but care should be exercised, because the Scheme48 run-time system may
  115. be confused if an I/O operation blocks.
  116. @cindex POSIX file creation masks
  117. @deffn procedure set-file-creation-mask! file-mode @returns{} file-mode
  118. Sets the file creation mask to be @var{file-mode}. Bits set in
  119. @var{file-mode} are cleared in the modes of any files or directories
  120. subsequently created by the current process.
  121. @end deffn
  122. @cindex POSIX links
  123. @cindex creating POSIX links
  124. @cindex creating directories
  125. @cindex making directories
  126. @cindex directory creation
  127. @cindex POSIX FIFOs
  128. @cindex creating POSIX FIFOs
  129. @deffn procedure link existing-pathname new-pathname @returns{} unspecified
  130. @deffnx procedure make-directory pathname file-mode @returns{} unspecified
  131. @deffnx procedure make-fifo pathname file-mode @returns{} unspecified
  132. @code{Link} creates a hard link for the file at @var{existing-pathname}
  133. at @var{new-pathname}. @code{Make-directory} creates a new directory
  134. at the locations specified by @var{pathname} with the the file mode
  135. @var{file-mode}. @code{Make-fifo} does similarly, but it creates a
  136. FIFO (first-in first-out) file instead of a directory.
  137. @end deffn
  138. @cindex deleting files
  139. @cindex file deletion
  140. @cindex removing files
  141. @cindex deleting directories
  142. @cindex directory deletion
  143. @cindex removing directories
  144. @cindex renaming files
  145. @deffn procedure unlink pathname @returns{} unspecified
  146. @deffnx procedure remove-directory pathname @returns{} unspecified
  147. @deffnx procedure rename old-pathname new-pathname @returns{} unspecified
  148. @code{Unlink} removes a link at the location @var{pathname}.
  149. @code{Remove-directory} removes a directory at the location specified
  150. by @var{pathname}. The directory must be empty; an exception is
  151. signalled if it is not. @code{Rename} moves the file at the location
  152. @var{old-pathname} to the new location @var{new-pathname}.
  153. @end deffn
  154. @cindex file access probing
  155. @deffn procedure accessible? pathname access-mode more-modes @dots{} @returns{} boolean
  156. @deffnx syntax access-mode name @returns{} access mode
  157. @code{Accessible?} returns true if @var{pathname} is accessible by all
  158. of the given access modes. (There must be at least one access mode
  159. argument.) @code{Access-mode} evaluates to an access mode, suitable for
  160. passing to @code{accessible?}, from the given name. The allowed names
  161. are @code{read}, @code{write}, @code{execute}, & @code{exists}.
  162. @end deffn
  163. @cindex file info
  164. Information about files can be queried using the @dfn{file info}
  165. abstraction. Every file has a corresponding file info record, which
  166. contains various data about the file including its name, its type,
  167. its device & inode numbers, the number of links to it, its size in
  168. bytes, its owner, its group, its file mode, and its access times.
  169. @deffn procedure get-file-info pathname @returns{} file-info
  170. @deffnx procedure get-file/link-info pathname @returns{} file-info
  171. @deffnx procedure get-port-info fd-port @returns{} file-info
  172. @code{Get-file-info} & @code{get-file/link-info} return a file info
  173. record for the files named by @var{pathname}. @code{Get-file-info}
  174. follows symbolic links, however, while @code{get-file/link} info does
  175. not. @code{Get-port-info} returns a file info record for the file that
  176. @var{fd-port} is a port atop a file descriptor for. If @var{fd-port}
  177. does not read from or write to a file descriptor, an error is
  178. signalled.
  179. @end deffn
  180. @deffn procedure file-info? object @returns{} boolean
  181. @deffnx procedure file-info-name file-info @returns{} string
  182. @deffnx procedure file-info-device file-info @returns{} integer
  183. @deffnx procedure file-info-inode file-info @returns{} integer
  184. @deffnx procedure file-info-link-count file-info @returns{} integer
  185. @deffnx procedure file-info-size file-info @returns{} integer
  186. @deffnx procedure file-info-owner file-info @returns{} user-id
  187. @deffnx procedure file-info-group file-info @returns{} group-id
  188. @deffnx procedure file-info-mode file-info @returns{} file-mode
  189. @deffnx procedure file-info-last-access file-info @returns{} time
  190. @deffnx procedure file-info-last-modification file-info @returns{} time
  191. @deffnx procedure file-info-last-change file-info @returns{} time
  192. Accessors for various file info record fields. The name is the string
  193. passed to @code{get-file-info} or @code{get-file/link-info}, if the
  194. file info record was created with either of those two, or the name of
  195. the file that the file descriptor of the port queried was created on,
  196. if the file info record was obtained with @code{get-port-info}.
  197. @end deffn
  198. @deffn procedure file-info-type file-info @returns{} file-type
  199. @deffnx syntax file-type name @returns{} file-type
  200. @deffnx procedure file-type? object @returns{} boolean
  201. @deffnx procedure file-type-name file-type @returns{} symbol
  202. @code{File-info-type} returns the type of the file as a @dfn{file type}
  203. object. File types may be compared using @code{eq?}. @code{File-type}
  204. evaluates to a file type of the given name. The disjoint type predicate
  205. for file types is @code{file-type?}. @code{File-type-name} returns the
  206. symbolic name that represents @var{file-type}.
  207. The valid file type names are:
  208. @itemize @w{}
  209. @item @code{regular}
  210. @item @code{directory}
  211. @item @code{character-device}
  212. @item @code{block-device}
  213. @item @code{fifo}
  214. @item @code{symbolic-link} (not required by POSIX)
  215. @item @code{socket} (not required by POSIX)
  216. @item @code{other}
  217. @end itemize
  218. @end deffn
  219. @cindex POSIX file permissions
  220. @cindex file permissions
  221. @dfn{File modes} are boxed integers that represent POSIX file
  222. protection masks.
  223. @deffn syntax file-mode permission-name @dots{} @returns{} file-mode
  224. @deffnx procedure file-mode? object @returns{} boolean
  225. @code{File-mode} evaluates to a file mode object that contains all of
  226. the specified permissions. @code{File-mode?} is the disjoint type
  227. predicate for file mode descriptor objects. These are all of the names,
  228. with their corresponding octal bit masks and meanings, allowed to be
  229. passed to @code{file-mode}:
  230. @multitable {Permission name} {Octal mask} {execute (or search) by others}
  231. @item Permission name @tab Octal mask @tab Description
  232. @item @code{set-uid} @tab @code{#o4000} @tab set user id when executing
  233. @item @code{set-gid} @tab @code{#o2000} @tab set group id when executing
  234. @item @code{owner-read} @tab @code{#o0400} @tab read by owner
  235. @item @code{owner-write} @tab @code{#o0200} @tab write by owner
  236. @item @code{owner-exec} @tab @code{#o0100} @tab execute (or search) by owner
  237. @item @code{group-read} @tab @code{#o0040} @tab read by group
  238. @item @code{group-write} @tab @code{#o0020} @tab write by group
  239. @item @code{group-exec} @tab @code{#o0010} @tab execute (or search) by group
  240. @item @code{other-read} @tab @code{#o0004} @tab read by others
  241. @item @code{other-write} @tab @code{#o0002} @tab write by others
  242. @item @code{other-exec} @tab @code{#o0001} @tab execute (or search) by others
  243. @end multitable
  244. Also, several compound masks are supported for convenience:
  245. @multitable {Permission set name} {Octal mask} {read, write, & execute by anyone}
  246. @item Permission set name @tab Octal mask @tab Description
  247. @item @code{owner} @tab @code{#o0700} @tab read, write, & execute by owner
  248. @item @code{group} @tab @code{#o0070} @tab read, write, & execute by group
  249. @item @code{other} @tab @code{#o0007} @tab read, write, & execute by others
  250. @item @code{read} @tab @code{#o0444} @tab read by anyone
  251. @item @code{write} @tab @code{#o0111} @tab write by anyone
  252. @item @code{exec} @tab @code{#o0777} @tab read, write, & execute by anyone
  253. @end multitable
  254. @end deffn
  255. @deffn procedure file-mode+ file-mode @dots{} @returns{} file-mode
  256. @deffnx procedure file-mode- file-mode@suba{a} file-mode@suba{b} @returns{} file-mode
  257. @deffnx procedure file-mode=? file-mode@suba{a} file-mode@suba{b} @returns{} boolean
  258. @deffnx procedure file-mode<=? file-mode@suba{a} file-mode@suba{b} @returns{} boolean
  259. @deffnx procedure file-mode>=? file-mode@suba{a} file-mode@suba{b} @returns{} boolean
  260. @code{File-mode+} returns a file mode that contains all of the
  261. permissions specified in any of its arguments. @code{File-mode-}
  262. returns a file mode that contains all of @var{file-mode@suba{a}}'s
  263. permissions not in @var{file-mode@suba{b}}. @code{File-mode=?} tests
  264. whether two file modes are the same. @code{File-mode<=?} returns true
  265. if each successive file mode argument has the same or more permissions
  266. as the previous one. @code{File-mode>=?} returns true if each
  267. successive file mode argument has the same or fewer permissions as the
  268. previous one.
  269. @end deffn
  270. @deffn procedure file-mode->integer file-mode @returns{} integer
  271. @deffnx procedure integer->file-mode integer @returns{} file-mode
  272. These convert between file mode objects and Unix file mode masks as
  273. integers. The integer representations may or may not be the masks used
  274. by the underlying operating system.
  275. @end deffn