GCS  0.2.3
gu_fdesc.hpp
1 /*
2  * Copyright (C) 2009-2013 Codership Oy <info@codership.com>
3  *
4  * $Id: gu_fdesc.hpp 3190 2013-08-07 21:49:17Z alex $
5  */
6 
7 #ifndef __GU_FDESC_HPP__
8 #define __GU_FDESC_HPP__
9 
10 #include "gu_exception.hpp"
11 #include "gu_types.hpp" // for off_t, byte_t
12 
13 #include <string>
14 
15 namespace gu
16 {
17 
19 {
20 public:
21 
22  /* open existing file */
23  FileDescriptor (const std::string& fname,
24  bool sync = true);
25 
26  /* (re)create file */
27  FileDescriptor (const std::string& fname,
28  size_t length,
29  bool allocate = true,
30  bool sync = true);
31 
32  ~FileDescriptor ();
33 
34  int get() const { return fd_; }
35  const std::string& name() const { return name_; }
36  off_t size() const { return size_; }
37 
38  void flush() const;
39 
40  void unlink() const { ::unlink (name_.c_str()); }
41 
42 private:
43 
44  std::string const name_;
45  int const fd_;
46  off_t const size_;
47  bool const sync_; // sync on close
48 
49  bool write_byte (off_t offset);
50  void write_file (off_t start = 0);
51  void prealloc (off_t start = 0);
52 
53  void constructor_common();
54 
56  FileDescriptor& operator = (const FileDescriptor);
57 };
58 
59 } /* namespace gu */
60 
61 #endif /* __GU_FDESC_HPP__ */
Definition: gu_fdesc.hpp:18