Subversion Repositories Spectranet

[/] [trunk/] [modules/] [basext/] [copy.asm] - Blame information for rev 442

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 437 winston
;The MIT License
2
;
3
;Copyright (c) 2011 Dylan Smith
4
;
5
;Permission is hereby granted, free of charge, to any person obtaining a copy
6
;of this software and associated documentation files (the "Software"), to deal
7
;in the Software without restriction, including without limitation the rights
8
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
;copies of the Software, and to permit persons to whom the Software is
10
;furnished to do so, subject to the following conditions:
11
;
12
;The above copyright notice and this permission notice shall be included in
13
;all copies or substantial portions of the Software.
14
;
15
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
;THE SOFTWARE.
22
 
23
; The copy command
24
 
25
.include        "defs.inc"
26
.include        "fcntl.inc"
27
.include        "spectranet.inc"
28 440 winston
.include        "stat.inc"
29 442 winston
.include        "errno.inc"
30 437 winston
 
31
;------------------------------------------------------------------------
32
; F_copy expects the destination filename in WORKSPACE and the
33
; source in WORKSPACE+256.
34
.globl F_copy
35
F_copy:
36 440 winston
        ; First stat the destination to find if it's a directory.
37
        ld hl, INTERPWKSPC
38
        ld de, INTERPWKSPC+512
39
        call STAT
40
        jr c, .trytocopy        ; non-existent destination, that's OK, it
41
                                ; might be specifying a file that's
42
                                ; not yet been created.
43
        ld a, (INTERPWKSPC+512+STAT_MODE+1)
44
        and S_IFDIR / 256       ; check directory flag
45 442 winston
        jr nz, .catdir
46 440 winston
 
47 442 winston
        ; If the destination is a file, we'll not overwrite it (this avoids
48
        ; having to compare paths to test for file being copied onto itself
49
        ; at the expense of not being able to use the copy command to
50
        ; overwrite a file).
51
        ld a, EEXIST
52
        scf
53
        ret
54
 
55
.catdir:
56 440 winston
        ld hl, INTERPWKSPC+256  ; source filename
57
        call F_basename         ; HL now points at the file name
58
        ex de, hl               ; move into DE for the catpath call
59
        ld hl, INTERPWKSPC      ; destination directory
60
        call F_catpath
61
 
62
.trytocopy:
63 437 winston
        ; Open the source file for read.
64
        ld hl, INTERPWKSPC+256
65
        ld d, 0                 ; no flags
66
        ld e, O_RDONLY          ; read-only access
67
        call OPEN
68
        ret c                   ; bale now if we can't open the src file
69
        ld (SRC_FD), a          ; save the returned FD
70
 
71
        ; Open the destination for write.
72
        ld hl, INTERPWKSPC
73
        ld d, O_CREAT           ; create the destination file
74
        ld e, O_WRONLY          ; write only
75
        call OPEN
76
        jr c, .cleanup          ; clean up opened file on error and return
77
        ld (DST_FD), a          ; save the returned fd
78
 
79
        ; We're now done with the filenames so we can re-use the workspace
80
        ; for the data that will be copied.
81
.cploop:
82
        ld a, (SRC_FD)
83
        ld de, INTERPWKSPC
84
        ld bc, 256
85
        call READ
86
        jr c, .closefiles       ; Probably at EOF, or an error occurred
87
        ld a, (DST_FD)
88
        ld hl, INTERPWKSPC
89
        call WRITE              ; BC already set to bytes read
90
        jr nc, .cploop
91
 
92
.closefiles:
93
        push af
94
        ld a, (SRC_FD)
95
        call VCLOSE
96
        ld a, (DST_FD)
97
        call VCLOSE
98
        pop af
99
        cp EOF                  ; At EOF?
100
        ret z
101
        scf                     ; no, then make sure carry is still set
102
        ret                     ; to indicate error condition.
103
 
104
.cleanup:
105
        push af                 ; store error
106
        ld a, (SRC_FD)
107
        call VCLOSE
108
        pop af
109
        ret
110