summaryrefslogtreecommitdiff
path: root/cache/src/tango_cache_xml.cpp
blob: 97280b6a2ce7d1c2b1ea7f74821fa69e055f9c6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#include <libxml/parser.h>
#include "tango_cache_xml.h"

bool parse_uploadID_xml(const char *content, int len, char **uploadID)
{
	xmlDoc *pdoc;
	xmlNode *pcur;
	
	if((pdoc = xmlParseMemory(content, len)) == NULL)
	{
		return false;
	}
	if((pcur = xmlDocGetRootElement(pdoc)) == NULL)
	{
		xmlFreeDoc(pdoc);
		return false;
	}

	while(pcur->type != XML_ELEMENT_NODE)
		pcur = pcur->next;
	if(xmlStrcmp(pcur->name, (const xmlChar *)"InitiateMultipartUploadResult"))
	{
		xmlFreeDoc(pdoc);
		return false;
	}
	pcur = pcur->children;
	while(pcur != NULL)
	{
		if(pcur->type != XML_ELEMENT_NODE || xmlStrcmp(pcur->name, (const xmlChar *)"UploadId"))
		{
			pcur = pcur->next;
			continue;
		}
		*uploadID = (char *)xmlNodeGetContent(pcur);
		xmlFreeDoc(pdoc);
		return true;
	}

	xmlFreeDoc(pdoc);
	return false;
}

void construct_complete_xml(struct tango_cache_ctx *ctx, char **xml, int *len)
{
	struct multipart_etag_list *etag;
	xmlDoc *pdoc;
	xmlNode *root, *child;
	char number[20];

	pdoc = xmlNewDoc((const xmlChar *)"1.0");
	root = xmlNewNode(NULL, (const xmlChar *)"CompleteMultipartUpload");
	/*Big data deletion of this field parsing, shielding this field**/
	//xmlNewProp(root, (const xmlChar *)"xmlns",(const xmlChar *)"http://s3.amazonaws.com/doc/2006-03-01/");
	xmlDocSetRootElement(pdoc, root);

	TAILQ_FOREACH(etag, &ctx->put.etag_head, node)
	{
		sprintf(number, "%u", etag->part_number);
		child = xmlNewChild(root, NULL, (const xmlChar*)"Part", NULL);
		xmlNewChild(child, NULL, (const xmlChar*)"ETag", (const xmlChar*)etag->etag);
		xmlNewChild(child, NULL, (const xmlChar*)"PartNumber", (const xmlChar*)number);
	}
	
	xmlDocDumpFormatMemory(pdoc, (xmlChar **)xml, len, 1);
	xmlFreeDoc(pdoc);
}

static void fill_multidelete_xml_errcode(xmlNode *error, char *out, int size)
{
	xmlChar *errcode;
	xmlNode *child = error->children;

	while(child != NULL)
	{
		if(child->type != XML_ELEMENT_NODE || xmlStrcmp(child->name, (const xmlChar *)"Message"))
		{
			child = child->next;
			continue;
		}
		errcode = xmlNodeGetContent(child);
		snprintf(out, size, "%s", (char *)errcode);
		xmlFree(errcode);
		break;
	}
}

bool parse_multidelete_xml(const char *xml, int len, u_int32_t *errnum, char *errstr, int size)
{
	xmlDoc *pdoc;
	xmlNode *pcur;
	int errornum=0;
	
	if((pdoc = xmlParseMemory(xml, len)) == NULL)
	{
		return false;
	}
	if((pcur = xmlDocGetRootElement(pdoc)) == NULL)
	{
		xmlFreeDoc(pdoc);
		return false;
	}

	while(pcur->type != XML_ELEMENT_NODE)
		pcur = pcur->next;
	if(xmlStrcmp(pcur->name, (const xmlChar *)"DeleteResult"))
	{
		xmlFreeDoc(pdoc);
		return false;
	}
	pcur = pcur->children;
	while(pcur != NULL)
	{
		if(pcur->type != XML_ELEMENT_NODE || xmlStrcmp(pcur->name, (const xmlChar *)"Error"))
		{
			pcur = pcur->next;
			continue;
		}
		if(errornum == 0)
		{
			fill_multidelete_xml_errcode(pcur, errstr, size);
		}
		errornum++;
		pcur = pcur->next;
	}
	*errnum = errornum;

	xmlFreeDoc(pdoc);
	return true;
}

void construct_multiple_delete_xml(const char *bucket, char *key[], u_int32_t num, int is_hash, char **xml, size_t *len)
{
	xmlDoc *pdoc;
	xmlNode *root, *child;
	int xmllen;

	pdoc = xmlNewDoc((const xmlChar *)"1.0");
	root = xmlNewNode(NULL, (const xmlChar *)"Delete");
	xmlDocSetRootElement(pdoc, root);

	xmlNewChild(root, NULL, (const xmlChar*)"Quiet", (const xmlChar*)"true");

	if(is_hash)
	{
		char hashkey[72], sha256[72];
		for(u_int32_t i=0; i<num; i++)
		{
			child = xmlNewChild(root, NULL, (const xmlChar*)"Object", NULL);			
			caculate_sha256(key[i], strlen(key[i]), sha256, 72);
			snprintf(hashkey, 256, "%c%c_%c%c_%s", sha256[0], sha256[1], sha256[2], sha256[3], sha256+4);
			xmlNewChild(child, NULL, (const xmlChar*)"Key", (const xmlChar*)hashkey);
		}
	}
	else
	{
		for(u_int32_t i=0; i<num; i++)
		{
			child = xmlNewChild(root, NULL, (const xmlChar*)"Object", NULL);
			xmlNewChild(child, NULL, (const xmlChar*)"Key", (const xmlChar*)key[i]);
		}
	}
	xmlDocDumpFormatMemoryEnc(pdoc, (xmlChar **)xml, &xmllen, "UTF-8", 1);
	xmlFreeDoc(pdoc);
	*len = xmllen;
}