File: monitor.c

package info (click to toggle)
glib2.0 2.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-backports, jessie-kfreebsd, jessie-kfreebsd-proposed-updates
  • size: 82,084 kB
  • sloc: ansic: 411,692; xml: 15,280; sh: 12,977; python: 5,145; makefile: 3,567; perl: 1,422; cpp: 9
file content (186 lines) | stat: -rw-r--r-- 3,995 bytes parent folder | download | duplicates (2)
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
#include <gio/gio.h>

typedef struct {
  GFile *file;
  GOutputStream *stream;
  GMainLoop *loop;
  gint state;
} MonitorData;

static gboolean
create_file_idle (gpointer data)
{
  MonitorData *d = data;
  GError *error = NULL;

  g_assert (d->state == 0);

  d->stream = (GOutputStream*)g_file_create (d->file, 0, NULL, &error);
  g_assert_no_error (error);

  d->state = 1;

  return G_SOURCE_REMOVE;
}

static gboolean
write_file_idle (gpointer data)
{
  MonitorData *d = data;
  GError *error = NULL;

  g_assert (d->state == 2);

  g_output_stream_write (d->stream, "abcd", 4, NULL, &error);
  g_assert_no_error (error);
  g_object_unref (d->stream);
  d->stream = NULL;

  d->state = 3;


  return G_SOURCE_REMOVE;
}

static gboolean
delete_file_idle (gpointer data)
{
  MonitorData *d = data;
  GError *error = NULL;

  g_assert (d->state == 4);

  g_file_delete (d->file, NULL, &error);
  g_assert_no_error (error);

  d->state = 5;

  return G_SOURCE_REMOVE;
}

static void
changed_cb (GFileMonitor      *monitor,
            GFile             *file,
            GFile             *other_file,
            GFileMonitorEvent  event,
            gpointer           data)
{
  MonitorData *d = data;

  switch (d->state)
    {
    case 1:
      g_assert (event == G_FILE_MONITOR_EVENT_CREATED);
      d->state = 2;
      g_idle_add (write_file_idle, data);
      break;
    case 3:
      g_assert (event == G_FILE_MONITOR_EVENT_CHANGED ||
                event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
      if (event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
        {
          d->state = 4;
          g_idle_add (delete_file_idle, data);
        }
      break;
    case 5:
      g_assert (event == G_FILE_MONITOR_EVENT_DELETED);
      d->state = 6;
      if (d->loop)
        g_main_loop_quit (d->loop);
      break;
    default:
      g_assert_not_reached ();
      break;
    }
}

static void
file_changed_cb (GFileMonitor      *monitor,
                 GFile             *file,
                 GFile             *other_file,
                 GFileMonitorEvent  event,
                 gpointer           data)
{
  gint *state = data;

  switch (*state)
    {
    case 0:
      g_assert (event == G_FILE_MONITOR_EVENT_CREATED);
      *state = 1;
      break;
    case 1:
      g_assert (event == G_FILE_MONITOR_EVENT_CHANGED ||
                event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
      if (event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
        *state = 2;
      break;
    case 2:
      g_assert (event == G_FILE_MONITOR_EVENT_DELETED);
      *state = 3;
      break;
    default:
      g_assert_not_reached ();
      break;
    }
}

static void
test_directory_monitor (void)
{
  gchar *path;
  GFile *file;
  GFile *child;
  GFileMonitor *dir_monitor;
  GFileMonitor *file_monitor;
  GError *error = NULL;
  MonitorData data;
  gint state;
  GMainLoop *loop;

  path = g_mkdtemp (g_strdup ("file_monitor_XXXXXX"));
  file = g_file_new_for_path (path);
  dir_monitor = g_file_monitor_directory (file, 0, NULL, &error);
  g_assert_no_error (error);

  child = g_file_get_child (file, "test-file");
  file_monitor = g_file_monitor_file (child, 0, NULL, &error);
  g_assert_no_error (error);

  loop = g_main_loop_new (NULL, FALSE);

  g_signal_connect (dir_monitor, "changed", G_CALLBACK (changed_cb), &data);
  g_signal_connect (file_monitor, "changed", G_CALLBACK (file_changed_cb), &state);

  data.loop = loop;
  data.file = child;
  data.state = 0;
  state = 0;

  g_idle_add (create_file_idle, &data);

  g_main_loop_run (loop);

  g_assert_cmpint (data.state, ==, 6);
  g_assert_cmpint (state, ==, 3);

  g_main_loop_unref (loop);
  g_object_unref (dir_monitor);
  g_object_unref (file_monitor);
  g_object_unref (child);
  g_object_unref (file);
  g_free (path);
}

int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

#ifdef __linux__
  g_test_add_func ("/monitor/directory", test_directory_monitor);
#endif

  return g_test_run ();
}