| 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 |
#pragma once |
| 23 |
#ifndef DAVIX_IOBUFFMAP_HPP |
| 24 |
#define DAVIX_IOBUFFMAP_HPP |
| 25 |
|
| 26 |
#include <davix_internal.hpp> |
| 27 |
#include <fileops/fileutils.hpp> |
| 28 |
#include <fileops/httpiochain.hpp> |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
namespace Davix { |
| 33 |
|
| 34 |
// |
| 35 |
// Internal POSIX like to HTTP RW operation mapper |
| 36 |
// provides facilities for caching |
| 37 |
// |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
/// RW operation mapped to pure HTTP ops |
| 42 |
class HttpIO : public HttpIOChain{ |
| 43 |
public: |
| 44 |
HttpIO(); |
| 45 |
virtual ~HttpIO(); |
| 46 |
|
| 47 |
|
| 48 |
// read to dynamically allocated buffer |
| 49 |
virtual dav_ssize_t readFull(IOChainContext & iocontext, std::vector<char> & buffer); |
| 50 |
|
| 51 |
|
| 52 |
// position independant read operation, |
| 53 |
// similar to pread except that does not need open() before |
| 54 |
virtual dav_ssize_t pread(IOChainContext & iocontext, void* buf, dav_size_t count, dav_off_t offset); |
| 55 |
|
| 56 |
// read to fd |
| 57 |
virtual dav_ssize_t readToFd(IOChainContext & iocontext, int fd, dav_size_t size); |
| 58 |
|
| 59 |
virtual dav_ssize_t writeFromProvider(IOChainContext & iocontext, ContentProvider &provider); |
| 60 |
|
| 61 |
private: |
| 62 |
|
| 63 |
|
| 64 |
HttpIO(const HttpIO & ); |
| 65 |
HttpIO & operator=(const HttpIO & ); |
| 66 |
|
| 67 |
|
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
struct IOBufferLocalFile; |
| 72 |
|
| 73 |
/// |
| 74 |
/// RW operation with buffering support and POSIX like interface |
| 75 |
class HttpIOBuffer : public HttpIOChain{ |
| 76 |
public: |
| 77 |
HttpIOBuffer(); |
| 78 |
virtual ~HttpIOBuffer(); |
| 79 |
|
| 80 |
// open the file associated with the davix IOBuffMap |
| 81 |
// do a simple check if the file exist and try to anticipate the next ops |
| 82 |
virtual bool open(IOChainContext & iocontext, int flags); |
| 83 |
|
| 84 |
// |
| 85 |
virtual dav_ssize_t read(IOChainContext & iocontext, void* buf, dav_size_t count); |
| 86 |
|
| 87 |
|
| 88 |
// give information on the future operation for prefecting |
| 89 |
virtual void prefetchInfo(IOChainContext & iocontext, off_t offset, dav_size_t size_read, advise_t adv); |
| 90 |
|
| 91 |
// |
| 92 |
virtual dav_ssize_t write(IOChainContext & iocontext, const void* buf, dav_size_t count); |
| 93 |
|
| 94 |
// |
| 95 |
virtual dav_off_t lseek(IOChainContext & iocontext, dav_off_t offset, int flags); |
| 96 |
|
| 97 |
// |
| 98 |
virtual void resetIO(IOChainContext & iocontext); |
| 99 |
|
| 100 |
|
| 101 |
void commitLocal(IOChainContext & iocontext); |
| 102 |
|
| 103 |
protected: |
| 104 |
|
| 105 |
dav_size_t _file_size; |
| 106 |
bool _file_exist; |
| 107 |
dav_off_t _pos; |
| 108 |
bool _opened; |
| 109 |
advise_t _last_advise; |
| 110 |
|
| 111 |
// locker |
| 112 |
std::recursive_mutex _rwlock; |
| 113 |
// write cache |
| 114 |
std::unique_ptr<IOBufferLocalFile> _local; |
| 115 |
|
| 116 |
dav_off_t _read_pos; //curent read file offset |
| 117 |
bool _read_endfile; |
| 118 |
HttpRequest * _read_req; |
| 119 |
|
| 120 |
private: |
| 121 |
|
| 122 |
inline bool isAdviseFullRead(){ |
| 123 |
return (_last_advise == AdviseAuto || _last_advise == AdviseSequential); |
| 124 |
} |
| 125 |
|
| 126 |
dav_ssize_t readInternal(IOChainContext & iocontext, void *buffer, dav_size_t size_read); |
| 127 |
|
| 128 |
HttpIOBuffer(const HttpIOBuffer & ); |
| 129 |
HttpIOBuffer & operator=(const HttpIOBuffer & ); |
| 130 |
}; |
| 131 |
|
| 132 |
// create a single-ton filestream for cache from |
| 133 |
int get_valid_cache_file(FILE** stream, DavixError** err); |
| 134 |
|
| 135 |
|
| 136 |
dav_ssize_t read_segment_request(HttpRequest* req, void* buffer, dav_size_t size_read, DavixError**err); |
| 137 |
|
| 138 |
|
| 139 |
} // namespace Davix |
| 140 |
|
| 141 |
#endif // DAVIX_IOBUFFMAP_HPP |