1 /*****************************************************************************
2  * Author:   Valient Gough <vgough@pobox.com>
3  *
4  *****************************************************************************
5  * Copyright (c) 2004, Valient Gough
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef _FileIO_incl_
22 #define _FileIO_incl_
23 
24 #include <inttypes.h>
25 #include <stdint.h>
26 #include <sys/types.h>
27 
28 #include "Interface.h"
29 #include "encfs.h"
30 
31 namespace encfs {
32 
33 struct IORequest {
34   off_t offset;
35 
36   // amount of bytes to read/write.
37   size_t dataLen;
38   unsigned char *data;
39 
40   IORequest();
41 };
42 
IORequest()43 inline IORequest::IORequest() : offset(0), dataLen(0), data(0) {}
44 
45 class FileIO {
46  public:
47   FileIO();
48   virtual ~FileIO();
49 
50   virtual Interface interface() const = 0;
51 
52   // default implementation returns 1, meaning this is not block oriented.
53   virtual unsigned int blockSize() const;
54 
55   virtual void setFileName(const char *fileName) = 0;
56   virtual const char *getFileName() const = 0;
57 
58   // Not sure about this -- it is specific to CipherFileIO, but the
59   // alternative methods of exposing this interface aren't much nicer..
60   virtual bool setIV(uint64_t iv);
61 
62   // open file for specified mode.  There is no corresponding close, so a
63   // file is open until the FileIO interface is destroyed.
64   virtual int open(int flags) = 0;
65 
66   // get filesystem attributes for a file
67   virtual int getAttr(struct stat *stbuf) const = 0;
68   virtual off_t getSize() const = 0;
69 
70   virtual ssize_t read(const IORequest &req) const = 0;
71   virtual ssize_t write(const IORequest &req) = 0;
72 
73   virtual int truncate(off_t size) = 0;
74 
75   virtual bool isWritable() const = 0;
76 
77  private:
78   // not implemented..
79   FileIO(const FileIO &);
80   FileIO &operator=(const FileIO &);
81 };
82 
83 }  // namespace encfs
84 
85 #endif
86