-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal_file_generator.go
154 lines (137 loc) · 4.55 KB
/
local_file_generator.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"encoding/csv"
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
const (
defaultFilePath = "listOfSourceFiles.csv"
)
// ----------------------- Name Generation Logic -----------------------------------------------------------------------
const fileNameMaxSize = 64
var fileGeneratorAllowedCharacters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
var fileGeneratorFileExtensions = []string{"", ".sh", ".exe", ".cpp", ".java", ".py", ".go", ".mp3", ".mp4", ".pkg", ".jpeg", ".png",
".pdf", ".gzip", ".txt", ".dat", ".bat", ".xlsx", ".vhd", ".tar.gz", ".deb"}
var fileGeneratorFileSizes = []string{"1K", "10K", "100K", "1M", "10M", "100M", "1G", "10G"}
var fileGeneratorCumulativeProbability = []int{400, 700, 800, 900, 1000, 1010, 1011, 1011}
var fileGeneratorSeededRandom = rand.New(rand.NewSource(time.Now().UnixNano()))
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
if i == 0 {
b[i] = fileGeneratorAllowedCharacters[fileGeneratorSeededRandom.Intn(len(fileGeneratorAllowedCharacters)-10)]
} else {
b[i] = fileGeneratorAllowedCharacters[fileGeneratorSeededRandom.Intn(len(fileGeneratorAllowedCharacters))]
}
}
return string(b)
}
func fileNameAndSize() (string, string) {
fileSizeIndex := rand.Intn(fileGeneratorCumulativeProbability[len(fileGeneratorCumulativeProbability)-1])
fileSize := "1K"
for i := 0; i < len(fileGeneratorCumulativeProbability); i++ {
if fileSizeIndex <= fileGeneratorCumulativeProbability[i] {
fileSize = fileGeneratorFileSizes[i]
break
}
}
return randSeq(rand.Intn(fileNameMaxSize)+1) + fileGeneratorFileExtensions[rand.Intn(len(fileGeneratorFileExtensions))], fileSize
}
func folderName() string {
return randSeq(rand.Intn(fileNameMaxSize) + 1)
}
type fileEntity struct {
name string
parentPath string
sizeIfFile string
}
type folderEntity struct {
path string
}
//func saveFolderNamesToFile(folders *[]folderEntity, saveToPath string) {
// folderHandler, err := os.Create(saveToPath)
// if err != nil {
// fmt.Println(err.Error())
// }
// defer func(folders *os.File) {
// _ = folders.Close()
// }(folderHandler)
//
// writer := csv.NewWriter(folderHandler)
// defer writer.Flush()
// writer.Write([]string{"Name"})
// for _, folder := range *folders {
// writer.Write([]string{folder.path})
// }
// writer.Flush()
//}
func saveFileNamesToFile(entities *[]fileEntity, saveToPath string) {
folderHandler, err := os.Create(saveToPath)
if err != nil {
fmt.Println(err.Error())
}
defer func(folders *os.File) {
_ = folders.Close()
}(folderHandler)
writer := csv.NewWriter(folderHandler)
defer writer.Flush()
//writer.Write([]string{"Name", "Parent", "Size"})
for _, entity := range *entities {
_ = writer.Write([]string{entity.name, entity.parentPath, entity.sizeIfFile})
}
writer.Flush()
}
func generateFilesAndFolders(basePath string, numEntity, numFolders, maxEntityPerLevel int) (*[]fileEntity, *[]folderEntity) {
files, folders := make([]fileEntity, numEntity), make([]folderEntity, numFolders)
fileCountSoFar, filesGenerated, folderCountSoFar, foldersGenerated := 0, false, 0, false
for !filesGenerated || !foldersGenerated {
dirQueue := make([]string, 0)
dirQueue = append(dirQueue, basePath+folderName())
for len(dirQueue) > 0 {
dirSize := len(dirQueue)
for i := 0; i < dirSize; i++ {
relativePath := dirQueue[0]
dirQueue = dirQueue[1:]
folderCount := rand.Intn(maxEntityPerLevel) + 1
for j := 0; j < folderCount && !foldersGenerated; j++ {
dirName := relativePath + "/" + folderName()
dirQueue = append(dirQueue, dirName)
folders[folderCountSoFar] = folderEntity{path: dirName}
folderCountSoFar += 1
if folderCountSoFar >= numFolders {
foldersGenerated = true
break
}
}
fileCount := maxEntityPerLevel - folderCount
for j := 0; j < fileCount && !filesGenerated; j++ {
fileName, fileSize := fileNameAndSize()
files[fileCountSoFar] = fileEntity{
name: fileName,
parentPath: relativePath + "/",
sizeIfFile: fileSize,
}
fileCountSoFar += 1
if fileCountSoFar >= numEntity {
filesGenerated = true
break
}
}
}
}
}
return &files, &folders
}
func main() {
arguments := os.Args[1:]
var numEntity, numFolders, maxEntityPerLevel int
numEntity, _ = strconv.Atoi(arguments[0])
maxEntityPerLevel, _ = strconv.Atoi(arguments[1])
numFolders = numEntity / 10
generationPath := arguments[2]
files, _ := generateFilesAndFolders(generationPath, numEntity, numFolders, maxEntityPerLevel)
saveFileNamesToFile(files, defaultFilePath)
}