|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2012 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
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 |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 #include <string.h> |
|
32 #include <libxml/xmlerror.h> |
|
33 |
|
34 #include "propfind.h" |
|
35 #include "utils.h" |
|
36 #include "main.h" |
|
37 |
|
38 void xmlerrorfnc(void * ctx, const char * msg, ... ) { |
|
39 // nothing |
|
40 } |
|
41 |
|
42 int main(int argc, char **argv) { |
|
43 xmlGenericErrorFunc fnc = xmlerrorfnc; |
|
44 initGenericErrorDefaultFunc(&fnc); |
|
45 |
|
46 if(argc < 2) { |
|
47 print_usage(); |
|
48 return -1; |
|
49 } |
|
50 |
|
51 if(!strcmp(argv[1], "list") || !strcmp(argv[1], "ls")) { |
|
52 return cmd_list(argc - 2, argv + 2); |
|
53 } else if(!strcmp(argv[1], "get")) { |
|
54 return cmd_get(argc - 2, argv + 2); |
|
55 } |
|
56 |
|
57 print_usage(); |
|
58 return -1; |
|
59 } |
|
60 |
|
61 void print_usage() { |
|
62 |
|
63 } |
|
64 |
|
65 int cmd_list(int argc, char **argv) { |
|
66 if(argc == 0) { |
|
67 return -1; |
|
68 } |
|
69 |
|
70 char *url = argv[0]; // TODO: use arg as url or alias |
|
71 |
|
72 CURL *curl = curl_easy_init(); |
|
73 curl_easy_setopt(curl, CURLOPT_URL, url); |
|
74 Propfind *propfind = dav_propfind(curl); |
|
75 |
|
76 UCX_FOREACH(UcxDlist*, propfind->children, ch) { |
|
77 DavResource *resource = ch->data; |
|
78 printf("%s\n", resource->name); |
|
79 } |
|
80 |
|
81 // TODO: free propfind stuff |
|
82 curl_easy_cleanup(curl); |
|
83 |
|
84 return 0; |
|
85 } |
|
86 |
|
87 int cmd_get(int argc, char **argv) { |
|
88 if(argc == 0) { |
|
89 return -1; |
|
90 } |
|
91 |
|
92 char *url = argv[0]; // TODO: use arg as url or alias |
|
93 |
|
94 CURL *curl = curl_easy_init(); |
|
95 curl_easy_setopt(curl, CURLOPT_URL, url); |
|
96 Propfind *propfind = dav_propfind(curl); |
|
97 |
|
98 if(propfind->resource->collection) { |
|
99 UCX_FOREACH(UcxDlist*, propfind->children, chd) { |
|
100 DavResource *resource = chd->data; |
|
101 if(!resource->collection) { |
|
102 char *resurl = util_child_url(propfind->url, resource->href); |
|
103 get_file(curl, resurl, resource->name); |
|
104 } |
|
105 } |
|
106 } else { |
|
107 get_file(curl, propfind->url, propfind->resource->name); |
|
108 } |
|
109 |
|
110 // TODO: free propfind stuff |
|
111 curl_easy_cleanup(curl); |
|
112 |
|
113 return 0; |
|
114 } |
|
115 |
|
116 void get_file(CURL *curl, char *url, char *path) { |
|
117 curl_easy_setopt(curl, CURLOPT_URL, url); |
|
118 |
|
119 FILE *out = fopen(path, "w"); |
|
120 |
|
121 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite); |
|
122 curl_easy_setopt(curl, CURLOPT_WRITEDATA, out); |
|
123 |
|
124 CURLcode res = curl_easy_perform(curl); |
|
125 |
|
126 fclose(out); |
|
127 } |