Skip to content

Commit

Permalink
To prevent wrong flag for OpenFile, use new file package
Browse files Browse the repository at this point in the history
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
jiangxin committed Mar 2, 2020
1 parent b3ee2aa commit b387439
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 27 deletions.
3 changes: 2 additions & 1 deletion cmd/manifest.go
Expand Up @@ -18,6 +18,7 @@ import (
"io"
"os"

"github.com/aliyun/git-repo-go/file"
"github.com/aliyun/git-repo-go/manifest"
log "github.com/jiangxin/multi-log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -105,7 +106,7 @@ func (v manifestCommand) Execute(args []string) error {
} else if v.O.OutputFile == "-" {
writer = os.Stdout
} else {
file, err := os.OpenFile(v.O.OutputFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
file, err := file.New(v.O.OutputFile).OpenCreateRewrite()
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/sync.go
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/aliyun/git-repo-go/cap"
"github.com/aliyun/git-repo-go/config"
"github.com/aliyun/git-repo-go/file"
"github.com/aliyun/git-repo-go/project"
"github.com/aliyun/git-repo-go/workspace"
log "github.com/jiangxin/multi-log"
Expand Down Expand Up @@ -626,9 +627,7 @@ func (v syncCommand) UpdateProjectList() error {
}

projectListLockFile := projectListFile + ".lock"
lockf, err := os.OpenFile(projectListLockFile,
os.O_RDWR|os.O_CREATE|os.O_EXCL,
0644)
lockf, err := file.New(projectListLockFile).OpenCreateRewriteExcl()
if err != nil {
return fmt.Errorf("fail to create lockfile '%s': %s", projectListLockFile, err)
}
Expand Down
15 changes: 6 additions & 9 deletions cmd/upgrade.go
Expand Up @@ -41,6 +41,7 @@ import (

"github.com/aliyun/git-repo-go/cap"
"github.com/aliyun/git-repo-go/config"
"github.com/aliyun/git-repo-go/file"
"github.com/aliyun/git-repo-go/format"
"github.com/aliyun/git-repo-go/version"
log "github.com/jiangxin/multi-log"
Expand Down Expand Up @@ -253,7 +254,7 @@ func (v upgradeCommand) Download(URL string, dir string, showProgress bool) (str
return "", fmt.Errorf("cannot access %s (status: %d)", URL, resp.StatusCode)
}

f, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
f, err := file.New(fileName).OpenCreateRewrite()
if err != nil {
return "", err
}
Expand Down Expand Up @@ -462,9 +463,7 @@ func (v upgradeCommand) ExtractZip(pkgFile, dir string) (binFile, shaFile, gpgFi
err = fmt.Errorf("extract error, cannot open %s", f.Name)
return
}
out, err = os.OpenFile(fullpath,
os.O_CREATE|os.O_RDWR|os.O_TRUNC,
0644)
out, err = file.New(fullpath).OpenCreateRewrite()
if err == nil {
_, err = io.Copy(out, in)
if err != nil {
Expand Down Expand Up @@ -548,9 +547,7 @@ func (v upgradeCommand) ExtractTar(pkgFile, dir string) (binFile, shaFile, gpgFi
log.Warningf("unknown file in package: %s", baseName)
continue
}
f, err = os.OpenFile(filepath.Join(dir, baseName),
os.O_CREATE|os.O_RDWR|os.O_TRUNC,
0644)
f, err = file.New(filepath.Join(dir, baseName)).OpenCreateRewrite()
if err != nil {
return
}
Expand Down Expand Up @@ -671,7 +668,7 @@ func (v upgradeCommand) InstallImage(bin, target string) error {
}
defer in.Close()

out, err := os.OpenFile(lockFile, os.O_RDWR|os.O_CREATE, 0755)
out, err := file.New(lockFile).SetExecutable().OpenCreateRewrite()
if err != nil {
log.Debugf("no permission to write file '%s', will write to tmpfile instead",
lockFile)
Expand All @@ -680,7 +677,7 @@ func (v upgradeCommand) InstallImage(bin, target string) error {
return err
}
lockFile = filepath.Join(tmpDir, filepath.Base(target))
out, err = os.OpenFile(lockFile, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
out, err = file.New(lockFile).SetExecutable().OpenCreateRewrite()
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/upgrade_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"testing"

"github.com/aliyun/git-repo-go/file"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
Expand Down Expand Up @@ -174,15 +175,15 @@ qUDzwGflENTCedt+7lz7oLhAk3Oor0Gxk95u43ki9REJMIkS68CXDfe3t4GKUrKa
binFile.WriteString(data)
binFile.Close()

checksumFile, err := os.OpenFile(binFile.Name()+".sha256", os.O_RDWR|os.O_CREATE, 0644)
checksumFile, err := file.New(binFile.Name() + ".sha256").OpenCreateRewrite()
if err != nil {
t.Fatalf("fail to create checksum file: %s", err)
}
defer os.Remove(checksumFile.Name())
checksumFile.WriteString(checksum)
checksumFile.Close()

sigFile, err := os.OpenFile(binFile.Name()+".sha256.gpg", os.O_RDWR|os.O_CREATE, 0644)
sigFile, err := file.New(binFile.Name() + ".sha256.gpg").OpenCreateRewrite()
if err != nil {
t.Fatalf("fail to create sig file: %s", err)
}
Expand Down
3 changes: 2 additions & 1 deletion config/git-extra-config.go
Expand Up @@ -6,6 +6,7 @@ import (
"os/exec"
"path/filepath"

"github.com/aliyun/git-repo-go/file"
"github.com/aliyun/git-repo-go/path"
"github.com/jiangxin/goconfig"
log "github.com/jiangxin/multi-log"
Expand Down Expand Up @@ -94,7 +95,7 @@ func saveExtraGitConfig() error {
}
}

file, err := os.OpenFile(lockfile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644)
file, err := file.New(lockfile).OpenCreateRewrite()
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion config/repo-config.go
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path/filepath"

"github.com/aliyun/git-repo-go/file"
log "github.com/jiangxin/multi-log"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func InstallRepoConfig() error {
}
}

file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
file, err := file.New(filename).OpenCreateRewrite()
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions config/repo-config_test.go
@@ -1,8 +1,8 @@
package config

import (
"github.com/aliyun/git-repo-go/file"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)
Expand Down Expand Up @@ -38,7 +38,7 @@ func Test_InstallRepoConfig(t *testing.T) {
filename := filepath.Join(configDir, DefaultGitRepoConfigFile+".yml.example")
assert.NoError(t, err)

fileOpen, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
fileOpen, err := file.New(filename).OpenCreateRewrite()
assert.NoError(t, err)
defer fileOpen.Close()

Expand All @@ -48,7 +48,7 @@ func Test_InstallRepoConfig(t *testing.T) {

InstallRepoConfig()

fileRead, err := os.OpenFile(filename, os.O_RDONLY, 0644)
fileRead, err := file.New(filename).Open()
assert.NoError(t, err)
defer fileRead.Close()

Expand Down
104 changes: 104 additions & 0 deletions file/file.go
@@ -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)
}
9 changes: 5 additions & 4 deletions project/init.go
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"

"github.com/aliyun/git-repo-go/file"
"github.com/aliyun/git-repo-go/path"
)

Expand Down Expand Up @@ -78,12 +79,12 @@ func (v *Repository) initMissing() error {
}
}

for file, content := range files {
file = filepath.Join(v.GitDir, file)
if _, err = os.Stat(file); err == nil {
for name, content := range files {
name = filepath.Join(v.GitDir, name)
if _, err = os.Stat(name); err == nil {
continue
}
f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, 0644)
f, err := file.New(name).OpenCreateRewrite()
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion project/local-half.go
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/aliyun/git-repo-go/cap"
"github.com/aliyun/git-repo-go/config"
"github.com/aliyun/git-repo-go/file"
"github.com/aliyun/git-repo-go/helper"
"github.com/aliyun/git-repo-go/path"
log "github.com/jiangxin/multi-log"
Expand Down Expand Up @@ -448,7 +449,7 @@ func (v Project) CopyFile(src, dest string) error {
}
defer srcFile.Close()

destFile, err := os.OpenFile(destAbs, os.O_RDWR|os.O_CREATE, finfo.Mode())
destFile, err := file.New(destAbs).SetPerm(finfo.Mode()).OpenCreateRewrite()
if err != nil {
return fmt.Errorf("fail to open '%s': %s", destAbs, err)
}
Expand Down
3 changes: 2 additions & 1 deletion project/network-half.go
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"

"github.com/aliyun/git-repo-go/file"
"github.com/aliyun/git-repo-go/path"
log "github.com/jiangxin/multi-log"
)
Expand Down Expand Up @@ -39,7 +40,7 @@ func (v *Repository) Fetch(remote string, o *FetchOptions) error {
os.MkdirAll(filepath.Dir(altFile), 0755)

var f *os.File
f, err = os.OpenFile(altFile, os.O_CREATE|os.O_RDWR, 0644)
f, err = file.New(altFile).OpenCreateRewrite()
defer f.Close()
if err == nil {
target := filepath.Join(v.Reference, "objects")
Expand Down
3 changes: 2 additions & 1 deletion project/repository.go
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/aliyun/git-repo-go/config"
"github.com/aliyun/git-repo-go/file"
"github.com/aliyun/git-repo-go/manifest"
"github.com/aliyun/git-repo-go/path"
"github.com/jiangxin/goconfig"
Expand Down Expand Up @@ -122,7 +123,7 @@ func (v Repository) setAlternates(reference string) {
altFile := filepath.Join(v.GitDir, "objects", "info", "alternates")
os.MkdirAll(filepath.Dir(altFile), 0755)
var f *os.File
f, err = os.OpenFile(altFile, os.O_CREATE|os.O_RDWR, 0644)
f, err = file.New(altFile).OpenCreateRewrite()
defer f.Close()
if err == nil {
relPath := filepath.Join(reference, "objects")
Expand Down

0 comments on commit b387439

Please sign in to comment.