dav/main.c

changeset 540
d18f92483945
parent 539
8deb52292c99
child 587
3c917df041b8
equal deleted inserted replaced
539:8deb52292c99 540:d18f92483945
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include <assert.h>
29 #include <stdio.h> 30 #include <stdio.h>
30 #include <stdlib.h> 31 #include <stdlib.h>
31 #include <string.h> 32 #include <string.h>
32 #include <stdbool.h> 33 #include <stdbool.h>
33 #include <errno.h> 34 #include <errno.h>
214 } 215 }
215 216
216 static char *cmdusageinfo[] = { 217 static char *cmdusageinfo[] = {
217 "list [-altdepcR] [-u <date>] <url>", 218 "list [-altdepcR] [-u <date>] <url>",
218 "get [-pcRK] [-o <file>] [-u <date>] [-V <version>] <url>", 219 "get [-pcRK] [-o <file>] [-u <date>] [-V <version>] <url>",
219 "put [-pcR] [-k <key>] [-L <lock>] <url> <file>", 220 "put [-pcR] [-k <key>] [-L <lock>] <url> <file...>",
220 "mkdir [-pc] [-k <key>] [-L <lock>] <url>", 221 "mkdir [-pc] [-k <key>] [-L <lock>] <url> [file...]",
221 "remove [-pc] [-L <lock>] <url>", 222 "remove [-pc] [-L <lock>] <url> [file...]",
222 "copy [-pcO] [-L <lock>] <url> <url>", 223 "copy [-pcO] [-L <lock>] <url> <url>",
223 "move [-pcO] [-L <lock>] <url> <url>", 224 "move [-pcO] [-L <lock>] <url> <url>",
224 "rename [-pcO] [-L <lock>] <url> <name>", 225 "rename [-pcO] [-L <lock>] <url> <name>",
225 "export [-pc] [-o <file>] [-u <date>] <url>", 226 "export [-pc] [-o <file>] [-u <date>] <url>",
226 "import [-pc] [-k <key>] [-L <lock>] <url> <file>", 227 "import [-pc] [-k <key>] [-L <lock>] <url> <file>",
1545 return -1; 1546 return -1;
1546 } 1547 }
1547 return 0; 1548 return 0;
1548 } 1549 }
1549 1550
1550 1551 static int dav_create_col(DavResource *res) {
1551 int cmd_remove(CmdArgs *a) { 1552 res->iscollection = 1;
1552 if(a->argc != 1) { 1553 return dav_create(res);
1553 // TODO: change, when removal of multiple files is supported 1554 }
1554 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many"); 1555
1555 fprintf(stderr, "Usage: dav %s\n", find_usage_str("remove")); 1556 static int cmd_operation_on_resources(CmdArgs* a,
1557 int(*operation)(DavResource*),
1558 const char* command,
1559 const char* message) {
1560 if(a->argc < 1) {
1561 fprintf(stderr, "Too few arguments\n");
1562 fprintf(stderr, "Usage: dav %s\n", find_usage_str(command));
1556 return -1; 1563 return -1;
1557 } 1564 }
1558 1565
1559 char *url = a->argv[0]; 1566 char *url = a->argv[0];
1560 char *path = NULL; 1567 char *path = NULL;
1561 Repository *repo = url2repo(url, &path); 1568 Repository *repo = url2repo(url, &path);
1562 DavSession *sn = connect_to_repo(repo, path, a); 1569 DavSession *sn = connect_to_repo(repo, path, a);
1563 1570
1571 int exit_code = -1;
1572 assert(!!path && !!sn);
1573
1564 if(set_session_config(sn, a)) { 1574 if(set_session_config(sn, a)) {
1565 return -1; 1575 goto cmd_oponres_exit;
1566 } 1576 }
1577
1567 set_session_lock(sn, a); 1578 set_session_lock(sn, a);
1568 1579
1580 if(check_encryption_key(a, sn)) {
1581 goto cmd_oponres_exit;
1582 }
1583
1569 DavResource *res = dav_resource_new(sn, path); 1584 DavResource *res = dav_resource_new(sn, path);
1570 if(!res) { 1585 assert(!!res);
1571 fprintf(stderr, "error\n");
1572 return -1;
1573 }
1574
1575 if(dav_delete(res)) {
1576 print_resource_error(sn, res->path);
1577 fprintf(stderr, "Cannot delete resource.\n");
1578 return -1;
1579 }
1580
1581 free(path);
1582 return 0;
1583 }
1584
1585 int cmd_mkdir(CmdArgs *a) {
1586 if(a->argc != 1) {
1587 // TODO: change, when creation of multiple dirs is supported
1588 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
1589 fprintf(stderr, "Usage: dav %s\n", find_usage_str("mkdir"));
1590 return -1;
1591 }
1592
1593 char *url = a->argv[0];
1594 char *path = NULL;
1595 Repository *repo = url2repo(url, &path);
1596 DavSession *sn = connect_to_repo(repo, path, a);
1597
1598 if(set_session_config(sn, a)) {
1599 return -1;
1600 }
1601 set_session_lock(sn, a);
1602
1603 if(check_encryption_key(a, sn)) {
1604 // TODO: free
1605 return -1;
1606 }
1607
1608 DavResource *res = dav_resource_new(sn, path);
1609 if(!res) {
1610 fprintf(stderr, "error\n");
1611 // TODO: free
1612 return -1;
1613 }
1614 res->iscollection = 1; 1586 res->iscollection = 1;
1615 1587
1616 if(dav_create(res)) { 1588 if(a->argc == 1) {
1617 print_resource_error(sn, res->path); 1589 if(operation(res)) {
1618 fprintf(stderr, "Cannot create collection.\n"); 1590 fprintf(stderr, "Cannot %s.\n", message);
1619 // TODO: free 1591 print_resource_error(sn, res->path);
1620 return -1; 1592 goto cmd_oponres_exit;
1621 } 1593 }
1622 1594 } else {
1595 for(int i = 1 ; i < a->argc ;++i) {
1596 DavResource *child = dav_resource_new_child(sn, res, a->argv[i]);
1597 assert(!!child);
1598 child->iscollection = 1;
1599 if(operation(child)) {
1600 fprintf(stderr, "Cannot %s %s.\n", message, a->argv[i]);
1601 print_resource_error(sn, child->path);
1602 goto cmd_oponres_exit;
1603 }
1604 }
1605 }
1606
1607 exit_code = 0;
1608 cmd_oponres_exit:
1623 free(path); 1609 free(path);
1624 dav_session_destroy(sn); 1610 dav_session_destroy(sn);
1625 return 0; 1611 return exit_code;
1612 }
1613
1614 int cmd_remove(CmdArgs *a) {
1615 return cmd_operation_on_resources(a, dav_delete,
1616 "remove", "delete resource");
1617 }
1618
1619 int cmd_mkdir(CmdArgs *a) {
1620 return cmd_operation_on_resources(a, dav_create_col,
1621 "mkdir", "create collection");
1626 } 1622 }
1627 1623
1628 int cmd_move(CmdArgs *a, int cp) { 1624 int cmd_move(CmdArgs *a, int cp) {
1629 const char* actionstr = cp ? "copy" : "move"; 1625 const char* actionstr = cp ? "copy" : "move";
1630 1626

mercurial