File tree Expand file tree Collapse file tree 1 file changed +8
-13
lines changed Expand file tree Collapse file tree 1 file changed +8
-13
lines changed Original file line number Diff line number Diff line change @@ -32,27 +32,22 @@ func PackFrom(snapshotName, sourceRoot string) error {
32
32
// if the file seems to exist and have existing content already.
33
33
// This is done to avoid accidental overwrites.
34
34
func OpenDestination (snapshotName string ) (* os.File , error ) {
35
- var f * os.File
36
- var err error
37
-
38
- if _ , err = os .Stat (snapshotName ); errors .Is (err , os .ErrNotExist ) {
39
- if f , err = os .Create (snapshotName ); err != nil {
35
+ f , err := os .OpenFile (snapshotName , os .O_WRONLY | os .O_CREATE | os .O_EXCL , 0600 )
36
+ if err != nil {
37
+ if ! errors .Is (err , os .ErrExist ) {
40
38
return nil , err
41
39
}
42
- } else if err != nil {
43
- return nil , err
44
- } else {
45
- f , err := os .OpenFile (snapshotName , os .O_WRONLY , 0600 )
40
+ fs , err := os .Stat (snapshotName )
46
41
if err != nil {
47
42
return nil , err
48
43
}
49
- fs , err := f .Stat ()
44
+ if fs .Size () > 0 {
45
+ return nil , fmt .Errorf ("file %s already exists and is of size > 0" , snapshotName )
46
+ }
47
+ f , err = os .OpenFile (snapshotName , os .O_WRONLY , 0600 )
50
48
if err != nil {
51
49
return nil , err
52
50
}
53
- if fs .Size () > 0 {
54
- return nil , fmt .Errorf ("File %s already exists and is of size >0" , snapshotName )
55
- }
56
51
}
57
52
return f , nil
58
53
}
You can’t perform that action at this time.
0 commit comments