1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3 #include "common.h"
4
5 #include "seafile-session.h"
6
7 #include "utils.h"
8 #include "wt-monitor.h"
9 #define DEBUG_FLAG SEAFILE_DEBUG_WATCH
10 #include "log.h"
11
12 #include "job-mgr.h"
13
14 int
seaf_wt_monitor_start(SeafWTMonitor * monitor)15 seaf_wt_monitor_start (SeafWTMonitor *monitor)
16 {
17 if (seaf_pipe (monitor->cmd_pipe) < 0) {
18 seaf_warning ("[wt mon] failed to create command pipe: %s.\n",
19 strerror(errno));
20 return -1;
21 }
22
23 if (seaf_pipe (monitor->res_pipe) < 0) {
24 seaf_warning ("[wt mon] failed to create result pipe: %s.\n",
25 strerror(errno));
26 return -1;
27 }
28
29 if (seaf_job_manager_schedule_job (monitor->seaf->job_mgr,
30 monitor->job_func,
31 NULL, monitor) < 0) {
32 seaf_warning ("[wt mon] failed to start monitor thread.\n");
33 return -1;
34 }
35
36 return 0;
37 }
38
39 int
seaf_wt_monitor_watch_repo(SeafWTMonitor * monitor,const char * repo_id,const char * worktree)40 seaf_wt_monitor_watch_repo (SeafWTMonitor *monitor,
41 const char *repo_id,
42 const char *worktree)
43 {
44 WatchCommand cmd;
45 int res;
46
47 memset (&cmd, 0, sizeof(cmd));
48 memcpy (cmd.repo_id, repo_id, 37);
49 cmd.type = CMD_ADD_WATCH;
50 g_strlcpy (cmd.worktree, worktree, SEAF_PATH_MAX);
51
52 int n = seaf_pipe_writen (monitor->cmd_pipe[1], &cmd, sizeof(cmd));
53
54 if (n != sizeof(cmd)) {
55 seaf_warning ("[wt mon] fail to write command pipe.\n");
56 return -1;
57 }
58
59 seaf_debug ("send a watch command, repo %s\n", repo_id);
60
61 n = seaf_pipe_readn (monitor->res_pipe[0], &res, sizeof(int));
62 if (n != sizeof(int)) {
63 seaf_warning ("[wt mon] fail to read result pipe.\n");
64 return -1;
65 }
66
67 return res;
68 }
69
70 int
seaf_wt_monitor_unwatch_repo(SeafWTMonitor * monitor,const char * repo_id)71 seaf_wt_monitor_unwatch_repo (SeafWTMonitor *monitor, const char *repo_id)
72 {
73 WatchCommand cmd;
74 int res;
75
76 memset (&cmd, 0, sizeof(cmd));
77 memcpy (cmd.repo_id, repo_id, 37);
78 cmd.type = CMD_DELETE_WATCH;
79
80 int n = seaf_pipe_writen (monitor->cmd_pipe[1], &cmd, sizeof(cmd));
81
82 if (n != sizeof(cmd)) {
83 seaf_warning ("[wt mon] fail to write command pipe.\n");
84 return -1;
85 }
86
87 seaf_debug ("send an unwatch command, repo %s\n", repo_id);
88
89 n = seaf_pipe_readn (monitor->res_pipe[0], &res, sizeof(int));
90 if (n != sizeof(int)) {
91 seaf_warning ("[wt mon] fail to read result pipe.\n");
92 return -1;
93 }
94
95 return res;
96 }
97
98 int
seaf_wt_monitor_refresh_repo(SeafWTMonitor * monitor,const char * repo_id)99 seaf_wt_monitor_refresh_repo (SeafWTMonitor *monitor, const char *repo_id)
100 {
101 WatchCommand cmd;
102 int res;
103
104 memset (&cmd, 0, sizeof(cmd));
105 memcpy (cmd.repo_id, repo_id, 37);
106 cmd.type = CMD_REFRESH_WATCH;
107
108 int n = seaf_pipe_writen (monitor->cmd_pipe[1], &cmd, sizeof(cmd));
109
110 if (n != sizeof(cmd)) {
111 seaf_warning ("[wt mon] fail to write command pipe.\n");
112 return -1;
113 }
114
115 seaf_debug ("send a refresh command, repo %s\n", repo_id);
116
117 n = seaf_pipe_readn (monitor->res_pipe[0], &res, sizeof(int));
118 if (n != sizeof(int)) {
119 seaf_warning ("[wt mon] fail to read result pipe.\n");
120 return -1;
121 }
122
123 return res;
124 }
125