2015-03-22 18:14:06 -04:00
|
|
|
package donut
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-03-26 13:41:40 -04:00
|
|
|
"github.com/minio-io/iodine"
|
2015-03-22 18:14:06 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
func newDonutObjectWriter(objectDir string) (Writer, error) {
|
2015-03-23 23:06:15 -04:00
|
|
|
dataFile, err := os.OpenFile(path.Join(objectDir, "data"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
2015-03-22 18:14:06 -04:00
|
|
|
if err != nil {
|
2015-03-26 13:41:40 -04:00
|
|
|
return nil, iodine.Error(err, map[string]string{"objectDir": objectDir})
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
2015-03-23 23:40:21 -04:00
|
|
|
return donutObjectWriter{
|
2015-03-22 18:14:06 -04:00
|
|
|
root: objectDir,
|
|
|
|
file: dataFile,
|
|
|
|
metadata: make(map[string]string),
|
|
|
|
donutMetadata: make(map[string]string),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
type donutObjectWriter struct {
|
2015-03-22 18:14:06 -04:00
|
|
|
root string
|
|
|
|
file *os.File
|
|
|
|
metadata map[string]string
|
|
|
|
donutMetadata map[string]string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutObjectWriter) Write(data []byte) (int, error) {
|
2015-03-26 13:41:40 -04:00
|
|
|
written, err := d.file.Write(data)
|
|
|
|
iodine.Error(err, nil)
|
|
|
|
return written, err
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutObjectWriter) Close() error {
|
2015-03-22 18:14:06 -04:00
|
|
|
if d.err != nil {
|
2015-03-26 13:41:40 -04:00
|
|
|
return iodine.Error(d.err, nil)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
metadata, _ := json.Marshal(d.metadata)
|
|
|
|
ioutil.WriteFile(path.Join(d.root, "metadata.json"), metadata, 0600)
|
|
|
|
donutMetadata, _ := json.Marshal(d.donutMetadata)
|
|
|
|
ioutil.WriteFile(path.Join(d.root, "donutMetadata.json"), donutMetadata, 0600)
|
|
|
|
|
2015-03-26 13:41:40 -04:00
|
|
|
return iodine.Error(d.file.Close(), nil)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutObjectWriter) CloseWithError(err error) error {
|
2015-03-22 18:14:06 -04:00
|
|
|
if d.err != nil {
|
|
|
|
d.err = err
|
|
|
|
}
|
2015-03-26 13:41:40 -04:00
|
|
|
// TODO semantics between this and d.Close are weird, work out something better
|
|
|
|
return iodine.Error(d.Close(), nil)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutObjectWriter) SetMetadata(metadata map[string]string) error {
|
2015-03-22 18:14:06 -04:00
|
|
|
for k := range d.metadata {
|
|
|
|
delete(d.metadata, k)
|
|
|
|
}
|
|
|
|
for k, v := range metadata {
|
|
|
|
d.metadata[k] = v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutObjectWriter) GetMetadata() (map[string]string, error) {
|
2015-03-22 18:14:06 -04:00
|
|
|
metadata := make(map[string]string)
|
|
|
|
for k, v := range d.metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutObjectWriter) SetDonutMetadata(metadata map[string]string) error {
|
2015-03-22 18:14:06 -04:00
|
|
|
for k := range d.donutMetadata {
|
|
|
|
delete(d.donutMetadata, k)
|
|
|
|
}
|
|
|
|
for k, v := range metadata {
|
|
|
|
d.donutMetadata[k] = v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutObjectWriter) GetDonutMetadata() (map[string]string, error) {
|
2015-03-22 18:14:06 -04:00
|
|
|
donutMetadata := make(map[string]string)
|
|
|
|
for k, v := range d.donutMetadata {
|
|
|
|
donutMetadata[k] = v
|
|
|
|
}
|
|
|
|
return donutMetadata, nil
|
|
|
|
}
|