/
get_files.go
113 lines (98 loc) · 3.33 KB
/
get_files.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
package instrumenter
/*
Copyright (c) 2023, Erik Kassubek
All rights reserved.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Author: Erik Kassubek <erik-kassubek@t-online.de>
Package: dedego-instrumenter
Project: Dynamic Analysis to detect potential deadlocks in concurrent Go programs
*/
/*
get_files.go
Get all files from the input path
*/
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/ErikKassubek/deadlockDetectorGo/src/gui"
)
// get all files in in and write them into file_names
// create folder structure in out
/*
Function to get all files in the in folder and add there names to file_names.
The function also copies the folder structure into the output folder.
@param path string: path to the input folder
@param status *gui.Status: Status object
@return []string: list of file names
@return error: Error or nil
*/
func getAllFiles(status *gui.Status) ([]string, error) {
// remove old output folder
if status.Instrument {
err := os.RemoveAll(status.Output)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to remove old output folder %s.\n", status.Output)
return make([]string, 0), err
}
}
var file_names []string = make([]string, 0)
// get all file names
err := filepath.Walk(status.FolderPath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "Could not walk through file path %s.", path)
return err
}
// only save go, mod and sum files
if len(path) >= 4 && (path[len(path)-3:] == ".go" ||
path[len(path)-4:] == ".mod" || path[len(path)-4:] == ".sum") {
file_names = append(file_names, path)
}
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to walk through file path.\n")
return make([]string, 0), err
}
// get folder structure in path and copy it to out
folders := make([]string, 0)
in_split := strings.Split(status.FolderPath, string(os.PathSeparator))
folders = append(folders, status.Output+in_split[len(in_split)-2])
err = filepath.WalkDir(status.FolderPath,
func(path string, info fs.DirEntry, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "Could not walk through dir path %s.\n", path)
return err
}
if info.IsDir() && path[:1] != "." {
folders = append(folders, status.Output+get_relative_path(path, status))
}
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to walk through dir path.\n")
return make([]string, 0), err
}
for _, folder := range folders {
err := os.MkdirAll(folder, os.ModePerm)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create folder %s.\n", folder)
return make([]string, 0), err
}
}
return file_names, nil
}