github.com/pavlo67/common@v0.5.3/common/filelib/directory.go (about)

     1  package filelib
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"path"
     8  	"path/filepath"
     9  	"runtime"
    10  	"strconv"
    11  	"strings"
    12  	"time"
    13  
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  func RelativePath(pathFull, pathBase, pathPrefix string) string {
    18  	path := ""
    19  
    20  	if pathFull[:len(pathBase)] == pathBase {
    21  		path = pathFull[len(pathBase):]
    22  	} else {
    23  		log.Printf(".RelativePath(%s, %s, %s)????", pathFull, pathBase, pathPrefix)
    24  	}
    25  
    26  	return pathPrefix + path
    27  }
    28  
    29  func CurrentPath() string {
    30  	if _, filename, _, ok := runtime.Caller(1); ok {
    31  		return path.Dir(filename) + "/"
    32  	}
    33  	return ""
    34  }
    35  
    36  const onGetDir = "on filelib.GetDir(): "
    37  
    38  func GetDir(path string) (string, error) {
    39  
    40  	// converting Windows-backslashed pathes to the normal ones
    41  	path = reBackslash.ReplaceAllString(path, "/")
    42  
    43  	fi, err := os.Stat(path)
    44  	if err != nil {
    45  		return "", errors.Wrapf(err, onGetDir+"can't os.Stat(%s)", path)
    46  	}
    47  
    48  	if !fi.IsDir() {
    49  		return "", errors.Wrapf(err, onGetDir+"path (%s) isn't a directory", path)
    50  	}
    51  
    52  	if path[len(path)-1] != '/' {
    53  		path += "/"
    54  	}
    55  
    56  	return path, nil
    57  }
    58  
    59  //func Dir(path string) (string, error) {
    60  //	path = strings.TrimSpace(path)
    61  //	if path == "" {
    62  //		return "", errors.New("can't create dir for empty path")
    63  //	}
    64  //
    65  //	// converting Windows-backslashed pathes to the normal ones
    66  //	path = reBackslash.ReplaceAllString(path, "/")
    67  //	if path[len(path)-1] != '/' {
    68  //		path += "/"
    69  //	}
    70  //
    71  //	if _, err := os.Stat(path); err != nil {
    72  //		if os.IsNotExist(err) {
    73  //			err = os.MkdirAll(path, os.ModePerm)
    74  //			if err != nil {
    75  //				return "", errors.Wrapf(err, "can't create dir '%s'", path)
    76  //			}
    77  //			return path, nil
    78  //		}
    79  //		return "", errors.Wrapf(err, "can't get stat for dir '%s'", path)
    80  //	}
    81  //
    82  //	return path, nil
    83  //}
    84  
    85  func ClearDir(dir string) error {
    86  	d, err := os.Open(dir)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	defer d.Close()
    91  
    92  	names, err := d.Readdirnames(-1)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	for _, name := range names {
    97  		if err = os.RemoveAll(filepath.Join(dir, name)); err != nil {
    98  			return err
    99  		}
   100  	}
   101  	return nil
   102  }
   103  
   104  //const maxRetries = 10
   105  //
   106  //func SubDirUnique(path string) (string, error) {
   107  //	path, err := Dir(path)
   108  //	if err != nil {
   109  //		return "", err
   110  //	}
   111  //
   112  //	var subpath string
   113  //
   114  //	for i := 0; i < maxRetries; i++ {
   115  //		subpath, err = Dir(path + CorrectFileName(time.Now().Format(time.RFC3339)) + "_" + strconv.Itoa(i))
   116  //		if err == nil {
   117  //			return subpath, nil
   118  //		}
   119  //	}
   120  //
   121  //	return "", fmt.Errorf("can't create unique subpath %d times, last try was '%s'", maxRetries, subpath)
   122  //}
   123  
   124  const maxRetries = 10
   125  
   126  func Dir(path string) (string, error) {
   127  	path = strings.TrimSpace(path)
   128  	if path == "" {
   129  		return "", nil
   130  	}
   131  
   132  	// converting Windows-backslashed pathes to the normal ones
   133  	path = reBackslash.ReplaceAllString(path, "/")
   134  	if path[len(path)-1] != '/' {
   135  		path += "/"
   136  	}
   137  
   138  	if _, err := os.Stat(path); err != nil {
   139  		if os.IsNotExist(err) {
   140  			err = os.MkdirAll(path, os.ModePerm)
   141  			if err != nil {
   142  				return "", errors.Wrapf(err, "can't create dir '%s'", path)
   143  			}
   144  			return path, nil
   145  		}
   146  		return "", errors.Wrapf(err, "can't get stat for dir '%s'", path)
   147  	}
   148  
   149  	return path, nil
   150  }
   151  
   152  func SubDirUnique(path, prefix string) (string, error) {
   153  	path, err := Dir(path)
   154  	if err != nil {
   155  		return "", err
   156  	}
   157  
   158  	if len(prefix) > 0 && prefix[len(prefix)-1] != '_' {
   159  		prefix += "_"
   160  	}
   161  
   162  	var subpath string
   163  
   164  	for i := 0; i < maxRetries; i++ {
   165  		subpath, err = Dir(path + CorrectFileName(prefix+time.Now().Format(time.RFC3339)) + "_" + strconv.Itoa(i))
   166  		if err == nil {
   167  			return subpath, nil
   168  		}
   169  	}
   170  
   171  	return "", fmt.Errorf("can't create unique subpath %d times, last try was '%s'", maxRetries, subpath)
   172  }
   173  
   174  func DirEmpty(path string) (string, error) {
   175  	dir, err := Dir(path)
   176  	if err != nil {
   177  		return "", err
   178  	}
   179  
   180  	if err := ClearDir(dir); err != nil {
   181  		return "", err
   182  	}
   183  
   184  	return dir, nil
   185  }