| 301 |
ashish |
1 |
// Copyright (c) 2007-2008 Facebook
|
|
|
2 |
//
|
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
|
5 |
// You may obtain a copy of the License at
|
|
|
6 |
//
|
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
//
|
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12 |
// See the License for the specific language governing permissions and
|
|
|
13 |
// limitations under the License.
|
|
|
14 |
//
|
|
|
15 |
// See accompanying file LICENSE or visit the Scribe site at:
|
|
|
16 |
// http://developers.facebook.com/scribe/
|
|
|
17 |
//
|
|
|
18 |
// @author Bobby Johnson
|
|
|
19 |
// @author Avinash Lakshman
|
|
|
20 |
// @author Jason Sobel
|
|
|
21 |
|
|
|
22 |
#ifndef SCRIBE_FILE_H
|
|
|
23 |
#define SCRIBE_FILE_H
|
|
|
24 |
|
|
|
25 |
#include "common.h"
|
|
|
26 |
|
|
|
27 |
class FileInterface {
|
|
|
28 |
public:
|
|
|
29 |
FileInterface(const std::string& name, bool framed);
|
|
|
30 |
virtual ~FileInterface();
|
|
|
31 |
|
|
|
32 |
static boost::shared_ptr<FileInterface> createFileInterface(const std::string& type,
|
|
|
33 |
const std::string& name,
|
|
|
34 |
bool framed = false);
|
|
|
35 |
static std::vector<std::string> list(const std::string& path, const std::string& fsType);
|
|
|
36 |
|
|
|
37 |
virtual bool openRead() = 0;
|
|
|
38 |
virtual bool openWrite() = 0;
|
|
|
39 |
virtual bool openTruncate() = 0;
|
|
|
40 |
virtual bool isOpen() = 0;
|
|
|
41 |
virtual void close() = 0;
|
|
|
42 |
virtual bool write(const std::string& data) = 0;
|
|
|
43 |
virtual void flush() = 0;
|
|
|
44 |
virtual unsigned long fileSize() = 0;
|
|
|
45 |
virtual bool readNext(std::string& _return) = 0; // returns a line if unframed or a record if framed
|
|
|
46 |
virtual void deleteFile() = 0;
|
|
|
47 |
virtual void listImpl(const std::string& path, std::vector<std::string>& _return) = 0;
|
|
|
48 |
virtual std::string getFrame(unsigned data_size) {return std::string();};
|
|
|
49 |
virtual bool createDirectory(std::string path) = 0;
|
|
|
50 |
virtual bool createSymlink(std::string oldpath, std::string newpath) = 0;
|
|
|
51 |
|
|
|
52 |
protected:
|
|
|
53 |
bool framed;
|
|
|
54 |
std::string filename;
|
|
|
55 |
|
|
|
56 |
unsigned unserializeUInt(const char* buffer);
|
|
|
57 |
void serializeUInt(unsigned data, char* buffer);
|
|
|
58 |
};
|
|
|
59 |
|
|
|
60 |
class StdFile : public FileInterface {
|
|
|
61 |
public:
|
|
|
62 |
StdFile(const std::string& name, bool framed);
|
|
|
63 |
virtual ~StdFile();
|
|
|
64 |
|
|
|
65 |
bool openRead();
|
|
|
66 |
bool openWrite();
|
|
|
67 |
bool openTruncate();
|
|
|
68 |
bool isOpen();
|
|
|
69 |
void close();
|
|
|
70 |
bool write(const std::string& data);
|
|
|
71 |
void flush();
|
|
|
72 |
unsigned long fileSize();
|
|
|
73 |
bool readNext(std::string& _return);
|
|
|
74 |
void deleteFile();
|
|
|
75 |
void listImpl(const std::string& path, std::vector<std::string>& _return);
|
|
|
76 |
std::string getFrame(unsigned data_size);
|
|
|
77 |
bool createDirectory(std::string path);
|
|
|
78 |
bool createSymlink(std::string newpath, std::string oldpath);
|
|
|
79 |
|
|
|
80 |
private:
|
|
|
81 |
bool open(std::ios_base::openmode mode);
|
|
|
82 |
|
|
|
83 |
char* inputBuffer;
|
|
|
84 |
unsigned bufferSize;
|
|
|
85 |
std::fstream file;
|
|
|
86 |
|
|
|
87 |
// disallow copy, assignment, and empty construction
|
|
|
88 |
StdFile();
|
|
|
89 |
StdFile(StdFile& rhs);
|
|
|
90 |
StdFile& operator=(StdFile& rhs);
|
|
|
91 |
};
|
|
|
92 |
|
|
|
93 |
#endif // !defined SCRIBE_FILE_H
|