Coverage Report

Created: 2026-04-10 07:07

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