Debian Codegraph @

browserpass-native/request/list.go

raw
1 package request
2
3 import (
4 "path/filepath"
5 "sort"
6 "strings"
7
8 "github.com/browserpass/browserpass-native/errors"
9 "github.com/browserpass/browserpass-native/response"
10 "github.com/mattn/go-zglob"
11 log "github.com/sirupsen/logrus"
12 )
13
14 func listFiles(request *request) {
15 responseData := response.MakeListResponse()
16
17 for _, store := range request.Settings.Stores {
18 normalizedStorePath, err := normalizePasswordStorePath(store.Path)
19 if err != nil {
20 log.Errorf(
21 "The password store '%+v' is not accessible at its location: %+v",
22 store, err,
23 )
24 response.SendErrorAndExit(
25 errors.CodeInaccessiblePasswordStore,
26 &map[errors.Field]string{
27 errors.FieldMessage: "The password store is not accessible",
28 errors.FieldAction: "list",
29 errors.FieldError: err.Error(),
30 errors.FieldStoreID: store.ID,
31 errors.FieldStoreName: store.Name,
32 errors.FieldStorePath: store.Path,
33 },
34 )
35 }
36
37 store.Path = normalizedStorePath
38
39 files, err := zglob.GlobFollowSymlinks(filepath.Join(store.Path, "/**/*.gpg"))
40 if err != nil {
41 log.Errorf(
42 "Unable to list the files in the password store '%+v' at its location: %+v",
43 store, err,
44 )
45 response.SendErrorAndExit(
46 errors.CodeUnableToListFilesInPasswordStore,
47 &map[errors.Field]string{
48 errors.FieldMessage: "Unable to list the files in the password store",
49 errors.FieldAction: "list",
50 errors.FieldError: err.Error(),
51 errors.FieldStoreID: store.ID,
52 errors.FieldStoreName: store.Name,
53 errors.FieldStorePath: store.Path,
54 },
55 )
56 }
57
58 for i, file := range files {
59 relativePath, err := filepath.Rel(store.Path, file)
60 if err != nil {
61 log.Errorf(
62 "Unable to determine the relative path for a file '%v' in the password store '%+v': %+v",
63 file, store, err,
64 )
65 response.SendErrorAndExit(
66 errors.CodeUnableToDetermineRelativeFilePathInPasswordStore,
67 &map[errors.Field]string{
68 errors.FieldMessage: "Unable to determine the relative path for a file in the password store",
69 errors.FieldAction: "list",
70 errors.FieldError: err.Error(),
71 errors.FieldFile: file,
72 errors.FieldStoreID: store.ID,
73 errors.FieldStoreName: store.Name,
74 errors.FieldStorePath: store.Path,
75 },
76 )
77 }
78 files[i] = strings.Replace(relativePath, "\\", "/", -1) // normalize Windows paths
79 }
80
81 sort.Strings(files)
82 responseData.Files[store.ID] = files
83 }
84
85 response.SendOk(responseData)
86 }