(src, dest string, cb func(int64, bool, string))
| 93 | } |
| 94 | |
| 95 | func Zip(src, dest string, cb func(int64, bool, string)) error { |
| 96 | _, err := os.Stat(src) |
| 97 | if err != nil { |
| 98 | return fmt.Errorf("cannot access source: %v", err) |
| 99 | } |
| 100 | |
| 101 | zipFile, err := os.Create(dest) |
| 102 | if err != nil { |
| 103 | return fmt.Errorf("cannot create zip file: %v", err) |
| 104 | } |
| 105 | defer zipFile.Close() |
| 106 | |
| 107 | zipWriter := zip.NewWriter(zipFile) |
| 108 | defer zipWriter.Close() |
| 109 | |
| 110 | var ( |
| 111 | totalSize int64 = 0 |
| 112 | currentUID int = os.Getuid() |
| 113 | currentGID int = os.Getgid() |
| 114 | ) |
| 115 | |
| 116 | remainingSpace, err := GetRemainingFolderSpace() |
| 117 | if err != nil { |
| 118 | return fmt.Errorf("cannot get remaining folder space: %v", err) |
| 119 | } |
| 120 | |
| 121 | err = filepath.Walk(src, func(path string, info os.FileInfo, err error) error { |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | |
| 126 | relPath, err := filepath.Rel(src, path) |
| 127 | if err != nil { |
| 128 | return fmt.Errorf("cannot get relative path: %v", err) |
| 129 | } |
| 130 | |
| 131 | if relPath == "." { |
| 132 | return nil |
| 133 | } |
| 134 | |
| 135 | relPath = filepath.ToSlash(relPath) |
| 136 | |
| 137 | cb(totalSize, false, "") |
| 138 | |
| 139 | err = archiveItem(zipWriter, path, relPath, info, &totalSize, currentUID, currentGID) |
| 140 | if err != nil { |
| 141 | log.Printf("Zip error: %v\n", err) |
| 142 | return fmt.Errorf("unable to archive file (%s): %v", relPath, err) |
| 143 | } |
| 144 | |
| 145 | if totalSize > remainingSpace { |
| 146 | zipWriter.Close() |
| 147 | zipFile.Close() |
| 148 | os.Remove(dest) |
| 149 | |
| 150 | cb(totalSize, false, "Zip process exceeds storage limit!") |
| 151 | return fmt.Errorf("zip process exceeds storage limit") |
| 152 | } |
no test coverage detected