|
1 | 1 | package filesystem |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "io/fs" |
5 | 4 | "path" |
6 | 5 | "path/filepath" |
7 | 6 | "strings" |
8 | | - "syscall" |
9 | | - |
10 | | - validation "github.com/go-ozzo/ozzo-validation/v4" |
11 | 7 |
|
12 | 8 | "github.com/ARM-software/golang-utils/utils/commonerrors" |
13 | 9 | "github.com/ARM-software/golang-utils/utils/platform" |
@@ -282,103 +278,3 @@ func EvalSymlinks(fs FS, pathWithSymlinks string) (populatedPath string, err err |
282 | 278 | func EndsWithPathSeparator(fs FS, filePath string) bool { |
283 | 279 | return strings.HasSuffix(filePath, "/") || strings.HasSuffix(filePath, string(fs.PathSeparator())) |
284 | 280 | } |
285 | | - |
286 | | -// NewPathValidationRule returns a validation rule to use in configuration. |
287 | | -// The rule checks whether a string is a valid not empty path. |
288 | | -// `when` describes whether the rule is enforced or not |
289 | | -func NewPathValidationRule(filesystem FS, when bool) validation.Rule { |
290 | | - return &pathValidationRule{condition: when, filesystem: filesystem} |
291 | | -} |
292 | | - |
293 | | -// NewOSPathValidationRule returns a validation rule to use in configuration. |
294 | | -// The rule checks whether a string is a valid path for the Operating System's filesystem. |
295 | | -// `when` describes whether the rule is enforced or not |
296 | | -func NewOSPathValidationRule(when bool) validation.Rule { |
297 | | - return NewPathValidationRule(GetGlobalFileSystem(), when) |
298 | | -} |
299 | | - |
300 | | -type pathValidationRule struct { |
301 | | - condition bool |
302 | | - filesystem FS |
303 | | -} |
304 | | - |
305 | | -func (r *pathValidationRule) Validate(value interface{}) error { |
306 | | - err := validation.Required.When(r.condition).Validate(value) |
307 | | - if err != nil { |
308 | | - return commonerrors.WrapErrorf(commonerrors.ErrUndefined, err, "path [%v] is required", value) |
309 | | - } |
310 | | - if !r.condition { |
311 | | - return nil |
312 | | - } |
313 | | - pathString, err := validation.EnsureString(value) |
314 | | - if err != nil { |
315 | | - return commonerrors.WrapErrorf(commonerrors.ErrInvalid, err, "path [%v] must be a string", value) |
316 | | - } |
317 | | - pathString = strings.TrimSpace(pathString) |
318 | | - // This check is here because it validates the path on any platform (it is a cross-platform check) |
319 | | - // Indeed if the path exists, then it can only be valid. |
320 | | - if r.filesystem.Exists(pathString) { |
321 | | - return nil |
322 | | - } |
323 | | - |
324 | | - // Inspired from https://github.com/go-playground/validator/blob/84254aeb5a59e615ec0b66ab53b988bc0677f55e/baked_in.go#L1604 and https://stackoverflow.com/questions/35231846/golang-check-if-string-is-valid-path |
325 | | - if pathString == "" { |
326 | | - return commonerrors.Newf(commonerrors.ErrUndefined, "the path [%v] is empty", value) |
327 | | - } |
328 | | - // This check is to catch errors on Linux. It does not work as well on Windows. |
329 | | - if _, err := r.filesystem.Stat(pathString); err != nil { |
330 | | - switch t := err.(type) { |
331 | | - case *fs.PathError: |
332 | | - if t.Err == syscall.EINVAL { |
333 | | - return commonerrors.WrapErrorf(commonerrors.ErrInvalid, err, "the path [%v] has invalid characters", value) |
334 | | - } |
335 | | - default: |
336 | | - // make the linter happy |
337 | | - } |
338 | | - } |
339 | | - // The following case is not caught on Windows by the check above. |
340 | | - if strings.Contains(pathString, "\n") { |
341 | | - return commonerrors.Newf(commonerrors.ErrInvalid, "the path [%v] has carriage returns characters", value) |
342 | | - } |
343 | | - |
344 | | - // TODO add platform validation checks: e.g. https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN on windows |
345 | | - |
346 | | - return nil |
347 | | -} |
348 | | - |
349 | | -// NewPathExistRule returns a validation rule to use in configuration. |
350 | | -// The rule checks whether a string is a valid not empty path and actually exists. |
351 | | -// `when` describes whether the rule is enforced or not. |
352 | | -func NewPathExistRule(filesystem FS, when bool) validation.Rule { |
353 | | - return &pathExistValidationRule{filesystem: filesystem, condition: when} |
354 | | -} |
355 | | - |
356 | | -// NewOSPathExistRule returns a validation rule to use in configuration. |
357 | | -// The rule checks whether a string is a valid path for the Operating system's filesystem and actually exists. |
358 | | -// `when` describes whether the rule is enforced or not. |
359 | | -func NewOSPathExistRule(when bool) validation.Rule { |
360 | | - return NewPathExistRule(GetGlobalFileSystem(), when) |
361 | | -} |
362 | | - |
363 | | -type pathExistValidationRule struct { |
364 | | - condition bool |
365 | | - filesystem FS |
366 | | -} |
367 | | - |
368 | | -func (r *pathExistValidationRule) Validate(value interface{}) error { |
369 | | - err := NewPathValidationRule(r.filesystem, r.condition).Validate(value) |
370 | | - if err != nil { |
371 | | - return err |
372 | | - } |
373 | | - if !r.condition { |
374 | | - return nil |
375 | | - } |
376 | | - path, err := validation.EnsureString(value) |
377 | | - if err != nil { |
378 | | - return commonerrors.WrapErrorf(commonerrors.ErrInvalid, err, "path [%v] must be a string", value) |
379 | | - } |
380 | | - if !r.filesystem.Exists(path) { |
381 | | - err = commonerrors.Newf(commonerrors.ErrNotFound, "path [%v] does not exist", path) |
382 | | - } |
383 | | - return err |
384 | | -} |
0 commit comments