From 1d2a7aac172c1096d028227a251db655e0974ff5 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Mon, 12 Aug 2024 00:35:14 +0200 Subject: [PATCH] Prevent files from being overridden --- .gitignore | 1 + main.go | 100 ++++++++++++++++++++--------------------------------- 2 files changed, 39 insertions(+), 62 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f4ebb8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +main.exe diff --git a/main.go b/main.go index 15382d4..e116098 100644 --- a/main.go +++ b/main.go @@ -12,71 +12,33 @@ import ( ) func init() { - log.SetFlags(log.Lmicroseconds) + log.SetFlags(log.Lmicroseconds | log.Lshortfile) } func main() { - recreate := flag.Bool("f", false, "First rename all files to .bak and then rename them to iter (to prevent conflicts of already existing files)") flag.Parse() - - originalExtensions := make(map[string]string) - var mapMutex sync.RWMutex var wg sync.WaitGroup + var renamedFiles map[string]struct{} = make(map[string]struct{}) + var mapMutex sync.RWMutex 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 = sync.WaitGroup{} for _, file := range flag.Args() { wg.Add(1) go func(file string) { defer wg.Done() - if *recreate { - file = file + ".bak" + file = path.Clean(file) + log.Printf("Checking file %s", file) + + if !DoesFileExist(file) { + log.Printf("File %s does not exist", file) + return } info, err := os.Stat(file) - if os.IsNotExist(err) { - log.Printf("File %s does not exist", file) + if info == nil { + log.Printf("Error checking file %s: %s", file, err) return } @@ -89,36 +51,41 @@ func main() { modtimeString := modtime.Local().Format("2006-01-02T15-04-05") newFile := modtimeString - bakless := strings.TrimRight(file, ".bak") - res := extensionRegex.FindAllString(bakless, -1) + res := extensionRegex.FindAllString(file, -1) if len(res) == 0 { log.Printf("No extension found for file %s", file) return } extension := res[0] - mapMutex.RLock() - prevExtension, ok := originalExtensions[file] - mapMutex.RUnlock() - if ok { - extension = prevExtension - } - dir := path.Dir(file) newFile = fmt.Sprintf("%s/%s%s", dir, newFile, extension) newFile = path.Clean(newFile) newFile = strings.TrimRight(newFile, ".bak") + log.Printf("New file name for %s: %s", file, newFile) - _, err = os.Stat(newFile) - if os.IsExist(err) { + if DoesFileExist(newFile) { log.Printf("File %s already exists", newFile) 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 } + if file == newFile { + log.Printf("File %s already has the correct name", file) + return + } log.Printf("%s -> %s", file, newFile) + mapMutex.Lock() + renamedFiles[newFile] = struct{}{} + mapMutex.Unlock() err = os.Rename(file, newFile) if err != nil { log.Printf("Error renaming file %s to %s: %s", file, newFile, err) @@ -128,3 +95,12 @@ func main() { } wg.Wait() } + +func DoesFileExist(input string) bool { + file, err := os.Open(input) + if err != nil { + return false + } + file.Close() + return true +}