// Recursively read a directory structure and create a tree structure out of it // TODO: fix symlinks/cyclic dependencies etc. func (fs StdFileSystem) readDir(curFile filesystem.File, parent *filesystem.FileTree) *filesystem.FileTree { tree := &filesystem.FileTree{} tree.StdFile = curFile tree.StdParentNode = parent // TODO: Symlink check not working... if curFile.IsDir() || curFile.Mode() == os.ModeSymlink { tree.StdChildNodes = make([]*filesystem.FileTree, 0) dirListing, _ := fs.Dir(curFile.Path()) if dirListing != nil && len(dirListing) > 0 { for _, file := range dirListing { tree.StdChildNodes = append(tree.StdChildNodes, fs.readDir(file, tree)) } } } return tree }
// Create a File struct for a new, possibly (likely) non-existent file // but based on the attributes of a given, real file // This is handy for creating a target File for syncing to a destination // Note that this expects any protocol part of the path to be removed func MkToFile(fromBase string, toBase string, file fs.File) fs.File { // src: /foo/bar/baz/bat.txt // dest: /lol/ // target: /lol/bat.txt // src: /foo/bar/baz // dest: s3:///lol // target: s3:///lol/foo/bar/baz // TODO: This needs some work path := fmt.Sprintf("%s", strings.Replace(LinuxPath(file.Path()), LinuxPath(fromBase), LinuxPath(toBase), -1)) toFile := fs.File{ FileName: file.Name(), FilePath: path, FileMode: file.Mode(), FileSize: file.Size(), FileModTime: file.ModTime(), } return toFile }
func (fs StdFileSystem) MkDir(file filesystem.File) error { return os.MkdirAll(file.Path(), file.Mode()) }