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 }
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 }
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 } // Check for existing object and collision. objSrv.rwLock.RLock() oldData, ok := objSrv.objectMap[hashVal] objSrv.rwLock.RUnlock() if ok { if err := collisionCheck(data, oldData); 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 } objSrv.rwLock.Lock() objSrv.objectMap[hashVal] = data objSrv.rwLock.Unlock() return hashVal, true, nil }