forked from cryptix/mountMgo
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfuse_document.go
More file actions
121 lines (94 loc) · 2.41 KB
/
fuse_document.go
File metadata and controls
121 lines (94 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"encoding/json"
"log"
"os"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"golang.org/x/net/context"
"labix.org/v2/mgo/bson"
)
// DocumentFile implements both Node and Handle for a document from a collection.
type DocumentFile struct {
coll string
Id interface{} `bson:"_id"`
Dirent fuse.Dirent
Fattr fuse.Attr
CTime time.Time
ATime time.Time
MTime time.Time
}
func (d DocumentFile) idQuery() bson.M {
return bson.M{"_id": d.Id}
}
func (d DocumentFile) Attr(a *fuse.Attr) {
log.Printf("DocumentFile.Attr() for: %+v", d)
_, size, err := d.readDocument()
if err != nil {
return
}
if d.CTime.IsZero() {
now := time.Now()
d.CTime = now
d.ATime = now
d.MTime = now
}
a.Uid = uint32(os.Getuid())
a.Gid = uint32(os.Getgid())
a.Mode = 0600
a.Size = size
a.Ctime = d.CTime
a.Atime = d.ATime
a.Mtime = d.MTime
}
func (d DocumentFile) Lookup(ctx context.Context, fname string) (fs.Node, error) {
log.Printf("DocumentFile[%s].Lookup(): %s\n", d.coll, fname)
return nil, fuse.ENOENT
}
func (d DocumentFile) ReadAll(ctx context.Context) ([]byte, error) {
log.Printf("DocumentFile[%s].ReadAll(): %s\n", d.coll, d.Id)
strval, _, err := d.readDocument()
if err != nil {
return nil, err
}
d.ATime = time.Now() // update last access time
return []byte(strval), nil
}
// Read a document and return it as a JSON string
func (d DocumentFile) readDocument() (string, uint64, error) {
db, s := getDb()
defer s.Close()
var f interface{}
err := db.C(d.coll).Find(d.idQuery()).One(&f)
if err != nil {
log.Fatal(err)
return "", 0, fuse.EIO
}
buf, err := json.MarshalIndent(f, "", " ")
if err != nil {
log.Fatal(err)
return "", 0, fuse.EIO
}
strval := string(buf) + "\n"
return strval, uint64(len(buf)), nil
}
func (d DocumentFile) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
log.Printf("DocumentFile.Write(%s) \n", d.Id)
db, s := getDb()
defer s.Close()
doc := make(map[string]interface{})
err := json.Unmarshal(req.Data, &doc)
if err != nil {
log.Printf("Could not parse the data as JSON[%s]: %s \n", d.Id, err.Error())
return fuse.EIO
}
delete(doc, "_id") // _id cannot be updated!
err = db.C(d.coll).Update(d.idQuery(), bson.M{"$set": doc})
if err != nil {
log.Printf("Could not update the document[%s]: %s \n", d.Id, err.Error())
return fuse.EIO
}
d.MTime = time.Now() // update last modified time
return nil
}