OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / toolbar / osssync / util / file.go
1 package util
2
3 import (
4         "os"
5 )
6
7 // FileUtil is a struct of File utility
8 type FileUtil struct {
9         LocalDir string
10 }
11
12 // IsExists if file or directory exist
13 func IsExists(path string) bool {
14         if _, err := os.Stat(path); err != nil && !os.IsExist(err) {
15                 return false
16         }
17
18         return true
19 }
20
21 // PathExists return if path exists
22 func PathExists(path string) (bool, error) {
23         if _, err := os.Stat(path); os.IsNotExist(err) {
24                 return false, nil
25         } else {
26                 return err == nil, err
27         }
28 }
29
30 // RemoveLocal deletes file
31 func (f *FileUtil) RemoveLocal(filename string) error {
32         return os.Remove(f.LocalDir + filename)
33 }
34
35 // BlockDirInitial initializes the blocks directory
36 func (f *FileUtil) BlockDirInitial() error {
37         ifPathExist, err := PathExists(f.LocalDir)
38         if err != nil {
39                 return err
40         }
41
42         if ifPathExist {
43                 if err = os.RemoveAll(f.LocalDir); err != nil {
44                         return err
45                 }
46         }
47
48         return os.Mkdir(f.LocalDir, 0755)
49 }