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)39 seaf_obj_store_init (SeafObjStore *obj_store)
40 {
41     return 0;
42 }
43 
44 int
seaf_obj_store_read_obj(struct SeafObjStore * obj_store,const char * repo_id,int version,const char * obj_id,void ** data,int * len)45 seaf_obj_store_read_obj (struct SeafObjStore *obj_store,
46                          const char *repo_id,
47                          int version,
48                          const char *obj_id,
49                          void **data,
50                          int *len)
51 {
52     ObjBackend *bend = obj_store->bend;
53 
54     if (!repo_id || !is_uuid_valid(repo_id) ||
55         !obj_id || !is_object_id_valid(obj_id))
56         return -1;
57 
58     return bend->read (bend, repo_id, version, obj_id, data, len);
59 }
60 
61 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)62 seaf_obj_store_write_obj (struct SeafObjStore *obj_store,
63                           const char *repo_id,
64                           int version,
65                           const char *obj_id,
66                           void *data,
67                           int len,
68                           gboolean need_sync)
69 {
70     ObjBackend *bend = obj_store->bend;
71 
72     if (!repo_id || !is_uuid_valid(repo_id) ||
73         !obj_id || !is_object_id_valid(obj_id))
74         return -1;
75 
76     return bend->write (bend, repo_id, version, obj_id, data, len, need_sync);
77 }
78 
79 gboolean
seaf_obj_store_obj_exists(struct SeafObjStore * obj_store,const char * repo_id,int version,const char * obj_id)80 seaf_obj_store_obj_exists (struct SeafObjStore *obj_store,
81                            const char *repo_id,
82                            int version,
83                            const char *obj_id)
84 {
85     ObjBackend *bend = obj_store->bend;
86 
87     if (!repo_id || !is_uuid_valid(repo_id) ||
88         !obj_id || !is_object_id_valid(obj_id))
89         return FALSE;
90 
91     return bend->exists (bend, repo_id, version, obj_id);
92 }
93 
94 void
seaf_obj_store_delete_obj(struct SeafObjStore * obj_store,const char * repo_id,int version,const char * obj_id)95 seaf_obj_store_delete_obj (struct SeafObjStore *obj_store,
96                            const char *repo_id,
97                            int version,
98                            const char *obj_id)
99 {
100     ObjBackend *bend = obj_store->bend;
101 
102     if (!repo_id || !is_uuid_valid(repo_id) ||
103         !obj_id || !is_object_id_valid(obj_id))
104         return;
105 
106     return bend->delete (bend, repo_id, version, obj_id);
107 }
108 
109 int
seaf_obj_store_foreach_obj(struct SeafObjStore * obj_store,const char * repo_id,int version,SeafObjFunc process,void * user_data)110 seaf_obj_store_foreach_obj (struct SeafObjStore *obj_store,
111                             const char *repo_id,
112                             int version,
113                             SeafObjFunc process,
114                             void *user_data)
115 {
116     ObjBackend *bend = obj_store->bend;
117 
118     return bend->foreach_obj (bend, repo_id, version, process, user_data);
119 }
120 
121 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)122 seaf_obj_store_copy_obj (struct SeafObjStore *obj_store,
123                          const char *src_repo_id,
124                          int src_version,
125                          const char *dst_repo_id,
126                          int dst_version,
127                          const char *obj_id)
128 {
129     ObjBackend *bend = obj_store->bend;
130 
131     if (strcmp (obj_id, EMPTY_SHA1) == 0)
132         return 0;
133 
134     return bend->copy (bend, src_repo_id, src_version, dst_repo_id, dst_version, obj_id);
135 }
136 
137 int
seaf_obj_store_remove_store(struct SeafObjStore * obj_store,const char * store_id)138 seaf_obj_store_remove_store (struct SeafObjStore *obj_store,
139                              const char *store_id)
140 {
141     ObjBackend *bend = obj_store->bend;
142 
143     return bend->remove_store (bend, store_id);
144 }
145