This repository was archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 534
utils/fs: add OpenFile method to filesystem interface. #104
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,8 @@ func NewOS(baseDir string) *OS { | |
| } | ||
| } | ||
|
|
||
| // Create creates a new GlusterFSFile | ||
| // Create creates a file and opens it with standard permissions | ||
| // and modes O_RDWR, O_CREATE and O_TRUNC. | ||
| func (fs *OS) Create(filename string) (File, error) { | ||
|
||
| fullpath := path.Join(fs.base, filename) | ||
|
|
||
|
|
@@ -42,6 +43,30 @@ func (fs *OS) Create(filename string) (File, error) { | |
| return newOSFile(filename, f), nil | ||
| } | ||
|
|
||
| // OpenFile is equivalent to standard os.OpenFile. | ||
| // If flag os.O_CREATE is set, all parent directories will be created. | ||
| func (fs *OS) OpenFile(filename string, flag int, perm os.FileMode) (File, error) { | ||
| fullpath := path.Join(fs.base, filename) | ||
|
|
||
| if flag|os.O_CREATE != 0 { | ||
| if err := fs.createDir(fullpath); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| f, err := os.OpenFile(fullpath, flag, perm) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| filename, err = filepath.Rel(fs.base, fullpath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return newOSFile(filename, f), nil | ||
| } | ||
|
|
||
| func (fs *OS) createDir(fullpath string) error { | ||
| dir := filepath.Dir(fullpath) | ||
| if dir != "." { | ||
|
|
@@ -82,8 +107,7 @@ func (fs *OS) Rename(from, to string) error { | |
| return os.Rename(from, to) | ||
| } | ||
|
|
||
| // Open opens the named file for reading. If successful, methods on the returned | ||
| // file can be used for reading only. | ||
| // Open opens a file in read-only mode. | ||
| func (fs *OS) Open(filename string) (File, error) { | ||
| fullpath := fs.Join(fs.base, filename) | ||
| f, err := os.Open(fullpath) | ||
|
|
@@ -147,15 +171,13 @@ func (fs *OS) Base() string { | |
|
|
||
| // osFile represents a file in the os filesystem | ||
| type osFile struct { | ||
| filename string | ||
| closed bool | ||
| file *os.File | ||
| BaseFile | ||
| file *os.File | ||
| } | ||
|
|
||
| func newOSFile(filename string, file *os.File) File { | ||
| return &osFile{ | ||
| filename: filename, | ||
| closed: false, | ||
| BaseFile: BaseFile{BaseFilename: filename}, | ||
| file: file, | ||
| } | ||
| } | ||
|
|
@@ -173,21 +195,11 @@ func (f *osFile) Write(p []byte) (int, error) { | |
| } | ||
|
|
||
| func (f *osFile) Close() error { | ||
| f.closed = true | ||
| f.BaseFile.Closed = true | ||
|
|
||
| return f.file.Close() | ||
| } | ||
|
|
||
| func (f *osFile) ReadAt(p []byte, off int64) (n int, err error) { | ||
| return f.file.ReadAt(p, off) | ||
| } | ||
|
|
||
| //Filename returns the filename from the File | ||
| func (f *osFile) Filename() string { | ||
| return f.filename | ||
| } | ||
|
|
||
| //IsClosed returns if te file is closed | ||
| func (f *osFile) IsClosed() bool { | ||
| return f.closed | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I though that we are adding this to the bare package not to the OS one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed wildcard import.