Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Unified Diff: client/cipd/local/fs.go

Issue 1872363004: cipd: accept a function in EnsureFile (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: rebased Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « client/cipd/local/deployer.go ('k') | client/cipd/local/fs_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/cipd/local/fs.go
diff --git a/client/cipd/local/fs.go b/client/cipd/local/fs.go
index da4043e386c1c7faecf34e6900e67c33b454d4a8..80bffe13dd72dbf99c8878024b97e7c3ced52241 100644
--- a/client/cipd/local/fs.go
+++ b/client/cipd/local/fs.go
@@ -44,9 +44,9 @@ type FileSystem interface {
// directory path to the symlink if necessary.
EnsureSymlink(path string, target string) error
- // EnsureFile creates a file with given content. If will create full directory
- // path to the file if necessary.
- EnsureFile(path string, body io.Reader) error
+ // EnsureFile creates a file and calls the function to write file content.
+ // It will create full directory path to the file if necessary.
+ EnsureFile(path string, write func(*os.File) error) error
// EnsureFileGone removes a file, logging the errors (if any). Missing file is
// not an error.
@@ -76,20 +76,29 @@ func NewFileSystem(root string, logger logging.Logger) FileSystem {
return &fsImpl{abs, logger}
}
+// EnsureFile creates a file with the given content.
+// It will create full directory path to the file if necessary.
+func EnsureFile(fs FileSystem, path string, content io.Reader) error {
+ return fs.EnsureFile(path, func(f *os.File) error {
+ _, err := io.Copy(f, content)
+ return err
+ })
+}
+
// fsImplErr implements FileSystem by returning given error from all methods.
type fsImplErr struct {
err error
}
-func (f *fsImplErr) Root() string { return "" }
-func (f *fsImplErr) CwdRelToAbs(path string) (string, error) { return "", f.err }
-func (f *fsImplErr) RootRelToAbs(path string) (string, error) { return "", f.err }
-func (f *fsImplErr) EnsureDirectory(path string) (string, error) { return "", f.err }
-func (f *fsImplErr) EnsureSymlink(path string, target string) error { return f.err }
-func (f *fsImplErr) EnsureFile(path string, body io.Reader) error { return f.err }
-func (f *fsImplErr) EnsureFileGone(path string) error { return f.err }
-func (f *fsImplErr) EnsureDirectoryGone(path string) error { return f.err }
-func (f *fsImplErr) Replace(oldpath, newpath string) error { return f.err }
+func (f *fsImplErr) Root() string { return "" }
+func (f *fsImplErr) CwdRelToAbs(path string) (string, error) { return "", f.err }
+func (f *fsImplErr) RootRelToAbs(path string) (string, error) { return "", f.err }
+func (f *fsImplErr) EnsureDirectory(path string) (string, error) { return "", f.err }
+func (f *fsImplErr) EnsureSymlink(path string, target string) error { return f.err }
+func (f *fsImplErr) EnsureFile(path string, write func(*os.File) error) error { return f.err }
+func (f *fsImplErr) EnsureFileGone(path string) error { return f.err }
+func (f *fsImplErr) EnsureDirectoryGone(path string) error { return f.err }
+func (f *fsImplErr) Replace(oldpath, newpath string) error { return f.err }
/// Implementation.
@@ -138,7 +147,7 @@ func (f *fsImpl) EnsureDirectory(path string) (string, error) {
return path, nil
}
-func (f *fsImpl) EnsureFile(path string, body io.Reader) error {
+func (f *fsImpl) EnsureFile(path string, write func(*os.File) error) error {
path, err := f.CwdRelToAbs(path)
if err != nil {
return err
@@ -149,7 +158,7 @@ func (f *fsImpl) EnsureFile(path string, body io.Reader) error {
// Create a temp file with new content.
temp := tempFileName(path)
- if err := createFile(temp, body); err != nil {
+ if err := createFile(temp, write); err != nil {
return err
}
@@ -316,8 +325,8 @@ func pseudoRand() string {
return fmt.Sprintf("%v_%v", os.Getpid(), ts)
}
-// createFile creates a file with the given content.
-func createFile(path string, content io.Reader) (err error) {
+// createFile creates a file and calls the function to write file content.
+func createFile(path string, write func(*os.File) error) (err error) {
file, err := os.Create(path)
if err != nil {
return err
@@ -328,6 +337,5 @@ func createFile(path string, content io.Reader) (err error) {
err = closeErr
}
}()
- _, err = io.Copy(file, content)
- return err
+ return write(file)
}
« no previous file with comments | « client/cipd/local/deployer.go ('k') | client/cipd/local/fs_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698