fix DryRun() for instruction/copy : create/remove file if does not exist, else try to open in WRITE mode

This commit is contained in:
Adrien Marquès 2018-11-14 20:21:34 +01:00
parent 1a7e1c2db0
commit fa8a0a5ae5
1 changed files with 7 additions and 7 deletions

View File

@ -54,16 +54,16 @@ func (d copy) DryRun(ctx ExecutionContext) ([]byte, error) {
} }
// 2. if destination to create : try to create (then remove) // 2. if destination to create : try to create (then remove)
if _, err := os.Stat(d.Src); os.IsNotExist(err) { if _, err := os.Stat(d.Dst); os.IsNotExist(err) {
file, err := os.OpenFile(d.Dst, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) file, err2 := os.OpenFile(d.Dst, os.O_APPEND|os.O_WRONLY|os.O_CREATE, os.FileMode(0777))
if err != nil { if err2 != nil {
return nil, fmt.Errorf("cannot copy '%s' to '%s' | %s", d.Src, d.Dst, err) return nil, fmt.Errorf("cannot copy to '%s' | %s", d.Dst, err2)
} }
file.Close() file.Close()
if err := os.Remove(d.Dst); err != nil { if err2 := os.Remove(d.Dst); err2 != nil {
return nil, fmt.Errorf("cannot tmp file at '%s' | %s", d.Dst, err) return nil, fmt.Errorf("cannot remove dry-run file '%s' | %s", d.Dst, err2)
} }
return nil, nil return nil, nil
@ -73,7 +73,7 @@ func (d copy) DryRun(ctx ExecutionContext) ([]byte, error) {
// 3. if destination exists : check write permission // 3. if destination exists : check write permission
file, err := os.OpenFile(d.Dst, os.O_APPEND|os.O_WRONLY, 0600) file, err := os.OpenFile(d.Dst, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot copy '%s' to '%s' | %s", d.Src, d.Dst, err) return nil, fmt.Errorf("cannot copy to '%s' | %s", d.Dst, err)
} }
file.Close() file.Close()