2 % (c) The AQUA Project, Glasgow University, 1994-1997
4 \section[Directory]{Directory interface}
6 A directory contains a series of entries, each of which is a named
7 reference to a file system object (file, directory etc.). Some
8 entries may be hidden, inaccessible, or have some administrative
9 function (e.g. "." or ".." under POSIX), but in this standard all such
10 entries are considered to form part of the directory contents.
11 Entries in sub-directories are not, however, considered to form part
12 of the directory contents.
14 Each file system object is referenced by a {\em path}. There is
15 normally at least one absolute path to each file system object. In
16 some operating systems, it may also be possible to have paths which
17 are relative to the current directory.
20 {-# OPTIONS -#include <sys/stat.h> #-}
23 Permissions(Permissions),
47 import PackedString ( packCBytesST, unpackPS, psToByteArrayST )
48 import Time ( ClockTime(..) )
52 %*********************************************************
54 \subsection{Signatures}
56 %*********************************************************
59 createDirectory :: FilePath -> IO ()
60 removeDirectory :: FilePath -> IO ()
61 removeFile :: FilePath -> IO ()
62 renameDirectory :: FilePath -> FilePath -> IO ()
63 renameFile :: FilePath -> FilePath -> IO ()
64 getDirectoryContents :: FilePath -> IO [FilePath]
65 getCurrentDirectory :: IO FilePath
66 setCurrentDirectory :: FilePath -> IO ()
67 doesFileExist :: FilePath -> IO Bool
68 doesDirectoryExist :: FilePath -> IO Bool
69 getPermissions :: FilePath -> IO Permissions
70 setPermissions :: FilePath -> Permissions -> IO ()
71 getModificationTime :: FilePath -> IO ClockTime
75 %*********************************************************
77 \subsection{Permissions}
79 %*********************************************************
81 The @Permissions@ type is used to record whether certain
82 operations are permissible on a file/directory:
83 [to whom? - owner/group/world - the Report don't say much]
89 executable, searchable :: Bool
90 } deriving (Eq, Ord, Read, Show)
93 %*********************************************************
95 \subsection{Implementation}
97 %*********************************************************
99 @createDirectory dir@ creates a new directory {\em dir} which is
100 initially empty, or as near to empty as the operating system
103 The operation may fail with:
106 \item @isPermissionError@ / @PermissionDenied@
107 The process has insufficient privileges to perform the operation.
109 \item @isAlreadyExistsError@ / @AlreadyExists@
110 The operand refers to a directory that already exists.
112 \item @HardwareFault@
113 A physical I/O error has occurred.
115 \item @InvalidArgument@
116 The operand is not a valid directory name.
117 @[ENAMETOOLONG, ELOOP]@
119 There is no path to the directory.
121 \item @ResourceExhausted@
122 Insufficient resources (virtual memory, process file descriptors,
123 physical disk space, etc.) are available to perform the operation.
124 @[EDQUOT, ENOSPC, ENOMEM, EMLINK]@
125 \item @InappropriateType@
126 The path refers to an existing non-directory object.
131 createDirectory path =
132 _ccall_ createDirectory path `thenIO_Prim` \ rc ->
136 constructErrorAndFail "createDirectory"
139 @removeDirectory dir@ removes an existing directory {\em dir}. The
140 implementation may specify additional constraints which must be
141 satisfied before a directory can be removed (e.g. the directory has to
142 be empty, or may not be in use by other processes). It is not legal
143 for an implementation to partially remove a directory unless the
144 entire directory is removed. A conformant implementation need not
145 support directory removal in all situations (e.g. removal of the root
148 The operation may fail with:
150 \item @HardwareFault@
151 A physical I/O error has occurred.
153 \item @InvalidArgument@
154 The operand is not a valid directory name.
155 @[ENAMETOOLONG, ELOOP]@
156 \item @isDoesNotExist@ / @NoSuchThing@
157 The directory does not exist.
159 \item @isPermissionError@ / @PermissionDenied@
160 The process has insufficient privileges to perform the operation.
161 @[EROFS, EACCES, EPERM]@
162 \item @UnsatisfiedConstraints@
163 Implementation-dependent constraints are not satisfied.
164 @[EBUSY, ENOTEMPTY, EEXIST]@
165 \item @UnsupportedOperation@
166 The implementation does not support removal in this situation.
168 \item @InappropriateType@
169 The operand refers to an existing non-directory object.
174 removeDirectory path =
175 _ccall_ removeDirectory path `thenIO_Prim` \ rc ->
179 constructErrorAndFail "removeDirectory"
182 @removeFile file@ removes the directory entry for an existing file
183 {\em file}, where {\em file} is not itself a directory. The
184 implementation may specify additional constraints which must be
185 satisfied before a file can be removed (e.g. the file may not be in
186 use by other processes).
188 The operation may fail with:
190 \item @HardwareFault@
191 A physical I/O error has occurred.
193 \item @InvalidArgument@
194 The operand is not a valid file name.
195 @[ENAMETOOLONG, ELOOP]@
196 \item @isDoesNotExist@ / @NoSuchThing@
197 The file does not exist.
199 \item @isPermissionError@ / @PermissionDenied@
200 The process has insufficient privileges to perform the operation.
201 @[EROFS, EACCES, EPERM]@
202 \item @UnsatisfiedConstraints@
203 Implementation-dependent constraints are not satisfied.
205 \item @InappropriateType@
206 The operand refers to an existing directory.
212 _ccall_ removeFile path `thenIO_Prim` \ rc ->
216 constructErrorAndFail "removeFile"
219 @renameDirectory old@ {\em new} changes the name of an existing
220 directory from {\em old} to {\em new}. If the {\em new} directory
221 already exists, it is atomically replaced by the {\em old} directory.
222 If the {\em new} directory is neither the {\em old} directory nor an
223 alias of the {\em old} directory, it is removed as if by
224 $removeDirectory$. A conformant implementation need not support
225 renaming directories in all situations (e.g. renaming to an existing
226 directory, or across different physical devices), but the constraints
229 The operation may fail with:
231 \item @HardwareFault@
232 A physical I/O error has occurred.
234 \item @InvalidArgument@
235 Either operand is not a valid directory name.
236 @[ENAMETOOLONG, ELOOP]@
237 \item @isDoesNotExistError@ / @NoSuchThing@
238 The original directory does not exist, or there is no path to the target.
240 \item @isPermissionError@ / @PermissionDenied@
241 The process has insufficient privileges to perform the operation.
242 @[EROFS, EACCES, EPERM]@
243 \item @ResourceExhausted@
244 Insufficient resources are available to perform the operation.
245 @[EDQUOT, ENOSPC, ENOMEM, EMLINK]@
246 \item @UnsatisfiedConstraints@
247 Implementation-dependent constraints are not satisfied.
248 @[EBUSY, ENOTEMPTY, EEXIST]@
249 \item @UnsupportedOperation@
250 The implementation does not support renaming in this situation.
252 \item @InappropriateType@
253 Either path refers to an existing non-directory object.
258 renameDirectory opath npath =
259 _ccall_ renameDirectory opath npath `thenIO_Prim` \ rc ->
263 constructErrorAndFail "renameDirectory"
266 @renameFile old@ {\em new} changes the name of an existing file system
267 object from {\em old} to {\em new}. If the {\em new} object already
268 exists, it is atomically replaced by the {\em old} object. Neither
269 path may refer to an existing directory. A conformant implementation
270 need not support renaming files in all situations (e.g. renaming
271 across different physical devices), but the constraints must be
274 The operation may fail with:
276 \item @HardwareFault@
277 A physical I/O error has occurred.
279 \item @InvalidArgument@
280 Either operand is not a valid file name.
281 @[ENAMETOOLONG, ELOOP]@
282 \item @isDoesNotExistError@ / @NoSuchThing@
283 The original file does not exist, or there is no path to the target.
285 \item @isPermissionError@ / @PermissionDenied@
286 The process has insufficient privileges to perform the operation.
287 @[EROFS, EACCES, EPERM]@
288 \item @ResourceExhausted@
289 Insufficient resources are available to perform the operation.
290 @[EDQUOT, ENOSPC, ENOMEM, EMLINK]@
291 \item @UnsatisfiedConstraints@
292 Implementation-dependent constraints are not satisfied.
294 \item @UnsupportedOperation@
295 The implementation does not support renaming in this situation.
297 \item @InappropriateType@
298 Either path refers to an existing directory.
299 @[ENOTDIR, EISDIR, EINVAL, EEXIST, ENOTEMPTY]@
303 renameFile opath npath =
304 _ccall_ renameFile opath npath `thenIO_Prim` \ rc ->
308 constructErrorAndFail "renameFile"
311 @getDirectoryContents dir@ returns a list of {\em all} entries
314 The operation may fail with:
316 \item @HardwareFault@
317 A physical I/O error has occurred.
319 \item @InvalidArgument@
320 The operand is not a valid directory name.
321 @[ENAMETOOLONG, ELOOP]@
322 \item @isDoesNotExistError@ / @NoSuchThing@
323 The directory does not exist.
325 \item @isPermissionError@ / @PermissionDenied@
326 The process has insufficient privileges to perform the operation.
328 \item @ResourceExhausted@
329 Insufficient resources are available to perform the operation.
331 \item @InappropriateType@
332 The path refers to an existing non-directory object.
337 getDirectoryContents path =
338 _ccall_ getDirectoryContents path `thenIO_Prim` \ ptr ->
339 if ptr == ``NULL'' then
340 constructErrorAndFail "getDirectoryContents"
342 stToIO (getEntries ptr 0) >>= \ entries ->
343 _ccall_ free ptr `thenIO_Prim` \ () ->
346 getEntries :: Addr -> Int -> PrimIO [FilePath]
348 _casm_ ``%r = ((char **)%0)[%1];'' ptr n >>= \ str ->
349 if str == ``NULL'' then
352 _ccall_ strlen str >>= \ len ->
353 packCBytesST len str >>= \ entry ->
354 _ccall_ free str >>= \ () ->
355 getEntries ptr (n+1) >>= \ entries ->
356 return (unpackPS entry : entries)
359 If the operating system has a notion of current directories,
360 @getCurrentDirectory@ returns an absolute path to the
361 current directory of the calling process.
363 The operation may fail with:
365 \item @HardwareFault@
366 A physical I/O error has occurred.
368 \item @isDoesNotExistError@ / @NoSuchThing@
369 There is no path referring to the current directory.
370 @[EPERM, ENOENT, ESTALE...]@
371 \item @isPermissionError@ / @PermissionDenied@
372 The process has insufficient privileges to perform the operation.
374 \item @ResourceExhausted@
375 Insufficient resources are available to perform the operation.
376 \item @UnsupportedOperation@
377 The operating system has no notion of current directory.
381 getCurrentDirectory =
382 _ccall_ getCurrentDirectory `thenIO_Prim` \ str ->
383 if str /= ``NULL'' then
384 _ccall_ strlen str `thenIO_Prim` \ len ->
385 stToIO (packCBytesST len str) >>= \ pwd ->
386 _ccall_ free str `thenIO_Prim` \ () ->
387 return (unpackPS pwd)
389 constructErrorAndFail "getCurrentDirectory"
392 If the operating system has a notion of current directories,
393 @setCurrentDirectory dir@ changes the current
394 directory of the calling process to {\em dir}.
396 The operation may fail with:
398 \item @HardwareFault@
399 A physical I/O error has occurred.
401 \item @InvalidArgument@
402 The operand is not a valid directory name.
403 @[ENAMETOOLONG, ELOOP]@
404 \item @isDoesNotExistError@ / @NoSuchThing@
405 The directory does not exist.
407 \item @isPermissionError@ / @PermissionDenied@
408 The process has insufficient privileges to perform the operation.
410 \item @UnsupportedOperation@
411 The operating system has no notion of current directory, or the
412 current directory cannot be dynamically changed.
413 \item @InappropriateType@
414 The path refers to an existing non-directory object.
419 setCurrentDirectory path =
420 _ccall_ setCurrentDirectory path `thenIO_Prim` \ rc ->
424 constructErrorAndFail "setCurrentDirectory"
430 --doesFileExist :: FilePath -> IO Bool
432 psToByteArrayST name `thenIO_Prim` \ path ->
433 _ccall_ access path (``F_OK''::Int) `thenIO_Prim` \ rc ->
436 --doesDirectoryExist :: FilePath -> IO Bool
437 doesDirectoryExist name =
438 (getFileStatus name >>= \ st -> return (isDirectory st))
440 (\ _ -> return False)
442 --getModificationTime :: FilePath -> IO ClockTime
443 getModificationTime name =
444 getFileStatus name >>= \ st ->
447 --getPermissions :: FilePath -> IO Permissions
448 getPermissions name =
449 getFileStatus name >>= \ st ->
452 isect v = intersectFileMode v fm == v
456 readable = isect ownerReadMode,
457 writeable = isect ownerWriteMode,
458 executable = not (isDirectory st) && isect ownerExecuteMode,
459 searchable = not (isRegularFile st) && isect ownerExecuteMode
463 --setPermissions :: FilePath -> Permissions -> IO ()
464 setPermissions name (Permissions r w e s) =
466 read# = case (if r then ownerReadMode else ``0'') of { W# x# -> x# }
467 write# = case (if w then ownerWriteMode else ``0'') of { W# x# -> x# }
468 exec# = case (if e || s then ownerExecuteMode else ``0'') of { W# x# -> x# }
470 mode = I# (word2Int# (read# `or#` write# `or#` exec#))
472 psToByteArrayST name `thenIO_Prim` \ path ->
473 _ccall_ chmod path mode `thenIO_Prim` \ rc ->
477 fail (IOError Nothing SystemError "Directory.setPermissions")
482 (Sigh)..copied from Posix.Files to avoid dep. on posix library
485 type FileStatus = ByteArray Int
487 getFileStatus :: FilePath -> IO FileStatus
489 psToByteArrayST name `thenIO_Prim` \ path ->
490 newCharArray (0,``sizeof(struct stat)'') `thenIO_Prim` \ bytes ->
491 _casm_ ``%r = stat(%0,(struct stat *)%1);'' path bytes
492 `thenIO_Prim` \ rc ->
494 unsafeFreezeByteArray bytes `thenIO_Prim` \ stat ->
497 fail (IOError Nothing SystemError "Directory.getFileStatus")
499 modificationTime :: FileStatus -> IO ClockTime
500 modificationTime stat =
501 malloc1 `thenIO_Prim` \ i1 ->
502 _casm_ ``((unsigned long *)%1)[0] = ((struct stat *)%0)->st_mtime;'' stat i1 `thenIO_Prim` \ () ->
503 cvtUnsigned i1 `thenIO_Prim` \ secs ->
506 malloc1 = ST $ \ (S# s#) ->
507 case newIntArray# 1# s# of
508 StateAndMutableByteArray# s2# barr# -> (MutableByteArray bnds barr#, S# s2#)
511 -- The C routine fills in an unsigned word. We don't have `unsigned2Integer#,'
512 -- so we freeze the data bits and use them for an MP_INT structure. Note that
513 -- zero is still handled specially, although (J# 1# 1# (ptr to 0#)) is probably
514 -- acceptable to gmp.
516 cvtUnsigned (MutableByteArray _ arr#) = ST $ \ (S# s#) ->
517 case readIntArray# arr# 0# s# of
518 StateAndInt# s2# r# ->
522 case unsafeFreezeByteArray# arr# s2# of
523 StateAndByteArray# s3# frozen# -> (J# 1# 1# frozen#, S# s3#)
525 isDirectory :: FileStatus -> Bool
526 isDirectory stat = unsafePerformPrimIO $
527 _casm_ ``%r = S_ISDIR(((struct stat *)%0)->st_mode);'' stat >>= \ rc ->
530 isRegularFile :: FileStatus -> Bool
531 isRegularFile stat = unsafePerformPrimIO $
532 _casm_ ``%r = S_ISREG(((struct stat *)%0)->st_mode);'' stat >>= \ rc ->
540 ownerReadMode :: FileMode
541 ownerReadMode = ``S_IRUSR''
543 ownerWriteMode :: FileMode
544 ownerWriteMode = ``S_IWUSR''
546 ownerExecuteMode :: FileMode
547 ownerExecuteMode = ``S_IXUSR''
549 intersectFileMode :: FileMode -> FileMode -> FileMode
550 intersectFileMode (W# m1#) (W# m2#) = W# (m1# `and#` m2#)
552 fileMode :: FileStatus -> FileMode
553 fileMode stat = unsafePerformPrimIO $
554 _casm_ ``%r = ((struct stat *)%0)->st_mode;'' stat >>= \ mode ->