Compare commits
2 Commits
951dff84fa
...
bb61c0c727
Author | SHA1 | Date | |
---|---|---|---|
bb61c0c727
|
|||
0d82e0d6a1
|
38
main.go
38
main.go
@@ -19,11 +19,29 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const fileQueueLen = 100
|
const fileQueueLen = 100
|
||||||
|
const fileProcessingDelay = 500 * time.Millisecond
|
||||||
|
const moveAttempts = 100
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
watchDir := "/home/av/temp/inbox"
|
// Проверка аргументов командной строки
|
||||||
destDir := "/home/av/temp/dest"
|
if len(os.Args) < 3 {
|
||||||
os.MkdirAll(destDir, 0755)
|
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")
|
counterFile, err := xdg.DataFile("filemover/counter")
|
||||||
if err != nil {
|
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 {
|
for {
|
||||||
if moveAttempt > 100 {
|
if attempt >= moveAttempts {
|
||||||
log.Printf("Moving failed after %d attempts, see messages", moveAttempt)
|
log.Printf("Moving failed after %d attempts, see messages", attempt)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,19 +174,19 @@ func processFile(filePath, destDir, counterPath string, counter *int) {
|
|||||||
_, err := os.Stat(destPath)
|
_, err := os.Stat(destPath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Printf("Moving failed, file already exists: %s", destPath)
|
log.Printf("Moving failed, file already exists: %s", destPath)
|
||||||
moveAttempt++
|
attempt++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if !errors.Is(err, fs.ErrNotExist) {
|
if !errors.Is(err, fs.ErrNotExist) {
|
||||||
log.Printf("Moving failed: %v", err)
|
log.Printf("Moving failed: %v", err)
|
||||||
moveAttempt++
|
attempt++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.Rename(filePath, destPath); err != nil {
|
if err := os.Rename(filePath, destPath); err != nil {
|
||||||
log.Printf("Moving failed: %v", err)
|
log.Printf("Moving failed: %v", err)
|
||||||
moveAttempt++
|
attempt++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user