我正在编写一个程序来映射非常大的文件系统(超过100TB)。它使用fd列出文件/目录。最后,该程序应该制作文本文件(稍后将进行合并),该文本文件包含我执行该文件以来文件系统上所有内容的路径。问题在于它会不断循环并将相同的输出打印到文件中,因此我认为错误是在声明/重写某些变量的某个地方。你觉得呢?
package main
import (
"strconv"
"os/exec"
"bufio"
"strings"
"fmt"
"bytes"
"log"
"os"
)
// declaring variables
var count int
var start_dir string
var cmd *exec.Cmd
var sizeof_dir_array int
var counter int
var index_output string
var dirs string
var out bytes.Buffer
var stderr bytes.Buffer
var byte_out []byte
var err error
var err1 error
var dir_array []string
func find_indexes(starting_directory string) {
start_dir = starting_directory
sizeof_dir_array = 0
counter = 0
index_output = ""
// deciding to use start_dir or not
if start_dir != "" {
// declaring byte output that contain file indexes and issigning it to index_output variable
byte_out, err = exec.Command("fd", "-d", "1", "-a", ".", start_dir).Output()
if err != nil {
log.Fatal(err)
}
index_output = string(byte_out)
// issigning string containing directories to cmd variable
cmd = exec.Command("fd", "-d", "1", "-a", "-t", "d", ".", start_dir)
}else {
byte_out, err = exec.Command("fd", "-d", "1", "-a").Output()
if err != nil {
log.Fatal(err)
}
index_output = string(byte_out)
cmd = exec.Command("fd", "-d", "1", "-a", "-t", "d")
}
// parsing from cmd to dirs variable
cmd.Stdout = &out
cmd.Stderr = &stderr
err1 = cmd.Run()
if err1 != nil {
fmt.Println(fmt.Sprint(err1) + ": " + stderr.String())
return
}
dirs = out.String()
// writing index_output to a file
file, err2 := os.Create("{your_path}/index-" + strconv.Itoa(count) + ".txt")
if err2 != nil {
log.Fatal("Cannot create file", err2)
}
count++
fmt.Fprintf(file, index_output)
file.Close()
// putting dirs from string to dir_array
scanner := bufio.NewScanner(strings.NewReader(string(dirs)))
for scanner.Scan() {
sizeof_dir_array++
}
dir_array = make([]string, sizeof_dir_array)
for scanner.Scan() {
dir_array[counter] = scanner.Text()
counter++
}
// if there are no more dirs, recursion continues
if dirs != "" {
for _, val := range dir_array {
find_indexes(val)
}
}
}
func main() {
count = 0
find_indexes("")
}