summary refs log tree commit diff stats
path: root/lib/std/files.nim
blob: b61b7dafdac003af6cfd8224894eab19601ec0e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
## This module implements file handling.
##
## **See also:**
## * `paths module <paths.html>`_ for path manipulation

from paths import Path, ReadDirEffect, WriteDirEffect

from std/private/osfiles import fileExists, removeFile,
                                moveFile


proc fileExists*(filename: Path): bool {.inline, tags: [ReadDirEffect], sideEffect.} =
  ## Returns true if `filename` exists and is a regular file or symlink.
  ##
  ## Directories, device files, named pipes and sockets return false.
  result = fileExists(filename.string)

proc removeFile*(file: Path) {.inline, tags: [WriteDirEffect].} =
  ## Removes the `file`.
  ##
  ## If this fails, `OSError` is raised. This does not fail
  ## if the file never existed in the first place.
  ##
  ## On Windows, ignores the read-only attribute.
  ##
  ## See also:
  ## * `removeDir proc <dirs.html#removeDir>`_
  ## * `moveFile proc`_
  removeFile(file.string)

proc moveFile*(source, dest: Path) {.inline,
    tags: [ReadDirEffect, ReadIOEffect, WriteIOEffect].} =
  ## Moves a file from `source` to `dest`.
  ##
  ## Symlinks are not followed: if `source` is a symlink, it is itself moved,
  ## not its target.
  ##
  ## If this fails, `OSError` is raised.
  ## If `dest` already exists, it will be overwritten.
  ##
  ## Can be used to `rename files`:idx:.
  ##
  ## See also:
  ## * `moveDir proc <dirs.html#moveDir>`_
  ## * `removeFile proc`_
  moveFile(source.string, dest.string)
. . # copy EAX to EDX # . EAX += 12 05/add-to-EAX 0xc/imm32 # allocate(ad, n) # . . push args 50/push-EAX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call e8/call allocate/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # EAX->length = elemsize*length 89/copy 1/mod/*+disp8 0/rm32/EAX . . . 2/r32/EDX 8/disp8 . # copy EDX to *(EAX+8) # clear-stream(EAX) # . . push args 50/push-EAX # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP $new-stream:end: # . restore registers 5a/pop-to-EDX # . epilog 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP 5d/pop-to-EBP c3/return $new-stream:abort: # . _write(2/stderr, error) # . . push args 68/push "new-stream: size too large"/imm32 68/push 2/imm32/stderr # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # . syscall(exit, 1) bb/copy-to-EBX 1/imm32 b8/copy-to-EAX 1/imm32/exit cd/syscall 0x80/imm8 # never gets here test-new-stream: # . prolog 55/push-EBP 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP # var ad/ECX : (address allocation-descriptor) = allocate-region(Heap, 512) # . EAX = allocate-region(Heap, 512) # . . push args 68/push 0x200/imm32 68/push Heap/imm32 # . . call e8/call allocate-region/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # . ECX = EAX 89/copy 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # copy EAX to ECX # var start/EDX = ad->curr 8b/copy 0/mod/indirect 1/rm32/ECX . . . 2/r32/EDX . . # copy *ECX to EDX # EAX = new-stream(ad, 3, 2) # . . push args 68/push 2/imm32 68/push 3/imm32 51/push-ECX # . . call e8/call new-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP # check-ints-equal(EAX, EDX, msg) # . . push args 68/push "F - test-new-stream: returns current pointer of allocation descriptor"/imm32 52/push-EDX 50/push-EAX # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP # check-ints-equal(EAX->length, 6, msg) # . . push args 68/push "F - test-new-stream: sets length correctly"/imm32 68/push 6/imm32 ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . . 8/disp8 # push *(EAX+8) # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP # the rest is delegated to clear-stream() so we won't bother checking it # . epilog 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP 5d/pop-to-EBP c3/return # . . vim:nowrap:textwidth=0