diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2012-03-25 20:00:08 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2012-03-25 20:00:08 +0000 |
| commit | 469bbcf0701e1eb8a6670c23145b0da87357e178 (patch) | |
| tree | b5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/cachestorefiles.h | |
| parent | ee1b79396076edc4e30aefb285fada03bb45e80d (diff) | |
| download | libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2 libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip | |
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/cachestorefiles.h')
| -rw-r--r-- | src/cachestorefiles.h | 207 |
1 files changed, 0 insertions, 207 deletions
diff --git a/src/cachestorefiles.h b/src/cachestorefiles.h deleted file mode 100644 index 10b2c1b..0000000 --- a/src/cachestorefiles.h +++ /dev/null | |||
| @@ -1,207 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
| 3 | * | ||
| 4 | * This file is part of the libbu++ library and is released under the | ||
| 5 | * terms of the license contained in the file LICENSE. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef BU_CACHE_STORE_FILES_H | ||
| 9 | #define BU_CACHE_STORE_FILES_H | ||
| 10 | |||
| 11 | #include "bu/string.h" | ||
| 12 | #include "bu/file.h" | ||
| 13 | #include "bu/cachestore.h" | ||
| 14 | #include "bu/archive.h" | ||
| 15 | #include "bu/membuf.h" | ||
| 16 | #include "bu/formatter.h" | ||
| 17 | #include "bu/sio.h" | ||
| 18 | |||
| 19 | #include <sys/types.h> | ||
| 20 | #include <sys/stat.h> | ||
| 21 | #include <dirent.h> | ||
| 22 | #include <unistd.h> | ||
| 23 | |||
| 24 | namespace Bu | ||
| 25 | { | ||
| 26 | template<class keytype, class obtype> | ||
| 27 | keytype __cacheGetKey( const obtype *pObj ); | ||
| 28 | |||
| 29 | template<class keytype, class obtype> | ||
| 30 | obtype *__cacheStoreFilesAlloc( const keytype &key ) | ||
| 31 | { | ||
| 32 | return new obtype(); | ||
| 33 | } | ||
| 34 | |||
| 35 | template<class keytype, class obtype> | ||
| 36 | void __cacheStoreFilesStore( Bu::Stream &s, obtype &rObj, | ||
| 37 | const keytype & ) | ||
| 38 | { | ||
| 39 | Bu::Archive ar( s, Bu::Archive::save ); | ||
| 40 | ar << rObj; | ||
| 41 | } | ||
| 42 | |||
| 43 | template<class keytype, class obtype> | ||
| 44 | obtype *__cacheStoreFilesLoad( Bu::Stream &s, const keytype &key ) | ||
| 45 | { | ||
| 46 | obtype *pObj = __cacheStoreFilesAlloc<keytype, obtype>( key ); | ||
| 47 | Bu::Archive ar( s, Bu::Archive::load ); | ||
| 48 | ar >> (*pObj); | ||
| 49 | return pObj; | ||
| 50 | } | ||
| 51 | |||
| 52 | template<class keytype, class obtype> | ||
| 53 | class CacheStoreFiles : public CacheStore<keytype, obtype> | ||
| 54 | { | ||
| 55 | public: | ||
| 56 | CacheStoreFiles( const Bu::String &sPrefix ) : | ||
| 57 | sPrefix( sPrefix ) | ||
| 58 | { | ||
| 59 | if( access( sPrefix.getStr(), W_OK|R_OK|X_OK ) ) | ||
| 60 | { | ||
| 61 | mkdir( sPrefix.getStr(), 0755 ); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | virtual ~CacheStoreFiles() | ||
| 66 | { | ||
| 67 | } | ||
| 68 | |||
| 69 | virtual obtype *load( const keytype &key ) | ||
| 70 | { | ||
| 71 | // try | ||
| 72 | // { | ||
| 73 | Bu::MemBuf mb; | ||
| 74 | Bu::Formatter f( mb ); | ||
| 75 | f << sPrefix << "/" << key; | ||
| 76 | Bu::File fIn( mb.getString(), Bu::File::Read ); | ||
| 77 | obtype *pOb = __cacheStoreFilesLoad<keytype, obtype>( fIn, key ); | ||
| 78 | return pOb; | ||
| 79 | // } | ||
| 80 | // catch( std::exception &e ) | ||
| 81 | // { | ||
| 82 | // throw Bu::HashException( e.what() ); | ||
| 83 | // } | ||
| 84 | } | ||
| 85 | |||
| 86 | virtual void unload( obtype *pObj, const keytype & ) | ||
| 87 | { | ||
| 88 | delete pObj; | ||
| 89 | } | ||
| 90 | |||
| 91 | virtual keytype create( obtype *pSrc ) | ||
| 92 | { | ||
| 93 | keytype key = __cacheGetKey<keytype, obtype>( pSrc ); | ||
| 94 | Bu::MemBuf mb; | ||
| 95 | Bu::Formatter f( mb ); | ||
| 96 | f << sPrefix << "/" << key; | ||
| 97 | |||
| 98 | Bu::File fTouch( mb.getString(), Bu::File::WriteNew ); | ||
| 99 | |||
| 100 | return key; | ||
| 101 | } | ||
| 102 | |||
| 103 | virtual void sync() | ||
| 104 | { | ||
| 105 | } | ||
| 106 | |||
| 107 | virtual void sync( obtype *pSrc, const keytype &key ) | ||
| 108 | { | ||
| 109 | Bu::MemBuf mb; | ||
| 110 | Bu::Formatter f( mb ); | ||
| 111 | f << sPrefix << "/" << key; | ||
| 112 | |||
| 113 | Bu::File fOut( mb.getString(), Bu::File::WriteNew ); | ||
| 114 | __cacheStoreFilesStore<keytype, obtype>( fOut, *pSrc, key ); | ||
| 115 | } | ||
| 116 | |||
| 117 | virtual void destroy( obtype *pObj, const keytype &key ) | ||
| 118 | { | ||
| 119 | Bu::MemBuf mb; | ||
| 120 | Bu::Formatter f( mb ); | ||
| 121 | f << sPrefix << "/" << key; | ||
| 122 | |||
| 123 | unlink( mb.getString().getStr() ); | ||
| 124 | delete pObj; | ||
| 125 | } | ||
| 126 | |||
| 127 | virtual void destroy( const keytype &key ) | ||
| 128 | { | ||
| 129 | Bu::MemBuf mb; | ||
| 130 | Bu::Formatter f( mb ); | ||
| 131 | f << sPrefix << "/" << key; | ||
| 132 | |||
| 133 | unlink( mb.getString().getStr() ); | ||
| 134 | } | ||
| 135 | |||
| 136 | virtual bool has( const keytype &key ) | ||
| 137 | { | ||
| 138 | Bu::MemBuf mb; | ||
| 139 | Bu::Formatter f( mb ); | ||
| 140 | f << sPrefix << "/"; | ||
| 141 | Bu::String sBase = mb.getString(); | ||
| 142 | f << key; | ||
| 143 | |||
| 144 | if( sBase == mb.getString() ) | ||
| 145 | return false; | ||
| 146 | |||
| 147 | return access( mb.getString().getStr(), F_OK ) == 0; | ||
| 148 | } | ||
| 149 | |||
| 150 | virtual Bu::List<keytype> getKeys() | ||
| 151 | { | ||
| 152 | DIR *dir = opendir( sPrefix.getStr() ); | ||
| 153 | struct dirent *de; | ||
| 154 | Bu::List<keytype> lKeys; | ||
| 155 | |||
| 156 | while( (de = readdir( dir ) ) ) | ||
| 157 | { | ||
| 158 | if( de->d_type != DT_REG ) | ||
| 159 | continue; | ||
| 160 | |||
| 161 | keytype tmp; | ||
| 162 | Bu::MemBuf mb( de->d_name ); | ||
| 163 | Bu::Formatter f( mb ); | ||
| 164 | try | ||
| 165 | { | ||
| 166 | Fmt fm; | ||
| 167 | fm.tokenize( false ); | ||
| 168 | f << fm; | ||
| 169 | f >> tmp; | ||
| 170 | } | ||
| 171 | catch( Bu::ExceptionBase &e ) | ||
| 172 | { | ||
| 173 | Bu::sio << "Parse error in dir-scan: " << e.what() | ||
| 174 | << Bu::sio.nl; | ||
| 175 | } | ||
| 176 | lKeys.append( tmp ); | ||
| 177 | } | ||
| 178 | closedir( dir ); | ||
| 179 | |||
| 180 | return lKeys; | ||
| 181 | } | ||
| 182 | |||
| 183 | virtual int getSize() | ||
| 184 | { | ||
| 185 | DIR *dir = opendir( sPrefix.getStr() ); | ||
| 186 | struct dirent *de; | ||
| 187 | int iCount = 0; | ||
| 188 | |||
| 189 | while( (de = readdir( dir ) ) ) | ||
| 190 | { | ||
| 191 | if( de->d_type != DT_REG ) | ||
| 192 | continue; | ||
| 193 | |||
| 194 | iCount++; | ||
| 195 | } | ||
| 196 | closedir( dir ); | ||
| 197 | |||
| 198 | return iCount; | ||
| 199 | } | ||
| 200 | |||
| 201 | private: | ||
| 202 | Bu::String sPrefix; | ||
| 203 | }; | ||
| 204 | |||
| 205 | }; | ||
| 206 | |||
| 207 | #endif | ||
