Made file move async
This commit is contained in:
parent
c7d5849307
commit
667d4d8916
@ -1096,68 +1096,24 @@ file_exists (char *fname)
|
||||
return (g_access (fname, F_OK) == 0) ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
copy_file (char *dl_src, char *dl_dest, int permissions)
|
||||
/* Called when copy is complete to delete source file. */
|
||||
void move_file_callback(GObject *source_object, GAsyncResult *res,
|
||||
gpointer user_data)
|
||||
{
|
||||
int tmp_src, tmp_dest;
|
||||
gboolean ok = FALSE;
|
||||
char dl_tmp[4096];
|
||||
int return_tmp, return_tmp2;
|
||||
|
||||
if ((tmp_src = g_open (dl_src, O_RDONLY | OFLAGS, 0600)) == -1)
|
||||
{
|
||||
g_fprintf (stderr, "Unable to open() file '%s' (%s) !", dl_src,
|
||||
strerror (errno));
|
||||
return FALSE;
|
||||
GFile *source_file = (GFile *)source_object;
|
||||
GError *error;
|
||||
if (!g_file_copy_finish(source_file, res, &error)) {
|
||||
fprintf(stderr,
|
||||
"Error while copying file to save directory: %s\n",
|
||||
error->message);
|
||||
g_error_free(error);
|
||||
} else {
|
||||
if (!g_file_delete(source_file, NULL, NULL))
|
||||
fprintf(stderr,
|
||||
"Error deleting source file after copying file "
|
||||
"to save directory: %s\n",
|
||||
g_file_get_path(source_file));
|
||||
}
|
||||
|
||||
if ((tmp_dest =
|
||||
g_open (dl_dest, O_WRONLY | O_CREAT | O_TRUNC | OFLAGS, permissions)) < 0)
|
||||
{
|
||||
close (tmp_src);
|
||||
g_fprintf (stderr, "Unable to create file '%s' (%s) !", dl_src,
|
||||
strerror (errno));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
return_tmp = read (tmp_src, dl_tmp, sizeof (dl_tmp));
|
||||
|
||||
if (!return_tmp)
|
||||
{
|
||||
ok = TRUE;
|
||||
break;
|
||||
}
|
||||
|
||||
if (return_tmp < 0)
|
||||
{
|
||||
fprintf (stderr, "download_move_to_completed_dir(): "
|
||||
"error reading while moving file to save directory (%s)",
|
||||
strerror (errno));
|
||||
break;
|
||||
}
|
||||
|
||||
return_tmp2 = write (tmp_dest, dl_tmp, return_tmp);
|
||||
|
||||
if (return_tmp2 < 0)
|
||||
{
|
||||
fprintf (stderr, "download_move_to_completed_dir(): "
|
||||
"error writing while moving file to save directory (%s)",
|
||||
strerror (errno));
|
||||
break;
|
||||
}
|
||||
|
||||
if (return_tmp < sizeof (dl_tmp))
|
||||
{
|
||||
ok = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close (tmp_dest);
|
||||
close (tmp_src);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* Takes care of moving a file from a temporary download location to a completed location. */
|
||||
@ -1196,10 +1152,14 @@ move_file (char *src_dir, char *dst_dir, char *fname, int dccpermissions)
|
||||
/* link failed because either the two paths aren't on the */
|
||||
/* same filesystem or the filesystem doesn't support hard */
|
||||
/* links, so we have to do a copy. */
|
||||
if (copy_file (src, dst, dccpermissions))
|
||||
g_unlink (src);
|
||||
GFile* src_file = g_file_new_for_path(src);
|
||||
GFile* dst_file = g_file_new_for_path(dst);
|
||||
g_file_copy_async(src_file, dst_file, G_FILE_COPY_NONE,
|
||||
G_PRIORITY_DEFAULT, NULL, NULL, NULL,
|
||||
&move_file_callback, NULL);
|
||||
g_object_unref(src_file);
|
||||
g_object_unref(dst_file);
|
||||
}
|
||||
|
||||
g_free (dst);
|
||||
g_free (src);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user