2015-11-03 03:40:38 +01:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <gui-fatal.h>
|
|
|
|
#include <libqubes-rpc-filecopy.h>
|
|
|
|
|
|
|
|
enum {
|
2018-05-26 00:54:09 +02:00
|
|
|
PROGRESS_FLAG_NORMAL,
|
|
|
|
PROGRESS_FLAG_INIT,
|
|
|
|
PROGRESS_FLAG_DONE
|
2015-11-03 03:40:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void do_notify_progress(long long total, int flag)
|
|
|
|
{
|
2018-05-26 00:54:09 +02:00
|
|
|
const char *du_size_env = getenv("FILECOPY_TOTAL_SIZE");
|
|
|
|
const char *progress_type_env = getenv("PROGRESS_TYPE");
|
|
|
|
const char *saved_stdout_env = getenv("SAVED_FD_1");
|
|
|
|
int ignore;
|
|
|
|
if (!progress_type_env)
|
|
|
|
return;
|
|
|
|
if (!strcmp(progress_type_env, "console") && du_size_env) {
|
|
|
|
char msg[256];
|
|
|
|
snprintf(msg, sizeof(msg), "sent %lld/%lld KB\r",
|
|
|
|
total / 1024, strtoull(du_size_env, NULL, 0));
|
|
|
|
ignore = write(2, msg, strlen(msg));
|
|
|
|
if (flag == PROGRESS_FLAG_DONE)
|
|
|
|
ignore = write(2, "\n", 1);
|
2018-07-25 14:44:52 +02:00
|
|
|
if (ignore < 0) {
|
|
|
|
/* silence gcc warning */
|
|
|
|
}
|
2018-05-26 00:54:09 +02:00
|
|
|
}
|
|
|
|
if (!strcmp(progress_type_env, "gui") && saved_stdout_env) {
|
|
|
|
char msg[256];
|
|
|
|
snprintf(msg, sizeof(msg), "%lld\n", total);
|
2018-07-25 14:44:52 +02:00
|
|
|
if (write(strtoul(saved_stdout_env, NULL, 0), msg, strlen(msg)) == -1
|
|
|
|
&& errno == EPIPE)
|
|
|
|
exit(32);
|
2018-05-26 00:54:09 +02:00
|
|
|
}
|
2015-11-03 03:40:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void notify_progress(int size, int flag)
|
|
|
|
{
|
2018-05-26 00:54:09 +02:00
|
|
|
static long long total = 0;
|
|
|
|
static long long prev_total = 0;
|
|
|
|
total += size;
|
|
|
|
if (total > prev_total + PROGRESS_NOTIFY_DELTA
|
|
|
|
|| (flag != PROGRESS_FLAG_NORMAL)) {
|
|
|
|
// check for possible error from qfile-unpacker; if error occured,
|
|
|
|
// exit() will be called, so don't bother with current state
|
|
|
|
// (notify_progress can be called as callback from copy_file())
|
|
|
|
if (flag == PROGRESS_FLAG_NORMAL)
|
|
|
|
wait_for_result();
|
|
|
|
do_notify_progress(total, flag);
|
|
|
|
prev_total = total;
|
|
|
|
}
|
2015-11-03 03:40:38 +01:00
|
|
|
}
|
|
|
|
|
2011-03-16 11:06:27 +01:00
|
|
|
|
2013-12-30 11:35:46 +01:00
|
|
|
char *get_abs_path(const char *cwd, const char *pathname)
|
2011-03-15 16:07:00 +01:00
|
|
|
{
|
2018-05-26 00:54:09 +02:00
|
|
|
char *ret;
|
|
|
|
if (pathname[0] == '/')
|
|
|
|
return strdup(pathname);
|
|
|
|
if (asprintf(&ret, "%s/%s", cwd, pathname) < 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return ret;
|
2011-03-15 16:07:00 +01:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:17:58 +02:00
|
|
|
int main(int argc, char **argv)
|
2011-03-15 16:07:00 +01:00
|
|
|
{
|
2018-05-26 00:54:09 +02:00
|
|
|
int i;
|
|
|
|
char *entry;
|
|
|
|
char *cwd;
|
|
|
|
char *sep;
|
|
|
|
int ignore_symlinks = 0;
|
2011-07-06 10:17:58 +02:00
|
|
|
|
2018-05-26 00:54:09 +02:00
|
|
|
qfile_pack_init();
|
|
|
|
register_error_handler(qfile_gui_fatal);
|
|
|
|
register_notify_progress(¬ify_progress);
|
|
|
|
notify_progress(0, PROGRESS_FLAG_INIT);
|
|
|
|
cwd = getcwd(NULL, 0);
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "--ignore-symlinks")==0) {
|
|
|
|
ignore_symlinks = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-14 22:12:46 +02:00
|
|
|
|
2018-05-26 00:54:09 +02:00
|
|
|
entry = get_abs_path(cwd, argv[i]);
|
2011-07-06 10:17:58 +02:00
|
|
|
|
2018-05-26 00:54:09 +02:00
|
|
|
do {
|
|
|
|
sep = rindex(entry, '/');
|
|
|
|
if (!sep)
|
|
|
|
gui_fatal
|
|
|
|
("Internal error: nonabsolute filenames not allowed");
|
|
|
|
*sep = 0;
|
|
|
|
} while (sep[1] == 0);
|
|
|
|
if (entry[0] == 0) {
|
|
|
|
if (chdir("/") < 0) {
|
|
|
|
gui_fatal("Internal error: chdir(\"/\") failed?!");
|
|
|
|
}
|
|
|
|
} else if (chdir(entry))
|
|
|
|
gui_fatal("chdir to %s", entry);
|
|
|
|
do_fs_walk(sep + 1, ignore_symlinks);
|
|
|
|
free(entry);
|
|
|
|
}
|
|
|
|
notify_end_and_wait_for_result();
|
|
|
|
notify_progress(0, PROGRESS_FLAG_DONE);
|
|
|
|
return 0;
|
2011-03-15 16:07:00 +01:00
|
|
|
}
|
2013-10-10 08:46:57 +02:00
|
|
|
|
|
|
|
|