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 _BlockFileIO_incl_
22 #define _BlockFileIO_incl_
23 
24 #include <sys/types.h>
25 
26 #include "FSConfig.h"
27 #include "FileIO.h"
28 
29 namespace encfs {
30 
31 /*
32     Implements block scatter / gather interface.  Requires derived classes to
33     implement readOneBlock() / writeOneBlock() at a minimum.
34 
35     When a partial block write is requested it will be turned into a read of
36     the existing block, merge with the write request, and a write of the full
37     block.
38 */
39 class BlockFileIO : public FileIO {
40  public:
41   BlockFileIO(unsigned int blockSize, const FSConfigPtr &cfg);
42   virtual ~BlockFileIO();
43 
44   // implemented in terms of blocks.
45   virtual ssize_t read(const IORequest &req) const;
46   virtual ssize_t write(const IORequest &req);
47 
48   virtual unsigned int blockSize() const;
49 
50  protected:
51   int truncateBase(off_t size, FileIO *base);
52   int padFile(off_t oldSize, off_t newSize, bool forceWrite);
53 
54   // same as read(), except that the request.offset field is guarenteed to be
55   // block aligned, and the request size will not be larger then 1 block.
56   virtual ssize_t readOneBlock(const IORequest &req) const = 0;
57   virtual ssize_t writeOneBlock(const IORequest &req) = 0;
58 
59   ssize_t cacheReadOneBlock(const IORequest &req) const;
60   ssize_t cacheWriteOneBlock(const IORequest &req);
61 
62   unsigned int _blockSize;
63   bool _allowHoles;
64   bool _noCache;
65 
66   // cache last block for speed...
67   mutable IORequest _cache;
68 };
69 
70 }  // namespace encfs
71 
72 #endif
73