aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/libicalss/icalcluster.c
blob: c0160cc6c34af961161d98977900ad220831a082 (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
/* -*- 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


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


#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>

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

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;
}

icalerrorenum icalcluster_create_cluster(char *path)
{

    FILE* f;
    int r;
    icalcomponent *c;
    struct icaltimetype tt;

    icalerror_clear_errno();

    f = fopen(path,"w");

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

    /* Create the root component in the cluster. This component holds
       all of the other components and stores a count of
       components. */

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

    c = icalcomponent_vanew(
    ICAL_VCALENDAR_COMPONENT,
    icalproperty_new_xlicclustercount(0),
    icalproperty_new_dtstart(tt), /* dtstart of earliest comp */
    icalproperty_new_dtend(tt), /* dtend of latest comp, excl. recuring */
    0
    );
    
    if (c == 0){
    fclose(f);
    icalerror_set_errno(ICAL_INTERNAL_ERROR);
    return ICAL_INTERNAL_ERROR;
    }


    /* Write the base component to the file */
    r = fputs(icalcomponent_as_ical_string(c),f);
        
    fclose(f);

    icalcomponent_free(c);

    if (r == EOF){
    icalerror_set_errno(ICAL_FILE_ERROR);
    return ICAL_FILE_ERROR;
    }

    return ICAL_NO_ERROR;
}

FILE* parser_file; /*HACK. Not Thread Safe */
char* read_from_file(char *s, size_t size)
{
    char *c = fgets(s,size, parser_file);
    return c;
}

icalerrorenum icalcluster_load(icalcluster* cluster, char* path)
{
    struct icalcluster_impl *impl = (struct icalcluster_impl*)cluster;
    icalerrorenum error;
    errno = 0;
 
    icalerror_check_arg_rz((cluster!=0),"cluster");
    icalerror_check_arg_rz((path!=0),"path");

    if(impl->path != 0 && strcmp(impl->path,path) == 0){
    /* Already have the right cluster, so return */
    return ICAL_NO_ERROR;
    }

    error = icalcluster_commit(cluster);
    
    if (error != ICAL_NO_ERROR){
    icalerror_set_errno(error);
    return error;
    }
    
    free(impl->path);
    
    impl->path= (char*)strdup(path);

    parser_file = fopen(impl->path,"r");
    
    /* HACK. Yeah, the following code is horrible....*/
    if (parser_file ==0 || errno != 0){
    
    /* Try to create the cluster */
    error = icalcluster_create_cluster(path);
    
    if (error == ICAL_NO_ERROR){
        /* Try to open the parser again. */
        errno = 0;
        parser_file = fopen(impl->path,"r");
        
        if (parser_file ==0 || errno != 0){
        impl->cluster = 0;
        icalerror_set_errno(ICAL_FILE_ERROR);
        return ICAL_FILE_ERROR;
        }
    } else {
        impl->cluster = 0;
        icalerror_set_errno(error); /* Redundant, actually */
        return error;
    }
    }
    
    impl->cluster = icalparser_parse(read_from_file);
    
    fclose(parser_file);
    
    if (impl->cluster == 0){
    icalerror_set_errno(ICAL_PARSE_ERROR);
    return ICAL_PARSE_ERROR;
    }
    
    return ICAL_NO_ERROR;
}


icalcluster* icalcluster_new(char* path)
{
    struct icalcluster_impl *impl = icalcluster_new_impl(); 
    struct stat sbuf;
    int createclusterfile = 0;
    icalerrorenum error;
    
    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;
        
    /* 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 */
    
    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 */
        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;
    }
    }
    
    error = icalcluster_load(impl,path);
    
    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;
    }

    free(impl);
}

icalerrorenum icalcluster_commit(icalcluster* cluster)
{
    int ws; /* Size in char of file written to disk */
    FILE *f;

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

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

    if (impl->changed != 0 ){
    /* write the cluster to disk */

    /* Construct a filename and write out the file */
    
    if ( (f = fopen(impl->path,"w")) != 0){

        char* str = icalcomponent_as_ical_string(impl->cluster);
        
        ws = fwrite(str,sizeof(char),strlen(str),f);
        
        if ( ws < strlen(str)){
        fclose(f);
        return ICAL_FILE_ERROR;
        }
        
        fclose(f);
            impl->changed = 0;
        return ICAL_NO_ERROR;
    } else {
        icalerror_set_errno(ICAL_FILE_ERROR);
        return ICAL_FILE_ERROR;
    }

    } 

    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);
}