github.com/Cloud-Foundations/Dominator@v0.3.4/lib/fsutil/force.go (about) 1 package fsutil 2 3 import ( 4 "os" 5 "syscall" 6 ) 7 8 func forceLink(oldname, newname string) error { 9 err := os.Link(oldname, newname) 10 if err == nil { 11 return nil 12 } 13 if os.IsPermission(err) { 14 // Blindly attempt to remove immutable attributes. 15 MakeMutable(oldname, newname) 16 } else if os.IsExist(err) { 17 forceRemove(newname) 18 } 19 return os.Link(oldname, newname) 20 } 21 22 func forceRemove(name string) error { 23 err := os.Remove(name) 24 if err == nil { 25 return nil 26 } 27 if os.IsPermission(err) { 28 // Blindly attempt to remove immutable attributes. 29 MakeMutable(name) 30 } 31 return os.Remove(name) 32 } 33 34 func forceRemoveAll(path string) error { 35 err := os.RemoveAll(path) 36 if err == nil { 37 return nil 38 } 39 if os.IsPermission(err) { 40 // Blindly attempt to remove immutable attributes. 41 MakeMutable(path) 42 } 43 return os.RemoveAll(path) 44 } 45 46 func forceRename(oldpath, newpath string) error { 47 err := os.Rename(oldpath, newpath) 48 if err == nil { 49 return nil 50 } 51 if os.IsPermission(err) { 52 // Blindly attempt to remove immutable attributes. 53 MakeMutable(oldpath, newpath) 54 } else if os.IsExist(err) { 55 if err := ForceRemoveAll(newpath); err != nil { 56 return err 57 } 58 } else if err.(*os.LinkError).Err == syscall.EISDIR { 59 if err := ForceRemoveAll(newpath); err != nil { 60 return err 61 } 62 } 63 return os.Rename(oldpath, newpath) 64 }