func (fs StdFileSystem) FileTree(file filesystem.File) *filesystem.FileTree { if !file.IsDir() { return nil } tree := &filesystem.FileTree{} tree.StdFile = file return fs.readDir(file, tree) }
func (fs StdFileSystem) FileMap(file filesystem.File) filesystem.FileMap { if !file.IsDir() { return nil } tree := &filesystem.FileTree{} tree.StdFile = file fileMap, _ := filesystem.FileTreeToMap(*fs.FileTree(file), file.Path()) return fileMap }
func (fs StdFileSystem) Write(file filesystem.File, data []byte, perm os.FileMode) error { parentPath := filepath.Dir(file.Path()) if _, err := os.Stat(parentPath); err != nil { dir := filesystem.File{ FilePath: parentPath, FileMode: 0755, // This is obviously not good, need a neat way to get perms about... } fs.MkDir(dir) } return ioutil.WriteFile(file.Path(), data, perm) }
// 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 }
// 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 }
func (fs StdFileSystem) MkDir(file filesystem.File) error { return os.MkdirAll(file.Path(), file.Mode()) }
func (fs StdFileSystem) Read(f filesystem.File) ([]byte, error) { return ioutil.ReadFile(f.Path()) }