[7] | 1 | |
---|
| 2 | /* |
---|
| 3 | Copyright 2013 Willem Vermin, SURFsara |
---|
| 4 | |
---|
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 6 | you may not use this file except in compliance with the License. |
---|
| 7 | You may obtain a copy of the License at |
---|
| 8 | |
---|
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | |
---|
| 11 | Unless required by applicable law or agreed to in writing, software |
---|
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | See the License for the specific language governing permissions and |
---|
| 15 | limitations under the License. |
---|
| 16 | */ |
---|
| 17 | #include "stopos_pool.h" |
---|
| 18 | #include "files_pool.h" |
---|
| 19 | #include <sys/types.h> |
---|
| 20 | #include <sys/stat.h> |
---|
| 21 | #include <fcntl.h> |
---|
| 22 | #include <stdlib.h> |
---|
| 23 | #include "wutils.h" |
---|
| 24 | #include <string.h> |
---|
| 25 | #include <strings.h> |
---|
| 26 | #include <stdio.h> |
---|
| 27 | #include <algorithm> |
---|
| 28 | #include <fstream> |
---|
| 29 | #include "stopos.h" |
---|
| 30 | #include <string> |
---|
| 31 | #include <list> |
---|
| 32 | #include <unistd.h> |
---|
| 33 | #include <errno.h> |
---|
| 34 | |
---|
| 35 | // constructor |
---|
| 36 | files_pool::files_pool() |
---|
| 37 | { |
---|
| 38 | this->whoami = "files"; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | // own version of set_db_name. |
---|
| 42 | void files_pool::set_db_name(std::string filename) |
---|
| 43 | { |
---|
| 44 | this->sdb_name = filename; |
---|
| 45 | this->db_name = strdup(sdb_name.c_str()); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | void files_pool::create_fname(std::string &fname,const std::string &k) |
---|
| 49 | { |
---|
| 50 | fname = this->sdb_name + "/" + k; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | int files_pool::get_record(std::string &r, const std::string &k, const std::string &l) |
---|
| 54 | { |
---|
| 55 | std::string fname; |
---|
| 56 | this->create_fname(fname,k); |
---|
| 57 | std::ifstream f; |
---|
| 58 | f.open(fname.c_str(),std::fstream::in); |
---|
| 59 | if (f.fail()) |
---|
| 60 | return FETCHERROR; |
---|
| 61 | |
---|
| 62 | char b[this->reclen]; |
---|
| 63 | |
---|
| 64 | f.getline(b,this->reclen); |
---|
| 65 | if (f.bad()) |
---|
| 66 | return FETCHERROR; |
---|
| 67 | |
---|
| 68 | r = b; |
---|
| 69 | |
---|
| 70 | f.close(); |
---|
| 71 | |
---|
| 72 | return 0; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | int files_pool::put_record(const std::string &b, |
---|
| 76 | const std::string &k, |
---|
| 77 | const std::string &l) |
---|
| 78 | { |
---|
| 79 | bool recordtoolong = 0; |
---|
| 80 | |
---|
| 81 | std::string buf = b; |
---|
| 82 | |
---|
| 83 | if (buf.size() >= reclen) |
---|
| 84 | recordtoolong=1; |
---|
| 85 | |
---|
| 86 | std::string fname; |
---|
| 87 | |
---|
| 88 | this->create_fname(fname,k); |
---|
| 89 | |
---|
| 90 | std::ofstream f; |
---|
| 91 | f.open(fname.c_str(),std::fstream::out); |
---|
| 92 | if (f.fail()) |
---|
| 93 | return STOREERROR; |
---|
| 94 | |
---|
| 95 | buf.resize(reclen); |
---|
| 96 | |
---|
| 97 | f.write(buf.c_str(),this->reclen); |
---|
| 98 | if (f.bad()) |
---|
| 99 | return STOREERROR; |
---|
| 100 | |
---|
| 101 | f.close(); |
---|
| 102 | |
---|
| 103 | if(recordtoolong) |
---|
| 104 | return RECORDTOOLONG; |
---|
| 105 | |
---|
| 106 | return 0; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | int files_pool::remove_record(const std::string &k,const std::string &l) |
---|
| 110 | { |
---|
| 111 | int rc; |
---|
| 112 | std::string fname; |
---|
| 113 | this->create_fname(fname,k); |
---|
| 114 | rc = unlink(fname.c_str()); |
---|
| 115 | if (rc == -1) |
---|
| 116 | return this->REMOVEERROR; |
---|
| 117 | |
---|
| 118 | return 0; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | int files_pool::create_db(const std::string &dbname) |
---|
| 122 | { |
---|
| 123 | int rc; |
---|
| 124 | rc = this->purge_db(); |
---|
| 125 | if (rc != 0) |
---|
| 126 | return rc; |
---|
| 127 | |
---|
| 128 | rc = mkdir_p(dbname,0755); |
---|
| 129 | if (rc != 0) |
---|
| 130 | return rc; |
---|
| 131 | |
---|
| 132 | return 0; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | int files_pool::purge_db(void) |
---|
| 136 | { |
---|
| 137 | int rc; |
---|
| 138 | if (this->db_name == 0) |
---|
| 139 | return NOFILENAME; |
---|
| 140 | |
---|
| 141 | std::list <std::string> v; |
---|
| 142 | |
---|
| 143 | rc = get_dir_list(v,this->sdb_name); |
---|
| 144 | for (std::list<std::string>::iterator it=v.begin(); it != v.end(); ++it) |
---|
| 145 | { |
---|
| 146 | // . and .. will be automaticalle skipped by unlink |
---|
| 147 | std::string f = this->sdb_name + '/' + (*it); |
---|
| 148 | unlink(f.c_str()); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | rc = rmdir(this->sdb_name.c_str()); |
---|
| 152 | if (rc != 0) |
---|
| 153 | if (errno == ENOENT) |
---|
| 154 | rc = 0; |
---|
| 155 | |
---|
| 156 | return rc; |
---|
| 157 | |
---|
| 158 | #if 0 |
---|
| 159 | std::string command; |
---|
| 160 | command = "rm -rf " + this->sdb_name; |
---|
| 161 | rc = system(command.c_str()); |
---|
| 162 | if (rc != 0) |
---|
| 163 | ; |
---|
| 164 | return 0; |
---|
| 165 | #endif |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | int files_pool::open_db(void) |
---|
| 169 | { |
---|
| 170 | if (this->db_name == 0) |
---|
| 171 | return NOFILENAME; |
---|
| 172 | |
---|
| 173 | if (this->db_open) |
---|
| 174 | return 0; |
---|
| 175 | |
---|
| 176 | // create lockfilename |
---|
| 177 | this->lockfilename = this->sdb_name+"/0lockfile"; |
---|
| 178 | |
---|
| 179 | int rc = access(this->db_name,R_OK|W_OK); |
---|
| 180 | if (rc == -1) |
---|
| 181 | return OPENERROR; |
---|
| 182 | |
---|
| 183 | struct stat status; |
---|
| 184 | stat(this->db_name, &status ); |
---|
| 185 | |
---|
| 186 | if ( !(status.st_mode & S_IFDIR) ) |
---|
| 187 | return OPENERROR; |
---|
| 188 | |
---|
| 189 | this->lockfile = open(this->lockfilename.c_str(),O_CREAT|O_RDWR,0600); |
---|
| 190 | if (this->lockfile < 0) |
---|
| 191 | return LOCKERROR; |
---|
| 192 | |
---|
| 193 | struct flock lock; |
---|
| 194 | lock.l_type = F_RDLCK|F_WRLCK; |
---|
| 195 | lock.l_whence = SEEK_SET; |
---|
| 196 | lock.l_start = 0; |
---|
| 197 | lock.l_len = 0; |
---|
| 198 | |
---|
| 199 | rc = fcntl(this->lockfile,F_SETLKW,&lock); |
---|
| 200 | if (rc < 0) |
---|
| 201 | return LOCKERROR; |
---|
| 202 | |
---|
| 203 | this->db_open = 1; |
---|
| 204 | |
---|
| 205 | return 0; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | int files_pool::close_db(void) |
---|
| 209 | { |
---|
| 210 | if (!this->db_open) |
---|
| 211 | return 0; |
---|
| 212 | |
---|
| 213 | struct flock lock; |
---|
| 214 | lock.l_type = F_UNLCK; |
---|
| 215 | lock.l_whence = SEEK_SET; |
---|
| 216 | lock.l_start = 0; |
---|
| 217 | lock.l_len = 0; |
---|
| 218 | |
---|
| 219 | int rc = fcntl(this->lockfile,F_SETLKW,&lock); |
---|
| 220 | if (rc < 0) |
---|
| 221 | return UNLOCKERROR; |
---|
| 222 | |
---|
| 223 | close(this->lockfile); |
---|
| 224 | |
---|
| 225 | this->db_open = 0; |
---|
| 226 | |
---|
| 227 | return 0; |
---|
| 228 | } |
---|
| 229 | |
---|