Example #1
0
func writeDirectoryMetadata(file io.Writer,
	directoryMetadata image.DirectoryMetadata) error {
	w := bufio.NewWriter(file)
	writer := fsutil.NewChecksumWriter(w)
	if err := gob.NewEncoder(writer).Encode(directoryMetadata); err != nil {
		return err
	}
	if err := writer.WriteChecksum(); err != nil {
		return err
	}
	return w.Flush()
}
Example #2
0
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
	}
}