File: utils.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (34 lines) | stat: -rw-r--r-- 919 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Package utils collects various services: simple operations, compression, etc.
package utils

import (
	"fmt"
	"os"
	"strings"

	"golang.org/x/sys/unix"
)

// DirIsAccessible verifies that directory exists and is accessible
func DirIsAccessible(filename string) error {
	fileStat, err := os.Stat(filename)
	if err != nil {
		if !os.IsNotExist(err) {
			return fmt.Errorf("error checking directory '%s': %s", filename, err)
		}
	} else {
		if fileStat.Mode().Perm() == 0000 || unix.Access(filename, unix.W_OK) != nil {
			return fmt.Errorf("'%s' is inaccessible, check access rights", filename)
		}
	}
	return nil
}

// SanitizePath removes leading '/', remove '..', '$' and '`'
func SanitizePath(path string) (result string) {
	result = strings.Replace(path, "..", "", -1)
	result = strings.Replace(result, "$", "", -1)
	result = strings.Replace(result, "`", "", -1)
	result = strings.TrimLeft(result, "/")
	return
}