func (backend *BlobsFileBackend) Delete(hash string) error { if !backend.loaded { panic("backend BlobsFileBackend not loaded") } if backend.writeOnly { return nil //panic("backend is in write-only mode") } blobPos, err := backend.index.GetPos(hash) if err != nil { return fmt.Errorf("Error fetching GetPos: %v", err) } if blobPos == nil { return fmt.Errorf("Blob %v not found in index", err) } var f *os.File // check if the file is already open for writing if blobPos.n == backend.n { f = backend.current } else { f, err = os.OpenFile(backend.filename(blobPos.n), os.O_RDWR, 0666) if err != nil { return fmt.Errorf("failed to open blobsfile %v", backend.filename(blobPos.n), err) } defer f.Close() } // Add Deleted to the flag if _, err := f.WriteAt([]byte{Deleted}, int64(blobPos.offset+hashSize)); err != nil { return err } // Delete the index entry if err := backend.index.DeletePos(hash); err != nil { return err } // Punch a hole in the file if possible if err := fileutil.PunchHole(f, int64(blobPos.offset+Overhead), int64(blobPos.size)); err != nil { return fmt.Errorf("failed to punch hole: %v", err) } return nil }
// PunchHole implements Filer. func (f *SimpleFileFiler) PunchHole(off, size int64) (err error) { return fileutil.PunchHole(f.file, off, size) }