Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To prevent wrong flag for OpenFile, use new file package
Use new file package to open file, in order to prevent wrong flag for os.OpenFile. See Bojun's commit v0.7.3.1-5-gbbffc09 (fix: config example file is not completely overwritten, 2020-02-28), and there are still some places need the same fix. Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
- Loading branch information
Showing
12 changed files
with
135 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package file | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
) | ||
|
||
// Common errors | ||
var ( | ||
ErrNoFileName = errors.New("file name is not set") | ||
) | ||
|
||
// File implements a helper for os.File. | ||
type File struct { | ||
Name string | ||
|
||
perm os.FileMode | ||
} | ||
|
||
// New creates File object. | ||
func New(name string) *File { | ||
return &File{Name: name} | ||
} | ||
|
||
// SetName set Name for file. | ||
func (v *File) SetName(name string) *File { | ||
v.Name = name | ||
return v | ||
} | ||
|
||
// SetPerm sets file permission. | ||
func (v *File) SetPerm(perm os.FileMode) *File { | ||
v.perm = perm | ||
return v | ||
} | ||
|
||
// SetExecutable sets perm to 0755. | ||
func (v *File) SetExecutable() *File { | ||
return v.SetPerm(0755) | ||
} | ||
|
||
func (v *File) openWrite(flag int) (*os.File, error) { | ||
if v.Name == "" { | ||
return nil, ErrNoFileName | ||
} | ||
|
||
if flag&(os.O_RDWR|os.O_WRONLY) == 0 { | ||
flag |= os.O_WRONLY | ||
} | ||
|
||
if v.perm == 0 { | ||
v.perm = 0644 | ||
} | ||
|
||
return os.OpenFile(v.Name, flag, v.perm) | ||
} | ||
|
||
// Open opens file for reading. | ||
func (v *File) Open() (*os.File, error) { | ||
if v.Name == "" { | ||
return nil, ErrNoFileName | ||
} | ||
return os.Open(v.Name) | ||
} | ||
|
||
// OpenReadWrite opens file for read and write. | ||
func (v *File) OpenReadWrite() (*os.File, error) { | ||
return v.openWrite(os.O_RDWR) | ||
} | ||
|
||
// OpenCreateReadWrite opens file for read and write, and create if not exist. | ||
func (v *File) OpenCreateReadWrite() (*os.File, error) { | ||
return v.openWrite(os.O_RDWR | os.O_CREATE) | ||
} | ||
|
||
// OpenCreateReadWriteExcl create file for read and write, and file must not exist. | ||
func (v *File) OpenCreateReadWriteExcl() (*os.File, error) { | ||
return v.openWrite(os.O_RDWR | os.O_CREATE | os.O_EXCL) | ||
} | ||
|
||
// OpenRewrite opens file for write, and will truncate file if already exist. | ||
func (v *File) OpenRewrite() (*os.File, error) { | ||
return v.openWrite(os.O_TRUNC) | ||
} | ||
|
||
// OpenCreateRewrite open file for rewrite, and will create file if not exist. | ||
func (v *File) OpenCreateRewrite() (*os.File, error) { | ||
return v.openWrite(os.O_CREATE | os.O_TRUNC) | ||
} | ||
|
||
// OpenCreateRewriteExcl create file for rewrite, and fail if file exist. | ||
func (v *File) OpenCreateRewriteExcl() (*os.File, error) { | ||
return v.openWrite(os.O_CREATE | os.O_TRUNC | os.O_EXCL) | ||
} | ||
|
||
// OpenAppend open file for append. | ||
func (v *File) OpenAppend() (*os.File, error) { | ||
return v.openWrite(os.O_APPEND) | ||
} | ||
|
||
// OpenCreateAppend open file for append, and create if not exist. | ||
func (v *File) OpenCreateAppend() (*os.File, error) { | ||
return v.openWrite(os.O_CREATE | os.O_APPEND) | ||
} |
This file contains 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 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 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 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