dav/main.c

changeset 354
067ea2315a8a
parent 353
e554f3d72d9e
child 355
5da2cf15eb44
equal deleted inserted replaced
353:e554f3d72d9e 354:067ea2315a8a
237 "with an optional path:\n"); 237 "with an optional path:\n");
238 fprintf(stderr, " <repository>/path/\n"); 238 fprintf(stderr, " <repository>/path/\n");
239 fprintf(stderr, "\n"); 239 fprintf(stderr, "\n");
240 } 240 }
241 241
242 int request_auth(Repository *repo, DavSession *sn, CmdArgs *a) { 242
243 if(cmd_getoption(a, "noinput")) { 243 int request_auth2(DavSession *sn, void *userdata) {
244 return 0; 244 Repository *repo = userdata;
245 }
246
247 static int login = 0;
248 if(login) {
249 return 0;
250 }
251 245
252 char *user = NULL; 246 char *user = NULL;
253 char ubuf[256]; 247 char ubuf[256];
254 if(repo->user) { 248 if(repo->user) {
255 user = repo->user; 249 user = repo->user;
272 user[ulen-1] = '\0'; 266 user[ulen-1] = '\0';
273 } 267 }
274 268
275 dav_session_set_auth(sn, user, password); 269 dav_session_set_auth(sn, user, password);
276 free(password); 270 free(password);
277 login = 1; 271
278 return 1; 272 return 0;
279 } 273 }
280 274
281 static Repository* url2repo(char *url, char **path) { 275 static Repository* url2repo(char *url, char **path) {
282 size_t ulen = strlen(url); 276 size_t ulen = strlen(url);
283 *path = NULL; 277 *path = NULL;
372 } 366 }
373 if(!repo->verification || cmd_getoption(a, "insecure")) { 367 if(!repo->verification || cmd_getoption(a, "insecure")) {
374 curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYPEER, 0); 368 curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYPEER, 0);
375 curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYHOST, 0); 369 curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYHOST, 0);
376 } 370 }
371 if(!cmd_getoption(a, "noinput")) {
372 dav_session_set_authcallback(sn, request_auth2, repo);
373 }
377 return sn; 374 return sn;
378 } 375 }
379 376
380 #define LIST_QUERY_ORDER_BY_NAME "select `idav:crypto-name`,`idav:crypto-key`,D:lockdiscovery,apache:executable from %s with depth = %d where lastmodified > %t order by iscollection desc, name" 377 #define LIST_QUERY_ORDER_BY_NAME "select `idav:crypto-name`,`idav:crypto-key`,D:lockdiscovery,apache:executable from %s with depth = %d where lastmodified > %t order by iscollection desc, name"
381 #define LIST_QUERY_ORDER_BY_DATE "select `idav:crypto-name`,`idav:crypto-key`,D:lockdiscovery,apache:executable from %s with depth = %d where lastmodified > %t order by iscollection desc, lastmodified desc" 378 #define LIST_QUERY_ORDER_BY_DATE "select `idav:crypto-name`,`idav:crypto-key`,D:lockdiscovery,apache:executable from %s with depth = %d where lastmodified > %t order by iscollection desc, lastmodified desc"
403 t = util_parse_lastmodified(update); 400 t = util_parse_lastmodified(update);
404 } 401 }
405 402
406 int depth = cmd_getoption(a, "recursive") ? -1 : 1; 403 int depth = cmd_getoption(a, "recursive") ? -1 : 1;
407 int ret = -1; 404 int ret = -1;
408 DavResource *ls; 405 DavResource *ls = dav_query(
409 while(ret != 0) { 406 sn,
410 ls = dav_query( 407 date ? LIST_QUERY_ORDER_BY_DATE : LIST_QUERY_ORDER_BY_NAME,
411 sn, 408 path,
412 date ? LIST_QUERY_ORDER_BY_DATE : LIST_QUERY_ORDER_BY_NAME, 409 depth,
413 path, 410 t);
414 depth, 411 if(ls) {
415 t);
416
417 if(!ls) {
418 if(sn->error == DAV_UNAUTHORIZED) {
419 if(request_auth(repo, sn, a)) {
420 continue;
421 }
422 }
423 print_resource_error(sn, path);
424 break;
425 }
426
427 // parameters 412 // parameters
428 void (*print_func)(DavResource*, char *, CmdArgs *); 413 void (*print_func)(DavResource*, char *, CmdArgs *);
429 if(cmd_getoption(a, "list") || cmd_getoption(a, "extended")) { 414 if(cmd_getoption(a, "list") || cmd_getoption(a, "extended")) {
430 print_func = ls_print_list_elm; 415 print_func = ls_print_list_elm;
431 } else { 416 } else {
435 DavResource *child = ls->children; 420 DavResource *child = ls->children;
436 while(child) { 421 while(child) {
437 print_func(child, path, a); 422 print_func(child, path, a);
438 child = child->next; 423 child = child->next;
439 } 424 }
440 425 } else {
441 // leave loop 426 print_resource_error(sn, path);
442 ret = 0;
443 } 427 }
444 428
445 free(path); 429 free(path);
446 //free(base); 430 //free(base);
447 431
687 671
688 int recursive = cmd_getoption(a, "recursive") ? 1 : 0; 672 int recursive = cmd_getoption(a, "recursive") ? 1 : 0;
689 DavResource *res; 673 DavResource *res;
690 674
691 int depth = recursive ? -1 : 1; 675 int depth = recursive ? -1 : 1;
692 for(int i=0;i<2;i++) { 676 res = dav_query(
693 res = dav_query( 677 sn,
694 sn, 678 "select - from %s with depth = %d where iscollection or lastmodified > %t",
695 "select - from %s with depth = %d where iscollection or lastmodified > %t", 679 path,
696 path, 680 depth,
697 depth, 681 t);
698 t);
699 if(!res && sn->error == DAV_UNAUTHORIZED) {
700 if(request_auth(repo, sn, a)) {
701 continue;
702 }
703 }
704 break;
705 }
706
707 if(!res) { 682 if(!res) {
708 print_resource_error(sn, path); 683 print_resource_error(sn, path);
709 return -1; 684 return -1;
710 } 685 }
711 if(!recursive && res->iscollection) { 686 if(!recursive && res->iscollection) {
1057 if(!in) { 1032 if(!in) {
1058 perror("Cannot open tar file"); 1033 perror("Cannot open tar file");
1059 return -1; 1034 return -1;
1060 } 1035 }
1061 1036
1062 DavResource *col = NULL; 1037 DavResource *col = dav_query(sn, "select - from %s", path);
1063 for(int i=0;i<2;i++) {
1064 col = dav_query(sn, "select - from %s", path);
1065 if(!col && sn->error == DAV_UNAUTHORIZED) {
1066 if(request_auth(repo, sn, a)) {
1067 continue;
1068 }
1069 }
1070 break;
1071 }
1072 if(!col) { 1038 if(!col) {
1073 if(sn->error == DAV_NOT_FOUND) { 1039 if(sn->error == DAV_NOT_FOUND) {
1074 col = dav_resource_new(sn, path); 1040 col = dav_resource_new(sn, path);
1075 col->iscollection = TRUE; 1041 col->iscollection = TRUE;
1076 if(dav_create(col)) { 1042 if(dav_create(col)) {
1135 1101
1136 return ret; 1102 return ret;
1137 } 1103 }
1138 1104
1139 int put_file(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *name, FILE *in, off_t len) { 1105 int put_file(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *name, FILE *in, off_t len) {
1140 DavResource *res = NULL; 1106 DavResource *res = dav_query(sn, "select - from %s", path);
1141 for(int i=0;i<2;i++) {
1142 res = dav_query(sn, "select - from %s", path);
1143 if(!res && sn->error == DAV_UNAUTHORIZED) {
1144 if(request_auth(repo, sn, a)) {
1145 continue;
1146 }
1147 }
1148 break;
1149 }
1150 1107
1151 if(!res) { 1108 if(!res) {
1152 if(sn->error == DAV_NOT_FOUND) { 1109 if(sn->error == DAV_NOT_FOUND) {
1153 res = dav_resource_new(sn, path); 1110 res = dav_resource_new(sn, path);
1154 if(dav_create(res)) { 1111 if(dav_create(res)) {
1218 if(!res) { 1175 if(!res) {
1219 fprintf(stderr, "error\n"); 1176 fprintf(stderr, "error\n");
1220 return -1; 1177 return -1;
1221 } 1178 }
1222 1179
1223 int err = 0; 1180 if(dav_delete(res)) {
1224 for(int i=0;i<2;i++) {
1225 err = dav_delete(res);
1226 if(err && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
1227 continue;
1228 } else {
1229 break;
1230 }
1231 }
1232
1233 if(err) {
1234 print_resource_error(sn, res->path); 1181 print_resource_error(sn, res->path);
1235 fprintf(stderr, "Cannot delete resource.\n"); 1182 fprintf(stderr, "Cannot delete resource.\n");
1236 return -1; 1183 return -1;
1237 } 1184 }
1238 1185
1276 fprintf(stderr, "error\n"); 1223 fprintf(stderr, "error\n");
1277 return -1; 1224 return -1;
1278 } 1225 }
1279 res->iscollection = 1; 1226 res->iscollection = 1;
1280 1227
1281 int err = 0; 1228 if(dav_create(res)) {
1282 for(int i=0;i<2;i++) {
1283 err = dav_create(res);
1284 if(err && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
1285 continue;
1286 } else {
1287 break;
1288 }
1289 }
1290
1291 if(err) {
1292 print_resource_error(sn, res->path); 1229 print_resource_error(sn, res->path);
1293 fprintf(stderr, "Cannot create collection.\n"); 1230 fprintf(stderr, "Cannot create collection.\n");
1294 return -1; 1231 return -1;
1295 } 1232 }
1296 1233
1442 return -1; 1379 return -1;
1443 } 1380 }
1444 } 1381 }
1445 1382
1446 DavResource *res = dav_resource_new(sn, path); 1383 DavResource *res = dav_resource_new(sn, path);
1447 for(int i=0;i<2;i++) { 1384 if(dav_load_prop(res, &propname, 1)) {
1448 if(dav_load_prop(res, &propname, 1)) { 1385 print_resource_error(sn, res->path);
1449 if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) { 1386 return -1;
1450 continue;
1451 }
1452 print_resource_error(sn, res->path);
1453 return -1;
1454 }
1455 break;
1456 } 1387 }
1457 free(path); 1388 free(path);
1458 1389
1459 DavXmlNode *x = dav_get_property_ns(res, propname.ns, propname.name); 1390 DavXmlNode *x = dav_get_property_ns(res, propname.ns, propname.name);
1460 if(!x) { 1391 if(!x) {
1496 return -1; 1427 return -1;
1497 } 1428 }
1498 set_session_lock(sn, a); 1429 set_session_lock(sn, a);
1499 1430
1500 DavResource *res = dav_resource_new(sn, path); 1431 DavResource *res = dav_resource_new(sn, path);
1501 for(int i=0;i<2;i++) { 1432 if(!dav_exists(res)) {
1502 if(!dav_exists(res)) { 1433 print_resource_error(sn, res->path);
1503 if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) { 1434 return -1;
1504 continue;
1505 }
1506 print_resource_error(sn, res->path);
1507 return -1;
1508 }
1509 } 1435 }
1510 1436
1511 char *namespace = cmd_getoption(a, "namespace"); 1437 char *namespace = cmd_getoption(a, "namespace");
1512 char *property = a->argv[1]; 1438 char *property = a->argv[1];
1513 char *value = a->argc > 2 ? a->argv[2] : stdin2str(); 1439 char *value = a->argc > 2 ? a->argv[2] : stdin2str();
1517 } else { 1443 } else {
1518 dav_set_property(res, property, value); 1444 dav_set_property(res, property, value);
1519 } 1445 }
1520 1446
1521 int ret = 0; 1447 int ret = 0;
1522 for(int i=0;i<2;i++) { 1448 if(dav_store(res)) {
1523 if(dav_store(res)) { 1449 print_resource_error(sn, res->path);
1524 if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) { 1450 fprintf(stderr, "Cannot set property.\n");
1525 continue; 1451 ret = -1;
1526 }
1527 print_resource_error(sn, res->path);
1528 fprintf(stderr, "Cannot set property.\n");
1529 ret = -1;
1530 }
1531 break;
1532 } 1452 }
1533 1453
1534 free(path); 1454 free(path);
1535 return ret; 1455 return ret;
1536 } 1456 }
1563 } 1483 }
1564 1484
1565 int ret = 0; 1485 int ret = 0;
1566 DavResource *res = dav_resource_new(sn, path); 1486 DavResource *res = dav_resource_new(sn, path);
1567 dav_remove_property_ns(res, propname.ns, propname.name); 1487 dav_remove_property_ns(res, propname.ns, propname.name);
1568 for(int i=0;i<2;i++) { 1488
1569 if(dav_store(res)) { 1489 if(dav_store(res)) {
1570 if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) { 1490 print_resource_error(sn, res->path);
1571 continue; 1491 fprintf(stderr, "Cannot set property.\n");
1572 } 1492 ret = -1;
1573 print_resource_error(sn, res->path);
1574 fprintf(stderr, "Cannot set property.\n");
1575 ret = -1;
1576 }
1577 break;
1578 } 1493 }
1579 1494
1580 free(path); 1495 free(path);
1581 return ret; 1496 return ret;
1582 } 1497 }
1607 timeout = (time_t)atoi(timeoutstr); // TODO: use strtol 1522 timeout = (time_t)atoi(timeoutstr); // TODO: use strtol
1608 } 1523 }
1609 } 1524 }
1610 1525
1611 DavResource *res = dav_resource_new(sn, path); 1526 DavResource *res = dav_resource_new(sn, path);
1612 for(int i=0;i<2;i++) { 1527 if(dav_lock_t(res, timeout)) {
1613 if(!dav_lock_t(res, timeout)) {
1614 break;
1615 }
1616 if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
1617 continue;
1618 }
1619 print_resource_error(sn, res->path); 1528 print_resource_error(sn, res->path);
1620 return -1; 1529 return -1;
1621 } 1530 }
1622 1531
1623 DavLock *lock = dav_get_lock(sn, res->path); 1532 DavLock *lock = dav_get_lock(sn, res->path);
1684 free(locktoken); 1593 free(locktoken);
1685 } 1594 }
1686 1595
1687 int ret = 0; 1596 int ret = 0;
1688 DavResource *res = dav_resource_new(sn, path); 1597 DavResource *res = dav_resource_new(sn, path);
1689 for(int i=0;i<2;i++) { 1598 if(dav_unlock(res)) {
1690 if(!dav_unlock(res)) {
1691 break;
1692 }
1693 if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
1694 continue;
1695 }
1696 print_resource_error(sn, res->path); 1599 print_resource_error(sn, res->path);
1697 ret = -1; 1600 ret = -1;
1698 break;
1699 } 1601 }
1700 1602
1701 dav_session_destroy(sn); 1603 dav_session_destroy(sn);
1702 return ret; 1604 return ret;
1703 } 1605 }
1727 if(set_session_config(sn, a)) { 1629 if(set_session_config(sn, a)) {
1728 return -1; 1630 return -1;
1729 } 1631 }
1730 1632
1731 DavResource *res = dav_resource_new(sn, path); 1633 DavResource *res = dav_resource_new(sn, path);
1732 for(int i=0;i<2;i++) { 1634 if(!dav_load(res)) {
1733 if(!dav_load(res)) { 1635 printf("name: %s\n", res->name);
1734 printf("name: %s\n", res->name); 1636 printf("path: %s\n", res->path);
1735 printf("path: %s\n", res->path); 1637
1736 1638 char *server = util_url_base(sn->base_url);
1737 char *server = util_url_base(sn->base_url); 1639 char *url = util_concat_path(server, res->href);
1738 char *url = util_concat_path(server, res->href); 1640 printf("url: %s\n", url);
1739 printf("url: %s\n", url); 1641 free(url);
1740 free(url); 1642 free(server);
1741 free(server); 1643
1742 1644 if(res->iscollection) {
1743 if(res->iscollection) { 1645 printf("type: collection\n");
1744 printf("type: collection\n"); 1646 printf("size: %d\n", count_children(res));
1745 printf("size: %d\n", count_children(res)); 1647 } else {
1648 printf("type: resource\n");
1649 char *len = ls_size_str(res);
1650 printf("size: %s\n", len);
1651 free(len);
1652 }
1653
1654 size_t count = 0;
1655 DavPropName *properties = dav_get_property_names(res, &count);
1656
1657 char *last_ns = NULL;
1658 for(int i=0;i<count;i++) {
1659 DavPropName p = properties[i];
1660 if(!last_ns || strcmp(last_ns, p.ns)) {
1661 printf("\nnamespace: %s\n", p.ns);
1662 last_ns = p.ns;
1663 }
1664
1665 DavXmlNode *xval = dav_get_property_ns(res, p.ns, p.name);
1666 if(dav_xml_isstring(xval)) {
1667 sstr_t value = sstr(dav_xml_getstring(xval));
1668 printf(" %s: %.*s\n", p.name, (int)value.length, value.ptr);
1746 } else { 1669 } else {
1747 printf("type: resource\n"); 1670 printf(" %s: $xml\n", p.name);
1748 char *len = ls_size_str(res); 1671 }
1749 printf("size: %s\n", len); 1672 }
1750 free(len); 1673
1751 } 1674 dav_session_free(sn, properties);
1752 1675 return 0;
1753 size_t count = 0; 1676 } else {
1754 DavPropName *properties = dav_get_property_names(res, &count); 1677 print_resource_error(sn, res->path);
1755 1678 }
1756 char *last_ns = NULL; 1679
1757 for(int i=0;i<count;i++) {
1758 DavPropName p = properties[i];
1759 if(!last_ns || strcmp(last_ns, p.ns)) {
1760 printf("\nnamespace: %s\n", p.ns);
1761 last_ns = p.ns;
1762 }
1763
1764 DavXmlNode *xval = dav_get_property_ns(res, p.ns, p.name);
1765 if(dav_xml_isstring(xval)) {
1766 sstr_t value = sstr(dav_xml_getstring(xval));
1767 printf(" %s: %.*s\n", p.name, (int)value.length, value.ptr);
1768 } else {
1769 printf(" %s: $xml\n", p.name);
1770 }
1771 }
1772
1773 dav_session_free(sn, properties);
1774 return 0;
1775 } else {
1776 if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
1777 continue;
1778 }
1779 print_resource_error(sn, res->path);
1780 break;
1781 }
1782 }
1783 return -1; 1680 return -1;
1784 } 1681 }
1785 1682
1786 1683
1787 char* stdin2str() { 1684 char* stdin2str() {

mercurial