2016-11-24 12:07:14 +00:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-11-24 12:07:14 +00:00
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2017-10-20 14:52:55 +00:00
|
|
|
"context"
|
2017-08-19 14:36:56 +00:00
|
|
|
"errors"
|
2016-11-24 12:07:14 +00:00
|
|
|
"io"
|
2022-12-21 22:42:22 +00:00
|
|
|
"io/fs"
|
2017-04-01 09:04:11 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-09-02 05:52:38 +00:00
|
|
|
"strings"
|
2016-11-24 12:07:14 +00:00
|
|
|
"time"
|
2022-07-26 06:24:58 +00:00
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore/ignoreresult"
|
2022-07-26 06:24:58 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2016-11-24 12:07:14 +00:00
|
|
|
)
|
|
|
|
|
2021-05-03 10:28:25 +00:00
|
|
|
type filesystemWrapperType int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
filesystemWrapperTypeNone filesystemWrapperType = iota
|
|
|
|
filesystemWrapperTypeMtime
|
|
|
|
filesystemWrapperTypeCase
|
|
|
|
filesystemWrapperTypeError
|
|
|
|
filesystemWrapperTypeWalk
|
|
|
|
filesystemWrapperTypeLog
|
2023-08-04 17:57:30 +00:00
|
|
|
filesystemWrapperTypeMetrics
|
2021-05-03 10:28:25 +00:00
|
|
|
)
|
|
|
|
|
2022-09-14 07:50:55 +00:00
|
|
|
type XattrFilter interface {
|
|
|
|
Permit(string) bool
|
|
|
|
GetMaxSingleEntrySize() int
|
|
|
|
GetMaxTotalSize() int
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:07:14 +00:00
|
|
|
// The Filesystem interface abstracts access to the file system.
|
|
|
|
type Filesystem interface {
|
|
|
|
Chmod(name string, mode FileMode) error
|
2022-07-26 06:24:58 +00:00
|
|
|
Lchown(name string, uid, gid string) error // uid/gid as strings; numeric on POSIX, SID on Windows, like in os/user package
|
2016-11-24 12:07:14 +00:00
|
|
|
Chtimes(name string, atime time.Time, mtime time.Time) error
|
|
|
|
Create(name string) (File, error)
|
2018-01-14 14:25:04 +00:00
|
|
|
CreateSymlink(target, name string) error
|
2016-11-24 12:07:14 +00:00
|
|
|
DirNames(name string) ([]string, error)
|
|
|
|
Lstat(name string) (FileInfo, error)
|
|
|
|
Mkdir(name string, perm FileMode) error
|
2017-08-19 14:36:56 +00:00
|
|
|
MkdirAll(name string, perm FileMode) error
|
2016-11-24 12:07:14 +00:00
|
|
|
Open(name string) (File, error)
|
2017-08-19 14:36:56 +00:00
|
|
|
OpenFile(name string, flags int, mode FileMode) (File, error)
|
2017-02-07 08:34:24 +00:00
|
|
|
ReadSymlink(name string) (string, error)
|
2016-11-24 12:07:14 +00:00
|
|
|
Remove(name string) error
|
2017-08-19 14:36:56 +00:00
|
|
|
RemoveAll(name string) error
|
2016-11-24 12:07:14 +00:00
|
|
|
Rename(oldname, newname string) error
|
|
|
|
Stat(name string) (FileInfo, error)
|
|
|
|
SymlinksSupported() bool
|
2017-10-20 14:52:55 +00:00
|
|
|
Walk(name string, walkFn WalkFunc) error
|
2019-05-25 19:08:26 +00:00
|
|
|
// If setup fails, returns non-nil error, and if afterwards a fatal (!)
|
|
|
|
// error occurs, sends that error on the channel. Afterwards this watch
|
|
|
|
// can be considered stopped.
|
|
|
|
Watch(path string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, <-chan error, error)
|
2017-08-19 14:36:56 +00:00
|
|
|
Hide(name string) error
|
|
|
|
Unhide(name string) error
|
|
|
|
Glob(pattern string) ([]string, error)
|
|
|
|
Roots() ([]string, error)
|
|
|
|
Usage(name string) (Usage, error)
|
|
|
|
Type() FilesystemType
|
|
|
|
URI() string
|
2021-03-11 14:23:56 +00:00
|
|
|
Options() []Option
|
lib/scanner: Fix UTF-8 normalization on ZFS (fixes #4649)
It turns out that ZFS doesn't do any normalization when storing files,
but does do normalization "as part of any comparison process".
In practice, this seems to mean that if you LStat a normalized filename,
ZFS will return the FileInfo for the un-normalized version of that
filename.
This meant that our test to see whether a separate file with a
normalized version of the filename already exists was failing, as we
were detecting the same file.
The fix is to use os.SameFile, to see whether we're getting the same
FileInfo from the normalized and un-normalized versions of the same
filename.
One complication is that ZFS also seems to apply its magic to os.Rename,
meaning that we can't use it to rename an un-normalized file to its
normalized filename. Instead we have to move via a temporary object. If
the move to the temporary object fails, that's OK, we can skip it and
move on. If the move from the temporary object fails however, I'm not
sure of the best approach: the current one is to leave the temporary
file name as-is, and get Syncthing to syncronize it, so at least we
don't lose the file. I'm not sure if there are any implications of this
however.
As part of reworking normalizePath, I spotted that it appeared to be
returning the wrong thing: the doc and the surrounding code expecting it
to return the normalized filename, but it was returning the
un-normalized one. I fixed this, but it seems suspicious that, if the
previous behaviour was incorrect, noone ever ran afoul of it. Maybe all
filesystems will do some searching and give you a normalized filename if
you request an unnormalized one.
As part of this, I found that TestNormalization was broken: it was
passing, when in fact one of the files it should have verified was
present was missing. Maybe this was related to the above issue with
normalizePath's return value, I'm not sure. Fixed en route.
Kindly tested by @khinsen on the forum, and it appears to work.
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4646
2018-01-05 18:11:09 +00:00
|
|
|
SameFile(fi1, fi2 FileInfo) bool
|
2022-09-14 07:50:55 +00:00
|
|
|
PlatformData(name string, withOwnership, withXattrs bool, xattrFilter XattrFilter) (protocol.PlatformData, error)
|
|
|
|
GetXattr(name string, xattrFilter XattrFilter) ([]protocol.Xattr, error)
|
|
|
|
SetXattr(path string, xattrs []protocol.Xattr, xattrFilter XattrFilter) error
|
2021-05-03 10:28:25 +00:00
|
|
|
|
|
|
|
// Used for unwrapping things
|
|
|
|
underlying() (Filesystem, bool)
|
|
|
|
wrapperType() filesystemWrapperType
|
2016-11-24 12:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The File interface abstracts access to a regular file, being a somewhat
|
|
|
|
// smaller interface than os.File
|
|
|
|
type File interface {
|
2017-08-19 14:36:56 +00:00
|
|
|
io.Closer
|
2016-11-24 12:07:14 +00:00
|
|
|
io.Reader
|
2017-08-19 14:36:56 +00:00
|
|
|
io.ReaderAt
|
|
|
|
io.Seeker
|
|
|
|
io.Writer
|
2016-11-24 12:07:14 +00:00
|
|
|
io.WriterAt
|
2017-08-19 14:36:56 +00:00
|
|
|
Name() string
|
2016-11-24 12:07:14 +00:00
|
|
|
Truncate(size int64) error
|
2017-04-01 09:04:11 +00:00
|
|
|
Stat() (FileInfo, error)
|
2017-08-19 14:36:56 +00:00
|
|
|
Sync() error
|
2016-11-24 12:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The FileInfo interface is almost the same as os.FileInfo, but with the
|
|
|
|
// Sys method removed (as we don't want to expose whatever is underlying)
|
|
|
|
// and with a couple of convenience methods added.
|
|
|
|
type FileInfo interface {
|
|
|
|
// Standard things present in os.FileInfo
|
|
|
|
Name() string
|
|
|
|
Mode() FileMode
|
|
|
|
Size() int64
|
|
|
|
ModTime() time.Time
|
|
|
|
IsDir() bool
|
2022-09-14 07:50:55 +00:00
|
|
|
Sys() interface{}
|
2016-11-24 12:07:14 +00:00
|
|
|
// Extensions
|
|
|
|
IsRegular() bool
|
|
|
|
IsSymlink() bool
|
2019-01-25 08:52:21 +00:00
|
|
|
Owner() int
|
|
|
|
Group() int
|
2022-09-14 07:50:55 +00:00
|
|
|
InodeChangeTime() time.Time // may be zero if not supported
|
2016-11-24 12:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileMode is similar to os.FileMode
|
|
|
|
type FileMode uint32
|
|
|
|
|
2018-10-31 11:49:50 +00:00
|
|
|
func (fm FileMode) String() string {
|
|
|
|
return os.FileMode(fm).String()
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
// Usage represents filesystem space usage
|
|
|
|
type Usage struct {
|
2020-10-02 13:22:28 +00:00
|
|
|
Free uint64
|
|
|
|
Total uint64
|
2017-08-19 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 14:52:55 +00:00
|
|
|
type Matcher interface {
|
2024-01-13 17:58:23 +00:00
|
|
|
Match(name string) ignoreresult.R
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Event struct {
|
|
|
|
Name string
|
|
|
|
Type EventType
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NonRemove EventType = 1 + iota
|
|
|
|
Remove
|
|
|
|
Mixed // Should probably not be necessary to be used in filesystem interface implementation
|
|
|
|
)
|
|
|
|
|
2018-05-26 09:08:23 +00:00
|
|
|
// Merge returns Mixed, except if evType and other are the same and not Mixed.
|
|
|
|
func (evType EventType) Merge(other EventType) EventType {
|
|
|
|
return evType | other
|
|
|
|
}
|
|
|
|
|
2017-10-20 14:52:55 +00:00
|
|
|
func (evType EventType) String() string {
|
|
|
|
switch {
|
|
|
|
case evType == NonRemove:
|
|
|
|
return "non-remove"
|
|
|
|
case evType == Remove:
|
|
|
|
return "remove"
|
|
|
|
case evType == Mixed:
|
|
|
|
return "mixed"
|
|
|
|
default:
|
|
|
|
panic("bug: Unknown event type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 07:50:55 +00:00
|
|
|
var (
|
|
|
|
ErrWatchNotSupported = errors.New("watching is not supported")
|
|
|
|
ErrXattrsNotSupported = errors.New("extended attributes are not supported on this platform")
|
|
|
|
)
|
2017-10-20 14:52:55 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
// Equivalents from os package.
|
2017-04-01 09:04:11 +00:00
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
const (
|
|
|
|
ModePerm = FileMode(os.ModePerm)
|
|
|
|
ModeSetgid = FileMode(os.ModeSetgid)
|
|
|
|
ModeSetuid = FileMode(os.ModeSetuid)
|
|
|
|
ModeSticky = FileMode(os.ModeSticky)
|
|
|
|
ModeSymlink = FileMode(os.ModeSymlink)
|
|
|
|
ModeType = FileMode(os.ModeType)
|
|
|
|
PathSeparator = os.PathSeparator
|
|
|
|
OptAppend = os.O_APPEND
|
|
|
|
OptCreate = os.O_CREATE
|
|
|
|
OptExclusive = os.O_EXCL
|
|
|
|
OptReadOnly = os.O_RDONLY
|
|
|
|
OptReadWrite = os.O_RDWR
|
|
|
|
OptSync = os.O_SYNC
|
|
|
|
OptTruncate = os.O_TRUNC
|
|
|
|
OptWriteOnly = os.O_WRONLY
|
|
|
|
)
|
2016-11-24 12:07:14 +00:00
|
|
|
|
|
|
|
// SkipDir is used as a return value from WalkFuncs to indicate that
|
|
|
|
// the directory named in the call is to be skipped. It is not returned
|
|
|
|
// as an error by any function.
|
2017-04-01 09:04:11 +00:00
|
|
|
var SkipDir = filepath.SkipDir
|
|
|
|
|
2022-12-21 22:42:22 +00:00
|
|
|
func IsExist(err error) bool {
|
|
|
|
return errors.Is(err, ErrExist)
|
|
|
|
}
|
2017-04-01 09:04:11 +00:00
|
|
|
|
2022-12-21 22:42:22 +00:00
|
|
|
// ErrExist is the equivalent of os.ErrExist
|
|
|
|
var ErrExist = fs.ErrExist
|
2020-06-16 13:20:08 +00:00
|
|
|
|
2017-04-01 09:04:11 +00:00
|
|
|
// IsNotExist is the equivalent of os.IsNotExist
|
2022-12-21 22:42:22 +00:00
|
|
|
func IsNotExist(err error) bool {
|
|
|
|
return errors.Is(err, ErrNotExist)
|
|
|
|
}
|
2017-08-19 14:36:56 +00:00
|
|
|
|
2020-06-16 13:20:08 +00:00
|
|
|
// ErrNotExist is the equivalent of os.ErrNotExist
|
2022-12-21 22:42:22 +00:00
|
|
|
var ErrNotExist = fs.ErrNotExist
|
2020-06-16 13:20:08 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
// IsPermission is the equivalent of os.IsPermission
|
2022-12-21 22:42:22 +00:00
|
|
|
func IsPermission(err error) bool {
|
|
|
|
return errors.Is(err, fs.ErrPermission)
|
|
|
|
}
|
2017-08-19 14:36:56 +00:00
|
|
|
|
|
|
|
// IsPathSeparator is the equivalent of os.IsPathSeparator
|
|
|
|
var IsPathSeparator = os.IsPathSeparator
|
|
|
|
|
2021-03-11 14:23:56 +00:00
|
|
|
// Option modifies a filesystem at creation. An option might be specific
|
|
|
|
// to a filesystem-type.
|
|
|
|
//
|
|
|
|
// String is used to detect options with the same effect, i.e. must be different
|
|
|
|
// for options with different effects. Meaning if an option has parameters, a
|
|
|
|
// representation of those must be part of the returned string.
|
|
|
|
type Option interface {
|
|
|
|
String() string
|
2022-04-10 18:55:05 +00:00
|
|
|
apply(Filesystem) Filesystem
|
2021-02-19 10:06:25 +00:00
|
|
|
}
|
2020-08-19 17:58:51 +00:00
|
|
|
|
|
|
|
func NewFilesystem(fsType FilesystemType, uri string, opts ...Option) Filesystem {
|
2022-04-10 18:55:05 +00:00
|
|
|
var caseOpt Option
|
|
|
|
var mtimeOpt Option
|
|
|
|
i := 0
|
|
|
|
for _, opt := range opts {
|
|
|
|
if caseOpt != nil && mtimeOpt != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch opt.(type) {
|
|
|
|
case *OptionDetectCaseConflicts:
|
|
|
|
caseOpt = opt
|
|
|
|
case *optionMtime:
|
|
|
|
mtimeOpt = opt
|
|
|
|
default:
|
|
|
|
opts[i] = opt
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opts = opts[:i]
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
var fs Filesystem
|
|
|
|
switch fsType {
|
|
|
|
case FilesystemTypeBasic:
|
2020-08-19 17:58:51 +00:00
|
|
|
fs = newBasicFilesystem(uri, opts...)
|
2018-10-02 18:29:06 +00:00
|
|
|
case FilesystemTypeFake:
|
2020-08-19 17:58:51 +00:00
|
|
|
fs = newFakeFilesystem(uri, opts...)
|
2017-08-19 14:36:56 +00:00
|
|
|
default:
|
|
|
|
l.Debugln("Unknown filesystem", fsType, uri)
|
|
|
|
fs = &errorFilesystem{
|
|
|
|
fsType: fsType,
|
|
|
|
uri: uri,
|
|
|
|
err: errors.New("filesystem with type " + fsType.String() + " does not exist."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 18:55:05 +00:00
|
|
|
// mtime handling should happen inside walking, as filesystem calls while
|
|
|
|
// walking should be mtime-resolved too
|
|
|
|
if mtimeOpt != nil {
|
|
|
|
fs = mtimeOpt.apply(fs)
|
|
|
|
}
|
|
|
|
|
2023-08-04 17:57:30 +00:00
|
|
|
fs = &metricsFS{next: fs}
|
|
|
|
|
2024-08-13 08:59:31 +00:00
|
|
|
layersAboveWalkFilesystem := 0
|
|
|
|
if caseOpt != nil {
|
|
|
|
// DirNames calls made to check the case of a name will also be
|
|
|
|
// attributed to the calling function.
|
|
|
|
layersAboveWalkFilesystem++
|
|
|
|
}
|
2018-02-05 10:07:56 +00:00
|
|
|
if l.ShouldDebug("walkfs") {
|
2024-08-13 08:59:31 +00:00
|
|
|
// A walkFilesystem is not a layer to skip, it embeds the underlying
|
|
|
|
// filesystem, passing calls directly trough. Except for calls made
|
|
|
|
// during walking, however those are truly originating in the walk
|
|
|
|
// filesystem.
|
|
|
|
fs = NewWalkFilesystem(newLogFilesystem(fs, layersAboveWalkFilesystem))
|
|
|
|
} else if l.ShouldDebug("fs") {
|
|
|
|
fs = newLogFilesystem(NewWalkFilesystem(fs), layersAboveWalkFilesystem)
|
|
|
|
} else {
|
|
|
|
fs = NewWalkFilesystem(fs)
|
2018-02-05 10:07:56 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 08:59:31 +00:00
|
|
|
// Case handling is at the outermost layer to resolve all input names.
|
|
|
|
// Reason being is that the only names/paths that are potentially "wrong"
|
|
|
|
// come from outside the fs package. Any paths that result from filesystem
|
|
|
|
// operations itself already have the correct case. Thus there's e.g. no
|
|
|
|
// point to check the case on all the stating the walk filesystem does, it
|
|
|
|
// just adds overhead.
|
|
|
|
if caseOpt != nil {
|
|
|
|
fs = caseOpt.apply(fs)
|
2017-08-19 14:36:56 +00:00
|
|
|
}
|
2018-02-05 10:07:56 +00:00
|
|
|
|
2024-08-13 08:59:31 +00:00
|
|
|
return fs
|
2017-08-19 14:36:56 +00:00
|
|
|
}
|
2017-09-02 05:52:38 +00:00
|
|
|
|
|
|
|
// IsInternal returns true if the file, as a path relative to the folder
|
|
|
|
// root, represents an internal file that should always be ignored. The file
|
|
|
|
// path must be clean (i.e., in canonical shortest form).
|
|
|
|
func IsInternal(file string) bool {
|
2023-08-09 07:10:06 +00:00
|
|
|
// fs cannot import config or versioner, so we hard code .stfolder
|
|
|
|
// (config.DefaultMarkerName) and .stversions (versioner.DefaultPath)
|
2017-09-02 05:52:38 +00:00
|
|
|
internals := []string{".stfolder", ".stignore", ".stversions"}
|
|
|
|
for _, internal := range internals {
|
|
|
|
if file == internal {
|
|
|
|
return true
|
|
|
|
}
|
2018-11-22 10:16:45 +00:00
|
|
|
if IsParent(file, internal) {
|
2017-09-02 05:52:38 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-03-12 12:18:59 +00:00
|
|
|
|
2022-01-29 17:47:55 +00:00
|
|
|
var (
|
|
|
|
errPathInvalid = errors.New("path is invalid")
|
|
|
|
errPathTraversingUpwards = errors.New("relative path traversing upwards (starting with ..)")
|
|
|
|
)
|
|
|
|
|
2018-03-12 12:18:59 +00:00
|
|
|
// Canonicalize checks that the file path is valid and returns it in the "canonical" form:
|
|
|
|
// - /foo/bar -> foo/bar
|
|
|
|
// - / -> "."
|
|
|
|
func Canonicalize(file string) (string, error) {
|
2021-11-23 18:55:58 +00:00
|
|
|
const pathSep = string(PathSeparator)
|
2018-03-12 12:18:59 +00:00
|
|
|
|
|
|
|
if strings.HasPrefix(file, pathSep+pathSep) {
|
|
|
|
// The relative path may pretend to be an absolute path within
|
|
|
|
// the root, but the double path separator on Windows implies
|
|
|
|
// something else and is out of spec.
|
2022-01-29 17:47:55 +00:00
|
|
|
return "", errPathInvalid
|
2018-03-12 12:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The relative path should be clean from internal dotdots and similar
|
|
|
|
// funkyness.
|
|
|
|
file = filepath.Clean(file)
|
|
|
|
|
|
|
|
// It is not acceptable to attempt to traverse upwards.
|
2021-03-17 22:12:26 +00:00
|
|
|
if file == ".." {
|
2022-01-29 17:47:55 +00:00
|
|
|
return "", errPathTraversingUpwards
|
2018-03-12 12:18:59 +00:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(file, ".."+pathSep) {
|
2022-01-29 17:47:55 +00:00
|
|
|
return "", errPathTraversingUpwards
|
2018-03-12 12:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(file, pathSep) {
|
|
|
|
if file == pathSep {
|
|
|
|
return ".", nil
|
|
|
|
}
|
|
|
|
return file[1:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|
2020-09-09 12:38:39 +00:00
|
|
|
|
2021-05-03 10:28:25 +00:00
|
|
|
// unwrapFilesystem removes "wrapping" filesystems to expose the filesystem of the requested wrapperType, if it exists.
|
|
|
|
func unwrapFilesystem(fs Filesystem, wrapperType filesystemWrapperType) (Filesystem, bool) {
|
|
|
|
var ok bool
|
2020-09-09 12:38:39 +00:00
|
|
|
for {
|
2021-05-03 10:28:25 +00:00
|
|
|
if fs.wrapperType() == wrapperType {
|
|
|
|
return fs, true
|
|
|
|
}
|
|
|
|
fs, ok = fs.underlying()
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
2020-09-09 12:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 08:25:36 +00:00
|
|
|
|
|
|
|
// 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 WriteFile(fs Filesystem, name string, data []byte, perm FileMode) error {
|
|
|
|
f, err := fs.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = f.Write(data)
|
|
|
|
if err1 := f.Close(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|