This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/gps: refactor filesystemState and functions
This commit moves filesystemState from being a test struct to become part of gps. It'll beused to optimize the prune functions by calculating the filesystem state and determining the files to prune in-memory. Signed-off-by: Ibrahim AshShohail <ibra.sho@gmail.com>
- Loading branch information
Showing
2 changed files
with
107 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2017 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package gps | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// filesystemState represents the state of a file system. | ||
type filesystemState struct { | ||
root string | ||
dirs []string | ||
files []string | ||
links []fsLink | ||
} | ||
|
||
// fsLink represents a symbolic link. | ||
type fsLink struct { | ||
path string | ||
to string | ||
} | ||
|
||
// deriveFilesystemState returns a filesystemState based on the state of | ||
// the filesystem on root. | ||
func deriveFilesystemState(root string) (filesystemState, error) { | ||
fs := filesystemState{ | ||
root: root, | ||
} | ||
|
||
err := filepath.Walk(fs.root, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if path == fs.root { | ||
return nil | ||
} | ||
|
||
relPath, err := filepath.Rel(fs.root, path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if (info.Mode() & os.ModeSymlink) != 0 { | ||
eval, err := filepath.EvalSymlinks(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fs.links = append(fs.links, fsLink{ | ||
path: relPath, | ||
to: eval, | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
if info.IsDir() { | ||
fs.dirs = append(fs.dirs, relPath) | ||
|
||
return nil | ||
} | ||
|
||
fs.files = append(fs.files, relPath) | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return filesystemState{}, err | ||
} | ||
|
||
return fs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters