717 // |
717 // |
718 |
718 |
719 // the first thing we need are all directories to put the files in |
719 // the first thing we need are all directories to put the files in |
720 UCX_FOREACH(elm, res_mkdir) { |
720 UCX_FOREACH(elm, res_mkdir) { |
721 DavResource *res = elm->data; |
721 DavResource *res = elm->data; |
722 char *res_path = resource_local_path(res); |
722 if(sync_get_collection(a, dir, res, db)) { |
723 char *local_path = create_local_path(dir, res->path); |
723 sync_error++; |
724 free(res_path); |
724 } |
725 if(sys_mkdir(local_path) && errno != EEXIST) { |
|
726 fprintf(stderr, |
|
727 "Cannot create directory %s: %s", |
|
728 local_path, strerror(errno)); |
|
729 } |
|
730 free(local_path); |
|
731 // TODO: create localres for dir |
|
732 } |
725 } |
733 |
726 |
734 // we need a map for all conflicts for fast lookups |
727 // we need a map for all conflicts for fast lookups |
735 UcxMap *conflicts = ucx_map_new(ucx_list_size(res_conflict)+16); |
728 UcxMap *conflicts = ucx_map_new(ucx_list_size(res_conflict)+16); |
736 UCX_FOREACH(elm, res_conflict) { |
729 UCX_FOREACH(elm, res_conflict) { |
1469 if(tmp_path) { |
1462 if(tmp_path) { |
1470 free(tmp_path); |
1463 free(tmp_path); |
1471 } |
1464 } |
1472 free(local_path); |
1465 free(local_path); |
1473 return ret; |
1466 return ret; |
|
1467 } |
|
1468 |
|
1469 int sync_get_collection( |
|
1470 CmdArgs *a, |
|
1471 SyncDirectory *dir, |
|
1472 DavResource *res, |
|
1473 SyncDatabase *db) |
|
1474 { |
|
1475 char *res_path = resource_local_path(res); |
|
1476 char *local_path = create_local_path(dir, res->path); |
|
1477 free(res_path); |
|
1478 |
|
1479 printf("get: %s\n", res->path); |
|
1480 // create directory |
|
1481 // ignore error if it already exists |
|
1482 if(sys_mkdir(local_path) && errno != EEXIST) { |
|
1483 fprintf(stderr, |
|
1484 "Cannot create directory %s: %s", |
|
1485 local_path, strerror(errno)); |
|
1486 free(local_path); |
|
1487 return 1; |
|
1488 } |
|
1489 SYS_STAT s; |
|
1490 if(sys_stat(local_path, &s)) { |
|
1491 fprintf(stderr, "Cannot stat directory %s: %s", local_path, strerror(errno)); |
|
1492 free(local_path); |
|
1493 return 1; |
|
1494 } |
|
1495 |
|
1496 // if it doesn't exist in the db, create an entry for the dir |
|
1497 LocalResource *local = ucx_map_cstr_get(db->resources, res->path); |
|
1498 if(!local) { |
|
1499 local = calloc(1, sizeof(LocalResource)); |
|
1500 local->path = strdup(res->path); |
|
1501 ucx_map_cstr_put(db->resources, local->path, local); |
|
1502 } |
|
1503 local->isdirectory = 1; |
|
1504 |
|
1505 // cleanup LocalResource |
|
1506 if(local->etag) { |
|
1507 free(local->etag); |
|
1508 local->etag = NULL; |
|
1509 } |
|
1510 if(local->hash) { |
|
1511 free(local->hash); |
|
1512 local->hash = NULL; |
|
1513 } |
|
1514 if(local->link_target) { |
|
1515 free(local->link_target); |
|
1516 local->link_target = NULL; |
|
1517 } |
|
1518 local->skipped = 0; |
|
1519 local->size = 0; |
|
1520 |
|
1521 // set metadata |
|
1522 if(sync_store_metadata(dir, local_path, local, res)) { |
|
1523 fprintf(stderr, "Cannot store metadata: %s\n", res->path); |
|
1524 } |
|
1525 sync_set_metadata_from_stat(local, &s); |
|
1526 |
|
1527 // cleanup |
|
1528 free(local_path); |
|
1529 return 0; |
1474 } |
1530 } |
1475 |
1531 |
1476 int sync_remove_local_resource(SyncDirectory *dir, LocalResource *res) { |
1532 int sync_remove_local_resource(SyncDirectory *dir, LocalResource *res) { |
1477 char *local_path = create_local_path(dir, res->path); |
1533 char *local_path = create_local_path(dir, res->path); |
1478 SYS_STAT s; |
1534 SYS_STAT s; |
1933 if(sync_mkdir(dir, res, local_res) && sn->error != DAV_METHOD_NOT_ALLOWED) { |
1989 if(sync_mkdir(dir, res, local_res) && sn->error != DAV_METHOD_NOT_ALLOWED) { |
1934 print_resource_error(sn, res->path); |
1990 print_resource_error(sn, res->path); |
1935 ret = -1; |
1991 ret = -1; |
1936 sync_error++; |
1992 sync_error++; |
1937 error = 1; |
1993 error = 1; |
1938 //abort = 1; |
1994 abort = 1; |
1939 } else { |
|
1940 // success |
|
1941 if(local_res->metadata_updated) { |
|
1942 sync_update_metadata(dir, sn, res, local_res); |
|
1943 } |
|
1944 |
|
1945 // remove old db entry (if it exists) |
|
1946 // and add add new entry |
|
1947 LocalResource *dbres = ucx_map_cstr_remove(db->resources, local_res->path); |
|
1948 ucx_map_cstr_put(db->resources, local_res->path, local_res); |
|
1949 } |
1995 } |
1950 } else if(sn->error != DAV_OK) { |
1996 } else if(sn->error != DAV_OK) { |
1951 // dav_exists() failed |
1997 // dav_exists() failed |
1952 print_resource_error(sn, local_res->path); |
1998 print_resource_error(sn, local_res->path); |
1953 ret = -1; |
1999 ret = -1; |
1954 sync_error++; |
2000 sync_error++; |
1955 error = 1; |
2001 error = 1; |
1956 //abort = 1; |
2002 abort = 1; |
|
2003 } |
|
2004 |
|
2005 if(!abort) { |
|
2006 // success |
|
2007 if(local_res->metadata_updated) { |
|
2008 sync_update_metadata(dir, sn, res, local_res); |
|
2009 } |
|
2010 |
|
2011 // remove old db entry (if it exists) |
|
2012 // and add add new entry |
|
2013 LocalResource *dbres = ucx_map_cstr_remove(db->resources, local_res->path); |
|
2014 ucx_map_cstr_put(db->resources, local_res->path, local_res); |
1957 } |
2015 } |
1958 |
2016 |
1959 dav_resource_free(res); |
2017 dav_resource_free(res); |
1960 } |
2018 } |
1961 |
2019 |