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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
/* filehandling.c: This is -*- c -*-
This file contains the implementation of the file handling
functions
*/
#include "proplistP.h"
#include "util.h"
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define pl_scan_string(c) yy_scan_string(c)
#define plparse() yyparse()
#define pl_delete_buffer(b) yy_delete_buffer(b)
extern void *pl_scan_string(const char *c);
extern void *plparse();
extern void pl_delete_buffer(void *buf);
proplist_t PLSetUnchanged(proplist_t pl);
/* declared in proplist.l (lex.pl.c) */
extern int pl_line_count;
proplist_t parse_result;
char *pl_curr_file = NULL;
proplist_t PLGetProplistWithDescription(const char *description)
{
void *bufstate;
proplist_t obj;
pl_line_count = 1;
bufstate = (void *)pl_scan_string(description);
plparse();
obj = parse_result; /* What was returned? */
pl_delete_buffer(bufstate);
if(obj)
PLSetUnchanged(obj);
return obj;
}
proplist_t PLGetProplistWithPath(const char *filename)
{
char *str;
proplist_t pl, filename_pl;
int fd;
struct stat fstat_buf;
char *actual_filename;
struct flock flk;
int got_lock;
if((!filename)||(strlen(filename)==0)) /* Refers to GNUstep defaults file */
actual_filename = MakeDefaultsFilename();
else
actual_filename = ManglePath(filename);
if((fd = open(actual_filename, O_RDONLY))<0)
{
free(actual_filename);
return NULL;
}
#if 0
if((flock(fd, LOCK_EX))<0)
#endif
flk.l_type = F_RDLCK;
flk.l_start = 0;
flk.l_whence = SEEK_SET;
flk.l_len = 0;
if (fcntl(fd, F_SETLK, &flk)<0)
{
if (errno != ENOLCK)
{
close(fd);
free(actual_filename);
return NULL;
}
got_lock = 0;
} else
got_lock = 1;
if(fstat(fd, &fstat_buf)<0)
{
close(fd);
free(actual_filename);
return NULL;
}
str = (char *)MyMalloc(__FILE__, __LINE__, sizeof(char)*(fstat_buf.st_size+32));
if(read(fd, str, fstat_buf.st_size) != fstat_buf.st_size)
{
close(fd);
MyFree(__FILE__, __LINE__, str);
#if 0
flock(fd, LOCK_UN);
#endif
flk.l_type = F_UNLCK;
fcntl(fd, F_SETLK, &flk);
return NULL;
}
str[fstat_buf.st_size] = '\0';
flk.l_type = F_UNLCK;
#if 0
if((flock(fd, LOCK_UN))<0)
#endif
if (got_lock && fcntl(fd, F_SETLK, &flk)<0)
{
close(fd);
MyFree(__FILE__, __LINE__, str);
fprintf(stderr, "PLGetPropListWithPath(): Couldn't unlock file!\n");
return NULL;
}
close(fd);
pl_curr_file = (char *)filename;
pl = PLGetProplistWithDescription(str);
MyFree(__FILE__, __LINE__, str);
pl_curr_file = NULL;
if(pl)
{
filename_pl = PLMakeString(actual_filename);
PLSetFilename(pl, filename_pl);
PLRelease(filename_pl);
MyFree(__FILE__, __LINE__, actual_filename);
return pl;
}
else
{
MyFree(__FILE__, __LINE__, actual_filename);
return NULL;
}
}
proplist_t PLSetUnchanged(proplist_t pl)
{
plptr_t internal = (plptr_t)pl;
int i;
/* Just to be sure */
if(internal == NULL)
return NULL;
switch(internal->type)
{
case PLARRAY:
for(i=0; i<internal->t.array.number; i++)
PLSetUnchanged(internal->t.array.elements[i]);
break;
case PLDICTIONARY:
for(i=0; i<internal->t.dict.number; i++)
{
PLSetUnchanged(internal->t.dict.keys[i]);
PLSetUnchanged(internal->t.dict.values[i]);
}
break;
}
internal->changed = NO;
return pl;
}
proplist_t PLSynchronize2(proplist_t pl1, proplist_t pl2, BOOL recurse)
/* pl1 is the proplist changed in the program, pl2 the one that comes
from the outside */
{
plptr_t int1, int2, tmp_v1, tmp_v2, tmp_k, tmp;
proplist_t arr1, arr2;
int i;
int num1, num2, num;
int tmpnum;
int1 = (plptr_t)pl1; int2 = (plptr_t)pl2;
/* Just to be sure */
if(int1 == NULL)
return NULL;
if (int1->type != int2->type)
{ /* FIXME */
puts("ERROR: DIFFERENT TYPE OBJECTS BEING SYNC'ED");
return pl1;
}
switch(int1->type)
{
case PLSTRING:
if(int1->changed)
{
MyFree(__FILE__, __LINE__, int2->t.str.string);
int2->t.str.string =
(char *)MyMalloc(__FILE__, __LINE__, strlen(int1->t.str.string));
strcpy(int2->t.str.string, int1->t.str.string);
}
else if(!PLIsEqual(int1, int2))
{
MyFree(__FILE__, __LINE__, int1->t.str.string);
int1->t.str.string =
(char *)MyMalloc(__FILE__, __LINE__, strlen(int2->t.str.string));
strcpy(int1->t.str.string, int2->t.str.string);
}
PLSetUnchanged(pl1);
break;
case PLDATA:
if(int1->changed)
{
MyFree(__FILE__, __LINE__, int2->t.data.data);
int2->t.data.data =
(unsigned char *)MyMalloc(__FILE__, __LINE__, int1->t.data.length);
memcpy(int2->t.data.data, int1->t.data.data,
int1->t.data.length);
}
else if(!PLIsEqual(int1, int2))
{
MyFree(__FILE__, __LINE__, int1->t.data.data);
int1->t.data.data =
(unsigned char *)MyMalloc(__FILE__, __LINE__, int2->t.data.length);
memcpy(int1->t.data.data, int2->t.data.data,
int2->t.data.length);
}
PLSetUnchanged(pl1);
break;
case PLARRAY:
/* if the list from the file has more elements than the local
array, append all to the local one. if it has less, check
which ones are changed by us and append those to the remote
one, remove the others from the local one. After that, the
numbers of elements are equal; check for changed ones and
synchronize them */
num1 = PLGetNumberOfElements(pl1);
num2 = PLGetNumberOfElements(pl2);
if(num1<num2)
for(i=num1; i<num2; i++)
{
if(int1->changed)
PLRemoveArrayElement(pl2, i);
else
{
PLAppendArrayElement(pl1, PLGetArrayElement(pl2, i));
PLSetUnchanged(PLGetArrayElement(pl1, i));
}
}
else if(num1>num2)
for(i=num2; i<num1; i++)
{
tmp = PLGetArrayElement(pl1, i);
if(tmp->changed)
{
PLAppendArrayElement(pl2, PLGetArrayElement(pl1, i));
tmpnum = PLGetNumberOfElements(pl2)-1;
PLSetUnchanged(PLGetArrayElement(pl2, tmpnum));
}
else
PLRemoveArrayElement(pl1, i);
}
num = PLGetNumberOfElements(pl1);
for(i=0; i<num; i++) {
if (recurse) {
PLSynchronize2(PLGetArrayElement(pl1, i),
PLGetArrayElement(pl2, i), YES);
} else {
tmp_v1 = PLGetArrayElement(pl1, i);
if(tmp_v1->changed) {
PLRemoveArrayElement(pl2, i);
PLInsertArrayElement(pl2, tmp_v1, i);
PLSetUnchanged(PLGetArrayElement(pl2, i));
} else {
PLRemoveArrayElement(pl1, i);
PLInsertArrayElement(pl1, PLGetArrayElement(pl2, i), i);
PLSetUnchanged(PLGetArrayElement(pl1, i));
}
}
}
break;
case PLDICTIONARY:
arr1 = PLGetAllDictionaryKeys(pl1);
arr2 = PLGetAllDictionaryKeys(pl2);
num1 = PLGetNumberOfElements(arr1);
num2 = PLGetNumberOfElements(arr2);
/* first check for entries that are in pl2 but not pl1 */
for(i=0; i<num2; i++)
{
tmp_k = PLGetArrayElement(arr2, i);
tmp_v2 = PLGetDictionaryEntry(pl2, tmp_k);
if(!(tmp_v1 = PLGetDictionaryEntry(pl1, tmp_k))) {
/* Entry is only in the file copy */
/* have we changed anything in the current
container? If yes, remove this entry. If no,
leave it where it is */
if(int1->changed)
PLRemoveDictionaryEntry(pl2, tmp_k);
else
PLInsertDictionaryEntry(pl1, tmp_k, tmp_v2);
} else if (recurse) {
/* Entry present in both pl1 & pl2 */
PLSynchronize2(tmp_v1, tmp_v2, YES);
} else {
/* Entry present in both pl1 & pl2, but no recursion */
if (tmp_v1->changed) {
PLInsertDictionaryEntry(pl2, tmp_k, tmp_v1);
} else {
PLInsertDictionaryEntry(pl1, tmp_k, tmp_v2);
}
}
}
/* Now, check for entries that aren't in pl2 (only entries unique in
* pl1 must be handled here!!!). The entries that are in both, were
* handled in the for() loop above. Don't handle them again. -Dan */
for(i=0; i<num1; i++)
{
tmp_k = PLGetArrayElement(arr1, i);
tmp_v1 = PLGetDictionaryEntry(pl1, tmp_k);
if(!(tmp_v2 = PLGetDictionaryEntry(pl2, tmp_k))) {
if(tmp_v1->changed)
PLInsertDictionaryEntry(pl2, tmp_k, tmp_v1);
else
PLRemoveDictionaryEntry(pl1, tmp_k);
}
}
break;
}
PLSetUnchanged(pl1);
PLSetUnchanged(pl2);
return pl1;
}
BOOL PLDeepSynchronize(proplist_t pl)
{
char lockfilename[255];
proplist_t fromFile;
plptr_t internal, fF_internal;
BOOL state;
if(!PLGetFilename(pl))
return NO;
sprintf(lockfilename, "%s.lock", PLGetString(PLGetFilename(pl)));
if((mkdir(lockfilename, 0755))<0)
return NO;
if(!(fromFile = PLGetProplistWithPath(PLGetString(PLGetFilename(pl)))))
{
rmdir(lockfilename);
return NO;
}
internal = (plptr_t)pl;
fF_internal = (plptr_t)fromFile;
internal = PLSynchronize2(internal, fF_internal, YES);
state = PLSave(fF_internal, YES);
PLRelease(fF_internal);
rmdir(lockfilename);
return state;
}
BOOL PLShallowSynchronize(proplist_t pl)
{
char lockfilename[255];
proplist_t fromFile;
plptr_t internal, fF_internal;
BOOL state;
if(!PLGetFilename(pl))
return NO;
sprintf(lockfilename, "%s.lock", PLGetString(PLGetFilename(pl)));
if((mkdir(lockfilename, 0755))<0)
return NO;
if(!(fromFile = PLGetProplistWithPath(PLGetString(PLGetFilename(pl)))))
{
rmdir(lockfilename);
return NO;
}
internal = (plptr_t)pl;
fF_internal = (plptr_t)fromFile;
internal = PLSynchronize2(internal, fF_internal, NO);
state = PLSave(fF_internal, YES);
PLRelease(fF_internal);
rmdir(lockfilename);
return state;
}
BOOL PLSave(proplist_t pl, BOOL atomically)
{
char *theFileName = NULL;
char *theRealFileName = NULL;
char tmp_realFileName[255];
char dirname[255];
char *tmp_dirname, *tmp2_dirname;
FILE *theFile;
int c;
char *desc = NULL;
theRealFileName = PLGetString(PLGetFilename(pl));
if(!theRealFileName) return NO;
if (atomically)
{
strcpy(tmp_realFileName, theRealFileName);
dirname[0]='\0';
if((tmp_dirname=strtok(tmp_realFileName, "/")))
{
if(theRealFileName[0]=='/')
strcat(dirname, "/");
tmp2_dirname = strtok(NULL, "/");
while((tmp2_dirname))
{
strcat(dirname, tmp_dirname);
strcat(dirname, "/");
tmp_dirname = tmp2_dirname;
tmp2_dirname = strtok(NULL, "/");
}
}
theFileName = tempnam (dirname, ".");
}
else
{
theFileName = theRealFileName;
}
/* Open the file (whether temp or real) for writing. */
theFile = fopen(theFileName, "w");
if (theFile == NULL) /* Something went wrong; we weren't
* even able to open the file. */
goto failure;
/* Pretty-print the description. I assume this is what we want? */
desc = PLGetDescriptionIndent(pl, 0);
/* And put a new line at the EOF */
c = fprintf(theFile, "%s\n", desc);
/* "c" should be strlen(desc)+1 */
if (c <= strlen(desc)) /* We failed to write everything for
* some reason. */
goto failure;
/* We're done, so close everything up. */
c = fclose(theFile);
if (c != 0) /* I can't imagine what went wrong
* closing the file, but we got here,
* so we need to deal with it. */
goto failure;
/* If we used a temporary file, we still need to rename() it be the
* real file. Am I forgetting anything here? */
if (atomically)
{
c = rename(theFileName, theRealFileName);
if (c != 0) /* Many things could go wrong, I
* guess. */
goto failure;
}
/* success: */
MyFree(__FILE__, __LINE__, desc);
if (theFileName)
MyFree(__FILE__, __LINE__, theFileName);
return YES;
/* Just in case the failure action needs to be changed. */
failure:
if(desc)
MyFree(__FILE__, __LINE__, desc);
if (theFileName)
MyFree(__FILE__, __LINE__, theFileName);
return NO;
}
proplist_t PLSetFilename(proplist_t pl, proplist_t filename)
{
int i;
plptr_t current;
/* Just to be sure */
if(pl == NULL)
return NULL;
((plptr_t)pl)->filename = filename;
PLRetain(filename);
switch(((plptr_t)pl)->type)
{
case PLARRAY:
for(i=0;i<PLGetNumberOfElements(pl);i++)
{
current = (plptr_t)PLGetArrayElement(pl, i);
PLSetFilename(current, filename);
}
break;
case PLDICTIONARY:
for(i=0;i<PLGetNumberOfElements(pl); i++)
{
current = (plptr_t)(((plptr_t)pl)->t.dict.keys[i]);
PLSetFilename(current, filename);
current = (plptr_t)(((plptr_t)pl)->t.dict.values[i]);
PLSetFilename(current, filename);
}
break;
}
return pl;
}
proplist_t PLGetFilename(proplist_t pl)
{
plptr_t internal = (plptr_t)pl;
if(internal && internal->filename)
return internal->filename;
else
return NULL;
}
|