~ubuntu-branches/debian/sid/glib2.0/sid

« back to all changes in this revision

Viewing changes to gio/kqueue/kqueue-missing.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-05-08 06:25:57 UTC
  • mfrom: (1.27.14) (3.1.181 experimental)
  • Revision ID: package-import@ubuntu.com-20130508062557-i7gbku66mls70gi2
Tags: 2.36.1-2
Merge experimental branch, upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
 
3
 
 
4
  Permission is hereby granted, free of charge, to any person obtaining a copy
 
5
  of this software and associated documentation files (the "Software"), to deal
 
6
  in the Software without restriction, including without limitation the rights
 
7
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
8
  copies of the Software, and to permit persons to whom the Software is
 
9
  furnished to do so, subject to the following conditions:
 
10
 
 
11
  The above copyright notice and this permission notice shall be included in
 
12
  all copies or substantial portions of the Software.
 
13
 
 
14
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
15
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
16
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
17
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
18
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
20
  THE SOFTWARE.
 
21
*******************************************************************************/
 
22
 
 
23
#include <glib.h>
 
24
 
 
25
#include "kqueue-helper.h"
 
26
#include "kqueue-sub.h"
 
27
#include "kqueue-missing.h"
 
28
 
 
29
 
 
30
#define SCAN_MISSING_TIME 4 /* 1/4 Hz */
 
31
 
 
32
static gboolean km_scan_missing (gpointer user_data);
 
33
 
 
34
static gboolean km_debug_enabled = FALSE;
 
35
#define KM_W if (km_debug_enabled) g_warning
 
36
 
 
37
static GSList *missing_subs_list = NULL;
 
38
G_LOCK_DEFINE_STATIC (missing_lock);
 
39
 
 
40
static volatile gboolean scan_missing_running = FALSE;
 
41
static on_create_cb file_appeared_callback;
 
42
 
 
43
 
 
44
/**
 
45
 * _km_init:
 
46
 * @cb: a callback function. It will be called when a watched file
 
47
 *     will appear.
 
48
 *
 
49
 * Initialize the kqueue-missing module (optional).
 
50
 **/
 
51
void
 
52
_km_init (on_create_cb cb)
 
53
{
 
54
  file_appeared_callback = cb;
 
55
}
 
56
 
 
57
 
 
58
/**
 
59
 * _km_add_missing:
 
60
 * @sub: a #kqueue_sub
 
61
 *
 
62
 * Adds a subscription to the missing files list.
 
63
 **/
 
64
void
 
65
_km_add_missing (kqueue_sub *sub)
 
66
{
 
67
  G_LOCK (missing_lock);
 
68
  if (g_slist_find (missing_subs_list, sub))
 
69
    {
 
70
      KM_W ("asked to add %s to missing list but it's already on the list!\n", sub->filename);
 
71
      return;
 
72
    }
 
73
 
 
74
  KM_W ("adding %s to missing list\n", sub->filename);
 
75
  missing_subs_list = g_slist_prepend (missing_subs_list, sub);
 
76
  G_UNLOCK (missing_lock);
 
77
 
 
78
  if (!scan_missing_running)
 
79
    {
 
80
      scan_missing_running = TRUE;
 
81
      g_timeout_add_seconds (SCAN_MISSING_TIME, km_scan_missing, NULL);
 
82
    }
 
83
}
 
84
 
 
85
 
 
86
/**
 
87
 * km_scan_missing:
 
88
 * @user_data: unused
 
89
 *
 
90
 * The core missing files watching routine.
 
91
 *
 
92
 * Traverses through a list of missing files, tries to start watching each with
 
93
 * kqueue, removes the appropriate entry and invokes a user callback if the file
 
94
 * has appeared.
 
95
 *
 
96
 * Returns: %FALSE if no missing files left, %TRUE otherwise.
 
97
 **/
 
98
static gboolean
 
99
km_scan_missing (gpointer user_data)
 
100
{
 
101
  GSList *head;
 
102
  GSList *not_missing = NULL;
 
103
  gboolean retval = FALSE;
 
104
  
 
105
  G_LOCK (missing_lock);
 
106
 
 
107
  if (missing_subs_list)
 
108
    KM_W ("we have a job");
 
109
 
 
110
  for (head = missing_subs_list; head; head = head->next)
 
111
    {
 
112
      kqueue_sub *sub = (kqueue_sub *) head->data;
 
113
      g_assert (sub != NULL);
 
114
      g_assert (sub->filename != NULL);
 
115
 
 
116
      if (_kh_start_watching (sub))
 
117
        {
 
118
          KM_W ("file %s now exists, starting watching", sub->filename);
 
119
          if (file_appeared_callback)
 
120
            file_appeared_callback (sub);
 
121
          not_missing = g_slist_prepend (not_missing, head);
 
122
        }
 
123
    }
 
124
 
 
125
  for (head = not_missing; head; head = head->next)
 
126
    {
 
127
      GSList *link = (GSList *) head->data;
 
128
      missing_subs_list = g_slist_remove_link (missing_subs_list, link);
 
129
    }
 
130
  g_slist_free (not_missing);
 
131
 
 
132
  if (missing_subs_list == NULL)
 
133
    {
 
134
      scan_missing_running = FALSE;
 
135
      retval = FALSE;
 
136
    }
 
137
  else
 
138
    retval = TRUE;
 
139
 
 
140
  G_UNLOCK (missing_lock);
 
141
  return retval;
 
142
}
 
143
 
 
144
 
 
145
/**
 
146
 * _km_remove:
 
147
 * @sub: a #kqueue_sub
 
148
 *
 
149
 * Removes a subscription from a list of missing files.
 
150
 **/
 
151
void
 
152
_km_remove (kqueue_sub *sub)
 
153
{
 
154
  G_LOCK (missing_lock);
 
155
  missing_subs_list = g_slist_remove (missing_subs_list, sub);
 
156
  G_UNLOCK (missing_lock);
 
157
}