1 #ifndef HTTP_TX_MGR_H
2 #define HTTP_TX_MGR_H
3 
4 #include <pthread.h>
5 
6 enum {
7     HTTP_TASK_TYPE_DOWNLOAD = 0,
8     HTTP_TASK_TYPE_UPLOAD,
9 };
10 
11 
12 /**
13  * The state that can be set by user.
14  *
15  * A task in NORMAL state can be canceled;
16  * A task in RT_STATE_FINISHED can be removed.
17  */
18 enum HttpTaskState {
19     HTTP_TASK_STATE_NORMAL = 0,
20     HTTP_TASK_STATE_CANCELED,
21     HTTP_TASK_STATE_FINISHED,
22     HTTP_TASK_STATE_ERROR,
23     N_HTTP_TASK_STATE,
24 };
25 
26 enum HttpTaskRuntimeState {
27     HTTP_TASK_RT_STATE_INIT = 0,
28     HTTP_TASK_RT_STATE_CHECK,
29     HTTP_TASK_RT_STATE_COMMIT,
30     HTTP_TASK_RT_STATE_FS,
31     HTTP_TASK_RT_STATE_BLOCK,         /* Only used in upload. */
32     HTTP_TASK_RT_STATE_UPDATE_BRANCH, /* Only used in upload. */
33     HTTP_TASK_RT_STATE_FINISHED,
34     N_HTTP_TASK_RT_STATE,
35 };
36 
37 struct _SeafileSession;
38 struct _HttpTxPriv;
39 
40 struct _HttpTxManager {
41     struct _SeafileSession   *seaf;
42 
43     struct _HttpTxPriv *priv;
44 };
45 
46 typedef struct _HttpTxManager HttpTxManager;
47 
48 struct _HttpTxTask {
49     HttpTxManager *manager;
50 
51     char repo_id[37];
52     int repo_version;
53     char *repo_name;
54     char *token;
55     int protocol_version;
56     int type;
57     char *host;
58     gboolean is_clone;
59     char *email;
60     gboolean use_fileserver_port;
61 
62     char head[41];
63 
64     char *passwd;
65     char *worktree;
66 
67     int state;
68     int runtime_state;
69     int error;
70     /* Used to signify stop transfer for all threads. */
71     gboolean all_stop;
72 
73     /* When downloading with multi-thread, a block may be shared by
74      * multiple files. We can't remove a block before all *fetched* files with
75      * this block have been checked out.
76      * block_id -> ref_count.
77      */
78     GHashTable *blk_ref_cnts;
79     pthread_mutex_t ref_cnt_lock;
80 
81     /* For clone fs object progress */
82     int n_fs_objs;
83     int done_fs_objs;
84 
85     /* For upload progress */
86     int n_blocks;
87     int done_blocks;
88     /* For download progress */
89     gint64 total_download;
90     gint64 done_download;
91 
92     gint tx_bytes;              /* bytes transferred in this second. */
93     gint last_tx_bytes;         /* bytes transferred in the last second. */
94 };
95 typedef struct _HttpTxTask HttpTxTask;
96 
97 HttpTxManager *
98 http_tx_manager_new (struct _SeafileSession *seaf);
99 
100 int
101 http_tx_manager_start (HttpTxManager *mgr);
102 
103 int
104 http_tx_manager_add_download (HttpTxManager *manager,
105                               const char *repo_id,
106                               int repo_version,
107                               const char *host,
108                               const char *token,
109                               const char *server_head_id,
110                               gboolean is_clone,
111                               const char *passwd,
112                               const char *worktree,
113                               int protocol_version,
114                               const char *email,
115                               gboolean use_fileserver_port,
116                               const char *repo_name,
117                               GError **error);
118 
119 int
120 http_tx_manager_add_upload (HttpTxManager *manager,
121                             const char *repo_id,
122                             int repo_version,
123                             const char *host,
124                             const char *token,
125                             int protocol_version,
126                             gboolean use_fileserver_port,
127                             GError **error);
128 
129 struct _HttpProtocolVersion {
130     gboolean check_success;     /* TRUE if we get response from the server. */
131     gboolean not_supported;
132     int version;
133     int error_code;
134 };
135 typedef struct _HttpProtocolVersion HttpProtocolVersion;
136 
137 typedef void (*HttpProtocolVersionCallback) (HttpProtocolVersion *result,
138                                              void *user_data);
139 
140 /* Asynchronous interface for getting protocol version from a server.
141  * Also used to determine if the server support http sync.
142  */
143 int
144 http_tx_manager_check_protocol_version (HttpTxManager *manager,
145                                         const char *host,
146                                         gboolean use_fileserver_port,
147                                         HttpProtocolVersionCallback callback,
148                                         void *user_data);
149 
150 struct _HttpHeadCommit {
151     gboolean check_success;
152     gboolean is_corrupt;
153     gboolean is_deleted;
154     char head_commit[41];
155     int error_code;
156 };
157 typedef struct _HttpHeadCommit HttpHeadCommit;
158 
159 typedef void (*HttpHeadCommitCallback) (HttpHeadCommit *result,
160                                         void *user_data);
161 
162 /* Asynchronous interface for getting head commit info from a server. */
163 int
164 http_tx_manager_check_head_commit (HttpTxManager *manager,
165                                    const char *repo_id,
166                                    int repo_version,
167                                    const char *host,
168                                    const char *token,
169                                    gboolean use_fileserver_port,
170                                    HttpHeadCommitCallback callback,
171                                    void *user_data);
172 
173 typedef struct _HttpFolderPermReq {
174     char repo_id[37];
175     char *token;
176     gint64 timestamp;
177 } HttpFolderPermReq;
178 
179 typedef struct _HttpFolderPermRes {
180     char repo_id[37];
181     gint64 timestamp;
182     GList *user_perms;
183     GList *group_perms;
184 } HttpFolderPermRes;
185 
186 void
187 http_folder_perm_req_free (HttpFolderPermReq *req);
188 
189 void
190 http_folder_perm_res_free (HttpFolderPermRes *res);
191 
192 struct _HttpFolderPerms {
193     gboolean success;
194     GList *results;             /* List of HttpFolderPermRes */
195 };
196 typedef struct _HttpFolderPerms HttpFolderPerms;
197 
198 typedef void (*HttpGetFolderPermsCallback) (HttpFolderPerms *result,
199                                             void *user_data);
200 
201 /* Asynchronous interface for getting folder permissions for a repo. */
202 int
203 http_tx_manager_get_folder_perms (HttpTxManager *manager,
204                                   const char *host,
205                                   gboolean use_fileserver_port,
206                                   GList *folder_perm_requests, /* HttpFolderPermReq */
207                                   HttpGetFolderPermsCallback callback,
208                                   void *user_data);
209 
210 typedef struct _HttpLockedFilesReq {
211     char repo_id[37];
212     char *token;
213     gint64 timestamp;
214 } HttpLockedFilesReq;
215 
216 typedef struct _HttpLockedFilesRes {
217     char repo_id[37];
218     gint64 timestamp;
219     GHashTable *locked_files;   /* path -> by_me */
220 } HttpLockedFilesRes;
221 
222 void
223 http_locked_files_req_free (HttpLockedFilesReq *req);
224 
225 void
226 http_locked_files_res_free (HttpLockedFilesRes *res);
227 
228 struct _HttpLockedFiles {
229     gboolean success;
230     GList *results;             /* List of HttpLockedFilesRes */
231 };
232 typedef struct _HttpLockedFiles HttpLockedFiles;
233 
234 typedef void (*HttpGetLockedFilesCallback) (HttpLockedFiles *result,
235                                             void *user_data);
236 
237 /* Asynchronous interface for getting locked files for a repo. */
238 int
239 http_tx_manager_get_locked_files (HttpTxManager *manager,
240                                   const char *host,
241                                   gboolean use_fileserver_port,
242                                   GList *locked_files_requests,
243                                   HttpGetLockedFilesCallback callback,
244                                   void *user_data);
245 
246 /* Synchronous interface for locking/unlocking a file on the server. */
247 int
248 http_tx_manager_lock_file (HttpTxManager *manager,
249                            const char *host,
250                            gboolean use_fileserver_port,
251                            const char *token,
252                            const char *repo_id,
253                            const char *path);
254 
255 int
256 http_tx_manager_unlock_file (HttpTxManager *manager,
257                              const char *host,
258                              gboolean use_fileserver_port,
259                              const char *token,
260                              const char *repo_id,
261                              const char *path);
262 
263 GHashTable *
264 http_tx_manager_get_head_commit_ids (HttpTxManager *manager,
265                                      const char *host,
266                                      gboolean use_fileserver_port,
267                                      GList *repo_id_list);
268 
269 int
270 http_tx_task_download_file_blocks (HttpTxTask *task, const char *file_id);
271 
272 GList*
273 http_tx_manager_get_upload_tasks (HttpTxManager *manager);
274 
275 GList*
276 http_tx_manager_get_download_tasks (HttpTxManager *manager);
277 
278 HttpTxTask *
279 http_tx_manager_find_task (HttpTxManager *manager, const char *repo_id);
280 
281 void
282 http_tx_manager_cancel_task (HttpTxManager *manager,
283                              const char *repo_id,
284                              int task_type);
285 
286 int
287 http_tx_task_get_rate (HttpTxTask *task);
288 
289 const char *
290 http_task_state_to_str (int state);
291 
292 const char *
293 http_task_rt_state_to_str (int rt_state);
294 
295 #endif
296