Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rauc/subprojects/glib-2.90.1/gio/inotify/inotify-path.c
Line
Count
Source
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
2
3
/* inotify-path.c - GVFS Monitor based on inotify.
4
5
   Copyright (C) 2006 John McCutchan
6
   Copyright (C) 2009 Codethink Limited
7
8
   SPDX-License-Identifier: LGPL-2.1-or-later
9
10
   This library is free software; you can redistribute it and/or
11
   modify it under the terms of the GNU Lesser General Public
12
   License as published by the Free Software Foundation; either
13
   version 2.1 of the License, or (at your option) any later version.
14
15
   This library is distributed in the hope that it will be useful,
16
   but WITHOUT ANY WARRANTY; without even the implied warranty of
17
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
   Lesser General Public License for more details.
19
20
   You should have received a copy of the GNU Lesser General Public License
21
   along with this library; if not, see <http://www.gnu.org/licenses/>.
22
23
   Authors:
24
     John McCutchan <john@johnmccutchan.com>
25
                 Ryan Lortie <desrt@desrt.ca>
26
*/
27
28
#include "config.h"
29
30
/* Don't put conflicting kernel types in the global namespace: */
31
#define __KERNEL_STRICT_NAMES
32
33
#include <sys/inotify.h>
34
#include <string.h>
35
#include <glib.h>
36
#include "inotify-kernel.h"
37
#include "inotify-path.h"
38
#include "inotify-missing.h"
39
40
0
#define IP_INOTIFY_DIR_MASK (IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_DELETE|IN_CREATE|IN_DELETE_SELF|IN_UNMOUNT|IN_MOVE_SELF|IN_CLOSE_WRITE)
41
42
0
#define IP_INOTIFY_FILE_MASK (IN_MODIFY|IN_ATTRIB|IN_CLOSE_WRITE)
43
44
/* Older libcs don't have this */
45
#ifndef IN_ONLYDIR
46
#define IN_ONLYDIR 0  
47
#endif
48
49
typedef struct ip_watched_file_s {
50
  gchar *filename;
51
  gchar *path;
52
  gint32 wd;
53
54
  GList *subs;
55
} ip_watched_file_t;
56
57
typedef struct ip_watched_dir_s {
58
  char *path;
59
  /* TODO: We need to maintain a tree of watched directories
60
   * so that we can deliver move/delete events to sub folders.
61
   * Or the application could do it...
62
   */
63
  struct ip_watched_dir_s* parent;
64
  GList*   children;
65
66
  /* basename -> ip_watched_file_t
67
   * Maps basename to a ip_watched_file_t if the file is currently
68
   * being directly watched for changes (ie: 'hardlinks' mode).
69
   */
70
  GHashTable *files_hash;
71
72
  /* Inotify state */
73
  gint32 wd;
74
  
75
  /* List of inotify subscriptions */
76
  GList *subs;
77
} ip_watched_dir_t;
78
79
static gboolean     ip_debug_enabled = FALSE;
80
0
#define IP_W if (ip_debug_enabled) g_warning
81
82
/* path -> ip_watched_dir */
83
static GHashTable * path_dir_hash = NULL;
84
/* inotify_sub * -> ip_watched_dir *
85
 *
86
 * Each subscription is attached to a watched directory or it is on
87
 * the missing list
88
 */
89
static GHashTable * sub_dir_hash = NULL;
90
/* This hash holds GLists of ip_watched_dir_t *'s
91
 * We need to hold a list because symbolic links can share
92
 * the same wd
93
 */
94
static GHashTable * wd_dir_hash = NULL;
95
/* This hash holds GLists of ip_watched_file_t *'s
96
 * We need to hold a list because links can share
97
 * the same wd
98
 */
99
static GHashTable * wd_file_hash = NULL;
100
101
static ip_watched_dir_t *ip_watched_dir_new  (const char       *path,
102
                int               wd);
103
static void              ip_watched_dir_free (ip_watched_dir_t *dir);
104
static gboolean          ip_event_callback   (ik_event_t       *event);
105
106
107
static gboolean (*event_callback)(ik_event_t *event, inotify_sub *sub, gboolean file_event);
108
109
gboolean
110
_ip_startup (gboolean (*cb)(ik_event_t *event, inotify_sub *sub, gboolean file_event))
111
0
{
112
0
  static gboolean initialized = FALSE;
113
0
  static gboolean result = FALSE;
114
  
115
0
  if (initialized == TRUE)
116
0
    return result;
117
118
0
  event_callback = cb;
119
0
  result = _ik_startup (ip_event_callback);
120
121
0
  if (!result)
122
0
    return FALSE;
123
124
0
  path_dir_hash = g_hash_table_new (g_str_hash, g_str_equal);
125
0
  sub_dir_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
126
0
  wd_dir_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
127
0
  wd_file_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
128
  
129
0
  initialized = TRUE;
130
0
  return TRUE;
131
0
}
132
133
static void
134
ip_map_path_dir (const char       *path, 
135
                 ip_watched_dir_t *dir)
136
0
{
137
0
  g_assert (path && dir);
138
0
  g_hash_table_insert (path_dir_hash, dir->path, dir);
139
0
}
140
141
static void
142
ip_map_sub_dir (inotify_sub      *sub, 
143
                ip_watched_dir_t *dir)
144
0
{
145
  /* Associate subscription and directory */
146
0
  g_assert (dir && sub);
147
0
  g_hash_table_insert (sub_dir_hash, sub, dir);
148
0
  dir->subs = g_list_prepend (dir->subs, sub);
149
0
}
150
151
static void
152
ip_map_wd_dir (gint32            wd, 
153
               ip_watched_dir_t *dir)
154
0
{
155
0
  GList *dir_list;
156
  
157
0
  g_assert (wd >= 0 && dir);
158
0
  dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (wd));
159
0
  dir_list = g_list_prepend (dir_list, dir);
160
0
  g_hash_table_replace (wd_dir_hash, GINT_TO_POINTER (dir->wd), dir_list);
161
0
}
162
163
static void
164
ip_map_wd_file (gint32             wd,
165
                ip_watched_file_t *file)
166
0
{
167
0
  GList *file_list;
168
169
0
  g_assert (wd >= 0 && file);
170
0
  file_list = g_hash_table_lookup (wd_file_hash, GINT_TO_POINTER (wd));
171
0
  file_list = g_list_prepend (file_list, file);
172
0
  g_hash_table_replace (wd_file_hash, GINT_TO_POINTER (wd), file_list);
173
0
}
174
175
static void
176
ip_unmap_wd_file (gint32             wd,
177
                  ip_watched_file_t *file)
178
0
{
179
0
  GList *file_list = g_hash_table_lookup (wd_file_hash, GINT_TO_POINTER (wd));
180
181
0
  if (!file_list)
182
0
    return;
183
184
0
  g_assert (wd >= 0 && file);
185
0
  file_list = g_list_remove (file_list, file);
186
0
  if (file_list == NULL)
187
0
    g_hash_table_remove (wd_file_hash, GINT_TO_POINTER (wd));
188
0
  else
189
0
    g_hash_table_replace (wd_file_hash, GINT_TO_POINTER (wd), file_list);
190
0
}
191
192
193
static ip_watched_file_t *
194
ip_watched_file_new (const gchar *dirname,
195
                     const gchar *filename)
196
0
{
197
0
  ip_watched_file_t *file;
198
199
0
  file = g_new0 (ip_watched_file_t, 1);
200
0
  file->path = g_strjoin ("/", dirname, filename, NULL);
201
0
  file->filename = g_strdup (filename);
202
0
  file->wd = -1;
203
204
0
  return file;
205
0
}
206
207
static void
208
ip_watched_file_free (ip_watched_file_t *file)
209
0
{
210
0
  g_assert (file->subs == NULL);
211
0
  g_free (file->filename);
212
0
  g_free (file->path);
213
0
  g_free (file);
214
0
}
215
216
static void
217
ip_watched_file_add_sub (ip_watched_file_t *file,
218
                         inotify_sub       *sub)
219
0
{
220
0
  file->subs = g_list_prepend (file->subs, sub);
221
0
}
222
223
static void
224
ip_watched_file_start (ip_watched_file_t *file)
225
0
{
226
0
  if (file->wd < 0)
227
0
    {
228
0
      gint err;
229
230
0
      file->wd = _ik_watch (file->path,
231
0
                            IP_INOTIFY_FILE_MASK,
232
0
                            &err);
233
234
0
      if (file->wd >= 0)
235
0
        ip_map_wd_file (file->wd, file);
236
0
    }
237
0
}
238
239
static void
240
ip_watched_file_stop (ip_watched_file_t *file)
241
0
{
242
0
  if (file->wd >= 0)
243
0
    {
244
0
      gint32 wd = file->wd;
245
246
      /* Drop this file from the wd list first, then only remove the kernel
247
       * watch if no other watched file (reached via a different path that
248
       * resolves to the same inode, e.g. a link) still shares this wd.
249
       * Otherwise we would tear down a watch that is still in use. */
250
0
      ip_unmap_wd_file (wd, file);
251
252
0
      if (g_hash_table_lookup (wd_file_hash, GINT_TO_POINTER (wd)) == NULL)
253
0
        _ik_ignore (file->path, wd);
254
255
0
      file->wd = -1;
256
0
    }
257
0
}
258
259
gboolean
260
_ip_start_watching (inotify_sub *sub)
261
0
{
262
0
  gint32 wd;
263
0
  int err;
264
0
  ip_watched_dir_t *dir;
265
  
266
0
  g_assert (sub);
267
0
  g_assert (!sub->cancelled);
268
0
  g_assert (sub->dirname);
269
  
270
0
  IP_W ("Starting to watch %s\n", sub->dirname);
271
0
  dir = g_hash_table_lookup (path_dir_hash, sub->dirname);
272
273
0
  if (dir == NULL)
274
0
    {
275
0
      IP_W ("Trying to add inotify watch ");
276
0
      wd = _ik_watch (sub->dirname, IP_INOTIFY_DIR_MASK|IN_ONLYDIR, &err);
277
0
      if (wd < 0)
278
0
        {
279
0
          IP_W ("Failed\n");
280
0
          return FALSE;
281
0
        }
282
0
      else
283
0
        {
284
          /* Create new watched directory and associate it with the
285
           * wd hash and path hash
286
           */
287
0
          IP_W ("Success\n");
288
0
          dir = ip_watched_dir_new (sub->dirname, wd);
289
0
          ip_map_wd_dir (wd, dir);
290
0
          ip_map_path_dir (sub->dirname, dir);
291
0
        }
292
0
    }
293
0
  else
294
0
    IP_W ("Already watching\n");
295
296
0
  if (sub->hardlinks)
297
0
    {
298
0
      ip_watched_file_t *file;
299
300
0
      file = g_hash_table_lookup (dir->files_hash, sub->filename);
301
302
0
      if (file == NULL)
303
0
        {
304
0
          file = ip_watched_file_new (sub->dirname, sub->filename);
305
0
          g_hash_table_insert (dir->files_hash, file->filename, file);
306
0
        }
307
308
0
      ip_watched_file_add_sub (file, sub);
309
0
      ip_watched_file_start (file);
310
0
    }
311
312
0
  ip_map_sub_dir (sub, dir);
313
  
314
0
  return TRUE;
315
0
}
316
317
static void
318
ip_unmap_path_dir (const char       *path, 
319
                   ip_watched_dir_t *dir)
320
0
{
321
0
  g_assert (path && dir);
322
0
  g_hash_table_remove (path_dir_hash, dir->path);
323
0
}
324
325
static void
326
ip_unmap_wd_dir (gint32            wd, 
327
                 ip_watched_dir_t *dir)
328
0
{
329
0
  GList *dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (wd));
330
  
331
0
  if (!dir_list)
332
0
    return;
333
  
334
0
  g_assert (wd >= 0 && dir);
335
0
  dir_list = g_list_remove (dir_list, dir);
336
0
  if (dir_list == NULL) 
337
0
    g_hash_table_remove (wd_dir_hash, GINT_TO_POINTER (dir->wd));
338
0
  else
339
0
    g_hash_table_replace (wd_dir_hash, GINT_TO_POINTER (dir->wd), dir_list);
340
0
}
341
342
static void
343
ip_unmap_wd (gint32 wd)
344
0
{
345
0
  GList *dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (wd));
346
0
  if (!dir_list)
347
0
    return;
348
0
  g_assert (wd >= 0);
349
0
  g_hash_table_remove (wd_dir_hash, GINT_TO_POINTER (wd));
350
0
  g_list_free (dir_list);
351
0
}
352
353
static void
354
ip_unmap_sub_dir (inotify_sub      *sub,
355
                  ip_watched_dir_t *dir)
356
0
{
357
0
  g_assert (sub && dir);
358
0
  g_hash_table_remove (sub_dir_hash, sub);
359
0
  dir->subs = g_list_remove (dir->subs, sub);
360
361
0
  if (sub->hardlinks)
362
0
    {
363
0
      ip_watched_file_t *file;
364
365
0
      file = g_hash_table_lookup (dir->files_hash, sub->filename);
366
0
      file->subs = g_list_remove (file->subs, sub);
367
368
0
      if (file->subs == NULL)
369
0
        {
370
0
          g_hash_table_remove (dir->files_hash, sub->filename);
371
0
          ip_watched_file_stop (file);
372
0
          ip_watched_file_free (file);
373
0
        }
374
0
    }
375
0
 }
376
377
static void
378
ip_unmap_all_subs (ip_watched_dir_t *dir)
379
0
{
380
0
  while (dir->subs != NULL)
381
0
    ip_unmap_sub_dir (dir->subs->data, dir);
382
0
}
383
384
gboolean
385
_ip_stop_watching (inotify_sub *sub)
386
0
{
387
0
  ip_watched_dir_t *dir = NULL;
388
  
389
0
  dir = g_hash_table_lookup (sub_dir_hash, sub);
390
0
  if (!dir) 
391
0
    return TRUE;
392
  
393
0
  ip_unmap_sub_dir (sub, dir);
394
395
  /* No one is subscribing to this directory any more */
396
0
  if (dir->subs == NULL)
397
0
    {
398
0
      gint32 wd = dir->wd;
399
400
      /* Drop this directory from the wd list first, then only remove the
401
       * kernel watch if no other watched directory (reached via a different
402
       * path that resolves to the same inode, e.g. a symlink) still shares
403
       * this wd. Since inotify watches are keyed by inode, removing the wd
404
       * while it is still shared would silently stop events for the other,
405
       * still-active monitors. */
406
0
      ip_unmap_wd_dir (wd, dir);
407
0
      ip_unmap_path_dir (dir->path, dir);
408
409
0
      if (g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (wd)) == NULL)
410
0
        _ik_ignore (dir->path, wd);
411
412
0
      ip_watched_dir_free (dir);
413
0
    }
414
  
415
0
  return TRUE;
416
0
}
417
418
419
static ip_watched_dir_t *
420
ip_watched_dir_new (const char *path, 
421
                    gint32      wd)
422
0
{
423
0
  ip_watched_dir_t *dir = g_new0 (ip_watched_dir_t, 1);
424
  
425
0
  dir->path = g_strdup (path);
426
0
  dir->files_hash = g_hash_table_new (g_str_hash, g_str_equal);
427
0
  dir->wd = wd;
428
  
429
0
  return dir;
430
0
}
431
432
static void
433
ip_watched_dir_free (ip_watched_dir_t *dir)
434
0
{
435
0
  g_assert (g_hash_table_size (dir->files_hash) == 0);
436
0
  g_assert (dir->subs == NULL);
437
0
  g_free (dir->path);
438
0
  g_hash_table_unref (dir->files_hash);
439
0
  g_free (dir);
440
0
}
441
442
static void
443
ip_wd_delete (gpointer data, 
444
              gpointer user_data)
445
0
{
446
0
  ip_watched_dir_t *dir = data;
447
0
  GList *l = NULL;
448
  
449
0
  for (l = dir->subs; l; l = l->next)
450
0
    {
451
0
      inotify_sub *sub = l->data;
452
      /* Add subscription to missing list */
453
0
      _im_add (sub);
454
0
    }
455
0
  ip_unmap_all_subs (dir);
456
  /* Unassociate the path and the directory */
457
0
  ip_unmap_path_dir (dir->path, dir);
458
0
  ip_watched_dir_free (dir);
459
0
}
460
461
static gboolean
462
ip_event_dispatch (GList      *dir_list, 
463
                   GList      *file_list,
464
                   ik_event_t *event)
465
0
{
466
0
  gboolean interesting = FALSE;
467
468
0
  GList *l;
469
  
470
0
  if (!event)
471
0
    return FALSE;
472
473
0
  for (l = dir_list; l; l = l->next)
474
0
    {
475
0
      GList *subl;
476
0
      ip_watched_dir_t *dir = l->data;
477
      
478
0
      for (subl = dir->subs; subl; subl = subl->next)
479
0
  {
480
0
    inotify_sub *sub = subl->data;
481
    
482
    /* If the subscription and the event
483
     * contain a filename and they don't
484
     * match, we don't deliver this event.
485
     */
486
0
    if (sub->filename &&
487
0
        event->name &&
488
0
        strcmp (sub->filename, event->name) &&
489
0
              (!event->pair || !event->pair->name || strcmp (sub->filename, event->pair->name)))
490
0
      continue;
491
    
492
    /* If the subscription has a filename
493
     * but this event doesn't, we don't
494
     * deliver this event.
495
     */
496
0
    if (sub->filename && !event->name)
497
0
      continue;
498
    
499
    /* If we're also watching the file directly
500
     * don't report events that will also be
501
     * reported on the file itself.
502
     */
503
0
    if (sub->hardlinks)
504
0
      {
505
0
        event->mask &= (guint32) ~IP_INOTIFY_FILE_MASK;
506
0
        if (!event->mask)
507
0
    continue;
508
0
      }
509
    
510
    /* FIXME: We might need to synthesize
511
     * DELETE/UNMOUNT events when
512
     * the filename doesn't match
513
     */
514
    
515
0
    interesting |= event_callback (event, sub, FALSE);
516
517
0
          if (sub->hardlinks)
518
0
            {
519
0
              ip_watched_file_t *file;
520
521
0
              file = g_hash_table_lookup (dir->files_hash, sub->filename);
522
523
0
              if (file != NULL)
524
0
                {
525
0
                  if (event->mask & (IN_MOVED_FROM | IN_DELETE))
526
0
                    ip_watched_file_stop (file);
527
528
0
                  if (event->mask & (IN_MOVED_TO | IN_CREATE))
529
0
                    ip_watched_file_start (file);
530
0
                }
531
0
            }
532
0
        }
533
0
    }
534
535
0
  for (l = file_list; l; l = l->next)
536
0
    {
537
0
      ip_watched_file_t *file = l->data;
538
0
      GList *subl;
539
540
0
      for (subl = file->subs; subl; subl = subl->next)
541
0
        {
542
0
    inotify_sub *sub = subl->data;
543
544
0
    interesting |= event_callback (event, sub, TRUE);
545
0
        }
546
0
    }
547
548
0
  return interesting;
549
0
}
550
551
static gboolean
552
ip_event_callback (ik_event_t *event)
553
0
{
554
0
  gboolean interesting = FALSE;
555
0
  GList* dir_list = NULL;
556
0
  GList *file_list = NULL;
557
558
  /* We can ignore the IGNORED events. Likewise, if the event queue overflowed,
559
   * there is not much we can do to recover. */
560
0
  if (event->mask & (IN_IGNORED | IN_Q_OVERFLOW))
561
0
    {
562
0
      _ik_event_free (event);
563
0
      return TRUE;
564
0
    }
565
566
0
  dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (event->wd));
567
0
  file_list = g_hash_table_lookup (wd_file_hash, GINT_TO_POINTER (event->wd));
568
569
0
  if (event->mask & IP_INOTIFY_DIR_MASK)
570
0
    interesting |= ip_event_dispatch (dir_list, file_list, event);
571
572
  /* Only deliver paired events if the wds are separate */
573
0
  if (event->pair && event->pair->wd != event->wd)
574
0
    {
575
0
      dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (event->pair->wd));
576
0
      file_list = g_hash_table_lookup (wd_file_hash, GINT_TO_POINTER (event->pair->wd));
577
578
0
      if (event->pair->mask & IP_INOTIFY_DIR_MASK)
579
0
        interesting |= ip_event_dispatch (dir_list, file_list, event->pair);
580
0
    }
581
582
  /* We have to manage the missing list
583
   * when we get an event that means the
584
   * file has been deleted/moved/unmounted.
585
   */
586
0
  if (event->mask & IN_DELETE_SELF ||
587
0
      event->mask & IN_MOVE_SELF ||
588
0
      event->mask & IN_UNMOUNT)
589
0
    {
590
      /* Add all subscriptions to missing list */
591
0
      g_list_foreach (dir_list, ip_wd_delete, NULL);
592
      /* Unmap all directories attached to this wd */
593
0
      ip_unmap_wd (event->wd);
594
0
    }
595
  
596
0
  _ik_event_free (event);
597
598
0
  return interesting;
599
0
}
600
601
const char *
602
_ip_get_path_for_wd (gint32 wd)
603
0
{
604
0
  GList *dir_list;
605
0
  ip_watched_dir_t *dir;
606
607
0
  g_assert (wd >= 0);
608
0
  dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (wd));
609
0
  if (dir_list)
610
0
    {
611
0
      dir = dir_list->data;
612
0
      if (dir)
613
0
  return dir->path;
614
0
    }
615
616
0
  return NULL;
617
0
}