-
Notifications
You must be signed in to change notification settings - Fork 1
/
truncate.go
40 lines (31 loc) · 944 Bytes
/
truncate.go
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
package wrfs
import "os"
// TruncateFile is a file that supports the Truncate method.
type TruncateFile interface {
File
// Truncate changes the size of the file.
Truncate(size int64) error
}
// TruncateFS is a file system with a Truncate method.
type TruncateFS interface {
FS
// Truncate changes the size of the named file.
Truncate(name string, size int64) error
}
// Truncate changes the size of the named file.
func Truncate(fsys FS, name string, size int64) (err error) {
if fsys, ok := fsys.(TruncateFS); ok {
return fsys.Truncate(name, size)
}
file, err := OpenFile(fsys, name, os.O_WRONLY, 0)
if err != nil {
return err
}
defer safeClose(file, &err)
if file, ok := file.(TruncateFile); ok {
return file.Truncate(size)
}
// We could try to manually truncate the file if the fs supports OpenFile,
// but that would be very inefficient.
return &PathError{Op: "truncate", Path: name, Err: ErrUnsupported}
}