@matheusbsbs / matheus-bagliano / commits / 3030baa7f0
Fork 0

http-push: refactor remote file/directory processing

Replace single-use functions with one that can get a list of remote collections and pass file/directory information to user-defined functions for processing. Signed-off-by: Junio C Hamano <junkio@cox.net>

Nick Hengeveld committed Mar 10, 2006 at 20:18 UTC 3030baa7f06e65d6de2d612d7edb94d22f27d239
1 file changed +106 -137
http-push.c
+106 -137
@@ -140,11 +140,23 @@ struct remote_lock
140 struct remote_lock *next;
141 };
142
143 -struct remote_dentry
143 +/* Flags that control remote_ls processing */
144 +#define PROCESS_FILES (1u << 0)
145 +#define PROCESS_DIRS (1u << 1)
146 +#define RECURSIVE (1u << 2)
147 +
148 +/* Flags that remote_ls passes to callback functions */
149 +#define IS_DIR (1u << 0)
150 +
151 +struct remote_ls_ctx
152 {
145 - char *base;
146 - char *name;
147 - int is_dir;
153 + char *path;
154 + void (*userFunc)(struct remote_ls_ctx *ls);
155 + void *userData;
156 + int flags;
157 + char *dentry_name;
158 + int dentry_flags;
159 + struct remote_ls_ctx *parent;
160 };
161
162 static void finish_request(struct transfer_request *request);
@@ -812,55 +824,6 @@ static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
824 }
825
826 static void one_remote_ref(char *refname);
815 -static void crawl_remote_refs(char *path);
816 -
817 -static void handle_crawl_ref_ctx(struct xml_ctx *ctx, int tag_closed)
818 -{
819 - struct remote_dentry *dentry = (struct remote_dentry *)ctx->userData;
820 -
821 -
822 - if (tag_closed) {
823 - if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && dentry->name) {
824 - if (dentry->is_dir) {
825 - if (strcmp(dentry->name, dentry->base)) {
826 - crawl_remote_refs(dentry->name);
827 - }
828 - } else {
829 - one_remote_ref(dentry->name);
830 - }
831 - } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
832 - dentry->name = xmalloc(strlen(ctx->cdata) -
833 - remote->path_len + 1);
834 - strcpy(dentry->name,
835 - ctx->cdata + remote->path_len);
836 - } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
837 - dentry->is_dir = 1;
838 - }
839 - } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
840 - dentry->name = NULL;
841 - dentry->is_dir = 0;
842 - }
843 -}
844 -
845 -static void handle_remote_object_list_ctx(struct xml_ctx *ctx, int tag_closed)
846 -{
847 - char *path;
848 - char *obj_hex;
849 -
850 - if (tag_closed) {
851 - if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
852 - path = ctx->cdata + remote->path_len;
853 - if (strlen(path) != 50)
854 - return;
855 - path += 9;
856 - obj_hex = xmalloc(strlen(path));
857 - strncpy(obj_hex, path, 2);
858 - strcpy(obj_hex + 2, path + 3);
859 - one_remote_object(obj_hex);
860 - free(obj_hex);
861 - }
862 - }
863 -}
827
828 static void
829 xml_start_tag(void *userData, const char *name, const char **atts)
@@ -1101,91 +1064,83 @@ static int unlock_remote(struct remote_lock *lock)
1064 return rc;
1065 }
1066
1104 -static void crawl_remote_refs(char *path)
1105 -{
1106 - char *url;
1107 - struct active_request_slot *slot;
1108 - struct slot_results results;
1109 - struct buffer in_buffer;
1110 - struct buffer out_buffer;
1111 - char *in_data;
1112 - char *out_data;
1113 - XML_Parser parser = XML_ParserCreate(NULL);
1114 - enum XML_Status result;
1115 - struct curl_slist *dav_headers = NULL;
1116 - struct xml_ctx ctx;
1117 - struct remote_dentry dentry;
1118 -
1119 - fprintf(stderr, " %s\n", path);
1067 +static void remote_ls(const char *path, int flags,
1068 + void (*userFunc)(struct remote_ls_ctx *ls),
1069 + void *userData);
1070
1121 - dentry.base = path;
1122 - dentry.name = NULL;
1123 - dentry.is_dir = 0;
1124 -
1125 - url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1126 - sprintf(url, "%s%s", remote->url, path);
1071 +static void process_ls_object(struct remote_ls_ctx *ls)
1072 +{
1073 + unsigned int *parent = (unsigned int *)ls->userData;
1074 + char *path = ls->dentry_name;
1075 + char *obj_hex;
1076
1128 - out_buffer.size = strlen(PROPFIND_ALL_REQUEST);
1129 - out_data = xmalloc(out_buffer.size + 1);
1130 - snprintf(out_data, out_buffer.size + 1, PROPFIND_ALL_REQUEST);
1131 - out_buffer.posn = 0;
1132 - out_buffer.buffer = out_data;
1077 + if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1078 + remote_dir_exists[*parent] = 1;
1079 + return;
1080 + }
1081
1134 - in_buffer.size = 4096;
1135 - in_data = xmalloc(in_buffer.size);
1136 - in_buffer.posn = 0;
1137 - in_buffer.buffer = in_data;
1082 + if (strlen(path) != 49)
1083 + return;
1084 + path += 8;
1085 + obj_hex = xmalloc(strlen(path));
1086 + strncpy(obj_hex, path, 2);
1087 + strcpy(obj_hex + 2, path + 3);
1088 + one_remote_object(obj_hex);
1089 + free(obj_hex);
1090 +}
1091
1139 - dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1140 - dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1092 +static void process_ls_ref(struct remote_ls_ctx *ls)
1093 +{
1094 + if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1095 + fprintf(stderr, " %s\n", ls->dentry_name);
1096 + return;
1097 + }
1098
1142 - slot = get_active_slot();
1143 - slot->results = &results;
1144 - curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1145 - curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1146 - curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1147 - curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1148 - curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1149 - curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1150 - curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1151 - curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1152 - curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1099 + if (!(ls->dentry_flags & IS_DIR))
1100 + one_remote_ref(ls->dentry_name);
1101 +}
1102
1154 - if (start_active_slot(slot)) {
1155 - run_active_slot(slot);
1156 - if (results.curl_result == CURLE_OK) {
1157 - ctx.name = xcalloc(10, 1);
1158 - ctx.len = 0;
1159 - ctx.cdata = NULL;
1160 - ctx.userFunc = handle_crawl_ref_ctx;
1161 - ctx.userData = &dentry;
1162 - XML_SetUserData(parser, &ctx);
1163 - XML_SetElementHandler(parser, xml_start_tag,
1164 - xml_end_tag);
1165 - XML_SetCharacterDataHandler(parser, xml_cdata);
1166 - result = XML_Parse(parser, in_buffer.buffer,
1167 - in_buffer.posn, 1);
1168 - free(ctx.name);
1103 +static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1104 +{
1105 + struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1106
1170 - if (result != XML_STATUS_OK) {
1171 - fprintf(stderr, "XML error: %s\n",
1172 - XML_ErrorString(
1173 - XML_GetErrorCode(parser)));
1107 + if (tag_closed) {
1108 + if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1109 + if (ls->dentry_flags & IS_DIR) {
1110 + if (ls->flags & PROCESS_DIRS) {
1111 + ls->userFunc(ls);
1112 + }
1113 + if (strcmp(ls->dentry_name, ls->path) &&
1114 + ls->flags & RECURSIVE) {
1115 + remote_ls(ls->dentry_name,
1116 + ls->flags,
1117 + ls->userFunc,
1118 + ls->userData);
1119 + }
1120 + } else if (ls->flags & PROCESS_FILES) {
1121 + ls->userFunc(ls);
1122 }
1123 + } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1124 + ls->dentry_name = xmalloc(strlen(ctx->cdata) -
1125 + remote->path_len + 1);
1126 + strcpy(ls->dentry_name, ctx->cdata + remote->path_len);
1127 + } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1128 + ls->dentry_flags |= IS_DIR;
1129 }
1176 - } else {
1177 - fprintf(stderr, "Unable to start request\n");
1130 + } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1131 + if (ls->dentry_name) {
1132 + free(ls->dentry_name);
1133 + }
1134 + ls->dentry_name = NULL;
1135 + ls->dentry_flags = 0;
1136 }
1179 -
1180 - free(url);
1181 - free(out_data);
1182 - free(in_buffer.buffer);
1183 - curl_slist_free_all(dav_headers);
1137 }
1138
1186 -static void get_remote_object_list(unsigned char parent)
1139 +static void remote_ls(const char *path, int flags,
1140 + void (*userFunc)(struct remote_ls_ctx *ls),
1141 + void *userData)
1142 {
1188 - char *url;
1143 + char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1144 struct active_request_slot *slot;
1145 struct slot_results results;
1146 struct buffer in_buffer;
@@ -1196,13 +1151,15 @@ static void get_remote_object_list(unsigned char parent)
1151 enum XML_Status result;
1152 struct curl_slist *dav_headers = NULL;
1153 struct xml_ctx ctx;
1199 - char path[] = "/objects/XX/";
1200 - static const char hex[] = "0123456789abcdef";
1201 - unsigned int val = parent;
1154 + struct remote_ls_ctx ls;
1155 +
1156 + ls.flags = flags;
1157 + ls.path = strdup(path);
1158 + ls.dentry_name = NULL;
1159 + ls.dentry_flags = 0;
1160 + ls.userData = userData;
1161 + ls.userFunc = userFunc;
1162
1203 - path[9] = hex[val >> 4];
1204 - path[10] = hex[val & 0xf];
1205 - url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1163 sprintf(url, "%s%s", remote->url, path);
1164
1165 out_buffer.size = strlen(PROPFIND_ALL_REQUEST);
@@ -1234,11 +1191,11 @@ static void get_remote_object_list(unsigned char parent)
1191 if (start_active_slot(slot)) {
1192 run_active_slot(slot);
1193 if (results.curl_result == CURLE_OK) {
1237 - remote_dir_exists[parent] = 1;
1194 ctx.name = xcalloc(10, 1);
1195 ctx.len = 0;
1196 ctx.cdata = NULL;
1241 - ctx.userFunc = handle_remote_object_list_ctx;
1197 + ctx.userFunc = handle_remote_ls_ctx;
1198 + ctx.userData = &ls;
1199 XML_SetUserData(parser, &ctx);
1200 XML_SetElementHandler(parser, xml_start_tag,
1201 xml_end_tag);
@@ -1252,19 +1209,31 @@ static void get_remote_object_list(unsigned char parent)
1209 XML_ErrorString(
1210 XML_GetErrorCode(parser)));
1211 }
1255 - } else {
1256 - remote_dir_exists[parent] = 0;
1212 }
1213 } else {
1259 - fprintf(stderr, "Unable to start request\n");
1214 + fprintf(stderr, "Unable to start PROPFIND request\n");
1215 }
1216
1217 + free(ls.path);
1218 free(url);
1219 free(out_data);
1220 free(in_buffer.buffer);
1221 curl_slist_free_all(dav_headers);
1222 }
1223
1224 +static void get_remote_object_list(unsigned char parent)
1225 +{
1226 + char path[] = "objects/XX/";
1227 + static const char hex[] = "0123456789abcdef";
1228 + unsigned int val = parent;
1229 +
1230 + path[8] = hex[val >> 4];
1231 + path[9] = hex[val & 0xf];
1232 + remote_dir_exists[val] = 0;
1233 + remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1234 + process_ls_object, &val);
1235 +}
1236 +
1237 static int locking_available(void)
1238 {
1239 struct active_request_slot *slot;
@@ -1534,7 +1503,7 @@ static void get_local_heads(void)
1503 static void get_dav_remote_heads(void)
1504 {
1505 remote_tail = &remote_refs;
1537 - crawl_remote_refs("refs/");
1506 + remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1507 }
1508
1509 static int is_zero_sha1(const unsigned char *sha1)