|
| 1 | +/* |
| 2 | +Copyright 2018 Google, Inc. All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package pkgutil |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "io" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/sirupsen/logrus" |
| 28 | +) |
| 29 | + |
| 30 | +// Directory stores a representation of a file directory. |
| 31 | +type Directory struct { |
| 32 | + Root string |
| 33 | + Content []string |
| 34 | +} |
| 35 | + |
| 36 | +type DirectoryEntry struct { |
| 37 | + Name string |
| 38 | + Size int64 |
| 39 | +} |
| 40 | + |
| 41 | +func GetSize(path string) int64 { |
| 42 | + stat, err := os.Lstat(path) |
| 43 | + if err != nil { |
| 44 | + logrus.Errorf("Could not obtain size for %s: %s", path, err) |
| 45 | + return -1 |
| 46 | + } |
| 47 | + if stat.IsDir() { |
| 48 | + size, err := getDirectorySize(path) |
| 49 | + if err != nil { |
| 50 | + logrus.Errorf("Could not obtain directory size for %s: %s", path, err) |
| 51 | + } |
| 52 | + return size |
| 53 | + } |
| 54 | + return stat.Size() |
| 55 | +} |
| 56 | + |
| 57 | +// GetFileContents returns the contents of a file at the specified path |
| 58 | +func GetFileContents(path string) (*string, error) { |
| 59 | + if _, err := os.Lstat(path); os.IsNotExist(err) { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + contents, err := ioutil.ReadFile(path) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + strContents := string(contents) |
| 69 | + //If file is empty, return nil |
| 70 | + if strContents == "" { |
| 71 | + return nil, nil |
| 72 | + } |
| 73 | + return &strContents, nil |
| 74 | +} |
| 75 | + |
| 76 | +func getDirectorySize(path string) (int64, error) { |
| 77 | + var size int64 |
| 78 | + err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { |
| 79 | + if !info.IsDir() { |
| 80 | + size += info.Size() |
| 81 | + } |
| 82 | + return err |
| 83 | + }) |
| 84 | + return size, err |
| 85 | +} |
| 86 | + |
| 87 | +// GetDirectoryContents converts the directory starting at the provided path into a Directory struct. |
| 88 | +func GetDirectory(path string, deep bool) (Directory, error) { |
| 89 | + var directory Directory |
| 90 | + directory.Root = path |
| 91 | + var err error |
| 92 | + if deep { |
| 93 | + walkFn := func(currPath string, info os.FileInfo, err error) error { |
| 94 | + newContent := strings.TrimPrefix(currPath, directory.Root) |
| 95 | + if newContent != "" { |
| 96 | + directory.Content = append(directory.Content, newContent) |
| 97 | + } |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + err = filepath.Walk(path, walkFn) |
| 102 | + } else { |
| 103 | + contents, err := ioutil.ReadDir(path) |
| 104 | + if err != nil { |
| 105 | + return directory, err |
| 106 | + } |
| 107 | + |
| 108 | + for _, file := range contents { |
| 109 | + fileName := "/" + file.Name() |
| 110 | + directory.Content = append(directory.Content, fileName) |
| 111 | + } |
| 112 | + } |
| 113 | + return directory, err |
| 114 | +} |
| 115 | + |
| 116 | +func GetDirectoryEntries(d Directory) []DirectoryEntry { |
| 117 | + return CreateDirectoryEntries(d.Root, d.Content) |
| 118 | +} |
| 119 | + |
| 120 | +func CreateDirectoryEntries(root string, entryNames []string) (entries []DirectoryEntry) { |
| 121 | + for _, name := range entryNames { |
| 122 | + entryPath := filepath.Join(root, name) |
| 123 | + size := GetSize(entryPath) |
| 124 | + |
| 125 | + entry := DirectoryEntry{ |
| 126 | + Name: name, |
| 127 | + Size: size, |
| 128 | + } |
| 129 | + entries = append(entries, entry) |
| 130 | + } |
| 131 | + return entries |
| 132 | +} |
| 133 | + |
| 134 | +func CheckSameSymlink(f1name, f2name string) (bool, error) { |
| 135 | + link1, err := os.Readlink(f1name) |
| 136 | + if err != nil { |
| 137 | + return false, err |
| 138 | + } |
| 139 | + link2, err := os.Readlink(f2name) |
| 140 | + if err != nil { |
| 141 | + return false, err |
| 142 | + } |
| 143 | + return (link1 == link2), nil |
| 144 | +} |
| 145 | + |
| 146 | +func CheckSameFile(f1name, f2name string) (bool, error) { |
| 147 | + // Check first if files differ in size and immediately return |
| 148 | + f1stat, err := os.Lstat(f1name) |
| 149 | + if err != nil { |
| 150 | + return false, err |
| 151 | + } |
| 152 | + f2stat, err := os.Lstat(f2name) |
| 153 | + if err != nil { |
| 154 | + return false, err |
| 155 | + } |
| 156 | + |
| 157 | + if f1stat.Size() != f2stat.Size() { |
| 158 | + return false, nil |
| 159 | + } |
| 160 | + |
| 161 | + // Next, check file contents |
| 162 | + f1, err := ioutil.ReadFile(f1name) |
| 163 | + if err != nil { |
| 164 | + return false, err |
| 165 | + } |
| 166 | + f2, err := ioutil.ReadFile(f2name) |
| 167 | + if err != nil { |
| 168 | + return false, err |
| 169 | + } |
| 170 | + |
| 171 | + if !bytes.Equal(f1, f2) { |
| 172 | + return false, nil |
| 173 | + } |
| 174 | + return true, nil |
| 175 | +} |
| 176 | + |
| 177 | +// HasFilepathPrefix checks if the given file path begins with prefix |
| 178 | +func HasFilepathPrefix(path, prefix string) bool { |
| 179 | + path = filepath.Clean(path) |
| 180 | + prefix = filepath.Clean(prefix) |
| 181 | + pathArray := strings.Split(path, "/") |
| 182 | + prefixArray := strings.Split(prefix, "/") |
| 183 | + |
| 184 | + if len(pathArray) < len(prefixArray) { |
| 185 | + return false |
| 186 | + } |
| 187 | + for index := range prefixArray { |
| 188 | + if prefixArray[index] == pathArray[index] { |
| 189 | + continue |
| 190 | + } |
| 191 | + return false |
| 192 | + } |
| 193 | + return true |
| 194 | +} |
| 195 | + |
| 196 | +// given a path to a directory, check if it has any contents |
| 197 | +func DirIsEmpty(path string) (bool, error) { |
| 198 | + f, err := os.Open(path) |
| 199 | + if err != nil { |
| 200 | + return false, err |
| 201 | + } |
| 202 | + defer f.Close() |
| 203 | + |
| 204 | + _, err = f.Readdir(1) |
| 205 | + if err == io.EOF { |
| 206 | + return true, nil |
| 207 | + } |
| 208 | + return false, err |
| 209 | +} |
| 210 | + |
| 211 | +// CleanFilePath removes characters from a given path that cannot be used |
| 212 | +// in paths by the underlying platform (e.g. Windows) |
| 213 | +func CleanFilePath(dirtyPath string) string { |
| 214 | + var windowsReplacements = []string{"<", "_", ">", "_", ":", "_", "?", "_", "*", "_", "?", "_", "|", "_"} |
| 215 | + replacer := strings.NewReplacer(windowsReplacements...) |
| 216 | + return filepath.Clean(replacer.Replace(dirtyPath)) |
| 217 | +} |
0 commit comments