forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfswatch.c
More file actions
250 lines (217 loc) · 7.76 KB
/
Copy pathfswatch.c
File metadata and controls
250 lines (217 loc) · 7.76 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
This file is part of darktable,
copyright (c) 2010 Henrik Andersson.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "common/darktable.h"
#include "common/dtpthread.h"
#include "common/image.h"
#include "common/fswatch.h"
#include "develop/develop.h"
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <glib.h>
#include <strings.h>
#if 0//def HAVE_INOTIFY
#include <sys/inotify.h>
#endif
typedef struct _watch_t
{
int descriptor; // Handle
dt_fswatch_type_t type; // DT_FSWATCH_* type
void *data; // Assigned data
int events; // events occured..
} _watch_t;
#if 0// def HAVE_INOTIFY
typedef struct inotify_event_t
{
uint32_t wd; /* Watch descriptor */
uint32_t mask; /* Mask of events */
uint32_t cookie; /* Unique cookie associating related events (for rename(2)) */
uint32_t len; /* Size of 'name' field */
char name[]; /* Optional null-terminated name */
} inotify_event_t;
// Compare func for GList
static gint _fswatch_items_by_data(const void* a,const void *b)
{
return (((_watch_t*)a)->data<b)?-1:((((_watch_t*)a)->data==b)?0:1);
}
// Compare func for GList
static gint _fswatch_items_by_descriptor(const void *a,const void *b)
{
gint result=(((_watch_t*)a)->descriptor < *(const int *)b)?-1:((((_watch_t*)a)->descriptor==*(const int *)b)?0:1);
return result;
}
static void *_fswatch_thread(void *data)
{
dt_fswatch_t *fswatch=(dt_fswatch_t *)data;
uint32_t res=0;
uint32_t event_hdr_size=sizeof(inotify_event_t);
inotify_event_t *event_hdr=g_malloc(sizeof(inotify_event_t));
memset (event_hdr,0,event_hdr_size);
char *name=g_malloc(2048);
memset (name,0, 2048);
dt_print(DT_DEBUG_FSWATCH,"[fswatch_thread] Starting thread of context %lx\n",(unsigned long int)data);
while(1)
{
// Blocking read loop of event fd into event
if(read(fswatch->inotify_fd,event_hdr,event_hdr_size) != event_hdr_size)
{
if(errno == EINTR) continue;
perror("[fswatch_thread] read inotify fd");
break;
}
// Read name into buffer if any...
if( event_hdr->len > 0 )
{
res=read(fswatch->inotify_fd,name,event_hdr_size);
name[res]='\0';
}
dt_pthread_mutex_lock(&fswatch->mutex);
GList *gitem=g_list_find_custom(fswatch->items,&event_hdr->wd,&_fswatch_items_by_descriptor);
if( gitem )
{
_watch_t *item = gitem->data;
item->events=item->events|event_hdr->mask;
switch( item->type )
{
case DT_FSWATCH_IMAGE:
{
if( (event_hdr->mask&IN_CLOSE) && (item->events&IN_MODIFY) ) // Check if file modified and closed...
{
// Something wrote on image externally and closed it, lets tag item as dirty...
dt_image_t *img=(dt_image_t *)item->data;
img->force_reimport = 1;
if(darktable.develop->image==img)
dt_dev_raw_reload(darktable.develop);
item->events=0;
}
else if( (event_hdr->mask&IN_ATTRIB) && (item->events&IN_DELETE_SELF) && (item->events&IN_IGNORED))
{
// This pattern showed up when another file is replacing the orginal...
dt_image_t *img=(dt_image_t *)item->data;
img->force_reimport = 1;
if(darktable.develop->image==img)
dt_dev_raw_reload(darktable.develop);
item->events=0;
}
}
break;
default:
dt_print(DT_DEBUG_FSWATCH,"[fswatch_thread] Unhandled object type %d for event descriptor %ld\n", item->type, event_hdr->wd );
break;
}
}
else
dt_print(DT_DEBUG_FSWATCH,"[fswatch_thread] Failed to found watch item for descriptor %ld\n", event_hdr->wd );
dt_pthread_mutex_unlock(&fswatch->mutex);
}
dt_print(DT_DEBUG_FSWATCH,"[fswatch_thread] terminating.\n");
g_free(event_hdr);
g_free(name);
return NULL;
}
const dt_fswatch_t* dt_fswatch_new()
{
dt_fswatch_t *fswatch=g_malloc(sizeof(dt_fswatch_t));
memset (fswatch, 0, sizeof(dt_fswatch_t));
if((fswatch->inotify_fd=inotify_init())==-1)
{
g_free(fswatch);
return NULL;
}
fswatch->items=NULL;
dt_pthread_mutex_init(&fswatch->mutex, NULL);
pthread_create(&fswatch->thread, NULL, &_fswatch_thread, fswatch);
dt_print(DT_DEBUG_FSWATCH,"[fswatch_new] Creating new context %lx\n",(unsigned long int)fswatch);
return fswatch;
}
void dt_fswatch_destroy(const dt_fswatch_t *fswatch)
{
dt_print(DT_DEBUG_FSWATCH,"[fswatch_destroy] Destroying context %lx\n",(unsigned long int)fswatch);
dt_fswatch_t *ctx=(dt_fswatch_t *)fswatch;
dt_pthread_mutex_destroy(&ctx->mutex);
GList *item=g_list_first(fswatch->items);
while(item)
{
g_free( item->data );
item=g_list_next(item);
}
g_list_free(fswatch->items);
g_free(ctx);
}
void dt_fswatch_add(const dt_fswatch_t * fswatch,dt_fswatch_type_t type, void *data)
{
char filename[1024];
uint32_t mask=0;
dt_fswatch_t *ctx=(dt_fswatch_t *)fswatch;
filename[0] = '\0';
switch(type)
{
case DT_FSWATCH_IMAGE:
mask=IN_ALL_EVENTS;
dt_image_full_path(((dt_image_t *)data)->id, filename, 1024);
break;
case DT_FSWATCH_CURVE_DIRECTORY:
break;
default:
dt_print(DT_DEBUG_FSWATCH,"[fswatch_add] Unhandled object type %d\n",type);
break;
}
if(filename[0] != '\0')
{
dt_pthread_mutex_lock(&ctx->mutex);
_watch_t *item = g_malloc(sizeof(_watch_t));
item->type=type;
item->data=data;
ctx->items=g_list_append(fswatch->items, item);
item->descriptor=inotify_add_watch(fswatch->inotify_fd,filename,mask);
dt_pthread_mutex_unlock(&ctx->mutex);
dt_print(DT_DEBUG_FSWATCH,"[fswatch_add] Watch on object %lx added on file %s\n",(unsigned long int)data,filename);
}
else
dt_print(DT_DEBUG_FSWATCH,"[fswatch_add] No watch added, failed to get related filename of object type %d\n",type);
}
void dt_fswatch_remove(const dt_fswatch_t * fswatch,dt_fswatch_type_t type, void *data)
{
dt_fswatch_t *ctx=(dt_fswatch_t *)fswatch;
dt_pthread_mutex_lock(&ctx->mutex);
dt_print(DT_DEBUG_FSWATCH,"[fswatch_remove] removing watch on object %lx\n",(unsigned long int)data);
GList *gitem=g_list_find_custom(fswatch->items,data,&_fswatch_items_by_data);
if( gitem )
{
_watch_t *item=gitem->data;
ctx->items=g_list_remove(ctx->items,item);
inotify_rm_watch(fswatch->inotify_fd,item->descriptor);
g_free(item);
}
else
dt_print(DT_DEBUG_FSWATCH,"[fswatch_remove] Didn't find watch on object %lx type %d\n",(unsigned long int)data,type);
dt_pthread_mutex_unlock(&ctx->mutex);
}
#else // HAVE_INOTIFY
const dt_fswatch_t* dt_fswatch_new()
{
dt_print(DT_DEBUG_FSWATCH,"[fswatch_new] fswatch not supported on your platform\n");
return NULL;
}
void dt_fswatch_destroy(const dt_fswatch_t *fswatch) {}
void dt_fswatch_add(const dt_fswatch_t *fswatch, dt_fswatch_type_t type, void *data) {}
void dt_fswatch_remove(const dt_fswatch_t * fswatch, dt_fswatch_type_t type, void *data) {}
#endif
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;