Beispiel #1
0
func (objSrv *ObjectServer) addObject(reader io.Reader, length uint64,
	expectedHash *hash.Hash) (hash.Hash, bool, error) {
	hashVal, data, err := objectcache.ReadObject(reader, length, expectedHash)
	if err != nil {
		return hashVal, false, err
	}
	length = uint64(len(data))
	filename := path.Join(objSrv.baseDir, objectcache.HashToFilename(hashVal))
	// Check for existing object and collision.
	fi, err := os.Lstat(filename)
	if err == nil {
		if !fi.Mode().IsRegular() {
			return hashVal, false, errors.New("Existing non-file: " + filename)
		}
		if err := collisionCheck(data, filename, fi.Size()); err != nil {
			return hashVal, false, errors.New(
				"Collision detected: " + err.Error())
		}
		// No collision and no error: it's the same object. Go home early.
		return hashVal, false, nil
	}
	if err = os.MkdirAll(path.Dir(filename), syscall.S_IRWXU); err != nil {
		return hashVal, false, err
	}
	if err := fsutil.CopyToFile(filename, filePerms, bytes.NewReader(data),
		length); err != nil {
		return hashVal, false, err
	}
	objSrv.rwLock.Lock()
	objSrv.sizesMap[hashVal] = uint64(len(data))
	objSrv.rwLock.Unlock()
	return hashVal, true, nil
}
Beispiel #2
0
func readOne(objectsDir string, hash hash.Hash, length uint64,
	reader io.Reader) error {
	filename := path.Join(objectsDir, objectcache.HashToFilename(hash))
	dirname := path.Dir(filename)
	if err := os.MkdirAll(dirname, syscall.S_IRWXU); err != nil {
		return err
	}
	return fsutil.CopyToFile(filename, reader, int64(length))
}
Beispiel #3
0
func writeObject(objectsReader objectserver.ObjectsReader, hash hash.Hash,
	inodeNumber uint64, length uint64, inodesDir string) error {
	rlength, reader, err := objectsReader.NextObject()
	if err != nil {
		return err
	}
	defer reader.Close()
	if rlength != length {
		return errors.New("mismatched lengths")
	}
	filename := path.Join(inodesDir, fmt.Sprintf("%d", inodeNumber))
	return fsutil.CopyToFile(filename, reader, int64(rlength))
}
Beispiel #4
0
func (objSrv *objectServer) AddObject(reader io.Reader, length uint64,
	expectedHash *hash.Hash) (hash.Hash, bool, error) {
	hashVal, data, err := objectcache.ReadObject(reader, length, expectedHash)
	if err != nil {
		return hashVal, false, err
	}
	filename := path.Join(objSrv.baseDir, objectcache.HashToFilename(hashVal))
	if err = os.MkdirAll(path.Dir(filename), dirPerms); err != nil {
		return hashVal, false, err
	}
	if err := fsutil.CopyToFile(filename, filePerms, bytes.NewReader(data),
		length); err != nil {
		return hashVal, false, err
	}
	return hashVal, true, nil
}
Beispiel #5
0
func getObject(objSrv objectserver.ObjectServer, hashVal hash.Hash,
	baseOutputFilename string) error {
	hashes := make([]hash.Hash, 1)
	hashes[0] = hashVal
	objectsReader, err := objSrv.GetObjects(hashes)
	if err != nil {
		return err
	}
	size, reader, err := objectsReader.NextObject()
	if err != nil {
		return err
	}
	defer reader.Close()
	filename := fmt.Sprintf("%s.%x", baseOutputFilename, hashVal)
	return fsutil.CopyToFile(filename, reader, int64(size))
}
Beispiel #6
0
func readOne(dirname string, hash hash.Hash, length uint64,
	reader io.Reader) error {
	filename := fmt.Sprintf("%s/%x", dirname, hash)
	return fsutil.CopyToFile(filename, reader, int64(length))
}