-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilepath_ext.go
More file actions
155 lines (132 loc) · 3.34 KB
/
Copy pathfilepath_ext.go
File metadata and controls
155 lines (132 loc) · 3.34 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package dt
import (
"os"
)
// Status classifies the filesystem entry referred to by fp.
//
// It returns IsMissingEntry when the entry does not exist (err == nil).
// It returns IsEntryError for all other filesystem errors (err != nil).
// By default it follows symlinks (like os.Stat). To inspect the entry
// itself, pass FlagDontFollowSymlinks.
//
// On platforms that don't support certain kinds (e.g., sockets/devices on
// Windows), those statuses will never be returned.
func (fp Filepath) Status(flags ...EntryStatusFlags) (status EntryStatus, err error) {
return EntryPath(fp).Status(flags...)
}
func (fp Filepath) Join(elems ...any) Filepath {
return Filepath(EntryPath(fp).Join(elems...))
}
// CopyToDir copies the file to the destination directory path with optional
// permission control
func (fp Filepath) CopyToDir(dest DirPath, opts *CopyOptions) (err error) {
var status EntryStatus
dir := dest
status, err = fp.Status()
if err != nil {
goto end
}
switch status {
case IsFileEntry:
err = NewErr(ErrIsADirectory)
case IsDirEntry:
destFP := FilepathJoin(dir, fp.Base())
err = fp.CopyTo(destFP, opts)
default:
err = NewErr(
ErrNotDirectory,
"entry_status", status,
)
}
end:
return err
}
// CopyTo copies the file to the destination filepath with optional permission
// control
func (fp Filepath) CopyTo(dest Filepath, opts *CopyOptions) (err error) {
var srcFile *os.File
var destFile *os.File
var srcInfo os.FileInfo
var destMode os.FileMode
var destExists bool
// Normalize opts
if opts == nil {
opts = new(CopyOptions)
}
// Read source file info
srcInfo, err = fp.Stat()
if err != nil {
goto end
}
if srcInfo.IsDir() {
err = NewErr(ErrIsADirectory)
goto end
}
// Check if destination exists
_, err = dest.Stat()
destExists = err == nil
// If dest exists and Overwrite is false, error
if destExists && !opts.Overwrite {
err = os.ErrExist
goto end
}
// Determine destination permissions
if opts.DestModeFunc != nil {
destMode = opts.DestModeFunc(EntryPath(dest))
if destMode == 0 {
// 0 means preserve source permissions
destMode = srcInfo.Mode()
}
} else {
// No callback, preserve source permissions
destMode = srcInfo.Mode()
}
// Create parent directory if needed
err = dest.Dir().MkdirAll(0755)
if err != nil {
goto end
}
// Open source file
srcFile, err = fp.Open()
if err != nil {
goto end
}
defer CloseOrLog(srcFile)
// Create destination file
destFile, err = dest.OpenFile(os.O_WRONLY|os.O_CREATE|os.O_TRUNC, destMode)
if err != nil {
goto end
}
defer CloseOrLog(destFile)
// Copy contents
_, err = srcFile.WriteTo(destFile)
if err != nil {
goto end
}
end:
return err
}
func (fp Filepath) HasDotDotPrefix() bool {
return EntryPath(fp).HasDotDotPrefix()
}
func (fp Filepath) Expand() (_ Filepath, err error) {
var ep EntryPath
ep, err = EntryPath(fp).Expand()
return Filepath(ep), err
}
func (fp Filepath) ToTilde(opt TildeOption) TildeFilepath {
return ToTilde[Filepath, TildeFilepath](fp, opt)
}
func (fp Filepath) TrimTilde() (tdp PathSegments) {
return TrimTilde[Filepath](fp)
}
func (fp Filepath) ErrKV() ErrKV {
return kv{k: "filepath", v: fp.ToTilde(OrFullPath)}
}
func (fp Filepath) Touch(mode os.FileMode) (err error) {
err = fp.WriteFile([]byte{}, mode)
if err != nil {
err = NewErr(ErrFailedtoCreateFile, fp.ErrKV(), err)
}
return err
}