Debian Codegraph @

src/tools/davix_tool_ls_main.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2013
4 * Author: Adrien Devresse <[email protected]>
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 <sstream>
23 #include <davix.hpp>
24 #include <tools/davix_tool_params.hpp>
25 #include <tools/davix_tool_util.hpp>
26 #include <tools/davix_taskqueue.hpp>
27 #include <tools/davix_op.hpp>
28 #include <tools/davix_thread_pool.hpp>
29 #include <utils/davix_logger_internal.hpp>
30 #include <pthread.h>
31
32 // @author : Devresse Adrien
33 // main file for davix ls tool
34
35
36 using namespace Davix;
37 using namespace std;
38
39
40 std::string get_base_listing_options(){
41 return " Listing Options:\n"
42 "\t--long-list, -l: long Listing mode\n"
43 "\t-r NUMBER_OF_THREADS List directories's content recursively using multiple threads\n"
44 "\t--no-cap: Disable size cap on task queue for pending listing operations\n"
45 "\t--s3-listing: S3 bucket listing mode - flat, semi or hierarchical(default)\n"
46 "\t--s3-maxkeys: Maximum number of entries returns by S3 list bucket request. default: 10000\n"
47 "\t--swift-listing: Swift listing mode - semi or hierarchical(default)\n";
48 }
49
50 static std::string help_msg(const std::string & cmd_path){
51 std::string help_msg = fmt::format("Usage : {} ", cmd_path);
52 help_msg += Tool::get_base_description_options();
53 help_msg += Tool::get_common_options();
54 help_msg += get_base_listing_options();
55
56 return help_msg;
57 }
58
59
60 static void display_file_entry(const std::string & filename, const Tool::OptParams & opts, FILE* filestream){
61 (void) opts;
62 fputs(filename.c_str(), filestream);
63 fputs("\n",filestream);
64 }
65
66
67 static void display_long_file_entry(const std::string & filename, struct stat* st, const Tool::OptParams & opts, FILE* filestream){
68 (void) opts;
69 std::ostringstream ss;
70 ss << Tool::string_from_mode(st->st_mode) << " ";
71 ss << Tool::string_from_size_t(static_cast<size_t>(st->st_nlink),4) << " ";
72 ss << Tool::string_from_size_t(st->st_size, 9) << " ";
73 ss << Tool::string_from_ptime(st->st_mtime) << " ";
74 ss << filename << "\n";
75
76 fputs(ss.str().c_str(), filestream);
77 }
78
79 static int listing(const Tool::OptParams & opts, FILE* filestream, DavixError** err ){
80 DAVIX_DIR* fd = NULL;
81 Context c;
82 configureContext(c, opts);
83 DavPosix pos(&c);
84 struct stat st;
85 struct dirent* d;
86 if( (fd = pos.opendirpp(&opts.params, opts.vec_arg[0], err)) == NULL)
87 return -1;
88 while( (d = pos.readdirpp(fd, &st, err)) != NULL ){
89 if(opts.pres_flag & LONG_LISTING_FLAG){
90 display_long_file_entry(d->d_name, &st, opts, filestream);
91 }else{
92 display_file_entry(d->d_name, opts, filestream);
93 }
94 }
95 pos.closedirpp(fd, NULL);
96 return (err && *err)?(-1):0;
97 }
98
99 static int get_info(const Tool::OptParams & opts, FILE* filestream, DavixError** err ){
100 Context c;
101 configureContext(c, opts);
102 File f(c, opts.vec_arg[0]);
103 struct stat st;
104 if( f.stat(&opts.params, &st, err) == 0){
105 if(opts.pres_flag & LONG_LISTING_FLAG){
106 if( opts.params.getProtocol() == RequestProtocol::AwsS3){
107 std::string tmp(opts.vec_arg[0]);
108 display_long_file_entry(tmp.substr(tmp.rfind('/')+1), &st, opts, filestream);
109 }
110 else{
111 display_long_file_entry(opts.vec_arg[0], &st, opts, filestream);
112 }
113 }else{
114 display_file_entry(opts.vec_arg[0], opts, filestream);
115 }
116 return 0;
117 }
118 return -1;
119 }
120
121 static int populateTaskQueue(Context& c, const Tool::OptParams & opts, std::string uri, DavixTaskQueue* listing_tq, DavixError** err, FILE* filestream, pthread_mutex_t& output_mutex){
122 DAVIX_DIR* fd = NULL;
123 DavPosix pos(&c);
124 DavixError* tmp_err=NULL;
125 std::string outputPath;
126 struct stat st;
127 struct dirent* d;
128 unsigned long entry_counter = 0;
129 std::string last_success_entry;
130
131 std::deque<std::string> dirQueue;
132
133 // set up first entry
134 if(!uri.empty()){
135 dirQueue.push_back(uri);
136 }
137 else
138 DavixError::setupError(&tmp_err, "Davix::ListOp", StatusCode::InvalidArgument, " target URL is empty.");
139
140 if( (fd = pos.opendirpp(&opts.params, dirQueue.front(), &tmp_err)) == NULL){
141 Tool::errorPrint(&tmp_err);
142 return -1;
143 }
144
145 Uri tmp(uri);
146 std::string fullpath = tmp.getPath();
147 if(fullpath[0] == '/') fullpath.erase(0,1);
148
149 while( ((d = pos.readdirpp(fd, &st, &tmp_err)) != NULL)){ // if one entry inside a directory fails, the loop exits, the other entires are not processed
150
151 last_success_entry = dirQueue.front()+d->d_name;
152 // for each entry, do a stat to see if it's a directory, if yes, push to dirQueue for further processing
153 if(st.st_mode & S_IFDIR){
154 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_CORE, "Directory entry found, pushing {}/ to dirQueue", dirQueue.front()+d->d_name);
155 dirQueue.push_back(dirQueue.front()+d->d_name+"/");
156 }
157
158 if(opts.pres_flag & LONG_LISTING_FLAG){
159 display_long_file_entry(fullpath+d->d_name, &st, opts, filestream);
160 }else{
161 display_file_entry(fullpath+d->d_name, opts, filestream);
162 }
163 } // while readdirpp
164
165 if(tmp_err){
166 Tool::errorPrint(&tmp_err);
167 std::cerr << std::endl << "Error occurred during listing " << dirQueue.front() << " Number of entries processed in current directory: " << entry_counter << ". Continuing..."<< std::endl;
168 std::cerr << std::endl << "Last successful entry is " << last_success_entry << std::endl;
169 }
170
171 pos.closedirpp(fd, NULL);
172 dirQueue.pop_front();
173
174 for(unsigned int i=0; i < dirQueue.size(); ++i){
175 //push listing op to task queue
176 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_CORE, "Adding item to (listing) work queue, target is {}.", dirQueue[i]);
177 ListOp* l_op = new ListOp(opts, dirQueue[i], c, listing_tq, filestream, output_mutex);
178 listing_tq->pushOp(l_op);
179 }
180
181 if(tmp_err){
182 Tool::errorPrint(&tmp_err);
183 return -1;
184 }
185 return 0;
186 }
187
188 static int recursiveListing(const Tool::OptParams & opts, FILE* filestream, DavixError** err ){
189 Context c;
190 configureContext(c, opts);
191 File f(c, opts.vec_arg[0]);
192 DavixError* tmp_err=NULL;
193
194 std::string url(opts.vec_arg[0]);
195
196 if (url[url.size()-1] != '/')
197 url += '/';
198
199 DavixTaskQueue listing_tq(opts, QueueType::LIFO); // for listing ops
200
201 // init output mutex for child listOps, only one listOp can output to a stream at a given time
202 pthread_mutex_t output_mutex;
203 pthread_mutex_init(&output_mutex, NULL);
204
205 // create threadpool instance
206 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_CORE, "Creating threadpool");
207 DavixThreadPool listing_tp(&listing_tq, opts.threadpool_size);
208
209 populateTaskQueue(c, opts, url, &listing_tq, &tmp_err, filestream, output_mutex);
210
211 do{
212 sleep(2);
213 }while((!listing_tq.isEmpty()) || !(listing_tp.allThreadsIdle()));
214
215
216 listing_tp.shutdown();
217 Tool::flushFinalLineShell(STDOUT_FILENO);
218
219
220 if(tmp_err){
221 DavixError::propagateError(err, tmp_err);
222 return -1;
223 }
224 return 0;
225
226 }
227
228 int main(int argc, char** argv){
229 int retcode;
230 Tool::OptParams opts;
231 DavixError* tmp_err=NULL;
232 opts.help_msg = help_msg(argv[0]);
233 FILE* fstream = stdout;
234
235 if( (retcode= Tool::parse_davix_ls_options(argc, argv, opts, &tmp_err)) ==0){
236 if( (retcode = Tool::configureAuth(opts)) == 0){
237 if(checkProtocolSanity(opts, opts.vec_arg[0], &tmp_err)) {
238 // if recursive and S3
239 if(opts.params.getRecursiveMode() && (opts.vec_arg[0].compare(0,2,"s3") ==0)){
240 // don't need to use -r switch for S3, just set listing mode to SemiHierarchical and do a normal listing
241 opts.params.setS3ListingMode(S3ListingMode::SemiHierarchical);
242 // unfortunately s3 defaults max-keys to 1000 and doesn't provide a way to disable the cap, set to large number
243 opts.params.setS3MaxKey(999999999);
244
245 retcode = listing(opts, fstream, &tmp_err);
246 }
247 else if(opts.params.getRecursiveMode() && (opts.vec_arg[0].compare(0, 5, "swift")==0)) {
248 // don't need to use -r switch for Swift, just set listing mode to SemiHierarchical and do a normal listing
249 opts.params.setSwiftListingMode(SwiftListingMode::SemiHierarchical);
250 retcode = listing(opts, fstream, &tmp_err);
251 }
252 else if(opts.params.getRecursiveMode()){ // dav recursive listing
253 retcode = recursiveListing(opts, fstream, &tmp_err);
254 }
255 else{ // normal listing
256 retcode = listing(opts, fstream, &tmp_err);
257 }
258 // just a single file
259 if(retcode < 0 && tmp_err->getStatus() == StatusCode::IsNotADirectory){
260 DavixError::clearError(&tmp_err);
261 retcode = get_info(opts, fstream, &tmp_err);
262 }
263 }
264 }
265 }
266 Tool::errorPrint(&tmp_err);
267 return retcode;
268 }