aboutsummaryrefslogtreecommitdiff
path: root/filesystem
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@google.com>2020-05-09 15:15:12 -0700
committerGitHub <noreply@github.com>2020-05-09 15:15:12 -0700
commit338347ac4766f899fdc471d57f293798ff0e6c29 (patch)
tree8f5c0969a49a396d60c33a324834d92d9911a240 /filesystem
parent1aef2541a434bd9e88ebd52be72f13d56c5ef748 (diff)
parente68d65c440125ff1e47627abf1fc5a97f700d38d (diff)
Merge pull request #218 from ebiggers/cli-tests
Add tests for command-line interface Add tests that directly test the fscrypt command-line tool. See cli-tests/README.md for information about the test framework. The following test scripts are included: * t_change_passphrase * t_encrypt_custom * t_encrypt_login * t_encrypt_raw_key * t_encrypt * t_lock * t_not_enabled * t_not_supported * t_passphrase_hashing * t_setup * t_status * t_unlock * t_v1_policy_fs_keyring * t_v1_policy Unfortunately, we can't actually make Travis CI run these tests yet because they need kernel v5.4 or later, and Travis CI doesn't support an Ubuntu version that has that yet. But for now, they can be run manually using make cli-test.
Diffstat (limited to 'filesystem')
-rw-r--r--filesystem/filesystem.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go
index ecdeae1..e01f9ff 100644
--- a/filesystem/filesystem.go
+++ b/filesystem/filesystem.go
@@ -38,7 +38,9 @@ import (
"log"
"os"
"path/filepath"
+ "sort"
"strings"
+ "time"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
@@ -63,6 +65,11 @@ var (
ErrCorruptMetadata = util.SystemError("on-disk metadata is corrupt")
)
+// SortDescriptorsByLastMtime indicates whether descriptors are sorted by last
+// modification time when being listed. This can be set to true to get
+// consistent output for testing.
+var SortDescriptorsByLastMtime = false
+
// Mount contains information for a specific mounted filesystem.
// Path - Absolute path where the directory is mounted
// FilesystemType - Type of the mounted filesystem, e.g. "ext4"
@@ -534,6 +541,37 @@ func (m *Mount) ListPolicies() ([]string, error) {
return policies, m.err(err)
}
+type namesAndTimes struct {
+ names []string
+ times []time.Time
+}
+
+func (c namesAndTimes) Len() int {
+ return len(c.names)
+}
+
+func (c namesAndTimes) Less(i, j int) bool {
+ return c.times[i].Before(c.times[j])
+}
+
+func (c namesAndTimes) Swap(i, j int) {
+ c.names[i], c.names[j] = c.names[j], c.names[i]
+ c.times[i], c.times[j] = c.times[j], c.times[i]
+}
+
+func sortFileListByLastMtime(directoryPath string, names []string) error {
+ c := namesAndTimes{names: names, times: make([]time.Time, len(names))}
+ for i, name := range names {
+ fi, err := os.Lstat(filepath.Join(directoryPath, name))
+ if err != nil {
+ return err
+ }
+ c.times[i] = fi.ModTime()
+ }
+ sort.Sort(c)
+ return nil
+}
+
// listDirectory returns a list of descriptors for a metadata directory,
// including files which are links to other filesystem's metadata.
func (m *Mount) listDirectory(directoryPath string) ([]string, error) {
@@ -549,6 +587,12 @@ func (m *Mount) listDirectory(directoryPath string) ([]string, error) {
return nil, err
}
+ if SortDescriptorsByLastMtime {
+ if err := sortFileListByLastMtime(directoryPath, names); err != nil {
+ return nil, err
+ }
+ }
+
descriptors := make([]string, 0, len(names))
for _, name := range names {
// Be sure to include links as well