func (imdb *ImageDataBase) loadFile(filename string, logger *log.Logger) error { pathname := path.Join(imdb.baseDir, filename) file, err := os.Open(pathname) if err != nil { return err } defer file.Close() reader := fsutil.NewChecksumReader(file) decoder := gob.NewDecoder(reader) var image image.Image if err := decoder.Decode(&image); err != nil { return err } if err := reader.VerifyChecksum(); err != nil { if err == fsutil.ErrorChecksumMismatch { logger.Printf("Checksum mismatch for image: %s\n", filename) return nil } if err != io.EOF { return err } } image.FileSystem.RebuildInodePointers() if err := image.Verify(); err != nil { return err } imdb.Lock() defer imdb.Unlock() if imdb.scheduleExpiration(&image, filename) { imdb.logger.Printf("Deleting already expired image: %s\n", filename) return os.Remove(pathname) } imdb.imageMap[filename] = &image return nil }
func (imdb *ImageDataBase) readDirectoryMetadata(dirname string) ( image.DirectoryMetadata, error) { file, err := os.Open(path.Join(imdb.baseDir, dirname, metadataFile)) if err != nil { if os.IsNotExist(err) { return image.DirectoryMetadata{}, nil } return image.DirectoryMetadata{}, err } defer file.Close() reader := fsutil.NewChecksumReader(file) decoder := gob.NewDecoder(reader) metadata := image.DirectoryMetadata{} if err := decoder.Decode(&metadata); err != nil { return image.DirectoryMetadata{}, fmt.Errorf( "unable to read directory metadata for \"%s\": %s", dirname, err) } return metadata, reader.VerifyChecksum() }