~bibledit/bibledit/client

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 Copyright (©) 2003-2020 Teus Benschop.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <sendreceive/files.h>
#include <sendreceive/logic.h>
#include <filter/url.h>
#include <filter/roles.h>
#include <filter/string.h>
#include <filter/merge.h>
#include <filter/date.h>
#include <tasks/logic.h>
#include <config/globals.h>
#include <client/logic.h>
#include <locale/translate.h>
#include <webserver/request.h>
#include <sync/logic.h>
#include <sync/files.h>
#include <checksum/logic.h>
#include <database/logs.h>
#include <database/config/general.h>
#include <database/privileges.h>


int sendreceive_files_watchdog = 0;


void sendreceive_files_done ()
{
  sendreceive_files_watchdog = 0;
  config_globals_syncing_files = false;
}


string sendreceive_files_text ()
{
  return translate("Files") + ": ";
}


string sendreceive_files_sendreceive_text ()
{
  return sendreceive_files_text () + translate ("Synchronizing");
}


string sendreceive_files_up_to_date_text ()
{
  return sendreceive_files_text () + translate ("Up to date");
}


void sendreceive_files ()
{
  // Watchdog handler.
  if (sendreceive_files_watchdog) {
    int time = filter_date_seconds_since_epoch ();
    if (time < (sendreceive_files_watchdog + 900)) {
      Database_Logs::log (sendreceive_files_text () + translate("Still busy"), Filter_Roles::translator ());
      return;
    }
    Database_Logs::log (sendreceive_files_text () + translate("Watchdog timeout"), Filter_Roles::translator ());
  }
  sendreceive_files_kick_watchdog ();
  config_globals_syncing_files = true;
  
  
  // If any of the prioritized synchronization tasks run, postpone the current task and do not start it.
  if (sendreceive_logic_prioritized_task_is_active ()) {
    sendreceive_files_done ();
    this_thread::sleep_for (chrono::seconds (5));
    tasks_logic_queue (SYNCFILES);
    return;
  }
  
  
  Webserver_Request request;
  Sync_Logic sync_logic = Sync_Logic (&request);

  
  Database_Logs::log (sendreceive_files_sendreceive_text (), Filter_Roles::translator ());
  
  
  string address = Database_Config_General::getServerAddress ();
  int port = Database_Config_General::getServerPort ();
  string url = client_logic_url (address, port, sync_files_url ());
  
  
  // The verion number of the set of directories this client uses.
  int version = 5;
  
  
  map <string, string> post;

  
  // The client user is the sole user registered on the system.
  vector <string> users = request.database_users ()->getUsers ();
  if (users.empty ()) {
    Database_Logs::log (translate("No user found"), Filter_Roles::translator ());
    sendreceive_files_done ();
    return;
  }
  string user = users [0];
  post ["u"] = bin2hex (user);

  
  post ["v"] = convert_to_string (version);
  string error;
  string response;
  int iresponse = 0;
  

  // Request the checksum of all relevant files on the server.
  // Compare it with the local checksum for the same set of files.
  // If the two match: Ready.
  post ["a"] = convert_to_string (Sync_Logic::files_total_checksum);
  response = sync_logic.post (post, url, error);
  if (!error.empty ()) {
    Database_Logs::log (sendreceive_files_text () + "Failure requesting total checksum: " + error, Filter_Roles::translator ());
    sendreceive_files_done ();
    return;
  }
  iresponse = convert_to_int (response);
  int checksum = Sync_Logic::files_get_total_checksum (version, user);
  if (iresponse == checksum) {
    Database_Logs::log (sendreceive_files_up_to_date_text (), Filter_Roles::translator ());
    sendreceive_files_done ();
    return;
  }

  
  // Go through the directories relevant to the version.
  vector <string> directories = Sync_Logic::files_get_directories (version, user);
  for (size_t d = 0; d < directories.size (); d++) {
    

    // The directory name itself is not posted to the server,
    // but rather the index of the directory in the entire list.
    // This is for security reasons.
    post ["d"] = convert_to_string (d);
    string directory = directories [d];
    

    // Request the total checksum of a directory on the server.
    // Compare it with the local checksum for the directory on the client.
    // If the two match: This directory is ready.
    post ["a"] = convert_to_string (Sync_Logic::files_directory_checksum);
    response = sync_logic.post (post, url, error);
    if (!error.empty ()) {
      Database_Logs::log (sendreceive_files_text () + "Failure requesting directory checksum: " + error, Filter_Roles::translator ());
      sendreceive_files_done ();
      return;
    }
    iresponse = convert_to_int (response);
    int checksum = Sync_Logic::files_get_directory_checksum (directory);
    if (iresponse == checksum) {
      continue;
    }

    
    // Retrieve the list of files in a directory on the server.
    post ["a"] = convert_to_string (Sync_Logic::files_directory_files);
    response = sync_logic.post (post, url, error);
    if (!error.empty ()) {
      Database_Logs::log (sendreceive_files_text () + "Failure requesting directory files: " + error, Filter_Roles::translator ());
      sendreceive_files_done ();
      return;
    }
    vector <string> server_files = filter_string_explode (response, '\n');
    
    
    // Delete files that exist locally but not on the server.
    vector <string> client_files = Sync_Logic::files_get_files (directory);
    vector <string> files = filter_string_array_diff (client_files, server_files);
    for (auto file : files) {
      Database_Logs::log (sendreceive_files_text () + "Deleting file: " + filter_url_create_path (directory, file), Filter_Roles::translator ());
      string path = filter_url_create_root_path (directory, file);
      filter_url_unlink (path);
      // Attempt to delete the directory, which will only succeed if it is empty.
      path = filter_url_dirname (path);
      filter_url_unlink (path);
    }
    

    // Deal with each file individually.
    for (auto & file : server_files) {


      // Request checksum of this file,
      // compare it with the local checksum,
      // and skip the file if the checksums match.
      post ["a"] = convert_to_string (Sync_Logic::files_file_checksum);
      post ["f"] = file;
      response = sync_logic.post (post, url, error);
      if (!error.empty ()) {
        Database_Logs::log (sendreceive_files_text () + "Failure requesting checksum file: " + error, Filter_Roles::translator ());
        sendreceive_files_done ();
        return;
      }
      int iresponse = convert_to_int (response);
      int checksum = Sync_Logic::files_get_file_checksum (directory, file);
      if (iresponse == checksum) {
        continue;
      }
      

      sendreceive_files_kick_watchdog ();
      
      
      // Download the file from the server, and store it locally on the client.
      Database_Logs::log (sendreceive_files_text () + "Downloading " + filter_url_create_path (directory, file), Filter_Roles::translator ());
      // Local file path where to save resource.
      string fullpath = filter_url_create_root_path (directory, file);
      // Create directory if it does not yet exist.
      string dirpath = filter_url_dirname (fullpath);
      if (!file_or_dir_exists (dirpath)) {
        filter_url_mkdir (dirpath);
      }
      string download_url = filter_url_build_http_query (url, "a", convert_to_string (Sync_Logic::files_file_download));
      download_url = filter_url_build_http_query (download_url, "v", convert_to_string (version));
      download_url = filter_url_build_http_query (download_url, "d", convert_to_string (d));
      download_url = filter_url_build_http_query (download_url, "u", bin2hex (user));
      download_url = filter_url_build_http_query (download_url, "f", filter_url_urlencode (file));
      // Download and save file locally.
      filter_url_download_file (download_url, fullpath, error, true);
      if (!error.empty ()) {
        Database_Logs::log (sendreceive_files_text () + "Failure downloading file: " + error, Filter_Roles::translator ());
        sendreceive_files_done ();
        return;
      }
      // When downloading privileges, load them in the database on the client.
      if (directory == database_privileges_directory (user)) {
        if (file == database_privileges_file ()) {
          Database_Privileges::load (user, filter_url_file_get_contents (fullpath));
        }
      }
    }
  }

  
  // Done.
  Database_Logs::log (sendreceive_files_text () + "Now up to date", Filter_Roles::translator ());
  sendreceive_files_done ();
}


void sendreceive_files_kick_watchdog ()
{
  sendreceive_files_watchdog = filter_date_seconds_since_epoch ();
}