Coverage Report

Created: 2025-07-01 07:09

/src/glib/gio/gpollfilemonitor.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 * 
3
 * Copyright (C) 2006-2007 Red Hat, Inc.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Author: Alexander Larsson <alexl@redhat.com>
19
 */
20
21
#include "config.h"
22
#include <string.h>
23
24
#include "gpollfilemonitor.h"
25
#include "gfile.h"
26
#include "gfilemonitor.h"
27
#include "gfileinfo.h"
28
29
30
static gboolean g_poll_file_monitor_cancel (GFileMonitor* monitor);
31
static void schedule_poll_timeout (GPollFileMonitor* poll_monitor);
32
33
struct _GPollFileMonitor
34
{
35
  GFileMonitor parent_instance;
36
  GFile *file;
37
  GFileInfo *last_info;
38
  GSource *timeout;
39
};
40
41
0
#define POLL_TIME_SECS 5
42
43
#define g_poll_file_monitor_get_type _g_poll_file_monitor_get_type
44
G_DEFINE_TYPE (GPollFileMonitor, g_poll_file_monitor, G_TYPE_FILE_MONITOR)
45
46
static void
47
g_poll_file_monitor_finalize (GObject* object)
48
0
{
49
0
  GPollFileMonitor* poll_monitor;
50
  
51
0
  poll_monitor = G_POLL_FILE_MONITOR (object);
52
53
0
  g_poll_file_monitor_cancel (G_FILE_MONITOR (poll_monitor));
54
0
  g_object_unref (poll_monitor->file);
55
0
  g_clear_object (&poll_monitor->last_info);
56
57
0
  G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize (object);
58
0
}
59
60
61
static void
62
g_poll_file_monitor_class_init (GPollFileMonitorClass* klass)
63
0
{
64
0
  GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
65
0
  GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
66
  
67
0
  gobject_class->finalize = g_poll_file_monitor_finalize;
68
69
0
  file_monitor_class->cancel = g_poll_file_monitor_cancel;
70
0
}
71
72
static void
73
g_poll_file_monitor_init (GPollFileMonitor* poll_monitor)
74
0
{
75
0
}
76
77
static int
78
calc_event_type (GFileInfo *last,
79
     GFileInfo *new)
80
0
{
81
0
  if (last == NULL && new == NULL)
82
0
    return -1;
83
84
0
  if (last == NULL && new != NULL)
85
0
    return G_FILE_MONITOR_EVENT_CREATED;
86
  
87
0
  if (last != NULL && new == NULL)
88
0
    return G_FILE_MONITOR_EVENT_DELETED;
89
90
0
  if (g_strcmp0 (g_file_info_get_etag (last), g_file_info_get_etag (new)))
91
0
    return G_FILE_MONITOR_EVENT_CHANGED;
92
  
93
0
  if (g_file_info_get_size (last) != g_file_info_get_size (new))
94
0
    return G_FILE_MONITOR_EVENT_CHANGED;
95
96
0
  return -1;
97
0
}
98
99
static void
100
got_new_info (GObject      *source_object,
101
              GAsyncResult *res,
102
              gpointer      user_data)
103
0
{
104
0
  GPollFileMonitor* poll_monitor = user_data;
105
0
  GFileInfo *info;
106
0
  int event;
107
108
0
  info = g_file_query_info_finish (poll_monitor->file, res, NULL);
109
110
0
  if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
111
0
    {
112
0
      event = calc_event_type (poll_monitor->last_info, info);
113
114
0
      if (event != -1)
115
0
  {
116
0
    g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
117
0
             poll_monitor->file,
118
0
             NULL, event);
119
    /* We're polling so slowly anyway, so always emit the done hint */
120
0
    if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)) &&
121
0
             (event == G_FILE_MONITOR_EVENT_CHANGED || event == G_FILE_MONITOR_EVENT_CREATED))
122
0
      g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
123
0
               poll_monitor->file,
124
0
               NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
125
0
  }
126
      
127
0
      if (poll_monitor->last_info)
128
0
  {
129
0
    g_object_unref (poll_monitor->last_info);
130
0
    poll_monitor->last_info = NULL;
131
0
  }
132
      
133
0
      if (info)
134
0
  poll_monitor->last_info = g_object_ref (info);
135
      
136
0
      schedule_poll_timeout (poll_monitor);
137
0
    }
138
139
0
  if (info)
140
0
    g_object_unref (info);
141
  
142
0
  g_object_unref (poll_monitor);
143
0
}
144
145
static gboolean
146
poll_file_timeout (gpointer data)
147
0
{
148
0
  GPollFileMonitor* poll_monitor = data;
149
150
0
  g_source_unref (poll_monitor->timeout);
151
0
  poll_monitor->timeout = NULL;
152
153
0
  g_file_query_info_async (poll_monitor->file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
154
0
       0, 0, NULL, got_new_info, g_object_ref (poll_monitor));
155
  
156
0
  return G_SOURCE_REMOVE;
157
0
}
158
159
static void
160
schedule_poll_timeout (GPollFileMonitor* poll_monitor)
161
0
{
162
0
  poll_monitor->timeout = g_timeout_source_new_seconds (POLL_TIME_SECS);
163
0
  g_source_set_callback (poll_monitor->timeout, poll_file_timeout, poll_monitor, NULL);
164
0
  g_source_attach (poll_monitor->timeout, g_main_context_get_thread_default ());
165
0
}
166
167
static void
168
got_initial_info (GObject      *source_object,
169
                  GAsyncResult *res,
170
                  gpointer      user_data)
171
0
{
172
0
  GPollFileMonitor* poll_monitor = user_data;
173
0
  GFileInfo *info;
174
175
0
  info = g_file_query_info_finish (poll_monitor->file, res, NULL);
176
177
0
  poll_monitor->last_info = info;
178
179
0
  if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
180
0
    schedule_poll_timeout (poll_monitor);
181
  
182
0
  g_object_unref (poll_monitor);
183
0
}
184
185
/**
186
 * _g_poll_file_monitor_new:
187
 * @file: a #GFile.
188
 * 
189
 * Polls @file for changes.
190
 * 
191
 * Returns: a new #GFileMonitor for the given #GFile. 
192
 **/
193
GFileMonitor*
194
_g_poll_file_monitor_new (GFile *file)
195
0
{
196
0
  GPollFileMonitor* poll_monitor;
197
  
198
0
  poll_monitor = g_object_new (G_TYPE_POLL_FILE_MONITOR, NULL);
199
200
0
  poll_monitor->file = g_object_ref (file);
201
202
0
  g_file_query_info_async (file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
203
0
         0, 0, NULL, got_initial_info, g_object_ref (poll_monitor));
204
  
205
0
  return G_FILE_MONITOR (poll_monitor);
206
0
}
207
208
static gboolean
209
g_poll_file_monitor_cancel (GFileMonitor* monitor)
210
0
{
211
0
  GPollFileMonitor *poll_monitor = G_POLL_FILE_MONITOR (monitor);
212
  
213
0
  if (poll_monitor->timeout)
214
0
    {
215
0
      g_source_destroy (poll_monitor->timeout);
216
0
      g_source_unref (poll_monitor->timeout);
217
0
      poll_monitor->timeout = NULL;
218
0
    }
219
  
220
0
  return TRUE;
221
0
}