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 prune and filesystem functions
This commit moves filesystemState from being a test struct to become part of gps. It's used to optimize the prune function by calculting the filesystem state and determining the files to prune in-memory. Signed-off-by: Ibrahim AshShohail <[email protected]>
- Loading branch information
Showing
8 changed files
with
398 additions
and
347 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
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 | ||
} | ||
|
||
// calculateFilesystemState returns a filesystemState based on the state of | ||
// the filesystem on root. | ||
func calculateFilesystemState(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
Oops, something went wrong.