// set sets the contents of a file. func (f *File) set(fs testfs.FileSystem, r core.ResponseWriter) { var ( file testfs.File err error ) // If we have a valid checksum for the input data and the file exists, read it // and avoid modifying the file if possible. if f.matching(fs) { r.Success(true) r.Changed(false) r.Message("info", f.path, " unchanged") return } file, err = fs.OpenFile(f.path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, f.mode) if err != nil { r.Message("error", err.Error()) r.Success(false) return } err = bufferedWrite(f.data, file) if err != nil { r.Message("error", err.Error()) r.Success(false) return } err = file.Close() if err != nil { r.Message("error", err.Error()) r.Success(false) return } r.Message("info", "set content of ", f.path) r.Success(true) r.Changed(true) return }
// touch a file. Same as system touch. func (f *File) touch(fs testfs.FileSystem, r core.ResponseWriter) { file, err := fs.OpenFile(f.path, os.O_CREATE|os.O_WRONLY, f.mode) if err != nil { r.Message("error", err.Error()) r.Success(false) return } err = file.Close() if err != nil { r.Message("error", err.Error()) r.Success(false) return } r.Message("info", "touched ", f.path) r.Success(true) r.Changed(true) return }
// remove removes a file. func (f *File) remove(fs testfs.FileSystem, r core.ResponseWriter) { err := fs.Remove(f.path) switch { // Don't error if the file is already removed case os.IsNotExist(err): r.Success(true) r.Changed(false) r.Message("info", f.path, " already removed") case err == nil: r.Success(true) r.Changed(true) r.Message("info", f.path, " removed") default: r.Message("error", err.Error()) r.Success(false) } return }