SearchNPathRecursively walks up the directory tree starting at the given path, calling the Search function in each directory and adding each matching file that it finds to a list until it reaches the root directory or the length of the list exceeds n. On supported operating systems, it will also che
(path string, possibleFilenames []string, n int)
| 171 | // length of the list exceeds n. On supported operating systems, it will also |
| 172 | // check if the user ID of the directory changes and abort if it does. |
| 173 | func SearchNPathRecursively(path string, possibleFilenames []string, n int) ([]string, error) { |
| 174 | var paths []string |
| 175 | |
| 176 | owner, err := sysinfo.Owner(path) |
| 177 | if err != nil { |
| 178 | return nil, err |
| 179 | } |
| 180 | |
| 181 | for n == -1 || len(paths) < n { |
| 182 | fpath, err := SearchPath(path, possibleFilenames) |
| 183 | if err == nil { |
| 184 | paths = append(paths, fpath) |
| 185 | } |
| 186 | |
| 187 | // Get the parent path/user id |
| 188 | parentPath := filepath.Dir(path) |
| 189 | parentOwner, err := sysinfo.Owner(parentPath) |
| 190 | if err != nil { |
| 191 | return nil, err |
| 192 | } |
| 193 | |
| 194 | // If the user id of the directory changes indicate a permission error, otherwise |
| 195 | // the calling code will infer an error condition based on the accumulated |
| 196 | // contents of paths. |
| 197 | if path == parentPath { |
| 198 | return paths, nil |
| 199 | } else if parentOwner != owner { |
| 200 | return paths, os.ErrPermission |
| 201 | } |
| 202 | |
| 203 | owner = parentOwner |
| 204 | path = parentPath |
| 205 | } |
| 206 | |
| 207 | return paths, nil |
| 208 | } |
no test coverage detected
searching dependent graphs…