Ubuntu Pastebin

Paste from laney at Wed, 13 May 2015 11:16:00 +0000

Download as text
 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
#include <glib.h>
#include <gio/gio.h>

/* gcc -g $(pkg-config --cflags glib-2.0 gio-2.0) test.c $(pkg-config --libs glib-2.0 gio-2.0) */

/* - Create an empty file
 * - Trash it
 * - See if trash::orig-path is set
 * - Delete it from the trash & quit
 */

static void trash_changed (GFileMonitor *monitor,
        GFile *file,
        GFile *other_file,
        GFileMonitorEvent event_type,
        gpointer user_data)
{
    GMainLoop *loop = (GMainLoop *) user_data;
    const char * orig_path;
    GFileInfo *info;
    char * uri = g_file_get_uri (file);

    switch (event_type)
    {
        case G_FILE_MONITOR_EVENT_CREATED:
            info = g_file_query_info (file, "trash::orig-path",
                    G_FILE_QUERY_INFO_NONE, NULL, NULL);

            orig_path = g_file_info_get_attribute_byte_string (info,
                    "trash::orig-path");

            if (orig_path)
                g_printf ("%s trashed from %s - bug not reproduced :D\n", uri, orig_path);
            else
                g_printf ("trash::orig-path is not set on %s - bug reproduced :(\n", uri);

            g_file_delete (file, NULL, NULL);
            g_object_unref (info);
            break;
        case G_FILE_MONITOR_EVENT_DELETED:
            g_printf ("Cleaned up the trash - exiting.\n");
            g_main_loop_quit (loop);
            break;
    }

}

void monitor_trash (GMainLoop *loop)
{
    GFile *file;
    GFileMonitor *monitor;

    file = g_file_new_for_uri ("trash://");

    monitor = g_file_monitor_directory (file,
            G_FILE_MONITOR_NONE,
            NULL,
            NULL);

    g_object_unref (file);

    g_signal_connect (monitor,
            "changed",
            G_CALLBACK (trash_changed),
            loop);
}

void trash_test_file ()
{
    GFile *file = g_file_new_for_path ("test-file");
    
    g_object_unref (g_file_create (file,
                G_FILE_CREATE_NONE,
                NULL, /* GCancellable */
                NULL)); /* GError */

    if (!g_file_trash (file, NULL, NULL))
        g_error("Couldn't trash file");

    g_object_unref (file);
}

void main ()
{
    GMainLoop *loop;

    loop = g_main_loop_new (NULL, TRUE);

    monitor_trash (loop);

    g_idle_add ((GSourceFunc) trash_test_file, NULL);

    g_main_loop_run (loop);
}
Download as text