Prevent files from being overridden

This commit is contained in:
2024-08-12 00:35:14 +02:00
parent 5bb26c9e15
commit 1d2a7aac17
2 changed files with 39 additions and 62 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
main.exe

100
main.go
View File

@@ -12,71 +12,33 @@ import (
) )
func init() { func init() {
log.SetFlags(log.Lmicroseconds) log.SetFlags(log.Lmicroseconds | log.Lshortfile)
} }
func main() { func main() {
recreate := flag.Bool("f", false, "First rename all files to <name>.bak and then rename them to iter (to prevent conflicts of already existing files)")
flag.Parse() flag.Parse()
originalExtensions := make(map[string]string)
var mapMutex sync.RWMutex
var wg sync.WaitGroup var wg sync.WaitGroup
var renamedFiles map[string]struct{} = make(map[string]struct{})
var mapMutex sync.RWMutex
extensionRegex := regexp.MustCompile(`\.[a-zA-Z0-9]+$`) extensionRegex := regexp.MustCompile(`\.[a-zA-Z0-9]+$`)
if *recreate {
log.Printf("Recreating all files first")
for _, file := range flag.Args() {
wg.Add(1)
go func(file string) {
defer wg.Done()
_, err := os.Stat(file)
if os.IsNotExist(err) {
log.Printf("File %s does not exist", file)
return
}
res := extensionRegex.FindAllString(file, -1)
if len(res) == 0 {
log.Printf("No extension found for file %s", file)
return
}
dir := path.Dir(file)
extension := res[0]
bckpFile := fmt.Sprintf("%s/%s.bak", dir, file)
_, err = os.Stat(bckpFile)
if !os.IsNotExist(err) {
log.Printf("File %s already exists", bckpFile)
return
}
log.Printf("%s -> %s", file, bckpFile)
err = os.Rename(file, bckpFile)
if err != nil {
log.Printf("Error renaming file %s to %s: %s", file, bckpFile, err)
}
mapMutex.Lock()
originalExtensions[bckpFile] = extension
mapMutex.Unlock()
}(file)
}
}
wg.Wait() wg.Wait()
wg = sync.WaitGroup{} wg = sync.WaitGroup{}
for _, file := range flag.Args() { for _, file := range flag.Args() {
wg.Add(1) wg.Add(1)
go func(file string) { go func(file string) {
defer wg.Done() defer wg.Done()
if *recreate { file = path.Clean(file)
file = file + ".bak" log.Printf("Checking file %s", file)
if !DoesFileExist(file) {
log.Printf("File %s does not exist", file)
return
} }
info, err := os.Stat(file) info, err := os.Stat(file)
if os.IsNotExist(err) { if info == nil {
log.Printf("File %s does not exist", file) log.Printf("Error checking file %s: %s", file, err)
return return
} }
@@ -89,36 +51,41 @@ func main() {
modtimeString := modtime.Local().Format("2006-01-02T15-04-05") modtimeString := modtime.Local().Format("2006-01-02T15-04-05")
newFile := modtimeString newFile := modtimeString
bakless := strings.TrimRight(file, ".bak") res := extensionRegex.FindAllString(file, -1)
res := extensionRegex.FindAllString(bakless, -1)
if len(res) == 0 { if len(res) == 0 {
log.Printf("No extension found for file %s", file) log.Printf("No extension found for file %s", file)
return return
} }
extension := res[0] extension := res[0]
mapMutex.RLock()
prevExtension, ok := originalExtensions[file]
mapMutex.RUnlock()
if ok {
extension = prevExtension
}
dir := path.Dir(file) dir := path.Dir(file)
newFile = fmt.Sprintf("%s/%s%s", dir, newFile, extension) newFile = fmt.Sprintf("%s/%s%s", dir, newFile, extension)
newFile = path.Clean(newFile) newFile = path.Clean(newFile)
newFile = strings.TrimRight(newFile, ".bak") newFile = strings.TrimRight(newFile, ".bak")
log.Printf("New file name for %s: %s", file, newFile)
_, err = os.Stat(newFile) if DoesFileExist(newFile) {
if os.IsExist(err) {
log.Printf("File %s already exists", newFile) log.Printf("File %s already exists", newFile)
return return
} else if !os.IsNotExist(err) { }
log.Printf("An unexpected error occurred: %v", err) log.Printf("%#v", newFile)
mapMutex.RLock()
_, ok := renamedFiles[newFile]
mapMutex.RUnlock()
if ok {
log.Printf("File %s already renamed", newFile)
return return
} }
if file == newFile {
log.Printf("File %s already has the correct name", file)
return
}
log.Printf("%s -> %s", file, newFile) log.Printf("%s -> %s", file, newFile)
mapMutex.Lock()
renamedFiles[newFile] = struct{}{}
mapMutex.Unlock()
err = os.Rename(file, newFile) err = os.Rename(file, newFile)
if err != nil { if err != nil {
log.Printf("Error renaming file %s to %s: %s", file, newFile, err) log.Printf("Error renaming file %s to %s: %s", file, newFile, err)
@@ -128,3 +95,12 @@ func main() {
} }
wg.Wait() wg.Wait()
} }
func DoesFileExist(input string) bool {
file, err := os.Open(input)
if err != nil {
return false
}
file.Close()
return true
}