forked from TrueCloudLab/rclone
vfs: add vfs.WriteFile to match os.WriteFile
This commit is contained in:
parent
c47c94e485
commit
44c3f5e1e8
1 changed files with 15 additions and 0 deletions
15
vfs/vfs.go
15
vfs/vfs.go
|
@ -770,6 +770,21 @@ func (vfs *VFS) ReadFile(filename string) (b []byte, err error) {
|
||||||
return io.ReadAll(f)
|
return io.ReadAll(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteFile writes data to the named file, creating it if necessary. If the
|
||||||
|
// file does not exist, WriteFile creates it with permissions perm (before
|
||||||
|
// umask); otherwise WriteFile truncates it before writing, without changing
|
||||||
|
// permissions. Since WriteFile requires multiple system calls to complete,
|
||||||
|
// a failure mid-operation can leave the file in a partially written state.
|
||||||
|
func (vfs *VFS) WriteFile(name string, data []byte, perm os.FileMode) (err error) {
|
||||||
|
fh, err := vfs.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer fs.CheckClose(fh, &err)
|
||||||
|
_, err = fh.Write(data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// AddVirtual adds the object (file or dir) to the directory cache
|
// AddVirtual adds the object (file or dir) to the directory cache
|
||||||
func (vfs *VFS) AddVirtual(remote string, size int64, isDir bool) (err error) {
|
func (vfs *VFS) AddVirtual(remote string, size int64, isDir bool) (err error) {
|
||||||
remote = strings.TrimRight(remote, "/")
|
remote = strings.TrimRight(remote, "/")
|
||||||
|
|
Loading…
Add table
Reference in a new issue