Debian Codegraph @

src/file/davposix.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 <iostream>
23 #include <utils/davix_logger_internal.hpp>
24
25 #include <core/ContentProvider.hpp>
26 #include <status/davixstatusrequest.hpp>
27 #include <fileops/chain_factory.hpp>
28 #include <xml/davpropxmlparser.hpp>
29 #include <utils/stringutils.hpp>
30 #include <file/davposix.hpp>
31
32 using namespace StrUtil;
33
34
35
36 namespace Davix{
37
38 const std::string scope = "DavFile";
39
40 static HttpIOChain & getIOChain(HttpIOChain & chain){
41 CreationFlags flags;
42 flags[CHAIN_POSIX] = true;
43
44 return ChainFactory::instanceChain(flags, chain);
45 }
46
47 static IOChainContext getIOContext( Context & context, const Uri & uri, const RequestParams* params){
48 return IOChainContext(context, uri, params);
49 }
50
51 size_t getPartSize() {static const int dfltPSIZE = 32*1024*1024;
52 const char *mpVal = getenv("DAVIX_PARTSIZE");
53 size_t pSize = dfltPSIZE;
54
55 if (mpVal)
56 {char *endp;
57 long mpSZ = strtol(mpVal, &endp, 10);
58 if (*endp == 0 && mpSZ >= 10 && mpSZ <= 500)
59 pSize = (size_t)mpSZ*1024*1024;
60 }
61 return pSize;
62 }
63
64 size_t PartSize = getPartSize();
65
66 } // namespace Davix
67
68 // static const std::string simple_listing("<propfind xmlns=\"DAV:\"><prop><resourcetype><collection/></resourcetype></prop></propfind>");
69 //
70 // static const std::string stat_listing("<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:propfind xmlns:D=\"DAV:\" xmlns:L=\"LCGDM:\"><D:prop>"
71 // "<D:displayname/><D:getlastmodified/><D:creationdate/><D:getcontentlength/><D:quota-used-bytes/>"
72 // "<D:resourcetype><D:collection/></D:resourcetype><L:mode/>"
73 // "</D:prop>"
74 // "</D:propfind>");
75
76 struct Davix_dir_handle{
77
78 Davix_dir_handle(Davix::Context & context, const Davix::Uri & u, const Davix::RequestParams * p):
79 params(p),
80 uri(u),
81 io_chain(),
82 io_context(context, uri, &params),
83 start_entry_name(),
84 start_entry_st(),
85 dir_info((struct dirent*) calloc(1,sizeof(struct dirent) + NAME_MAX +1)),
86 dir_offset(0),
87 end(false){
88 Davix::getIOChain(io_chain);
89 }
90
91 ~Davix_dir_handle(){
92 free(dir_info);
93 }
94
95
96 // params
97 Davix::RequestParams params;
98 Davix::Uri uri;
99
100 Davix::HttpIOChain io_chain;
101 Davix::IOChainContext io_context;
102
103 //first entry
104 std::string start_entry_name;
105 Davix::StatInfo start_entry_st;
106
107 // dir struct
108 struct dirent* dir_info;
109 off_t dir_offset;
110
111 // end listing
112 bool end;
113
114 private:
115 Davix_dir_handle(const Davix_dir_handle & );
116 Davix_dir_handle & operator=(const Davix_dir_handle & );
117 };
118
119 /******************************************************************************/
120 /* C l a s s P a r k l e t */
121 /******************************************************************************/
122
123 // The Davix_Parklet class implements an in-memory buffer that is used for
124 // multi-part uploads (a.k.a. chunked streaming). This avoids the need to create
125 // a local file that is then transferred to the destination upon close;
126 // drastically reducing disk resource utilization while dramatically improving
127 // the transfer rate by avoiding the latency introduced by intermediate file
128 // creation. The default buffer size which is equal to the chunk size is 32 MiB.
129 // The size can be changed by setting the envar DAVIX_PARTSIZE to the desired
130 // value in megabytes (e.g. a value of 50 is converted to 50 MiB). Invalid
131 // values are ignored. The valid range if from 10 to 500, inclusive. Once set
132 // it cannot be changed until the library is reloaded.
133
134 // Multi-part uploads offer distinct advantages when the size of the incomming
135 // data is not known. This is usually the case for streaming servers and not
136 // basic file uploads encountered in user applications (i.e. we obviously know
137 // the upload size when the source is a file). This update is meant to solve
138 // the streaming server problem and avoid creating intermediate files, thus
139 // simplifying streaming server configuration and performance. Hence, multi-
140 // part uploads are disabled unless envar DAVPOSIX_MPUPLOAD is set.
141
142 struct Davix_fd;
143
144 class Davix_Parklet
145 {
146 public:
147
148 bool Active() {return myBuff != 0;}
149
150 ssize_t Consume(const char* iBuf, size_t iCount);
151
152 void Flush();
153
154 Davix_Parklet(Davix_fd* fd)
155 : myFD(*fd), myBuff(0), mySize(0), myOffs(0), myLeft(0),
156 partNum(1) {}
157
158 ~Davix_Parklet() {if (myBuff) delete[] myBuff;}
159
160 private:
161
162 Davix_fd& myFD;
163 std::string uploadid;
164 char* myBuff;
165 size_t mySize;
166 size_t myOffs;
167 size_t myLeft;
168 int partNum;
169 std::vector<std::string> etags;
170 };
171
172 /******************************************************************************/
173 /* S t r u c t D a v i x _ f d */
174 /******************************************************************************/
175
176 struct Davix_fd{
177 Davix_fd(Davix::Context & context, const Davix::Uri & uri, const Davix::RequestParams * params) : _uri(uri), _params(params),
178 io_handler(), io_context(Davix::getIOContext(context, _uri, &_params)),
179 Parklet(this) {
180 Davix::getIOChain(io_handler);
181 }
182 virtual ~Davix_fd(){
183 if (!Parklet.Active())
184 try{
185 io_handler.resetIO(io_context);
186 }catch(Davix::DavixException & e){
187 DAVIX_SLOG(DAVIX_LOG_VERBOSE, DAVIX_LOG_POSIX,
188 "Error when closed file descriptor, possibly file corrupted {}",
189 e.what());
190 }
191 }
192
193 Davix::Uri _uri;
194 Davix::RequestParams _params;
195 Davix::HttpIOChain io_handler;
196 Davix::IOChainContext io_context;
197 Davix_Parklet Parklet;
198 };
199
200 /******************************************************************************/
201 /* D a v i x _ P a r k l e t : : C o n s u m e */
202 /******************************************************************************/
203
204 ssize_t Davix_Parklet::Consume(const char* iBuf, size_t iCount)
205 {
206 ssize_t ret = iCount;
207
208 // Make sure we have initialized. We don't allocate a buffer until needed.
209 //
210 if (myBuff == 0)
211 {myBuff = new char[Davix::PartSize];
212 mySize = myLeft = Davix::PartSize;
213
214 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_POSIX,
215 "Parklet: Initiating multipart!");
216
217 uploadid = myFD.io_handler.initiateMultipart(myFD.io_context);
218
219 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_POSIX, "Parklet: Multipart size: "
220 "{} id: {}", std::to_string(mySize), uploadid);
221 }
222
223 // If we have already placed something in the buffer we must continue to fill it.
224 // We do try to optimize by using the caller's buffer if possible.
225 //
226 while(iCount)
227 {if (myOffs || iCount < mySize)
228 {size_t mvSize = (myLeft <= iCount ? myLeft : iCount);
229 memcpy(myBuff+myOffs, iBuf, mvSize);
230 myOffs += mvSize;
231 myLeft -= mvSize;
232 iBuf += mvSize;
233 iCount -= mvSize;
234 if (myLeft == 0)
235 {myFD.io_handler.writeFromBuffer(myFD.io_context,
236 myBuff, mySize,
237 uploadid, etags, partNum);
238 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_POSIX, "Parklet: "
239 "Buff upload {} part {} tag {}",
240 std::to_string(mySize), std::to_string(partNum),
241 etags[partNum-1]);
242 partNum++; // The next part number!
243 myOffs = 0;
244 myLeft = mySize;
245 }
246 } else {
247 myFD.io_handler.writeFromBuffer(myFD.io_context,
248 iBuf, mySize,
249 uploadid, etags, partNum);
250 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_POSIX, "Parklet: "
251 "Drct upload {} part {} tag {}",
252 std::to_string(mySize), std::to_string(partNum),
253 etags[partNum-1]);
254 partNum++; // The next part number!
255 iBuf += mySize;
256 iCount -= mySize;
257 }
258 }
259
260 // All done, return the expected number of bytes.
261 //
262 return ret;
263 }
264
265 /******************************************************************************/
266 /* D a v i x _ P a r k l e t : : F l u s h */
267 /******************************************************************************/
268
269 void Davix_Parklet::Flush()
270 {
271 size_t bytes = mySize - myLeft;
272
273 // If we have a partially filled buffer then write it out now.
274 //
275 if (bytes) {myFD.io_handler.writeFromBuffer(myFD.io_context,
276 myBuff, bytes,
277 uploadid, etags, partNum);
278 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_POSIX, "Parklet: "
279 "Last upload {} part {} tag {}",
280 std::to_string(bytes), std::to_string(partNum),
281 etags[partNum-1]);
282 }
283
284 // Commit the upload
285 //
286 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_POSIX, "Parklet: Commit {} part(s) "
287 "upload {}", std::to_string(etags.size()), uploadid);
288
289 myFD.io_handler.commitChunks(myFD.io_context, uploadid, etags);
290 }
291
292
293 namespace Davix {
294
295
296
297 static void toDirent(struct dirent * d, const std::string & filename, const StatInfo & info){
298 StrUtil::copy_std_string_to_buff(d->d_name, NAME_MAX, filename);
299 if (S_ISDIR(info.mode))
300 d->d_type = DT_DIR;
301 else if (S_ISLNK(info.mode))
302 d->d_type = DT_LNK;
303 else
304 d->d_type = DT_REG;
305 }
306
307
308
309 DavPosix::DavPosix(Context* _context) :
310 context(_context),
311 _timeout(180),
312 _s_buff(2048),
313 d_ptr(NULL)
314
315 {
316 (void ) d_ptr; // silence warning
317 }
318
319 DavPosix::~DavPosix(){
320
321 }
322
323 DAVIX_DIR* internal_opendir(Context & context, const RequestParams* params, const std::string & url){
324 std::unique_ptr<DAVIX_DIR> dir(new DAVIX_DIR(context, url, params));
325 dir->end = ! dir->io_chain.nextSubItem(dir->io_context,dir->start_entry_name, dir->start_entry_st);
326 return dir.release();
327 }
328
329
330 DAVIX_DIR* DavPosix::opendir(const RequestParams* params, const std::string &url, DavixError** err){
331
332 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, odir);
333
334 DAVIX_DIR* r = NULL;
335 TRY_DAVIX{
336 r = internal_opendir(*context, params, url);
337 }CATCH_DAVIX(err)
338
339 return (DAVIX_DIR*) r;
340 }
341
342 DAVIX_DIR* DavPosix::opendirpp(const RequestParams* params, const std::string &url, DavixError** err){
343
344 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, odirpp);
345
346 DAVIX_DIR* r = NULL;
347
348 TRY_DAVIX{
349 r = internal_opendir(*context, params, url);
350 }CATCH_DAVIX(err)
351
352 return (DAVIX_DIR*) r;
353 }
354
355
356 struct dirent* internal_readdir(DAVIX_DIR * dir, struct stat* st){
357 if( dir==NULL){
358 throw DavixException(davix_scope_directory_listing_str(), StatusCode::InvalidFileHandle, "Invalid file descriptor for DAVIX_DIR*");
359 }
360
361 if(dir->end)
362 return NULL;
363
364 if(dir->start_entry_name.size() >0) { // first entry
365 if(st){
366 dir->start_entry_st.toPosixStat(*st);
367 }
368 toDirent(dir->dir_info, dir->start_entry_name, dir->start_entry_st);
369 dir->start_entry_name.clear();
370 return dir->dir_info;
371 }
372
373 StatInfo info;
374 std::string name;
375 if( dir->io_chain.nextSubItem(dir->io_context, name, info) ==false){
376 return NULL;
377 }
378
379 if(st){
380 info.toPosixStat(*st);
381 }
382 toDirent(dir->dir_info, name, info);
383 return dir->dir_info;
384
385 }
386
387
388 struct dirent* DavPosix::readdir(DAVIX_DIR * d, DavixError** err){
389 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, rdir);
390
391 DavixError* tmp_err=NULL;
392 DAVIX_DIR* dir = static_cast<DAVIX_DIR*>(d);
393
394 TRY_DAVIX{
395 return internal_readdir(dir, NULL);
396 }CATCH_DAVIX(&tmp_err)
397
398 DavixError::propagateError(err, tmp_err);
399 return NULL;
400 }
401
402 struct dirent* DavPosix::readdirpp(DAVIX_DIR * d, struct stat *st, DavixError** err){
403 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, rdirpp);
404
405 DavixError* tmp_err=NULL;
406 DAVIX_DIR* dir = static_cast<DAVIX_DIR*>(d);
407
408 TRY_DAVIX{
409 return internal_readdir(dir, st);
410 }CATCH_DAVIX(&tmp_err)
411
412 DavixError::propagateError(err, tmp_err);
413 return NULL;
414 }
415
416
417 int DavPosix::closedirpp(DAVIX_DIR * d, DavixError** err){
418 int ret =-1;
419
420 TRY_DAVIX{
421 if( d==NULL){
422 throw DavixException(davix_scope_directory_listing_str(), Davix::StatusCode::InvalidFileHandle, "Invalid file descriptor for DAVIX_DIR*");
423 }else{
424 delete (static_cast<DAVIX_DIR*>(d));
425 ret =0;
426 }
427
428 }CATCH_DAVIX(err)
429
430 return ret;
431 }
432
433 int DavPosix::closedir(DAVIX_DIR * d, DavixError** err){
434 return closedirpp(d,err);
435 }
436
437 void DavPosix::fadvise(DAVIX_FD *fd, dav_off_t offset, dav_size_t len, advise_t advise){
438 try{
439 if( fd==NULL)
440 return;
441 fd->io_handler.prefetchInfo(fd->io_context, offset, len, advise);
442
443 }catch(DavixException & e){
444 DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_POSIX, "fdadvise error {}", e.what());
445 }catch(...){
446 DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_POSIX, "Unknown error, aborted");
447 }
448 }
449
450
451 ////////////////////////////////////////////////////
452 //////////////////// Davix POSIX meta ops
453 /////////////////////////////////////////////////////
454
455 int DavPosix::rename(const RequestParams * _params, const std::string &source_url, const std::string &target_url, DavixError** err){
456
457 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, renm);
458 int ret=-1;
459
460 TRY_DAVIX{
461 Uri uri(source_url);
462 HttpIOChain chain;
463 IOChainContext io_context = getIOContext(*context, uri, _params);
464
465 getIOChain(chain).move(io_context, target_url);
466 ret = 0;
467 }CATCH_DAVIX(err)
468
469 return ret;
470 }
471
472
473
474 int DavPosix::mkdir(const RequestParams * _params, const std::string &url, mode_t right, DavixError** err){
475 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, fun_mkdir);
476 (void) right;
477 int ret=-1;
478
479 TRY_DAVIX{
480 Uri uri(url);
481 HttpIOChain chain;
482 IOChainContext io_context = getIOContext(*context, uri, _params);
483
484 getIOChain(chain).makeCollection(io_context);
485 ret = 0;
486 }CATCH_DAVIX(err)
487 return ret;
488 }
489
490
491 int DavPosix::stat(const RequestParams * params, const std::string & url, struct stat* st, DavixError** err){
492 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, fun_stat);
493
494 File f(*context, url);
495 int ret = f.stat(params, st, err);
496
497 return ret;
498
499 }
500
501 int DavPosix::stat64(const RequestParams *params, const std::string & url, StatInfo *st, DavixError **err){
502 TRY_DAVIX{
503 if(st == NULL)
504 throw DavixException(davix_scope_meta(), StatusCode::InvalidArgument, "Argument stat NULL");
505
506 File f(*context, url);
507 f.statInfo(params, *st);
508 return 0;
509 }CATCH_DAVIX(err)
510 return -1;
511 }
512
513 int DavPosix::get_quota(const RequestParams *params, const std::string & url, QuotaInfo *info, DavixError **err) {
514 TRY_DAVIX{
515 if(info == NULL)
516 throw DavixException(davix_scope_meta(), StatusCode::InvalidArgument, "Argument info is NULL");
517
518 File f(*context, url);
519 f.quotaInfo(params, *info);
520 return 0;
521 }CATCH_DAVIX(err)
522 return -1;
523 }
524
525 int davix_remove_posix(Context* context, const RequestParams * params, const std::string & url, bool directory, DavixError** err){
526 DavixError* tmp_err = NULL;
527 int ret = -1;
528 Uri uri(url);
529
530 TRY_DAVIX{
531 if(params && params->getProtocol() == RequestProtocol::Http){ // pure protocol http : ignore posix semantic, execute a simple delete
532 HttpIOChain chain;
533 IOChainContext io_context = getIOContext(*context, uri, params);
534 getIOChain(chain).deleteResource(io_context);
535 ret = 0;
536 }else{
537 // full posix semantic support
538 HttpIOChain chain;
539 IOChainContext io_context = getIOContext(*context, uri, params);
540 struct StatInfo infos;
541
542 getIOChain(chain).statInfo(io_context, infos);
543
544 if( S_ISDIR(infos.mode)){ // directory : impossible to delete if not empty
545 if(directory == true){
546 chain.deleteResource(io_context);
547 ret =0;
548 }else{
549 ret = -1;
550 throw DavixException(davix_scope_davOps_str(), StatusCode::IsADirectory, fmt::format(" {} is a directory, impossible to unlink\n", uri));
551 }
552 }else{ // file, rock & roll
553 if(directory == false){
554 chain.deleteResource(io_context);
555 ret =0;
556 }else{
557 ret = -1;
558 throw DavixError(davix_scope_davOps_str(), StatusCode::IsNotADirectory, fmt::format(" {} is not a directory, impossible to rmdir", uri));
559 }
560 }
561 }
562 }CATCH_DAVIX(&tmp_err)
563
564 DavixError::propagateError(err, tmp_err);
565 return ret;
566
567 }
568
569
570 int DavPosix::unlink(const RequestParams * params, const std::string &uri, DavixError** err){
571 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_POSIX, " -> davix_unlink");
572 int ret=-1;
573 DavixError* tmp_err=NULL;
574
575 TRY_DAVIX{
576 ret = davix_remove_posix(context, params, uri, false, &tmp_err);
577 }CATCH_DAVIX(&tmp_err)
578
579 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_POSIX, " davix_unlink <-");
580 DavixError::propagatePrefixedError(err, tmp_err, "DavPosix::unlink ");
581 return ret;
582 }
583
584
585 int DavPosix::rmdir(const RequestParams * params, const std::string &uri, DavixError** err){
586 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_POSIX, " -> davix_rmdir");
587 int ret=-1;
588 DavixError* tmp_err=NULL;
589
590 TRY_DAVIX{
591 ret = davix_remove_posix(context, params, uri, true, &tmp_err);
592 }CATCH_DAVIX(&tmp_err)
593
594 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_POSIX, " davix_rmdir <-");
595 DavixError::propagatePrefixedError(err, tmp_err, "DavPosix::rmdir ");
596 return ret;
597 }
598
599
600
601 ////////////////////////////////////////////////////
602 //////////////////// Davix POSIX I/O
603 /////////////////////////////////////////////////////
604
605
606
607 inline int davix_check_rw_fd(DAVIX_FD* fd, DavixError** err){
608 if(fd == NULL){
609 DavixError::setupError(err, davix_scope_http_request(),StatusCode::InvalidFileHandle, "Invalid Davix file descriptor");
610 return -1;
611 }
612 return 0;
613 }
614
615
616 DAVIX_FD* DavPosix::open(const RequestParams * _params, const std::string & url, int flags, DavixError** err){
617 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, fun_open);
618 DavixError* tmp_err=NULL;
619 Davix_fd* fd = NULL;
620
621
622 TRY_DAVIX{
623 Uri uri(url);
624
625 if(uri.getStatus() != StatusCode::OK){
626 throw DavixException(davix_scope_http_request(), uri.getStatus(), " Uri invalid in Davix::Open");
627 }
628 fd = new Davix_fd(*context, uri, _params);
629 fd->io_handler.open(fd->io_context, flags);
630 }CATCH_DAVIX(&tmp_err)
631
632 if(tmp_err){
633 DavixError::propagateError(err, tmp_err);
634 delete fd;
635 fd= NULL;
636 }
637 return fd;
638 }
639
640
641 ssize_t DavPosix::read(DAVIX_FD* fd, void* buf, size_t count, Davix::DavixError** err){
642 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, fun_read);
643
644 ssize_t ret =-1;
645 DavixError* tmp_err=NULL;
646
647 TRY_DAVIX{
648 if( davix_check_rw_fd(fd, &tmp_err) ==0){
649 ret = (ssize_t) fd->io_handler.read(fd->io_context, buf, (dav_size_t) count);
650 }
651 }CATCH_DAVIX(&tmp_err)
652
653 DavixError::propagateError(err, tmp_err);
654 return ret;
655 }
656
657
658 ssize_t DavPosix::pread(DAVIX_FD* fd, void* buf, size_t count, off_t offset, DavixError** err){
659 return static_cast<ssize_t>(pread64(fd, buf, static_cast<dav_size_t>(count), static_cast<dav_off_t>(offset), err));
660 }
661
662 dav_ssize_t DavPosix::pread64(DAVIX_FD *fd, void *buf, dav_size_t count, dav_off_t offset, DavixError **err){
663 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, fun_pread);
664 dav_ssize_t ret =-1;
665 DavixError* tmp_err=NULL;
666
667 TRY_DAVIX{
668 if( davix_check_rw_fd(fd, &tmp_err) ==0){
669 ret = fd->io_handler.pread(fd->io_context, buf, count, offset);
670 }
671 }CATCH_DAVIX(&tmp_err)
672
673 DavixError::propagateError(err, tmp_err);;
674 return ret;
675 }
676
677 ssize_t DavPosix::pwrite(DAVIX_FD* fd, const void* buf, size_t count, off_t offset, DavixError** err){
678 (void) fd;
679 (void) buf;
680 (void) count;
681 (void) offset;
682 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, fun_pwrite);
683 DavixError::setupError(err, davix_scope_io_buff(), StatusCode::OperationNonSupported, "Operation pwrite Not supported");
684 return -1;
685 }
686
687 dav_ssize_t DavPosix::pwrite64(DAVIX_FD *fd, const void *buf, dav_size_t count, dav_off_t offset, DavixError **err){
688 (void) fd;
689 (void) buf;
690 (void) count;
691 (void) offset;
692 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, fun_pwrite);
693 DavixError::setupError(err, davix_scope_io_buff(), StatusCode::OperationNonSupported, "Operation pwrite Not supported");
694 return -1;
695 }
696
697 dav_ssize_t DavPosix::preadVec(DAVIX_FD* fd, const DavIOVecInput * input_vec,
698 DavIOVecOuput * output_vec,
699 dav_size_t count_vec, DavixError** err){
700 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, fun_preadvec);
701 ssize_t ret =-1;
702 DavixError* tmp_err=NULL;
703
704 TRY_DAVIX{
705 if( davix_check_rw_fd(fd, &tmp_err) ==0){
706 ret = fd->io_handler.preadVec(fd->io_context, input_vec, output_vec, count_vec);
707 }
708 }CATCH_DAVIX(&tmp_err)
709
710 DavixError::propagateError(err, tmp_err);
711 return ret;
712 }
713
714 ssize_t DavPosix::write(DAVIX_FD* fd, const void* buf, size_t count, Davix::DavixError** err){
715
716 // By default, POSIX writes create an intermediate file that is uploaded when
717 // closed. When envar DAVPOSIX_MPUPLOAD is set, a multi-part upload is used with
718 // a default chunk size of 32 MiB. The chunk size can be changed by setting the
719 // via envar DAVIX_PARTSIZE (see getPartSize() in this file). This envar is
720 // typically set by S3 proxy servers using the POSIX API.
721
722 static bool useMPUpload = getenv("DAVPOSIX_MPUPLOAD") != 0;
723
724 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, fun_write);
725
726 ssize_t ret =-1;
727 DavixError* tmp_err=NULL;
728
729 TRY_DAVIX{
730 if( davix_check_rw_fd(fd, &tmp_err) ==0){
731 // BufferContentProvider provider( (const char*) buf, count);
732 // ret = (ssize_t) fd->io_handler.writeFromProvider(fd->io_context, provider);
733 if (useMPUpload) ret = fd->Parklet.Consume((const char*) buf, count);
734 else ret = (ssize_t) fd->io_handler.write(fd->io_context, buf,
735 (dav_size_t) count);
736 }
737 }CATCH_DAVIX(&tmp_err)
738
739 DavixError::propagateError(err, tmp_err);
740 return ret;
741 }
742
743
744
745 off_t DavPosix::lseek(DAVIX_FD* fd, off_t offset, int flags, Davix::DavixError** err){
746 dav_off_t res = lseek64(fd, static_cast<dav_off_t>(offset), flags, err);
747 if(res > std::numeric_limits<off_t>::max())
748 return std::numeric_limits<off_t>::max();
749 return res;
750 }
751
752 dav_off_t DavPosix::lseek64(DAVIX_FD *fd, dav_off_t offset, int flags, DavixError **err){
753 DAVIX_SCOPE_TRACE(DAVIX_LOG_POSIX, fun_lseek);
754 ssize_t ret =-1;
755 DavixError* tmp_err=NULL;
756
757 TRY_DAVIX{
758 if( davix_check_rw_fd(fd, &tmp_err) ==0){
759 ret = fd->io_handler.lseek(fd->io_context, offset, flags);
760 }
761 }CATCH_DAVIX(&tmp_err)
762
763 DavixError::propagateError(err, tmp_err);
764 return ret;
765 }
766
767
768 int DavPosix::close(DAVIX_FD* fd, Davix::DavixError** err){
769 TRY_DAVIX{
770 if(fd){
771 if (fd->Parklet.Active()) fd->Parklet.Flush();
772 delete fd;
773 fd = NULL;
774 }
775 }CATCH_DAVIX(err)
776 return 0;
777 }
778
779
780 } // namespace Davix