IsInsideDir checks if path is within directory. Both path and directory have to be absolute paths.
(path string, directory string)
| 53 | // IsInsideDir checks if path is within directory. |
| 54 | // Both path and directory have to be absolute paths. |
| 55 | func IsInsideDir(path string, directory string) (isInside bool, err error) { |
| 56 | if !filepath.IsAbs(path) { |
| 57 | err = fmt.Errorf( |
| 58 | "argument `path` (%s) is not an absolute path", |
| 59 | path, |
| 60 | ) |
| 61 | return |
| 62 | } |
| 63 | if !filepath.IsAbs(directory) { |
| 64 | err = fmt.Errorf( |
| 65 | "argument `directory` (%s) is not an absolute path", |
| 66 | directory, |
| 67 | ) |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | rel, err := filepath.Rel(directory, path) |
| 72 | if err != nil { |
| 73 | return false, err |
| 74 | } |
| 75 | |
| 76 | isInside = rel == "." || !strings.HasPrefix(rel, ".") |
| 77 | |
| 78 | return |
| 79 | } |
no outgoing calls