File: copy.c

package info (click to toggle)
megatools 1.9.98-1%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,420 kB
  • sloc: ansic: 5,803; sh: 4,094; makefile: 72
file content (312 lines) | stat: -rw-r--r-- 8,332 bytes parent folder | download
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 *  megatools - Mega.nz client library and tools
 *  Copyright (C) 2013  Ondřej Jirman <megous@megous.com>
 *
 *  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 2 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 "tools.h"

static gchar* opt_remote_path;
static gchar* opt_local_path;
static gboolean opt_download;
static gboolean opt_noprogress;
static gboolean opt_dryrun;
static gboolean opt_no_previews;
static mega_session* s;

static GOptionEntry entries[] =
{
  { "remote",            'r',   0, G_OPTION_ARG_STRING,  &opt_remote_path,  "Remote directory",                 "PATH"  },
  { "local",             'l',   0, G_OPTION_ARG_STRING,  &opt_local_path,   "Local directory",                  "PATH"  },
  { "download",          'd',   0, G_OPTION_ARG_NONE,    &opt_download,     "Download files from mega",         NULL    },
  { "no-progress",       '\0',  0, G_OPTION_ARG_NONE,    &opt_noprogress,   "Disable progress bar",             NULL    },
  { "dryrun",            'n',   0, G_OPTION_ARG_NONE,    &opt_dryrun,       "Don't perform any actual changes", NULL    },
  { "disable-previews",  '\0',  0, G_OPTION_ARG_NONE,    &opt_no_previews,  "Don't generate previews",          NULL    },
  { NULL }
};

static gboolean status_callback(mega_status_data* data, gpointer userdata)
{
  if (!opt_noprogress && data->type == MEGA_STATUS_PROGRESS)
    tool_show_progress("copying...", data);

  return FALSE;
}

// upload operation

static gboolean up_sync_file(GFile* root, GFile* file, const gchar* remote_path)
{
  GError *local_err = NULL;

  mega_node* node = mega_session_stat(s, remote_path);
  if (node)
  {
    g_printerr("ERROR: File already exists at %s\n", remote_path);
    return FALSE;
  }

  g_print("F %s\n", remote_path);

  if (!opt_dryrun)
  {
    if (!mega_session_put(s, remote_path, g_file_get_path(file), &local_err))
    {
      if (!opt_noprogress)
        g_print("\r" ESC_CLREOL);

      g_printerr("ERROR: Upload failed for %s: %s\n", remote_path, local_err->message);
      g_clear_error(&local_err);
      return FALSE;
    }

    if (!opt_noprogress)
      g_print("\r" ESC_CLREOL);
  }

  return TRUE;
}

static gboolean up_sync_dir(GFile* root, GFile* file, const gchar* remote_path)
{
  GError *local_err = NULL;
  GFileInfo* i;

  if (root != file)
  {
    mega_node* node = mega_session_stat(s, remote_path);
    if (node && node->type == MEGA_NODE_FILE)
    {
      g_printerr("ERROR: File already exists at %s\n", remote_path);
      return FALSE;
    }

    if (!node)
    {
      g_print("D %s\n", remote_path);

      if (!opt_dryrun)
      {
        if (!mega_session_mkdir(s, remote_path, &local_err))
        {
          g_printerr("ERROR: Can't create remote directory %s: %s\n", remote_path, local_err->message);
          g_clear_error(&local_err);
          return FALSE;
        }
      }
    }
  }

  // sync children
  GFileEnumerator* e = g_file_enumerate_children(file, "standard::*", G_FILE_QUERY_INFO_NONE, NULL, &local_err);
  if (!e)
  {
    g_printerr("ERROR: Can't read local directory %s: %s\n", g_file_get_relative_path(root, file), local_err->message);
    g_clear_error(&local_err);
    return FALSE;
  }

  gboolean status = TRUE;
  while ((i = g_file_enumerator_next_file(e, NULL, NULL)))
  {
    const gchar* name = g_file_info_get_name(i);
    GFile* child = g_file_get_child(file, name);
    GFileType type = g_file_query_file_type(child, 0, NULL);
    gchar* child_remote_path = g_strconcat(remote_path, "/", name, NULL);

    if (type == G_FILE_TYPE_DIRECTORY)
    {
      if (!up_sync_dir(root, child, child_remote_path))
        status = FALSE;
    }
    else if (type == G_FILE_TYPE_REGULAR)
    {
      if (!up_sync_file(root, child, child_remote_path))
        status = FALSE;
    }
    else
    {
      g_printerr("WARNING: Skipping special file %s\n", g_file_get_relative_path(root, file));
    }

    g_free(child_remote_path);
    g_object_unref(child);
    g_object_unref(i);
  }

  g_object_unref(e);
  return status;
}

// download operation

static gboolean dl_sync_file(mega_node* node, GFile* file, const gchar* remote_path)
{
  GError *local_err = NULL;
  gchar* local_path = g_file_get_path(file);

  if (g_file_query_exists(file, NULL))
  {
    g_printerr("ERROR: File already exists at %s\n", local_path);
    return FALSE;
  }

  g_print("F %s\n", local_path);

  if (!opt_dryrun)
  {
    if (!mega_session_get(s, g_file_get_path(file), remote_path, &local_err))
    {
      if (!opt_noprogress)
        g_print("\r" ESC_CLREOL);

      g_printerr("ERROR: Download failed for %s: %s\n", remote_path, local_err->message);
      g_clear_error(&local_err);
      return FALSE;
    }

    if (!opt_noprogress)
      g_print("\r" ESC_CLREOL);
  }

  return TRUE;
}

static gboolean dl_sync_dir(mega_node* node, GFile* file, const gchar* remote_path)
{
  GError *local_err = NULL;
  gchar* local_path = g_file_get_path(file);

  if (!g_file_query_exists(file, NULL))
  {
    g_print("D %s\n", local_path);

    if (!opt_dryrun)
    {
      if (!g_file_make_directory(file, NULL, &local_err))
      {
        g_printerr("ERROR: Can't create local directory %s: %s\n", local_path, local_err->message);
        g_clear_error(&local_err);
        return FALSE;
      }
    }
  }
  else
  {
    if (g_file_query_file_type(file, 0, NULL) != G_FILE_TYPE_DIRECTORY)
    {
      g_printerr("ERROR: Can't create local directory %s: file exists\n", local_path);
      return FALSE;
    }
  }

  // sync children
  gboolean status = TRUE;
  GSList* children = mega_session_get_node_chilren(s, node), *i;
  for (i = children; i; i = i->next)
  {
    mega_node* child = i->data;
    gchar* child_remote_path = g_strconcat(remote_path, "/", child->name, NULL);
    GFile* child_file = g_file_get_child(file, child->name);

    if (child->type == MEGA_NODE_FILE)
    {
      if (!dl_sync_file(child, child_file, child_remote_path))
        status = FALSE;
    }
    else
    {
      if (!dl_sync_dir(child, child_file, child_remote_path))
        status = FALSE;
    }

    g_object_unref(child_file);
    g_free(child_remote_path);
  }

  g_slist_free(children);
  return status;
}

// main program

int main(int ac, char* av[])
{
  tool_init(&ac, &av, "- synchronize local and remote mega.nz directories", entries, TOOL_INIT_AUTH);

  if (!opt_local_path || !opt_remote_path)
  {
    g_printerr("ERROR: You must specify local and remote paths\n");
    return 1;
  }

  s = tool_start_session(TOOL_SESSION_OPEN);
  if (!s)
  {
    tool_fini(NULL);
    return 1;
  }

  mega_session_enable_previews(s, !opt_no_previews);

  mega_session_watch_status(s, status_callback, NULL);

  // check remote dir existence
  mega_node* remote_dir = mega_session_stat(s, opt_remote_path);
  if (!remote_dir)
  {
    g_printerr("ERROR: Remote directory not found %s\n", opt_remote_path);
    goto err0;
  }
  else if (!mega_node_is_container(remote_dir))
  {
    g_printerr("ERROR: Remote path must be a folder: %s\n", opt_remote_path);
    goto err0;
  }

  // check local dir existence
  GFile* local_file = g_file_new_for_path(opt_local_path);
  gint status = 0;

  if (opt_download)
  {
    if (!dl_sync_dir(remote_dir, local_file, opt_remote_path))
      status = 1;
  }
  else
  {
    if (g_file_query_file_type(local_file, 0, NULL) != G_FILE_TYPE_DIRECTORY)
    {
      g_printerr("ERROR: Local directory not found %s\n", opt_local_path);
      goto err1;
    }

    if (!up_sync_dir(local_file, local_file, opt_remote_path))
      status = 1;

    mega_session_save(s, NULL);
  }

  g_object_unref(local_file);
  tool_fini(s);
  return status;

err1:
  g_object_unref(local_file);
err0:
  tool_fini(s);
  return 1;
}