| 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 |
#ifndef HTTPIOCHAIN_HPP |
| 23 |
#define HTTPIOCHAIN_HPP |
| 24 |
|
| 25 |
#include <davix_internal.hpp> |
| 26 |
|
| 27 |
namespace Davix{ |
| 28 |
|
| 29 |
class HttpIOChain; |
| 30 |
class ContentProvider; |
| 31 |
|
| 32 |
#define CHAIN_FORWARD(X) \ |
| 33 |
do{ \ |
| 34 |
if(_next.get() != NULL){ \ |
| 35 |
return _next->X; \ |
| 36 |
} \ |
| 37 |
throw DavixException(davix_scope_io_buff(), StatusCode::OperationNonSupported, "I/O operation not supported"); \ |
| 38 |
}while(0) |
| 39 |
|
| 40 |
|
| 41 |
// stores state for readToFd operations - necessary, so as not to write the same |
| 42 |
// data again to an fd after a retry / metalink recovery. |
| 43 |
struct FdHandler { |
| 44 |
FdHandler() : fd(-1), bytes_written_to_fd(0) { } |
| 45 |
|
| 46 |
int fd; |
| 47 |
dav_ssize_t bytes_written_to_fd; |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
// parameter handler for any IO Chain operation |
| 52 |
struct IOChainContext{ |
| 53 |
IOChainContext(Context & c, const Uri & u, const RequestParams * p): _context(c), _uri(u), _reqparams(p), _end_time() { |
| 54 |
if(_reqparams->getOperationTimeout()->tv_sec > 0){ |
| 55 |
_end_time = Chrono::Clock(Chrono::Clock::Monolitic).now(); |
| 56 |
_end_time += Chrono::Duration(_reqparams->getOperationTimeout()->tv_sec); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
void checkTimeout(){ |
| 62 |
if( _end_time.isValid() && _end_time < Chrono::Clock(Chrono::Clock::Monolitic).now()){ |
| 63 |
std::ostringstream ss; |
| 64 |
ss << "operation timeout of " << _reqparams->getOperationTimeout()->tv_sec << "s expired"; |
| 65 |
throw DavixException(davix_scope_io_buff(), StatusCode::OperationTimeout, ss.str()); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
// context parameter |
| 73 |
Context& _context; |
| 74 |
Uri const & _uri; |
| 75 |
RequestParams const * _reqparams; |
| 76 |
|
| 77 |
// Operation parameter |
| 78 |
Chrono::TimePoint _end_time; |
| 79 |
|
| 80 |
// Keep track of how many bytes we've written to an fd, so as to avoid |
| 81 |
// writing the same bytes again in an event of retries / metalink recovery |
| 82 |
FdHandler fdHandler; |
| 83 |
}; |
| 84 |
|
| 85 |
// Davix IO chain |
| 86 |
// Complete I/O Stack implementation of Davix |
| 87 |
class HttpIOChain : NonCopyable{ |
| 88 |
public: |
| 89 |
|
| 90 |
HttpIOChain(); |
| 91 |
virtual ~HttpIOChain(); |
| 92 |
|
| 93 |
HttpIOChain* add(HttpIOChain* elem); |
| 94 |
|
| 95 |
/* |
| 96 |
* Meta data opts |
| 97 |
* |
| 98 |
**/ |
| 99 |
// calculate hecksum |
| 100 |
virtual void checksum(IOChainContext & iocontext, std::string & checksm, const std::string & chk_algo); |
| 101 |
|
| 102 |
// calc replica |
| 103 |
virtual std::vector<DavFile> & getReplicas(IOChainContext & iocontext, std::vector<DavFile> & vec); |
| 104 |
|
| 105 |
// delete resource |
| 106 |
virtual void deleteResource(IOChainContext & iocontext); |
| 107 |
|
| 108 |
// make collection |
| 109 |
virtual void makeCollection(IOChainContext & iocontext); |
| 110 |
|
| 111 |
// move/rename resource |
| 112 |
virtual void move(IOChainContext & iocontext, const std::string & target_url); |
| 113 |
|
| 114 |
// get statInfo |
| 115 |
virtual StatInfo & statInfo(IOChainContext & iocontext, StatInfo & st_info); |
| 116 |
|
| 117 |
// get quota info |
| 118 |
virtual QuotaInfo & quotaInfo(IOChainContext & iocontext, QuotaInfo & info); |
| 119 |
|
| 120 |
// listing |
| 121 |
// return false if end of directory is reached |
| 122 |
virtual bool nextSubItem(IOChainContext & iocontext, std::string & entry_name, StatInfo & info); |
| 123 |
|
| 124 |
|
| 125 |
/* |
| 126 |
* I/O Layer |
| 127 |
**/ |
| 128 |
virtual bool open(IOChainContext & iocontext, int flags); |
| 129 |
|
| 130 |
virtual void prefetchInfo(IOChainContext & iocontext, off_t offset, dav_size_t size_read, advise_t adv); |
| 131 |
|
| 132 |
virtual dav_ssize_t readFull(IOChainContext & iocontext, std::vector<char> & buffer); |
| 133 |
|
| 134 |
// overloaded version for string content |
| 135 |
virtual dav_ssize_t readFull(IOChainContext & iocontext, std::string & buffer); |
| 136 |
|
| 137 |
// read to fd |
| 138 |
virtual dav_ssize_t readToFd(IOChainContext & iocontext, int fd, dav_size_t size); |
| 139 |
|
| 140 |
virtual dav_ssize_t preadVec(IOChainContext & iocontext, const DavIOVecInput * input_vec, |
| 141 |
DavIOVecOuput * output_vec, |
| 142 |
const dav_size_t count_vec); |
| 143 |
|
| 144 |
// reset file position and status |
| 145 |
virtual void resetIO(IOChainContext & iocontext); |
| 146 |
|
| 147 |
// position independant read operation, |
| 148 |
// similar to pread except that does not need open() before |
| 149 |
virtual dav_ssize_t pread(IOChainContext & iocontext, void* buf, dav_size_t count, dav_off_t offset); |
| 150 |
|
| 151 |
// sequential read of a file from begining to the end |
| 152 |
virtual dav_ssize_t read(IOChainContext & iocontext, void* buf, dav_size_t count); |
| 153 |
|
| 154 |
// lseek prototype |
| 155 |
virtual dav_off_t lseek(IOChainContext & iocontext, dav_off_t offset, int flags); |
| 156 |
|
| 157 |
// sequential write |
| 158 |
virtual dav_ssize_t write(IOChainContext & iocontext, const void* buf, dav_size_t count); |
| 159 |
|
| 160 |
// write provided contents |
| 161 |
virtual dav_ssize_t writeFromProvider(IOChainContext & iocontext, ContentProvider &provider); |
| 162 |
|
| 163 |
// S3-type operations needed for streaming writes especially when the size |
| 164 |
// is unknown (common case). |
| 165 |
|
| 166 |
// Returns uploadId |
| 167 |
virtual std::string initiateMultipart(IOChainContext& iocontext); |
| 168 |
|
| 169 |
// Given the upload id and part#, write the given buffer and add the |
| 170 |
// object id to the etags vector. |
| 171 |
virtual bool writeFromBuffer(IOChainContext& iocontext, const char* buff, |
| 172 |
dav_size_t size, const std::string& uploadId, |
| 173 |
std::vector<std::string>& etags, |
| 174 |
int partNumber); |
| 175 |
|
| 176 |
// Given upload id and list of object tags, commit chunks |
| 177 |
virtual bool commitChunks(IOChainContext& iocontext, |
| 178 |
const std::string &uploadId, |
| 179 |
const std::vector<std::string> &etags); |
| 180 |
|
| 181 |
protected: |
| 182 |
std::unique_ptr<HttpIOChain> _next; |
| 183 |
HttpIOChain* _start; |
| 184 |
|
| 185 |
|
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
#endif // HTTPIOCHAIN_HPP |