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),
45 import PrelUnsafe ( unsafePerformIO )
47 import PrelPack ( unpackNBytesST )
48 import PrelForeign ( Word(..) )
50 import Time ( ClockTime(..) )
54 %*********************************************************
56 \subsection{Signatures}
58 %*********************************************************
61 createDirectory :: FilePath -> IO ()
62 removeDirectory :: FilePath -> IO ()
63 removeFile :: FilePath -> IO ()
64 renameDirectory :: FilePath -> FilePath -> IO ()
65 renameFile :: FilePath -> FilePath -> IO ()
66 getDirectoryContents :: FilePath -> IO [FilePath]
67 getCurrentDirectory :: IO FilePath
68 setCurrentDirectory :: FilePath -> IO ()
69 doesFileExist :: FilePath -> IO Bool
70 doesDirectoryExist :: FilePath -> IO Bool
71 getPermissions :: FilePath -> IO Permissions
72 setPermissions :: FilePath -> Permissions -> IO ()
73 getModificationTime :: FilePath -> IO ClockTime
77 %*********************************************************
79 \subsection{Permissions}
81 %*********************************************************
83 The @Permissions@ type is used to record whether certain
84 operations are permissible on a file/directory:
85 [to whom? - owner/group/world - the Report don't say much]
91 executable, searchable :: Bool
92 } deriving (Eq, Ord, Read, Show)
95 %*********************************************************
97 \subsection{Implementation}
99 %*********************************************************
101 @createDirectory dir@ creates a new directory {\em dir} which is
102 initially empty, or as near to empty as the operating system
105 The operation may fail with:
108 \item @isPermissionError@ / @PermissionDenied@
109 The process has insufficient privileges to perform the operation.
111 \item @isAlreadyExistsError@ / @AlreadyExists@
112 The operand refers to a directory that already exists.
114 \item @HardwareFault@
115 A physical I/O error has occurred.
117 \item @InvalidArgument@
118 The operand is not a valid directory name.
119 @[ENAMETOOLONG, ELOOP]@
121 There is no path to the directory.
123 \item @ResourceExhausted@
124 Insufficient resources (virtual memory, process file descriptors,
125 physical disk space, etc.) are available to perform the operation.
126 @[EDQUOT, ENOSPC, ENOMEM, EMLINK]@
127 \item @InappropriateType@
128 The path refers to an existing non-directory object.
133 createDirectory path = do
134 rc <- _ccall_ createDirectory path
135 if rc == 0 then return () else
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 = do
175 rc <- _ccall_ removeDirectory path
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 rc <- _ccall_ removeFile path
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 = do
259 rc <- _ccall_ renameDirectory opath npath
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 = do
304 rc <- _ccall_ renameFile opath npath
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 = do
338 ptr <- _ccall_ getDirectoryContents path
340 then constructErrorAndFail "getDirectoryContents"
342 entries <- getEntries ptr 0
346 getEntries :: Addr -> Int -> IO [FilePath]
347 getEntries ptr n = do
348 str <- _casm_ ``%r = ((char **)%0)[%1];'' ptr n
352 len <- _ccall_ strlen str
353 entry <- stToIO (unpackNBytesST str len)
355 entries <- getEntries ptr (n+1)
356 return (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 = do
382 str <- _ccall_ getCurrentDirectory
385 len <- _ccall_ strlen str
386 pwd <- stToIO (unpackNBytesST str len)
390 constructErrorAndFail "getCurrentDirectory"
393 If the operating system has a notion of current directories,
394 @setCurrentDirectory dir@ changes the current
395 directory of the calling process to {\em dir}.
397 The operation may fail with:
399 \item @HardwareFault@
400 A physical I/O error has occurred.
402 \item @InvalidArgument@
403 The operand is not a valid directory name.
404 @[ENAMETOOLONG, ELOOP]@
405 \item @isDoesNotExistError@ / @NoSuchThing@
406 The directory does not exist.
408 \item @isPermissionError@ / @PermissionDenied@
409 The process has insufficient privileges to perform the operation.
411 \item @UnsupportedOperation@
412 The operating system has no notion of current directory, or the
413 current directory cannot be dynamically changed.
414 \item @InappropriateType@
415 The path refers to an existing non-directory object.
420 setCurrentDirectory path = do
421 rc <- _ccall_ setCurrentDirectory path
424 else constructErrorAndFail "setCurrentDirectory"
429 --doesFileExist :: FilePath -> IO Bool
430 doesFileExist name = do
431 rc <- _ccall_ access name (``F_OK''::Int)
434 --doesDirectoryExist :: FilePath -> IO Bool
435 doesDirectoryExist name =
436 (getFileStatus name >>= \ st -> return (isDirectory st))
438 (\ _ -> return False)
440 --getModificationTime :: FilePath -> IO ClockTime
441 getModificationTime name =
442 getFileStatus name >>= \ st ->
445 --getPermissions :: FilePath -> IO Permissions
446 getPermissions name =
447 getFileStatus name >>= \ st ->
450 isect v = intersectFileMode v fm == v
454 readable = isect ownerReadMode,
455 writeable = isect ownerWriteMode,
456 executable = not (isDirectory st) && isect ownerExecuteMode,
457 searchable = not (isRegularFile st) && isect ownerExecuteMode
461 --setPermissions :: FilePath -> Permissions -> IO ()
462 setPermissions name (Permissions r w e s) = do
464 read# = case (if r then ownerReadMode else ``0'') of { W# x# -> x# }
465 write# = case (if w then ownerWriteMode else ``0'') of { W# x# -> x# }
466 exec# = case (if e || s then ownerExecuteMode else ``0'') of { W# x# -> x# }
468 mode = I# (word2Int# (read# `or#` write# `or#` exec#))
470 rc <- _ccall_ chmod name mode
473 else fail (IOError Nothing SystemError "Directory.setPermissions")
478 (Sigh)..copied from Posix.Files to avoid dep. on posix library
481 type FileStatus = ByteArray Int
483 getFileStatus :: FilePath -> IO FileStatus
484 getFileStatus name = do
485 bytes <- stToIO (newCharArray (0,``sizeof(struct stat)''))
486 rc <- _casm_ ``%r = stat(%0,(struct stat *)%1);'' name bytes
488 then stToIO (unsafeFreezeByteArray bytes)
489 else fail (IOError Nothing SystemError "Directory.getFileStatus")
491 modificationTime :: FileStatus -> IO ClockTime
492 modificationTime stat = do
494 _casm_ ``((unsigned long *)%1)[0] = ((struct stat *)%0)->st_mtime;'' stat i1
495 secs <- cvtUnsigned i1
498 malloc1 = IO $ \ s# ->
499 case newIntArray# 1# s# of
500 StateAndMutableByteArray# s2# barr# ->
501 IOok s2# (MutableByteArray bnds barr#)
504 -- The C routine fills in an unsigned word. We don't have `unsigned2Integer#,'
505 -- so we freeze the data bits and use them for an MP_INT structure. Note that
506 -- zero is still handled specially, although (J# 1# 1# (ptr to 0#)) is probably
507 -- acceptable to gmp.
509 cvtUnsigned (MutableByteArray _ arr#) = IO $ \ s# ->
510 case readIntArray# arr# 0# s# of
511 StateAndInt# s2# r# ->
515 case unsafeFreezeByteArray# arr# s2# of
516 StateAndByteArray# s3# frozen# ->
517 IOok s3# (J# 1# 1# frozen#)
519 isDirectory :: FileStatus -> Bool
520 isDirectory stat = unsafePerformIO $ do
521 rc <- _casm_ ``%r = S_ISDIR(((struct stat *)%0)->st_mode);'' stat
524 isRegularFile :: FileStatus -> Bool
525 isRegularFile stat = unsafePerformIO $ do
526 rc <- _casm_ ``%r = S_ISREG(((struct stat *)%0)->st_mode);'' stat
532 ownerReadMode :: FileMode
533 ownerReadMode = ``S_IRUSR''
535 ownerWriteMode :: FileMode
536 ownerWriteMode = ``S_IWUSR''
538 ownerExecuteMode :: FileMode
539 ownerExecuteMode = ``S_IXUSR''
541 intersectFileMode :: FileMode -> FileMode -> FileMode
542 intersectFileMode (W# m1#) (W# m2#) = W# (m1# `and#` m2#)
544 fileMode :: FileStatus -> FileMode
545 fileMode stat = unsafePerformIO (
546 _casm_ ``%r = ((struct stat *)%0)->st_mode;'' stat)