github.com/robhaswell/grandperspective-scan@v0.1.0/gpscan.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/deckarep/golang-set"
    11  )
    12  
    13  func main() {
    14  	flag.Parse()
    15  
    16  	dir := flag.Arg(0)
    17  	out := flag.Arg(1)
    18  
    19  	if dir == "" {
    20  		usage("Scan directory not provided")
    21  	}
    22  
    23  	if out == "" {
    24  		usage("Output file not provided")
    25  	}
    26  
    27  	dirInfo, err := os.Stat(dir)
    28  	if err != nil {
    29  		if os.IsNotExist(err) {
    30  			usage("Scan directory does not exist")
    31  		} else {
    32  			usage(err.Error())
    33  		}
    34  	} else {
    35  		if !dirInfo.IsDir() {
    36  			usage("Provided scan path is not a directory")
    37  		}
    38  	}
    39  
    40  	dir, err = filepath.Abs(dir)
    41  	if err != nil {
    42  		usage(err.Error())
    43  	}
    44  	dirLen := len(dir)
    45  
    46  	var outFile *os.File
    47  
    48  	if out == "-" {
    49  		outFile = os.Stdout
    50  	} else {
    51  		outFile, err = os.Create(out)
    52  		if err != nil {
    53  			usage(err.Error())
    54  		}
    55  		defer outFile.Close()
    56  	}
    57  
    58  	outFile.WriteString(`<?xml version="1.0" encoding="UTF-8"?>` + "\n")
    59  	outFile.WriteString(`<GrandPerspectiveScanDump appVersion="1.8.1" formatVersion="5">` + "\n")
    60  	outFile.WriteString(`<ScanInfo volumePath="` + dir + `" volumeSize="0" freeSpace="0" scanTime="1970-01-01T00:00:00Z" fileSizeMeasure="logical">` + "\n")
    61  
    62  	lastDirSet := mapset.NewSet()
    63  
    64  	walkFunc := func(path string, info os.FileInfo, err error) error {
    65  		path = path[dirLen:]
    66  		if info.IsDir() {
    67  			curDirSet := mapset.NewSet()
    68  			dirParts := strings.Split(path, "/")
    69  			_path := ""
    70  			for _, dirName := range dirParts {
    71  				_path += dirName + "/"
    72  				curDirSet.Add(_path)
    73  			}
    74  
    75  			isSubDir := curDirSet.IsSuperset(lastDirSet)
    76  
    77  			if !isSubDir {
    78  				upLevels := lastDirSet.Difference(curDirSet).Cardinality()
    79  				for i := 0; i < upLevels; i++ {
    80  					outFile.WriteString("</Folder>\n")
    81  				}
    82  
    83  			}
    84  			lastDirSet = curDirSet
    85  
    86  			outFile.WriteString(fmt.Sprintf(
    87  				`<Folder name="%s" created="1970-01-01T00:00:00Z" modified="1970-01-01T00:00:00Z" accessed="1970-01-01T00:00:00Z">`+"\n", info.Name()))
    88  		} else {
    89  			outFile.WriteString(fmt.Sprintf(
    90  				`<File name="%s" size="%d" created="1970-01-01T00:00:00Z" modified="1970-01-01T00:00:00Z" accessed="1970-01-01T00:00:00Z"/>`+"\n",
    91  				info.Name(), info.Size()))
    92  		}
    93  		return nil
    94  	}
    95  
    96  	filepath.Walk(dir, walkFunc)
    97  
    98  	upLevels := lastDirSet.Cardinality()
    99  	for i := 0; i < upLevels; i++ {
   100  		outFile.WriteString("</Folder>\n")
   101  	}
   102  	outFile.WriteString("</ScanInfo>\n")
   103  	outFile.WriteString("</GrandPerspectiveScanDump>\n")
   104  }
   105  
   106  func usage(message string) {
   107  	cmd := os.Args[0]
   108  	fmt.Print("Error: " + message + "\n\nUsage: " + cmd + " dir outfile\n")
   109  	os.Exit(1)
   110  }