1 #include "common.h"
2 
3 #include "log.h"
4 
5 #include "seafile-session.h"
6 
7 #include "utils.h"
8 
9 #include "obj-backend.h"
10 #include "obj-store.h"
11 
12 struct SeafObjStore {
13     ObjBackend   *bend;
14 };
15 typedef struct SeafObjStore SeafObjStore;
16 
17 extern ObjBackend *
18 obj_backend_fs_new (const char *seaf_dir, const char *obj_type);
19 
20 struct SeafObjStore *
seaf_obj_store_new(SeafileSession * seaf,const char * obj_type)21 seaf_obj_store_new (SeafileSession *seaf, const char *obj_type)
22 {
23     SeafObjStore *store = g_new0 (SeafObjStore, 1);
24 
25     if (!store)
26         return NULL;
27 
28     store->bend = obj_backend_fs_new (seaf->seaf_dir, obj_type);
29     if (!store->bend) {
30         seaf_warning ("[Object store] Failed to load backend.\n");
31         g_free (store);
32         return NULL;
33     }
34 
35     return store;
36 }
37 
38 int
seaf_obj_store_init(SeafObjStore * obj_store,gboolean enable_async,CEventManager * ev_mgr)39 seaf_obj_store_init (SeafObjStore *obj_store,
40                      gboolean enable_async,
41                      CEventManager *ev_mgr)
42 {
43     return 0;
44 }
45 
46 int
seaf_obj_store_read_obj(struct SeafObjStore * obj_store,const char * repo_id,int version,const char * obj_id,void ** data,int * len)47 seaf_obj_store_read_obj (struct SeafObjStore *obj_store,
48                          const char *repo_id,
49                          int version,
50                          const char *obj_id,
51                          void **data,
52                          int *len)
53 {
54     ObjBackend *bend = obj_store->bend;
55 
56     if (!repo_id || !is_uuid_valid(repo_id) ||
57         !obj_id || !is_object_id_valid(obj_id))
58         return -1;
59 
60     return bend->read (bend, repo_id, version, obj_id, data, len);
61 }
62 
63 int
seaf_obj_store_write_obj(struct SeafObjStore * obj_store,const char * repo_id,int version,const char * obj_id,void * data,int len,gboolean need_sync)64 seaf_obj_store_write_obj (struct SeafObjStore *obj_store,
65                           const char *repo_id,
66                           int version,
67                           const char *obj_id,
68                           void *data,
69                           int len,
70                           gboolean need_sync)
71 {
72     ObjBackend *bend = obj_store->bend;
73 
74     if (!repo_id || !is_uuid_valid(repo_id) ||
75         !obj_id || !is_object_id_valid(obj_id))
76         return -1;
77 
78     return bend->write (bend, repo_id, version, obj_id, data, len, need_sync);
79 }
80 
81 gboolean
seaf_obj_store_obj_exists(struct SeafObjStore * obj_store,const char * repo_id,int version,const char * obj_id)82 seaf_obj_store_obj_exists (struct SeafObjStore *obj_store,
83                            const char *repo_id,
84                            int version,
85                            const char *obj_id)
86 {
87     ObjBackend *bend = obj_store->bend;
88 
89     if (!repo_id || !is_uuid_valid(repo_id) ||
90         !obj_id || !is_object_id_valid(obj_id))
91         return FALSE;
92 
93     return bend->exists (bend, repo_id, version, obj_id);
94 }
95 
96 void
seaf_obj_store_delete_obj(struct SeafObjStore * obj_store,const char * repo_id,int version,const char * obj_id)97 seaf_obj_store_delete_obj (struct SeafObjStore *obj_store,
98                            const char *repo_id,
99                            int version,
100                            const char *obj_id)
101 {
102     ObjBackend *bend = obj_store->bend;
103 
104     if (!repo_id || !is_uuid_valid(repo_id) ||
105         !obj_id || !is_object_id_valid(obj_id))
106         return;
107 
108     return bend->delete (bend, repo_id, version, obj_id);
109 }
110 
111 int
seaf_obj_store_foreach_obj(struct SeafObjStore * obj_store,const char * repo_id,int version,SeafObjFunc process,void * user_data)112 seaf_obj_store_foreach_obj (struct SeafObjStore *obj_store,
113                             const char *repo_id,
114                             int version,
115                             SeafObjFunc process,
116                             void *user_data)
117 {
118     ObjBackend *bend = obj_store->bend;
119 
120     return bend->foreach_obj (bend, repo_id, version, process, user_data);
121 }
122 
123 int
seaf_obj_store_copy_obj(struct SeafObjStore * obj_store,const char * src_repo_id,int src_version,const char * dst_repo_id,int dst_version,const char * obj_id)124 seaf_obj_store_copy_obj (struct SeafObjStore *obj_store,
125                          const char *src_repo_id,
126                          int src_version,
127                          const char *dst_repo_id,
128                          int dst_version,
129                          const char *obj_id)
130 {
131     ObjBackend *bend = obj_store->bend;
132 
133     if (strcmp (obj_id, EMPTY_SHA1) == 0)
134         return 0;
135 
136     return bend->copy (bend, src_repo_id, src_version, dst_repo_id, dst_version, obj_id);
137 }
138 
139 int
seaf_obj_store_remove_store(struct SeafObjStore * obj_store,const char * store_id)140 seaf_obj_store_remove_store (struct SeafObjStore *obj_store,
141                              const char *store_id)
142 {
143     ObjBackend *bend = obj_store->bend;
144 
145     return bend->remove_store (bend, store_id);
146 }
147