You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
1.9 KiB
89 lines
1.9 KiB
10 years ago
|
package donut
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"path"
|
||
|
)
|
||
|
|
||
10 years ago
|
func newDonutObjectWriter(objectDir string) (Writer, error) {
|
||
10 years ago
|
dataFile, err := os.OpenFile(path.Join(objectDir, "data"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
||
10 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
10 years ago
|
return donutObjectWriter{
|
||
10 years ago
|
root: objectDir,
|
||
|
file: dataFile,
|
||
|
metadata: make(map[string]string),
|
||
|
donutMetadata: make(map[string]string),
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
10 years ago
|
type donutObjectWriter struct {
|
||
10 years ago
|
root string
|
||
|
file *os.File
|
||
|
metadata map[string]string
|
||
|
donutMetadata map[string]string
|
||
|
err error
|
||
|
}
|
||
|
|
||
10 years ago
|
func (d donutObjectWriter) Write(data []byte) (int, error) {
|
||
10 years ago
|
return d.file.Write(data)
|
||
|
}
|
||
|
|
||
10 years ago
|
func (d donutObjectWriter) Close() error {
|
||
10 years ago
|
if d.err != nil {
|
||
|
return d.err
|
||
|
}
|
||
|
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)
|
||
|
|
||
|
return d.file.Close()
|
||
|
}
|
||
|
|
||
10 years ago
|
func (d donutObjectWriter) CloseWithError(err error) error {
|
||
10 years ago
|
if d.err != nil {
|
||
|
d.err = err
|
||
|
}
|
||
|
return d.Close()
|
||
|
}
|
||
|
|
||
10 years ago
|
func (d donutObjectWriter) SetMetadata(metadata map[string]string) error {
|
||
10 years ago
|
for k := range d.metadata {
|
||
|
delete(d.metadata, k)
|
||
|
}
|
||
|
for k, v := range metadata {
|
||
|
d.metadata[k] = v
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
10 years ago
|
func (d donutObjectWriter) GetMetadata() (map[string]string, error) {
|
||
10 years ago
|
metadata := make(map[string]string)
|
||
|
for k, v := range d.metadata {
|
||
|
metadata[k] = v
|
||
|
}
|
||
|
return metadata, nil
|
||
|
}
|
||
|
|
||
10 years ago
|
func (d donutObjectWriter) SetDonutMetadata(metadata map[string]string) error {
|
||
10 years ago
|
for k := range d.donutMetadata {
|
||
|
delete(d.donutMetadata, k)
|
||
|
}
|
||
|
for k, v := range metadata {
|
||
|
d.donutMetadata[k] = v
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
10 years ago
|
func (d donutObjectWriter) GetDonutMetadata() (map[string]string, error) {
|
||
10 years ago
|
donutMetadata := make(map[string]string)
|
||
|
for k, v := range d.donutMetadata {
|
||
|
donutMetadata[k] = v
|
||
|
}
|
||
|
return donutMetadata, nil
|
||
|
}
|