|
|
@ -192,9 +192,9 @@ func (fs Filesystem) CreateObject(bucket, object, expectedMD5Sum string, size in |
|
|
|
fs.lock.Lock() |
|
|
|
fs.lock.Lock() |
|
|
|
defer fs.lock.Unlock() |
|
|
|
defer fs.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
di, err := disk.GetInfo(fs.path) |
|
|
|
di, e := disk.GetInfo(fs.path) |
|
|
|
if err != nil { |
|
|
|
if e != nil { |
|
|
|
return ObjectMetadata{}, probe.NewError(err) |
|
|
|
return ObjectMetadata{}, probe.NewError(e) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Remove 5% from total space for cumulative disk space used for journalling, inodes etc.
|
|
|
|
// Remove 5% from total space for cumulative disk space used for journalling, inodes etc.
|
|
|
@ -210,7 +210,7 @@ func (fs Filesystem) CreateObject(bucket, object, expectedMD5Sum string, size in |
|
|
|
|
|
|
|
|
|
|
|
bucket = fs.denormalizeBucket(bucket) |
|
|
|
bucket = fs.denormalizeBucket(bucket) |
|
|
|
bucketPath := filepath.Join(fs.path, bucket) |
|
|
|
bucketPath := filepath.Join(fs.path, bucket) |
|
|
|
if _, e := os.Stat(bucketPath); e != nil { |
|
|
|
if _, e = os.Stat(bucketPath); e != nil { |
|
|
|
if os.IsNotExist(e) { |
|
|
|
if os.IsNotExist(e) { |
|
|
|
return ObjectMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket}) |
|
|
|
return ObjectMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket}) |
|
|
|
} |
|
|
|
} |
|
|
@ -225,8 +225,8 @@ func (fs Filesystem) CreateObject(bucket, object, expectedMD5Sum string, size in |
|
|
|
objectPath := filepath.Join(bucketPath, object) |
|
|
|
objectPath := filepath.Join(bucketPath, object) |
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" { |
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" { |
|
|
|
var expectedMD5SumBytes []byte |
|
|
|
var expectedMD5SumBytes []byte |
|
|
|
expectedMD5SumBytes, err = base64.StdEncoding.DecodeString(strings.TrimSpace(expectedMD5Sum)) |
|
|
|
expectedMD5SumBytes, e = base64.StdEncoding.DecodeString(strings.TrimSpace(expectedMD5Sum)) |
|
|
|
if err != nil { |
|
|
|
if e != nil { |
|
|
|
// pro-actively close the connection
|
|
|
|
// pro-actively close the connection
|
|
|
|
return ObjectMetadata{}, probe.NewError(InvalidDigest{Md5: expectedMD5Sum}) |
|
|
|
return ObjectMetadata{}, probe.NewError(InvalidDigest{Md5: expectedMD5Sum}) |
|
|
|
} |
|
|
|
} |
|
|
@ -234,9 +234,19 @@ func (fs Filesystem) CreateObject(bucket, object, expectedMD5Sum string, size in |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// write object
|
|
|
|
// write object
|
|
|
|
file, err := atomic.FileCreateWithPrefix(objectPath, "") |
|
|
|
file, e := atomic.FileCreateWithPrefix(objectPath, "") |
|
|
|
if err != nil { |
|
|
|
if e != nil { |
|
|
|
return ObjectMetadata{}, probe.NewError(err) |
|
|
|
switch e := e.(type) { |
|
|
|
|
|
|
|
case *os.PathError: |
|
|
|
|
|
|
|
if e.Op == "mkdir" { |
|
|
|
|
|
|
|
if strings.Contains(e.Error(), "not a directory") { |
|
|
|
|
|
|
|
return ObjectMetadata{}, probe.NewError(ObjectExistsAsPrefix{Bucket: bucket, Prefix: object}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return ObjectMetadata{}, probe.NewError(e) |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return ObjectMetadata{}, probe.NewError(e) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
h := md5.New() |
|
|
|
h := md5.New() |
|
|
@ -244,33 +254,33 @@ func (fs Filesystem) CreateObject(bucket, object, expectedMD5Sum string, size in |
|
|
|
mw := io.MultiWriter(file, h, sh) |
|
|
|
mw := io.MultiWriter(file, h, sh) |
|
|
|
|
|
|
|
|
|
|
|
if size > 0 { |
|
|
|
if size > 0 { |
|
|
|
_, err = io.CopyN(mw, data, size) |
|
|
|
_, e = io.CopyN(mw, data, size) |
|
|
|
if err != nil { |
|
|
|
if e != nil { |
|
|
|
file.CloseAndPurge() |
|
|
|
file.CloseAndPurge() |
|
|
|
return ObjectMetadata{}, probe.NewError(err) |
|
|
|
return ObjectMetadata{}, probe.NewError(e) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
_, err = io.Copy(mw, data) |
|
|
|
_, e = io.Copy(mw, data) |
|
|
|
if err != nil { |
|
|
|
if e != nil { |
|
|
|
file.CloseAndPurge() |
|
|
|
file.CloseAndPurge() |
|
|
|
return ObjectMetadata{}, probe.NewError(err) |
|
|
|
return ObjectMetadata{}, probe.NewError(e) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
md5Sum := hex.EncodeToString(h.Sum(nil)) |
|
|
|
md5Sum := hex.EncodeToString(h.Sum(nil)) |
|
|
|
// Verify if the written object is equal to what is expected, only if it is requested as such
|
|
|
|
// Verify if the written object is equal to what is expected, only if it is requested as such
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" { |
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" { |
|
|
|
if err := isMD5SumEqual(strings.TrimSpace(expectedMD5Sum), md5Sum); err != nil { |
|
|
|
if e := isMD5SumEqual(strings.TrimSpace(expectedMD5Sum), md5Sum); e != nil { |
|
|
|
file.CloseAndPurge() |
|
|
|
file.CloseAndPurge() |
|
|
|
return ObjectMetadata{}, probe.NewError(BadDigest{Md5: expectedMD5Sum, Bucket: bucket, Object: object}) |
|
|
|
return ObjectMetadata{}, probe.NewError(BadDigest{Md5: expectedMD5Sum, Bucket: bucket, Object: object}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
sha256Sum := hex.EncodeToString(sh.Sum(nil)) |
|
|
|
sha256Sum := hex.EncodeToString(sh.Sum(nil)) |
|
|
|
if signature != nil { |
|
|
|
if signature != nil { |
|
|
|
ok, perr := signature.DoesSignatureMatch(sha256Sum) |
|
|
|
ok, err := signature.DoesSignatureMatch(sha256Sum) |
|
|
|
if perr != nil { |
|
|
|
if err != nil { |
|
|
|
file.CloseAndPurge() |
|
|
|
file.CloseAndPurge() |
|
|
|
return ObjectMetadata{}, perr.Trace() |
|
|
|
return ObjectMetadata{}, err.Trace() |
|
|
|
} |
|
|
|
} |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
file.CloseAndPurge() |
|
|
|
file.CloseAndPurge() |
|
|
@ -279,9 +289,9 @@ func (fs Filesystem) CreateObject(bucket, object, expectedMD5Sum string, size in |
|
|
|
} |
|
|
|
} |
|
|
|
file.Close() |
|
|
|
file.Close() |
|
|
|
|
|
|
|
|
|
|
|
st, err := os.Stat(objectPath) |
|
|
|
st, e := os.Stat(objectPath) |
|
|
|
if err != nil { |
|
|
|
if e != nil { |
|
|
|
return ObjectMetadata{}, probe.NewError(err) |
|
|
|
return ObjectMetadata{}, probe.NewError(e) |
|
|
|
} |
|
|
|
} |
|
|
|
contentType := "application/octet-stream" |
|
|
|
contentType := "application/octet-stream" |
|
|
|
if objectExt := filepath.Ext(objectPath); objectExt != "" { |
|
|
|
if objectExt := filepath.Ext(objectPath); objectExt != "" { |
|
|
@ -303,28 +313,28 @@ func deleteObjectPath(basePath, deletePath, bucket, object string) *probe.Error |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fi, err := os.Stat(deletePath) |
|
|
|
fi, e := os.Stat(deletePath) |
|
|
|
if err != nil { |
|
|
|
if e != nil { |
|
|
|
if os.IsNotExist(err) { |
|
|
|
if os.IsNotExist(e) { |
|
|
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object}) |
|
|
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object}) |
|
|
|
} |
|
|
|
} |
|
|
|
return probe.NewError(err) |
|
|
|
return probe.NewError(e) |
|
|
|
} |
|
|
|
} |
|
|
|
if fi.IsDir() { |
|
|
|
if fi.IsDir() { |
|
|
|
empty, err := isDirEmpty(deletePath) |
|
|
|
empty, err := isDirEmpty(deletePath) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err.Trace() |
|
|
|
return err.Trace(deletePath) |
|
|
|
} |
|
|
|
} |
|
|
|
if !empty { |
|
|
|
if !empty { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if err := os.Remove(deletePath); err != nil { |
|
|
|
if e := os.Remove(deletePath); e != nil { |
|
|
|
return probe.NewError(err) |
|
|
|
return probe.NewError(e) |
|
|
|
} |
|
|
|
} |
|
|
|
if err := deleteObjectPath(basePath, filepath.Dir(deletePath), bucket, object); err != nil { |
|
|
|
if err := deleteObjectPath(basePath, filepath.Dir(deletePath), bucket, object); err != nil { |
|
|
|
return err.Trace() |
|
|
|
return err.Trace(basePath, deletePath, bucket, object) |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
@ -367,7 +377,7 @@ func (fs Filesystem) DeleteObject(bucket, object string) *probe.Error { |
|
|
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object}) |
|
|
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object}) |
|
|
|
} |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err.Trace() |
|
|
|
return err.Trace(bucketPath, objectPath, bucket, object) |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|