source: tags/0.57/files_pool.cpp @ 9

Last change on this file since 9 was 9, checked in by willem, 11 years ago

willem

File size: 3.6 KB
Line 
1#include "files_pool.h"
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <fcntl.h>
5#include <stdlib.h>
6#include "wutils.h"
7#include <string.h>
8#include <strings.h>
9#include <stdio.h>
10#include <algorithm>
11#include <fstream>
12#include "stopos.h"
13
14// constructor
15files_pool::files_pool()
16{
17  this->dir_suffix = "_stopos_files_dir";
18}
19
20// own version of set_db_name.
21// this will be a directory, and on creation
22// it will be recursively removed.
23// to avoid oopses, a special name is formed from the input
24void files_pool::set_db_name(std::string filename)
25{
26  this->sdb_name = filename + this->dir_suffix;
27  this->db_name  = strdup(sdb_name.c_str());
28}
29
30void files_pool::create_fname(std::string &fname,const std::string &k)
31{
32  fname = this->sdb_name + "/" + k;
33}
34
35int files_pool::get_record(std::string &r, const std::string &k, const std::string &l)
36{
37  std::string fname;
38  this->create_fname(fname,k);
39  std::ifstream f;
40  f.open(fname.c_str(),std::fstream::in);
41  if (f.fail())
42    return FETCHERROR;
43
44  char b[this->reclen];
45
46  f.getline(b,this->reclen);
47  if (f.bad())
48    return FETCHERROR;
49
50  r = b;
51
52  f.close();
53
54  return 0;
55}
56
57int files_pool::put_record(const std::string &b, const std::string &k, const std::string &l)
58{
59  std::string fname;
60
61  if (stopos_key::KeyToNumber(k) == 0)
62  {
63    // this is a statusrecord;
64    // no special action required
65  }
66  this->create_fname(fname,k);
67
68  std::ofstream f;
69  f.open(fname.c_str(),std::fstream::out);
70  if (f.fail())
71    return STOREERROR;
72
73  std::string buf = b;
74  buf.resize(reclen);
75
76  f.write(buf.c_str(),this->reclen);
77  if (f.bad())
78    return STOREERROR;
79
80  f.close();
81  return 0;
82}
83
84int files_pool::remove_record(const stopos_key &k)
85{
86  int rc;
87  std::string fname;
88  this->create_fname(fname,k.str());
89  rc = unlink(fname.c_str());
90  if (rc == -1)
91    return this->REMOVEERROR;
92
93  return 0;
94}
95
96int files_pool::create_db(const std::string &dbname)
97{
98  int rc;
99  rc = this->purge_db();
100  if (rc != 0)
101    return rc;
102
103  rc = mkdir_p(dbname,0755);
104  if (rc != 0)
105    return rc;
106
107  return 0;
108}
109
110int files_pool::purge_db(void)
111{
112  if (this->db_name == 0)
113    return NOFILENAME;
114
115  if ( strstr(this->db_name,"_stopos_files_dir") == 0)
116    return NOTDIR;
117
118  std::string command;
119  command = "rm -rf " + this->sdb_name;
120  int rc = system(command.c_str());
121  if (rc != 0)
122    ;
123  return 0;
124}
125
126int files_pool::open_db(void)
127{
128  if (this->db_name == 0)
129    return NOFILENAME;
130
131  if (this->db_open)
132    return 0;
133
134  // create lockfilename
135  this->lockfilename = this->sdb_name+"/0lockfile";
136
137  int rc = access(this->db_name,R_OK|W_OK);
138  if (rc == -1)
139    return OPENERROR;
140
141  struct stat status;
142  stat(this->db_name, &status );
143
144  if ( !(status.st_mode & S_IFDIR) )
145     return OPENERROR;
146
147  this->lockfile = open(this->lockfilename.c_str(),O_CREAT|O_RDWR,0600);
148  if (this->lockfile < 0)
149    return LOCKERROR;
150
151  struct flock lock;
152  lock.l_type = F_RDLCK|F_WRLCK;
153  lock.l_whence = SEEK_SET;
154  lock.l_start  = 0;
155  lock.l_len    = 0;
156
157  rc = fcntl(this->lockfile,F_SETLKW,&lock);
158  if (rc < 0)
159    return LOCKERROR;
160
161  this->db_open = 1;
162
163  return 0;
164}
165
166int files_pool::close_db(void)
167{
168  if (!this->db_open)
169    return 0;
170
171  struct flock lock;
172  lock.l_type = F_UNLCK;
173  lock.l_whence = SEEK_SET;
174  lock.l_start  = 0;
175  lock.l_len    = 0;
176
177  int rc = fcntl(this->lockfile,F_SETLKW,&lock);
178  if (rc < 0)
179    return UNLOCKERROR;
180
181  close(this->lockfile);
182
183  this->db_open = 0;
184
185  return 0;
186}
187
188void files_pool::dump_db(void)
189{
190  std::string command;
191  command ="(for i in ";
192  command += db_name;
193  command += "/* ; do echo -n `basename $i`:;cat $i | tr -d '\\0' ; echo ; done |sort -n ) 1>&2";
194  int rc = system(command.c_str());
195  if (rc)
196    ;
197}
Note: See TracBrowser for help on using the repository browser.