| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2021 |
| 4 |
* Author: Shiting Long <[email protected]> (Forschungszentrum Juelich) |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "swiftpropparser.hpp" |
| 23 |
#include <utils/davix_swift_utils.hpp> |
| 24 |
|
| 25 |
#include <stack> |
| 26 |
#include <utils/davix_logger_internal.hpp> |
| 27 |
|
| 28 |
namespace Davix { |
| 29 |
|
| 30 |
const std::string col_prop = "Name"; |
| 31 |
const std::string delimiter_prop = "Contents"; |
| 32 |
const std::string name_prop = "Key"; |
| 33 |
const std::string size_prop = "Size"; |
| 34 |
|
| 35 |
const std::string prefix_prop = "Prefix"; |
| 36 |
const std::string com_prefix_prop = "CommonPrefixes"; |
| 37 |
const std::string listbucketresult_prop = "ListBucketResult"; |
| 38 |
const std::string last_modified_prop = "LastModified"; |
| 39 |
|
| 40 |
|
| 41 |
struct SwiftPropParser::Internal { |
| 42 |
std::string current; |
| 43 |
std::string name; |
| 44 |
std::string prefix_to_remove; |
| 45 |
std::deque<FileProperties> props; |
| 46 |
FileProperties property; |
| 47 |
|
| 48 |
int start_elem(const std::string &elem){ |
| 49 |
// new tag, clean content; |
| 50 |
current.clear(); |
| 51 |
|
| 52 |
// if new entry (a directory), clear entry |
| 53 |
if(StrUtil::compare_ncase("subdir", elem) ==0){ |
| 54 |
property.clear(); |
| 55 |
} |
| 56 |
|
| 57 |
// if new entry (an object), clear entry |
| 58 |
if(StrUtil::compare_ncase("object", elem) == 0) { |
| 59 |
property.clear(); |
| 60 |
} |
| 61 |
|
| 62 |
// if a new container, clear entry |
| 63 |
if(StrUtil::compare_ncase("container", elem) == 0) { |
| 64 |
property.clear(); |
| 65 |
} |
| 66 |
|
| 67 |
return 1; |
| 68 |
} |
| 69 |
|
| 70 |
int add_chunk(const std::string & chunk){ |
| 71 |
current.append(chunk); |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
int end_elem(const std::string &elem){ |
| 76 |
// name |
| 77 |
if(StrUtil::compare_ncase("name", elem) == 0) { |
| 78 |
property.filename = current.erase(0, prefix_to_remove.size()); |
| 79 |
} |
| 80 |
|
| 81 |
// processing a Subdir? Add new dir |
| 82 |
if( StrUtil::compare_ncase("subdir", elem) == 0 && !property.filename.empty()){ |
| 83 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push new common prefix {}", current.c_str()); |
| 84 |
property.info.mode = 0755 | S_IFDIR; |
| 85 |
property.info.mode &= ~(S_IFREG); |
| 86 |
props.push_back(property); |
| 87 |
property.clear(); |
| 88 |
} |
| 89 |
|
| 90 |
// object has ended? push new entry |
| 91 |
if( StrUtil::compare_ncase("object", elem) == 0){ |
| 92 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push new element {}", elem.c_str()); |
| 93 |
property.info.mode = 0755; |
| 94 |
props.push_back(property); |
| 95 |
property.clear(); |
| 96 |
} |
| 97 |
|
| 98 |
if( StrUtil::compare_ncase("bytes", elem) == 0){ |
| 99 |
try{ |
| 100 |
dav_size_t size = toType<dav_size_t, std::string>()(current); |
| 101 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "element size {}", size); |
| 102 |
property.info.size = size; |
| 103 |
}catch(...){ |
| 104 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "Unable to parse element size"); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if( StrUtil::compare_ncase("last_modified", elem) == 0){ |
| 109 |
try{ |
| 110 |
time_t mtime = S3::s3TimeConverter(current); |
| 111 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "element LastModified {}", current); |
| 112 |
property.info.mtime = mtime; |
| 113 |
property.info.ctime = mtime; |
| 114 |
}catch(...){ |
| 115 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "Unable to parse element LastModified"); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// listing containers? push new entry |
| 120 |
if( StrUtil::compare_ncase("container", elem) == 0 && !property.filename.empty()){ |
| 121 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push new element {}", elem.c_str()); |
| 122 |
property.info.mode = 0755 | S_IFDIR; |
| 123 |
property.info.mode &= ~(S_IFREG); |
| 124 |
props.push_back(property); |
| 125 |
} |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
SwiftPropParser::SwiftPropParser() : d_ptr(new Internal()) |
| 133 |
{ |
| 134 |
SwiftPropParser(""); |
| 135 |
} |
| 136 |
|
| 137 |
SwiftPropParser::SwiftPropParser(std::string prefix) : d_ptr(new Internal()) |
| 138 |
{ |
| 139 |
if(!prefix.empty()){ |
| 140 |
if(prefix[prefix.size() - 1] != '/') { |
| 141 |
d_ptr->prefix_to_remove = prefix.erase(0,1) + "/"; |
| 142 |
} |
| 143 |
else { |
| 144 |
d_ptr->prefix_to_remove = prefix; |
| 145 |
} |
| 146 |
|
| 147 |
if(d_ptr->prefix_to_remove == "/") { |
| 148 |
d_ptr->prefix_to_remove = ""; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
SwiftPropParser::~SwiftPropParser(){ } |
| 155 |
|
| 156 |
|
| 157 |
int SwiftPropParser::parserStartElemCb(int parent, const char *nspace, const char *name, const char **atts){ |
| 158 |
/*(void) parent; |
| 159 |
(void) nspace; |
| 160 |
(void) atts; |
| 161 |
return d_ptr->start_elem(std::string(name)); */ |
| 162 |
|
| 163 |
return d_ptr->start_elem(std::string(name)); |
| 164 |
} |
| 165 |
|
| 166 |
int SwiftPropParser::parserCdataCb(int state, const char *cdata, size_t len){ |
| 167 |
/*(void) state; |
| 168 |
(void) len; |
| 169 |
return d_ptr->add_chunk(std::string(cdata, len));*/ |
| 170 |
//std::cout << "cdata: " << std::string(cdata, len) << std::endl; |
| 171 |
return d_ptr->add_chunk(std::string(cdata, len)); |
| 172 |
} |
| 173 |
|
| 174 |
int SwiftPropParser::parserEndElemCb(int state, const char *nspace, const char *name){ |
| 175 |
/*(void) state; |
| 176 |
(void) nspace; |
| 177 |
return d_ptr->end_elem(std::string(name));*/ |
| 178 |
|
| 179 |
return d_ptr->end_elem(std::string(name)); |
| 180 |
} |
| 181 |
|
| 182 |
std::deque<FileProperties> & SwiftPropParser::getProperties(){ |
| 183 |
return d_ptr->props; |
| 184 |
} |
| 185 |
} |