1/********************************************************************
2 * Copyright (c) 2013 - 2014, Pivotal Inc.
3 * All rights reserved.
4 *
5 * Author: Zhanwei Wang
6 ********************************************************************/
7/********************************************************************
8 * 2014 -
9 * open source under Apache License Version 2.0
10 ********************************************************************/
11/**
12 * Licensed to the Apache Software Foundation (ASF) under one
13 * or more contributor license agreements. See the NOTICE file
14 * distributed with this work for additional information
15 * regarding copyright ownership. The ASF licenses this file
16 * to you under the Apache License, Version 2.0 (the
17 * "License"); you may not use this file except in compliance
18 * with the License. You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28#ifndef _HDFS_LIBHDFS3_COMMON_FILEWRAPPER_H_
29#define _HDFS_LIBHDFS3_COMMON_FILEWRAPPER_H_
30
31#include <string>
32#include <cassert>
33#include <cstdio>
34#include <string>
35#include <vector>
36
37namespace Hdfs {
38namespace Internal {
39
40class FileWrapper {
41public:
42 virtual ~FileWrapper() {
43 }
44
45 virtual bool open(int fd, bool delegate) = 0;
46 virtual bool open(const std::string & path) = 0;
47 virtual void close() = 0;
48 virtual const char * read(std::vector<char> & buffer, int32_t size) = 0;
49 virtual void copy(char * buffer, int32_t size) = 0;
50 virtual void seek(int64_t position) = 0;
51};
52
53class CFileWrapper: public FileWrapper {
54public:
55 CFileWrapper();
56 ~CFileWrapper();
57 bool open(int fd, bool delegate);
58 bool open(const std::string & path);
59 void close();
60 const char * read(std::vector<char> & buffer, int32_t size);
61 void copy(char * buffer, int32_t size);
62 void seek(int64_t offset);
63
64private:
65 FILE * file;
66 std::string path;
67};
68
69class MappedFileWrapper: public FileWrapper {
70public:
71 MappedFileWrapper();
72 ~MappedFileWrapper();
73 bool open(int fd, bool delegate);
74 bool open(const std::string & path);
75 void close();
76 const char * read(std::vector<char> & buffer, int32_t size);
77 void copy(char * buffer, int32_t size);
78 void seek(int64_t offset);
79
80private:
81 bool openInternal(int fd, bool delegate, size_t size);
82
83private:
84 bool delegate;
85 const char * begin;
86 const char * position;
87 int fd;
88 int64_t size;
89 std::string path;
90};
91
92}
93}
94
95#endif /* _HDFS_LIBHDFS3_COMMON_FILEWRAPPER_H_ */
96