dav/sync.c

changeset 574
c36eddf167a8
parent 573
b8f798d240ab
child 576
62cc92445234
equal deleted inserted replaced
573:b8f798d240ab 574:c36eddf167a8
2399 } 2399 }
2400 2400
2401 LocalResource* local_resource_new(SyncDirectory *dir, SyncDatabase *db, char *path) { 2401 LocalResource* local_resource_new(SyncDirectory *dir, SyncDatabase *db, char *path) {
2402 char *file_path = create_local_path(dir, path); 2402 char *file_path = create_local_path(dir, path);
2403 SYS_STAT s; 2403 SYS_STAT s;
2404 if(sys_stat(file_path, &s)) { 2404 if(sys_lstat(file_path, &s)) {
2405 fprintf(stderr, "Cannot stat file %s\n", file_path); 2405 fprintf(stderr, "Cannot stat file %s\n", file_path);
2406 free(file_path); 2406 free(file_path);
2407 return NULL; 2407 return NULL;
2408 } 2408 }
2409
2410 LocalResource *res = calloc(1, sizeof(LocalResource));
2411 res->mode = s.st_mode & 07777;
2412 res->uid = s.st_uid;
2413 res->gid = s.st_gid;
2414 res->last_modified = s.st_mtime;
2415 if(!S_ISDIR(s.st_mode)) {
2416 res->path = strdup(path);
2417 res->size = s.st_size;
2418 } else {
2419 res->path = util_concat_path(path, "/");
2420 res->isdirectory = 1;
2421 }
2422
2423 if(S_ISLNK(s.st_mode)) {
2424 size_t lnksize = s.st_size > 256 ? s.st_size : 256;
2425 char *lnkbuf = malloc(lnksize);
2426
2427 ssize_t len = 0;
2428 for(int i=0;i<4;i++) {
2429 // we try to read the link at most 4 times
2430 // only repeat if the buffer is too small
2431 len = sys_readlink(file_path, lnkbuf, lnksize);
2432 if(len != lnksize) {
2433 break;
2434 }
2435 lnksize *= 2;
2436 lnkbuf = realloc(lnkbuf, lnksize);
2437 }
2438
2439 if(len > 0) {
2440 res->link_target = lnkbuf;
2441 res->link_target[len] = 0;
2442 } else {
2443 free(lnkbuf);
2444 }
2445
2446 free(lnkbuf);
2447 }
2448
2409 free(file_path); 2449 free(file_path);
2410 2450
2411 if(!S_ISDIR(s.st_mode)) { 2451 return res;
2412 LocalResource *res = calloc(1, sizeof(LocalResource));
2413 res->path = strdup(path);
2414 res->etag = NULL;
2415 res->last_modified = s.st_mtime;
2416 res->size = s.st_size;
2417 res->mode = s.st_mode & 07777;
2418 res->uid = s.st_uid;
2419 res->gid = s.st_gid;
2420 return res;
2421 } else {
2422 LocalResource *res = calloc(1, sizeof(LocalResource));
2423 res->path = util_concat_path(path, "/");
2424 res->last_modified = s.st_mtime;
2425 res->mode = s.st_mode & 07777;
2426 res->uid = s.st_uid;
2427 res->gid = s.st_gid;
2428 res->isdirectory = 1;
2429 return res;
2430 }
2431 } 2452 }
2432 2453
2433 int local_resource_is_changed( 2454 int local_resource_is_changed(
2434 SyncDirectory *dir, 2455 SyncDirectory *dir,
2435 SyncDatabase *db, 2456 SyncDatabase *db,

mercurial