source: gs2-extensions/parallel-building/trunk/src/src/db2txtl-src/db2txtl.cpp@ 27683

Last change on this file since 27683 was 24835, checked in by jmt12, 13 years ago

Altered lockfile generation so that it occurs in specific collections tmp directory instead.

File size: 3.9 KB
Line 
1/**********************************************************************
2 *
3 * db2txt.cpp --
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#ifdef __WIN32__
27#include "autoconf.h"
28#include "systems.h"
29#include "gdbmconst.h"
30#include "gdbm.h"
31
32#else
33#include <gdbm.h>
34#endif
35
36#include "gsdlconf.h"
37#include <stdlib.h>
38#include <fcntl.h>
39#include <string.h>
40#include <sys/stat.h>
41
42#if defined(GSDL_USE_OBJECTSPACE)
43#include <ospace\std\iostream>
44#elif defined(GSDL_USE_IOS_H)
45#include <iostream.h>
46#else
47#include <iostream>
48#endif
49
50// use the standard namespace
51#if !defined (GSDL_NAMESPACE_BROKEN)
52#if defined(GSDL_USE_OBJECTSPACE)
53using namespace ospace::std;
54#else
55using namespace std;
56#endif
57#endif
58
59void print_usage (char *program_name) {
60 cerr << "usage: " << program_name << " database-name" << endl << endl;
61}
62
63// lock a file on linux
64// [hs, 2 july 2010]
65// - modified to create a locl file local to the collection [jmt12]
66int lock ()
67{
68 string file_path ("");
69 char *collect_dir = getenv ("GSDLCOLLECTDIR");
70 if (collect_dir != NULL)
71 {
72 file_path += collect_dir;
73 }
74 file_path += "/tmp";
75 if ( access( file_path.c_str(), 00 ) != 0 )
76 {
77 mkdir(file_path.c_str(), 00777);
78 }
79 file_path += "/gdb.lock";
80 ///out << "txt2dbl::lock(" << file_path << ") => ";
81 int fd2 = open (file_path.c_str(), O_CREAT|O_RDWR, 00644);
82 close (fd2);
83 int fd = open (file_path.c_str(), O_RDWR);
84 flock lock = {F_WRLCK, SEEK_SET, 0, 0, 0};
85 fcntl (fd, F_SETLKW, &lock);
86 ///out << "locked!" << endl;
87 return fd;
88}
89
90// unlock a file on linux
91// [hs, 2 july 2010]
92int unlock ( int fd )
93{
94 flock lock1 = {F_UNLCK, SEEK_SET, 0, 0, 0};
95 fcntl (fd, F_SETLKW, &lock1);
96 return 0;
97}
98
99int main (int argc, char *argv[]) {
100 int block_size = 0, i;
101 GDBM_FILE dbf;
102 datum key, value, nextkey;
103
104 // sanity check
105 if (argc != 2) {
106 print_usage (argv[0]);
107 exit (0);
108 }
109
110 // open the database
111 // - acquire a lock over the database so we can read from it.
112 int thelock = lock();
113#ifdef __WIN32__
114 dbf = gdbm_open (argv[1], block_size, GDBM_READER, 00664, NULL, 0);
115#else
116 dbf = gdbm_open (argv[1], block_size, GDBM_READER, 00664, NULL);
117#endif
118 if (dbf == NULL) {
119 cerr << argv[0] << ": couldn't open " << argv[1] << endl;
120 exit (0);
121 }
122
123 key = gdbm_firstkey (dbf);
124 while (key.dptr != NULL) {
125 cout << "[";
126 for (i = 0; i < key.dsize; ++i)
127 cout << key.dptr[i];
128 cout << "]" << endl;
129 value = gdbm_fetch (dbf, key);
130 for (i = 0; i < value.dsize; ++i)
131 cout << value.dptr[i];
132 cout << endl << "----------------------------------------------------------------------" << endl;
133 free(value.dptr);
134
135 /* get next key */
136 nextkey = gdbm_nextkey (dbf, key);
137
138 /* free old key's dptr, otherwise causes memory leak */
139 free(key.dptr);
140
141 /* can now safely copy content of nextkey into key */
142 key = nextkey;
143 }
144
145 gdbm_close (dbf);
146
147 // Unlock now that we've written the entire database
148 unlock (thelock);
149
150 return 0;
151}
Note: See TracBrowser for help on using the repository browser.