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 addImage(imageSClient *srpc.Client, name string, img *image.Image) error { if *expiresIn > 0 { img.ExpiresAt = time.Now().Add(*expiresIn) } else { img.ExpiresAt = time.Time{} } if err := img.Verify(); err != nil { return err } if err := img.VerifyRequiredPaths(requiredPaths); err != nil { return err } if err := client.AddImage(imageSClient, name, img); err != nil { return errors.New("remote error: " + err.Error()) } return nil }
func (imdb *ImageDataBase) addImage(image *image.Image, name string, username *string) error { if err := image.Verify(); err != nil { return err } imdb.Lock() defer imdb.Unlock() if imdb.scheduleExpiration(image, name) { imdb.logger.Printf("Ignoring already expired image: %s\n", name) return nil } if _, ok := imdb.imageMap[name]; ok { return errors.New("image: " + name + " already exists") } else { err := imdb.checkDirectoryPermissions(path.Dir(name), username) if err != nil { return err } filename := path.Join(imdb.baseDir, name) file, err := os.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_RDWR, filePerms) if err != nil { if os.IsExist(err) { return errors.New("cannot add a previously deleted image") } return err } defer file.Close() w := bufio.NewWriter(file) defer w.Flush() writer := fsutil.NewChecksumWriter(w) defer writer.WriteChecksum() encoder := gob.NewEncoder(writer) encoder.Encode(image) imdb.imageMap[name] = image imdb.addNotifiers.sendPlain(name, "add", imdb.logger) return nil } }