/** * When the source .torrent file is deleted * (such as, if it was a temp file that a web browser passed to us), * gtk invokes this callback and `filename' will be NULL. * The `filename' tests here are to prevent us from losing the current * metadata when that happens. */ static void sourceChanged (GtkFileChooserButton * b, gpointer gdata) { struct OpenData * o = gdata; char * filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (b)); /* maybe instantiate a torrent */ if (filename || !o->tor) { int err = 0; int new_file = 0; int duplicate_id = 0; tr_torrent * torrent; if (filename && (!o->filename || !tr_sys_path_is_same (filename, o->filename, NULL))) { g_free (o->filename); o->filename = g_strdup (filename); tr_ctorSetMetainfoFromFile (o->ctor, o->filename); new_file = 1; } tr_ctorSetDownloadDir (o->ctor, TR_FORCE, o->downloadDir); tr_ctorSetPaused (o->ctor, TR_FORCE, TRUE); tr_ctorSetDeleteSource (o->ctor, FALSE); if ((torrent = tr_torrentNew (o->ctor, &err, &duplicate_id))) { removeOldTorrent (o); o->tor = torrent; } else if (new_file) { tr_torrent * tor; if (duplicate_id) tor = gtr_core_find_torrent (o->core, duplicate_id); else tor = NULL; gtr_add_torrent_error_dialog (GTK_WIDGET (b), err, tor, o->filename); } updateTorrent (o); } g_free (filename); }
static void downloadDirChanged (GtkFileChooserButton * b, gpointer gdata) { struct OpenData * data = gdata; char * fname = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (b)); if (fname && (!data->downloadDir || !tr_sys_path_is_same (fname, data->downloadDir, NULL))) { g_free (data->downloadDir); data->downloadDir = g_strdup (fname); updateTorrent (data); gtr_freespace_label_set_dir (data->freespace_label, data->downloadDir); } g_free (fname); }
static int test_path_is_same (void) { char * const test_dir = create_test_dir (__FUNCTION__); tr_error * err = NULL; char * path1, * path2, * path3; path1 = tr_buildPath (test_dir, "a", NULL); path2 = tr_buildPath (test_dir, "b", NULL); path3 = tr_buildPath (path2, "c", NULL); /* Two non-existent files are not the same */ check (!tr_sys_path_is_same (path1, path1, &err)); check (err == NULL); check (!tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); /* Two same files are the same */ libtest_create_file_with_string_contents (path1, "test"); check (tr_sys_path_is_same (path1, path1, &err)); check (err == NULL); /* Existent and non-existent files are not the same */ check (!tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); check (!tr_sys_path_is_same (path2, path1, &err)); check (err == NULL); /* Two separate files (even with same content) are not the same */ libtest_create_file_with_string_contents (path2, "test"); check (!tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); tr_sys_path_remove (path1, NULL); /* Two same directories are the same */ tr_sys_dir_create (path1, 0, 0777, NULL); check (tr_sys_path_is_same (path1, path1, &err)); check (err == NULL); /* File and directory are not the same */ check (!tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); check (!tr_sys_path_is_same (path2, path1, &err)); check (err == NULL); tr_sys_path_remove (path2, NULL); /* Two separate directories are not the same */ tr_sys_dir_create (path2, 0, 0777, NULL); check (!tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); tr_sys_path_remove (path1, NULL); tr_sys_path_remove (path2, NULL); if (create_symlink (path1, ".", true)) { /* Directory and symlink pointing to it are the same */ check (tr_sys_path_is_same (path1, test_dir, &err)); check (err == NULL); check (tr_sys_path_is_same (test_dir, path1, &err)); check (err == NULL); /* Non-existent file and symlink are not the same */ check (!tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); check (!tr_sys_path_is_same (path2, path1, &err)); check (err == NULL); /* Symlinks pointing to different directories are not the same */ create_symlink (path2, "..", true); check (!tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); check (!tr_sys_path_is_same (path2, path1, &err)); check (err == NULL); tr_sys_path_remove (path2, NULL); /* Symlinks pointing to same directory are the same */ create_symlink (path2, ".", true); check (tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); tr_sys_path_remove (path2, NULL); /* Directory and symlink pointing to another directory are not the same */ tr_sys_dir_create (path2, 0, 0777, NULL); check (!tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); check (!tr_sys_path_is_same (path2, path1, &err)); check (err == NULL); /* Symlinks pointing to same directory are the same */ create_symlink (path3, "..", true); check (tr_sys_path_is_same (path1, path3, &err)); check (err == NULL); tr_sys_path_remove (path1, NULL); /* File and symlink pointing to directory are not the same */ libtest_create_file_with_string_contents (path1, "test"); check (!tr_sys_path_is_same (path1, path3, &err)); check (err == NULL); check (!tr_sys_path_is_same (path3, path1, &err)); check (err == NULL); tr_sys_path_remove (path3, NULL); /* File and symlink pointing to same file are the same */ create_symlink (path3, path1, false); check (tr_sys_path_is_same (path1, path3, &err)); check (err == NULL); check (tr_sys_path_is_same (path3, path1, &err)); check (err == NULL); /* Symlinks pointing to non-existent files are not the same */ tr_sys_path_remove (path1, NULL); create_symlink (path1, "missing", false); tr_sys_path_remove (path3, NULL); create_symlink (path3, "missing", false); check (!tr_sys_path_is_same (path1, path3, &err)); check (err == NULL); check (!tr_sys_path_is_same (path3, path1, &err)); check (err == NULL); tr_sys_path_remove (path3, NULL); /* Symlinks pointing to same non-existent file are not the same */ create_symlink (path3, ".." NATIVE_PATH_SEP "missing", false); check (!tr_sys_path_is_same (path1, path3, &err)); check (err == NULL); check (!tr_sys_path_is_same (path3, path1, &err)); check (err == NULL); /* Non-existent file and symlink pointing to non-existent file are not the same */ tr_sys_path_remove (path3, NULL); check (!tr_sys_path_is_same (path1, path3, &err)); check (err == NULL); check (!tr_sys_path_is_same (path3, path1, &err)); check (err == NULL); tr_sys_path_remove (path2, NULL); tr_sys_path_remove (path1, NULL); } else { fprintf (stderr, "WARNING: [%s] unable to run symlink tests\n", __FUNCTION__); } tr_free (path3); path3 = tr_buildPath (test_dir, "c", NULL); libtest_create_file_with_string_contents (path1, "test"); if (create_hardlink (path2, path1)) { /* File and hardlink to it are the same */ check (tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); /* Two hardlinks to the same file are the same */ create_hardlink (path3, path2); check (tr_sys_path_is_same (path2, path3, &err)); check (err == NULL); check (tr_sys_path_is_same (path1, path3, &err)); check (err == NULL); tr_sys_path_remove (path2, NULL); check (tr_sys_path_is_same (path1, path3, &err)); check (err == NULL); tr_sys_path_remove (path3, NULL); /* File and hardlink to another file are not the same */ libtest_create_file_with_string_contents (path3, "test"); create_hardlink (path2, path3); check (!tr_sys_path_is_same (path1, path2, &err)); check (err == NULL); check (!tr_sys_path_is_same (path2, path1, &err)); check (err == NULL); tr_sys_path_remove (path3, NULL); tr_sys_path_remove (path2, NULL); } else { fprintf (stderr, "WARNING: [%s] unable to run hardlink tests\n", __FUNCTION__); } if (create_symlink (path2, path1, false) && create_hardlink (path3, path1)) { check (tr_sys_path_is_same (path2, path3, &err)); check (err == NULL); } else { fprintf (stderr, "WARNING: [%s] unable to run combined symlink and hardlink tests\n", __FUNCTION__); } tr_sys_path_remove (path3, NULL); tr_sys_path_remove (path2, NULL); tr_sys_path_remove (path1, NULL); tr_free (path3); tr_free (path2); tr_free (path1); tr_free (test_dir); return 0; }