aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/libicalss/icalcluster.c
blob: 36bdccc74388a645e7a9d32e8e996ca21615c9cf (plain) (blame)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* -*- Mode: C -*-
  ======================================================================
  FILE: icalcluster.c
  CREATOR: eric 23 December 1999
  
  $Id$
  $Locker$
    
 (C) COPYRIGHT 1999 Eric Busboom
 http://www.softwarestudio.org

 The contents of this file are subject to the Mozilla Public License
 Version 1.0 (the "License"); you may not use this file except in
 compliance with the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/
 
 Software distributed under the License is distributed on an "AS IS"
 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 the License for the specific language governing rights and
 limitations under the License.
 
 The Original Code is eric. The Initial Developer of the Original
 Code is Eric Busboom


 ======================================================================*/


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif


#include "icalcluster.h"
#include <errno.h>
#include <limits.h> /* For PATH_MAX */
#include <sys/stat.h> /* for stat */
#include <unistd.h> /* for stat, getpid */
#include <stdlib.h>
#include <string.h>
#include <fcntl.h> /* for fcntl */
#include <unistd.h> /* for fcntl */

icalerrorenum icalcluster_create_cluster(char *path);

struct icalcluster_impl {
    char *path;
    icalcomponent* cluster;
    int changed;
    FILE* stream;
};

icalcluster* icalcluster_new_impl()
{
    struct icalcluster_impl* comp;

    if ( ( comp = (struct icalcluster_impl*)
       malloc(sizeof(struct icalcluster_impl))) == 0) {
    icalerror_set_errno(ICAL_NEWFAILED_ERROR);
    errno = ENOMEM;
    return 0;
    }

    return comp;
}

char* read_from_file(char *s, size_t size, void *d)
{
    char *c = fgets(s,size, (FILE*)d);
    return c;
}

icalcluster* icalcluster_new(char* path)
{
    struct icalcluster_impl *impl = icalcluster_new_impl(); 
    struct stat sbuf;
    int createclusterfile = 0;
    icalerrorenum error = ICAL_NO_ERROR;
    icalparser *parser;
    struct icaltimetype tt;
    off_t cluster_file_size;

    memset(&tt,0,sizeof(struct icaltimetype));

    icalerror_clear_errno();
    icalerror_check_arg_rz( (path!=0), "path");

    if (impl == 0){
    return 0;
    }

    /*impl->path = strdup(path); icalcluster_load does this */
    impl->changed  = 0;

    impl->cluster = 0;

    impl->path = 0;
    impl->stream = 0;
        
    /* Check if the path already exists and if it is a regular file*/
    if (stat(path,&sbuf) != 0){
    
    /* A file by the given name does not exist, or there was
           another error */
    cluster_file_size = 0;
    if (errno == ENOENT) {
        /* It was because the file does not exist */
        createclusterfile = 1;
    } else {
        /* It was because of another error */
        icalerror_set_errno(ICAL_FILE_ERROR);
        return 0;
    }
    } else {
    /* A file by the given name exists, but is it a regular file */
    
    if (!S_ISREG(sbuf.st_mode)){ 
        /* Nope, not a directory */
        icalerror_set_errno(ICAL_FILE_ERROR);
        return 0;
    } else {
        /* Lets assume that it is a file of the right type */
          cluster_file_size = sbuf.st_size;
          createclusterfile = 0;
    }   
    }
    
    /* if cluster does not already exist, create it */
    
    if (createclusterfile == 1) {
    error = icalcluster_create_cluster(path);

    if (error != ICAL_NO_ERROR){
        icalerror_set_errno(error);
        return 0;
    }
    }

    impl->path = (char*)strdup(path);

    errno = 0;
    impl->stream = fopen(impl->path,"r");
    
    if (impl->stream ==0 || errno != 0){
    impl->cluster = 0;
    icalerror_set_errno(ICAL_FILE_ERROR); /* Redundant, actually */
    return 0;
    }

    icalcluster_lock(impl);

    if(cluster_file_size > 0){
      parser = icalparser_new();
      icalparser_set_gen_data(parser,impl->stream);
      impl->cluster = icalparser_parse(parser,read_from_file);
      icalparser_free(parser);

      if (icalcomponent_isa(impl->cluster) != ICAL_XROOT_COMPONENT){
        /* The parser got a single component, so it did not put it in
           an XROOT. */
        icalcomponent *cl = impl->cluster;
        impl->cluster = icalcomponent_new(ICAL_XROOT_COMPONENT);
        icalcomponent_add_component(impl->cluster,cl);
      }

    } else {

      impl->cluster = icalcomponent_new(ICAL_XROOT_COMPONENT);
    }      

    if (impl->cluster == 0){
    icalerror_set_errno(ICAL_PARSE_ERROR);
    return 0;
    }
    
    if (error != ICAL_NO_ERROR){
    return 0;
    }
    
    return impl;
}
    
void icalcluster_free(icalcluster* cluster)
{
    struct icalcluster_impl *impl = (struct icalcluster_impl*)cluster;

    icalerror_check_arg_rv((cluster!=0),"cluster");

    if (impl->cluster != 0){
    icalcluster_commit(cluster);
    icalcomponent_free(impl->cluster);
    impl->cluster=0;
    }

    if(impl->path != 0){
    free(impl->path);
    impl->path = 0;
    }

    if(impl->stream != 0){
    icalcluster_unlock(impl);
    fclose(impl->stream);
    impl->stream = 0;
    }

    free(impl);
}

char* icalcluster_path(icalcluster* cluster)
{
    struct icalcluster_impl *impl = (struct icalcluster_impl*)cluster;
    icalerror_check_arg_rz((cluster!=0),"cluster");

    return impl->path;
}


int icalcluster_lock(icalcluster *cluster)
{
    struct icalcluster_impl *impl = (struct icalcluster_impl*)cluster;
    struct flock lock;
    int fd; 

    icalerror_check_arg_rz((impl->stream!=0),"impl->stream");

    fd  = fileno(impl->stream);

    lock.l_type = F_WRLCK;     /* F_RDLCK, F_WRLCK, F_UNLCK */
    lock.l_start = 0;  /* byte offset relative to l_whence */
    lock.l_whence = SEEK_SET; /* SEEK_SET, SEEK_CUR, SEEK_END */
    lock.l_len = 0;       /* #bytes (0 means to EOF) */

    return (fcntl(fd, F_SETLKW, &lock)); 
}

int icalcluster_unlock(icalcluster *cluster)
{
    struct icalcluster_impl *impl = (struct icalcluster_impl*)cluster;
    int fd;
    struct flock lock;
    icalerror_check_arg_rz((impl->stream!=0),"impl->stream");

    fd  = fileno(impl->stream);

    lock.l_type = F_WRLCK;     /* F_RDLCK, F_WRLCK, F_UNLCK */
    lock.l_start = 0;  /* byte offset relative to l_whence */
    lock.l_whence = SEEK_SET; /* SEEK_SET, SEEK_CUR, SEEK_END */
    lock.l_len = 0;       /* #bytes (0 means to EOF) */

    return (fcntl(fd, F_UNLCK, &lock)); 

}

icalerrorenum icalcluster_create_cluster(char *path)
{

    FILE* f;
    int r;
    icalcomponent *c;

    icalerror_clear_errno();

    f = fopen(path,"w");

    if (f == 0){
    icalerror_set_errno(ICAL_FILE_ERROR);
    return ICAL_FILE_ERROR;
    }

    
    /* This used to write data to the file... */

        
    fclose(f);

    return ICAL_NO_ERROR;
}

icalerrorenum icalcluster_commit(icalcluster* cluster)
{
    FILE *f;
    char tmp[PATH_MAX]; /* HACK Buffer overflow potential */
    char *str;
    icalparser *parser;
    icalcomponent *c;
    
    struct icalcluster_impl *impl = (struct icalcluster_impl*)cluster;

    icalerror_check_arg_re((impl!=0),"cluster",ICAL_BADARG_ERROR);

    if (impl->changed == 0 ){
    return ICAL_NO_ERROR;
    }
    
#ifdef ICAL_SAFESAVES
    snprintf(tmp,PATH_MAX,"%s-tmp",impl->path);
#else   
    strcpy(tmp,impl->path);
#endif
    
    if ( (f = fopen(tmp,"w")) < 0 ){
    icalerror_set_errno(ICAL_FILE_ERROR);
    return ICAL_FILE_ERROR;
    }
    
    for(c = icalcomponent_get_first_component(impl->cluster,ICAL_ANY_COMPONENT);
    c != 0;
    c = icalcomponent_get_next_component(impl->cluster,ICAL_ANY_COMPONENT)){

    str = icalcomponent_as_ical_string(c);
    
    if (  fwrite(str,sizeof(char),strlen(str),f) < strlen(str)){
        fclose(f);
        return ICAL_FILE_ERROR;
    }
    }
    
    fclose(f);
    impl->changed = 0;    
        
#ifdef ICAL_SAFESAVES
    rename(tmp,impl->path); /* HACK, should check for error here */
#endif
    
    return ICAL_NO_ERROR;
    
} 

void icalcluster_mark(icalcluster* cluster){

    struct icalcluster_impl *impl = (struct icalcluster_impl*)cluster;

    icalerror_check_arg_rv((impl!=0),"cluster");

    impl->changed = 1;

}

icalcomponent* icalcluster_get_component(icalcluster* cluster){
   struct icalcluster_impl *impl = (struct icalcluster_impl*)cluster;

   icalerror_check_arg_re((impl!=0),"cluster",ICAL_BADARG_ERROR);

   return impl->cluster;
}


/* manipulate the components in the cluster */

icalerrorenum icalcluster_add_component(icalcluster *cluster,
                   icalcomponent* child)
{
    struct icalcluster_impl* impl = (struct icalcluster_impl*)cluster;

    icalerror_check_arg_rv((cluster!=0),"cluster");
    icalerror_check_arg_rv((child!=0),"child");

    icalcomponent_add_component(impl->cluster,child);

    icalcluster_mark(cluster);

    return ICAL_NO_ERROR;

}

icalerrorenum icalcluster_remove_component(icalcluster *cluster,
                  icalcomponent* child)
{
    struct icalcluster_impl* impl = (struct icalcluster_impl*)cluster;

    icalerror_check_arg_rv((cluster!=0),"cluster");
    icalerror_check_arg_rv((child!=0),"child");

    icalcomponent_remove_component(impl->cluster,child);

    icalcluster_mark(cluster);

    return ICAL_NO_ERROR;
}

int icalcluster_count_components(icalcluster *cluster,
                 icalcomponent_kind kind)
{
    struct icalcluster_impl* impl = (struct icalcluster_impl*)cluster;

    if(cluster == 0){
    icalerror_set_errno(ICAL_BADARG_ERROR);
    return -1;
    }

    return icalcomponent_count_components(impl->cluster,kind);
}

/* Iterate through components */
icalcomponent* icalcluster_get_current_component (icalcluster* cluster)
{
    struct icalcluster_impl* impl = (struct icalcluster_impl*)cluster;

    icalerror_check_arg_rz((cluster!=0),"cluster");

    return icalcomponent_get_current_component(impl->cluster);
}

icalcomponent* icalcluster_get_first_component(icalcluster* cluster,
                           icalcomponent_kind kind)
{
    struct icalcluster_impl* impl = (struct icalcluster_impl*)cluster;

    icalerror_check_arg_rz((cluster!=0),"cluster");

    return icalcomponent_get_first_component(impl->cluster,kind);
}

icalcomponent* icalcluster_get_next_component(icalcluster* cluster,
                          icalcomponent_kind kind)
{
    struct icalcluster_impl* impl = (struct icalcluster_impl*)cluster;

    icalerror_check_arg_rz((cluster!=0),"cluster");

    return icalcomponent_get_next_component(impl->cluster,kind);
}