Compare commits

...

2 Commits

Author SHA1 Message Date
bb61c0c727 Read arguments from command line 2025-07-21 12:04:44 +03:00
0d82e0d6a1 Add some constants 2025-07-21 12:01:30 +03:00

38
main.go
View File

@@ -19,11 +19,29 @@ import (
)
const fileQueueLen = 100
const fileProcessingDelay = 500 * time.Millisecond
const moveAttempts = 100
func main() {
watchDir := "/home/av/temp/inbox"
destDir := "/home/av/temp/dest"
os.MkdirAll(destDir, 0755)
// Проверка аргументов командной строки
if len(os.Args) < 3 {
log.Fatalf("Usage: %s <watch_dir> <dest_dir>", os.Args[0])
}
watchDir := os.Args[1]
destDir := os.Args[2]
// Проверка существования watchDir
if _, err := os.Stat(watchDir); os.IsNotExist(err) {
log.Fatalf("Watch directory does not exist: %s", watchDir)
} else if err != nil {
log.Fatalf("Error accessing watch directory: %v", err)
}
// Создание destDir если не существует
if err := os.MkdirAll(destDir, 0755); err != nil {
log.Fatalf("Failed to create destination directory: %v", err)
}
counterFile, err := xdg.DataFile("filemover/counter")
if err != nil {
@@ -137,13 +155,13 @@ func processFile(filePath, destDir, counterPath string, counter *int) {
}
// Ожидание завершения записи
time.Sleep(500 * time.Millisecond)
time.Sleep(fileProcessingDelay)
moveAttempt := 0
attempt := 0
for {
if moveAttempt > 100 {
log.Printf("Moving failed after %d attempts, see messages", moveAttempt)
if attempt >= moveAttempts {
log.Printf("Moving failed after %d attempts, see messages", attempt)
break
}
@@ -156,19 +174,19 @@ func processFile(filePath, destDir, counterPath string, counter *int) {
_, err := os.Stat(destPath)
if err == nil {
log.Printf("Moving failed, file already exists: %s", destPath)
moveAttempt++
attempt++
continue
}
if !errors.Is(err, fs.ErrNotExist) {
log.Printf("Moving failed: %v", err)
moveAttempt++
attempt++
continue
}
if err := os.Rename(filePath, destPath); err != nil {
log.Printf("Moving failed: %v", err)
moveAttempt++
attempt++
continue
}