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 | if (b.size() >= reclen) |
---|
80 | return RECORDTOOLONG; |
---|
81 | |
---|
82 | std::string fname; |
---|
83 | |
---|
84 | this->create_fname(fname,k); |
---|
85 | |
---|
86 | std::ofstream f; |
---|
87 | f.open(fname.c_str(),std::fstream::out); |
---|
88 | if (f.fail()) |
---|
89 | return STOREERROR; |
---|
90 | |
---|
91 | std::string buf = b; |
---|
92 | buf.resize(reclen); |
---|
93 | |
---|
94 | f.write(buf.c_str(),this->reclen); |
---|
95 | if (f.bad()) |
---|
96 | return STOREERROR; |
---|
97 | |
---|
98 | f.close(); |
---|
99 | return 0; |
---|
100 | } |
---|
101 | |
---|
102 | int files_pool::remove_record(const std::string &k,const std::string &l) |
---|
103 | { |
---|
104 | int rc; |
---|
105 | std::string fname; |
---|
106 | this->create_fname(fname,k); |
---|
107 | rc = unlink(fname.c_str()); |
---|
108 | if (rc == -1) |
---|
109 | return this->REMOVEERROR; |
---|
110 | |
---|
111 | return 0; |
---|
112 | } |
---|
113 | |
---|
114 | int files_pool::create_db(const std::string &dbname) |
---|
115 | { |
---|
116 | int rc; |
---|
117 | rc = this->purge_db(); |
---|
118 | if (rc != 0) |
---|
119 | return rc; |
---|
120 | |
---|
121 | rc = mkdir_p(dbname,0755); |
---|
122 | if (rc != 0) |
---|
123 | return rc; |
---|
124 | |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|
128 | int files_pool::purge_db(void) |
---|
129 | { |
---|
130 | int rc; |
---|
131 | if (this->db_name == 0) |
---|
132 | return NOFILENAME; |
---|
133 | |
---|
134 | std::list <std::string> v; |
---|
135 | |
---|
136 | rc = get_dir_list(v,this->sdb_name); |
---|
137 | for (std::list<std::string>::iterator it=v.begin(); it != v.end(); ++it) |
---|
138 | { |
---|
139 | // . and .. will be automaticalle skipped by unlink |
---|
140 | std::string f = this->sdb_name + '/' + (*it); |
---|
141 | unlink(f.c_str()); |
---|
142 | } |
---|
143 | |
---|
144 | rc = rmdir(this->sdb_name.c_str()); |
---|
145 | if (rc != 0) |
---|
146 | if (errno == ENOENT) |
---|
147 | rc = 0; |
---|
148 | |
---|
149 | return rc; |
---|
150 | |
---|
151 | #if 0 |
---|
152 | std::string command; |
---|
153 | command = "rm -rf " + this->sdb_name; |
---|
154 | rc = system(command.c_str()); |
---|
155 | if (rc != 0) |
---|
156 | ; |
---|
157 | return 0; |
---|
158 | #endif |
---|
159 | } |
---|
160 | |
---|
161 | int files_pool::open_db(void) |
---|
162 | { |
---|
163 | if (this->db_name == 0) |
---|
164 | return NOFILENAME; |
---|
165 | |
---|
166 | if (this->db_open) |
---|
167 | return 0; |
---|
168 | |
---|
169 | // create lockfilename |
---|
170 | this->lockfilename = this->sdb_name+"/0lockfile"; |
---|
171 | |
---|
172 | int rc = access(this->db_name,R_OK|W_OK); |
---|
173 | if (rc == -1) |
---|
174 | return OPENERROR; |
---|
175 | |
---|
176 | struct stat status; |
---|
177 | stat(this->db_name, &status ); |
---|
178 | |
---|
179 | if ( !(status.st_mode & S_IFDIR) ) |
---|
180 | return OPENERROR; |
---|
181 | |
---|
182 | this->lockfile = open(this->lockfilename.c_str(),O_CREAT|O_RDWR,0600); |
---|
183 | if (this->lockfile < 0) |
---|
184 | return LOCKERROR; |
---|
185 | |
---|
186 | struct flock lock; |
---|
187 | lock.l_type = F_RDLCK|F_WRLCK; |
---|
188 | lock.l_whence = SEEK_SET; |
---|
189 | lock.l_start = 0; |
---|
190 | lock.l_len = 0; |
---|
191 | |
---|
192 | rc = fcntl(this->lockfile,F_SETLKW,&lock); |
---|
193 | if (rc < 0) |
---|
194 | return LOCKERROR; |
---|
195 | |
---|
196 | this->db_open = 1; |
---|
197 | |
---|
198 | return 0; |
---|
199 | } |
---|
200 | |
---|
201 | int files_pool::close_db(void) |
---|
202 | { |
---|
203 | if (!this->db_open) |
---|
204 | return 0; |
---|
205 | |
---|
206 | struct flock lock; |
---|
207 | lock.l_type = F_UNLCK; |
---|
208 | lock.l_whence = SEEK_SET; |
---|
209 | lock.l_start = 0; |
---|
210 | lock.l_len = 0; |
---|
211 | |
---|
212 | int rc = fcntl(this->lockfile,F_SETLKW,&lock); |
---|
213 | if (rc < 0) |
---|
214 | return UNLOCKERROR; |
---|
215 | |
---|
216 | close(this->lockfile); |
---|
217 | |
---|
218 | this->db_open = 0; |
---|
219 | |
---|
220 | return 0; |
---|
221 | } |
---|
222 | |
---|