This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchiveFilesByFileSize.go
112 lines (101 loc) · 3.22 KB
/
archiveFilesByFileSize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
)
//#####################################################
// run function gets size of files should be archived, destination and source folder
func runByFileSize(size int64, dest string, source string) {
//check if destination exists, source check in searchFileByFileSize
if _, err := os.Stat(dest); os.IsNotExist(err) {
message(1, "Destination does not exist!")
fmt.Println(err)
return
}
//analyze which files should be archived
filesToArchive, filenames, err := searchFileByFileSize(size, source)
if err != nil {
fmt.Println(err)
message(1, "")
return
}
randPrefix := GenerateRandomString()
var accept string
fmt.Printf("Archiving will now start and cannot be cancelled!\nconfirm (y/n)\n\n")
_, err = fmt.Scanln(&accept)
if accept == "y" && err == nil {
//archive this files
err = archive(filesToArchive, filenames, dest, randPrefix)
if err != nil {
fmt.Println(err)
message(1, "")
return
}
//proof if symlinks successfully set and files archived
isArchived, err := proveSymLink(filesToArchive, filenames, dest, randPrefix)
if isArchived && err == nil {
fmt.Println("[runByFileSize] Successfully archived!")
} else {
fmt.Println("[ERROR - runFileSize] While proving symlinks")
fmt.Println(err)
message(1, "")
return
}
//if all done, make success
message(0, "")
} else if accept == "n" && err == nil {
message(4, "User Interrupt")
} else {
log.Fatal(err)
}
}
//#####################################################
//#####################################################
// analyze which files should be archived, gets size of files should be archived and source folder
// returns a string array of filenames and error if occoured
func searchFileByFileSize(size int64, source string) ([]string, []string, error) {
fmt.Printf("[searchFileByFileSize] size: %d Byte; source: %s\n", size, source)
var filesToArchiveWithPath []string
var fileNames []string
if whichOS == "windows" {
source = filepath.FromSlash(source) //only for windows
}
var totalFileSize int64
filesCount := 0
if _, err := os.Stat(source); os.IsNotExist(err) {
message(1, "Source does not exist!")
return nil, nil, err
}
err := filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if info.Size() >= size {
fi, err := os.Lstat(path)
if err != nil {
message(1, "[searchFileByFileSize]")
return err
}
if (os.ModeSymlink&fi.Mode()) != 0 || fi.Mode().IsDir() { //check if symlink
return nil
} else {
filesToArchiveWithPath = append(filesToArchiveWithPath, path)
fileNames = append(fileNames, info.Name())
totalFileSize += info.Size() / 1000000
filesCount++
message(0, "[searchFileByFileSize]", "File: \""+path+"\" with size: "+strconv.FormatInt(info.Size()/1000000, 10)+" MB")
}
}
return nil
})
if err != nil {
return nil, nil, err
}
if filesCount == 0 {
return nil, nil, errors.New("[searchFileByFileSize]: Nothing to move")
}
message(0, "[searchFileByFileSize]", "Successfully analyzed!", "Total File Size: "+strconv.FormatInt(totalFileSize, 10)+" MB")
return filesToArchiveWithPath, fileNames, nil
}
//#####################################################