(w io.Writer, dirpath string)
| 148 | } |
| 149 | |
| 150 | func tarDirectory(w io.Writer, dirpath string) error { |
| 151 | tw := tar.NewWriter(w) |
| 152 | defer tw.Close() |
| 153 | |
| 154 | return filepath.Walk(dirpath, func(path string, info os.FileInfo, err error) error { |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
| 158 | if info.IsDir() { |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | // Create a new tar header |
| 163 | header, err := tar.FileInfoHeader(info, info.Name()) |
| 164 | if err != nil { |
| 165 | return fmt.Errorf("creating tar header for %s: %w", path, err) |
| 166 | } |
| 167 | |
| 168 | // Update the name to be relative to the directory we're archiving |
| 169 | relPath, err := filepath.Rel(dirpath, path) |
| 170 | if err != nil { |
| 171 | return fmt.Errorf("getting relative path for %s: %w", path, err) |
| 172 | } |
| 173 | header.Name = relPath |
| 174 | |
| 175 | // Write the header |
| 176 | if err := tw.WriteHeader(header); err != nil { |
| 177 | return fmt.Errorf("writing tar header for %s: %w", path, err) |
| 178 | } |
| 179 | |
| 180 | // Open the file |
| 181 | file, err := os.Open(path) |
| 182 | if err != nil { |
| 183 | return fmt.Errorf("opening file %s: %w", path, err) |
| 184 | } |
| 185 | defer file.Close() |
| 186 | |
| 187 | // Copy the file contents |
| 188 | if _, err := io.Copy(tw, file); err != nil { |
| 189 | return fmt.Errorf("copying file %s to tar archive: %w", path, err) |
| 190 | } |
| 191 | |
| 192 | return nil |
| 193 | }) |
| 194 | } |
| 195 | |
| 196 | func IsTarArchive(r io.Reader) bool { |
| 197 | if r == nil { |
no test coverage detected