~ubuntu-branches/ubuntu/trusty/lifelines/trusty

« back to all changes in this revision

Viewing changes to src/gedlib/dbcontext.c

  • Committer: Bazaar Package Importer
  • Author(s): Felipe Augusto van de Wiel (faw)
  • Date: 2007-08-14 00:02:04 UTC
  • mfrom: (1.1.4 upstream) (3.1.4 gutsy)
  • Revision ID: james.westby@ubuntu.com-20070814000204-mpv5faygl0dgq3qi
Tags: 3.0.61-1
* New upstream release. (Closes: #387856).
* Fixing documentation build problems also fixes FTBFS if built twice in a
  row. (Closes: #424543).
* Adding lynx as a build dependency. This is necessary to generate txt files
  from html, discovered while fixing #424543.
* Upstream fix: charset for gedcom file in unicode. (Closes: #396206).
* Upstream fim: updating documentation about desc-tex2. (Closes: #405501).
* Bumping Standards-Version to 3.7.2 without package changes.
* Dropping local debian patches added upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
#include "mychar.h"
44
44
#include "charprops.h"
45
45
#include "xlat.h"
 
46
#include "dbcontext.h"
46
47
 
47
48
 
48
49
/*********************************************
53
54
extern STRING readpath,readpath_file;
54
55
 
55
56
/*********************************************
 
57
 * local function prototypes
 
58
 *********************************************/
 
59
 
 
60
static BOOLEAN alterdb(INT alteration, INT *lldberr);
 
61
static BOOLEAN open_database_impl(LLDATABASE lldb, INT alteration, INT *lldberr);
 
62
 
 
63
/*********************************************
56
64
 * local variables
57
65
 *********************************************/
58
66
 
67
75
 /*==================================================
68
76
 * alterdb -- force open, lock, or unlock a database
69
77
 *  alteration: [in] 1=unlock, 2=lock, 3=force open
70
 
 *  returns FALSE & sets bterrno if error (eg, keyfile corrupt)
 
78
 *  returns FALSE & sets lldberr if error (eg, keyfile corrupt)
71
79
 *================================================*/
72
80
static BOOLEAN
73
 
alterdb (INT alteration)
 
81
alterdb (INT alteration, INT *lldberr)
74
82
{
75
83
        /*
76
84
        Forcefully alter reader/writer count to 0.
85
93
        struct stat sbuf;
86
94
        sprintf(scratch, "%s/key", readpath);
87
95
        if (stat(scratch, &sbuf) || !S_ISREG(sbuf.st_mode)) {
88
 
                bterrno = BTERR_KFILE_ALTERDB;
 
96
                *lldberr = BTERR_KFILE_ALTERDB;
89
97
                goto force_open_db_exit;
90
98
        }
91
99
        if (!(fp = fopen(scratch, LLREADBINARYUPDATE)) ||
92
100
                  fread(&kfile1, sizeof(kfile1), 1, fp) != 1) {
93
 
                bterrno = BTERR_KFILE_ALTERDB;
 
101
                *lldberr = BTERR_KFILE_ALTERDB;
94
102
                goto force_open_db_exit;
95
103
        }
96
104
        if (fread(&kfile2, sizeof(kfile2), 1, fp) == 1) {
97
 
                if (!validate_keyfile2(&kfile2)) {
98
 
                        /* validate set bterrno */
 
105
                if (!validate_keyfile2(&kfile2, lldberr)) {
 
106
                        /* validate set lldberr */
99
107
                        goto force_open_db_exit;
100
108
                }
101
109
        }
103
111
                /* unlock db */
104
112
                if (kfile1.k_ostat != -2) {
105
113
                        /* can't unlock a db unless it is locked */
106
 
                        bterrno = BTERR_UNLOCKED;
 
114
                        *lldberr = BTERR_UNLOCKED;
107
115
                        goto force_open_db_exit;
108
116
                }
109
117
                kfile1.k_ostat = 0;
111
119
                /* lock db */
112
120
                if (kfile1.k_ostat == -2) {
113
121
                        /* can't lock a db that is already locked */
114
 
                        bterrno = BTERR_LOCKED;
 
122
                        *lldberr = BTERR_LOCKED;
115
123
                        goto force_open_db_exit;
116
124
                }
117
125
                if (kfile1.k_ostat != 0) {
118
126
                        /* can't lock a db unless it is unused currently*/
119
 
                        bterrno = kfile1.k_ostat < 0 ? BTERR_WRITER : BTERR_READERS;
 
127
                        *lldberr = kfile1.k_ostat < 0 ? BTERR_WRITER : BTERR_READERS;
120
128
                        goto force_open_db_exit;
121
129
                }
122
130
                kfile1.k_ostat = -2;
124
132
                /* force open db */
125
133
                if (kfile1.k_ostat == -2) {
126
134
                        /* cannot force open a locked database */
127
 
                        bterrno = BTERR_LOCKED;
 
135
                        *lldberr = BTERR_LOCKED;
128
136
                        goto force_open_db_exit;
129
137
                }
130
138
                kfile1.k_ostat = 0;
134
142
        }
135
143
        rewind(fp);
136
144
        if (fwrite(&kfile1, sizeof(kfile1), 1, fp) != 1) {
137
 
                bterrno = BTERR_KFILE_ALTERDB;
 
145
                *lldberr = BTERR_KFILE_ALTERDB;
138
146
                goto force_open_db_exit;
139
147
        }
140
148
        /* ok everything went successfully */
148
156
 * open_database_impl -- open database
149
157
 *  alteration:  [in] flag for forceopen (3), lock (2), & unlock (1)
150
158
 *  uses global readpath
151
 
 * Upon failure, sets bterrno and returns false
 
159
 * Upon failure, sets lldberr and returns false
152
160
 *================================================*/
153
161
static BOOLEAN
154
 
open_database_impl (LLDATABASE lldb, INT alteration)
 
162
open_database_impl (LLDATABASE lldb, INT alteration, INT *lldberr)
155
163
{
156
164
        int c;
157
165
        INT writ = !readonly + writeable;
158
166
        BTREE btree = 0;
159
167
 
160
168
        /* handle db adjustments (which only affect keyfile) first */
161
 
        if (alteration > 0 && !alterdb(alteration)) return FALSE;
 
169
        if (alteration > 0 && !alterdb(alteration, lldberr)) return FALSE;
162
170
 
163
171
        /* call btree module to do actual open of BTR */
164
 
        if (!(btree = bt_openbtree(readpath, FALSE, writ, immutable)))
 
172
        if (!(btree = bt_openbtree(readpath, FALSE, writ, immutable, lldberr)))
165
173
                return FALSE;
166
174
        lldb_set_btree(lldb, btree);
167
175
        /* we have to set the global variable readonly correctly, because
171
179
        if (readonly && writeable) {
172
180
                c = bkfile(btree).k_ostat;
173
181
                if (c < 0) {
174
 
                        bterrno = BTERR_WRITER;
 
182
                        *lldberr = BTERR_WRITER;
175
183
                } else {
176
184
                        rdr_count = c-1;
177
 
                        bterrno = BTERR_READERS;
 
185
                        *lldberr = BTERR_READERS;
178
186
                }
179
187
                return FALSE;
180
188
        }
186
194
 *  dbpath:       [in] database path to open
187
195
 *================================================*/
188
196
BOOLEAN
189
 
open_database (INT alteration, STRING dbpath)
 
197
open_database (INT alteration, STRING dbpath, INT *lldberr)
190
198
{
191
199
        LLDATABASE lldb = lldb_alloc();
192
200
        BOOLEAN rtn = FALSE;
201
209
        if (f_dbnotify)
202
210
                (*f_dbnotify)(readpath, TRUE);
203
211
 
204
 
        rtn = open_database_impl(lldb, alteration);
 
212
        rtn = open_database_impl(lldb, alteration, lldberr);
205
213
        if (!rtn) {
206
 
                /* open failed so clean up, preserve bterrno */
207
 
                int myerr = bterrno;
 
214
                /* open failed so clean up, preserve lldberr */
 
215
                int myerr = *lldberr;
208
216
                lldb_close(&lldb);
209
 
                bterrno = myerr;
 
217
                *lldberr = myerr;
210
218
        }
211
219
        def_lldb = lldb;
212
220
        return rtn;
216
224
 *  dbpath:  [IN]  path of database about to create
217
225
 *================================================*/
218
226
BOOLEAN
219
 
create_database (STRING dbpath)
 
227
create_database (STRING dbpath, INT *lldberr)
220
228
{
221
229
        LLDATABASE lldb = lldb_alloc();
222
230
        BTREE btree = 0;
227
235
                TABLE dbopts = create_table_str();
228
236
                STRING msg=0;
229
237
                if (!init_valtab_from_string(props, dbopts, '=', &msg)) {
230
 
                        bterrno = BTERR_BADPROPS;
 
238
                        *lldberr = BTERR_BADPROPS;
231
239
                        destroy_table(dbopts);
232
240
                        return FALSE;
233
241
                }
238
246
        readpath_file=strsave(lastpathname(dbpath));
239
247
        readpath=strsave(dbpath);
240
248
 
241
 
        if (!(btree = bt_openbtree(dbpath, TRUE, 2, immutable))) {
242
 
                /* open failed so clean up, preserve bterrno */
243
 
                int myerr = bterrno;
 
249
        if (!(btree = bt_openbtree(dbpath, TRUE, 2, immutable, lldberr))) {
 
250
                /* open failed so clean up, preserve lldberr */
 
251
                int myerr = *lldberr;
244
252
                lldb_close(&lldb);
245
 
                bterrno = myerr;
 
253
                *lldberr = myerr;
246
254
                return FALSE;
247
255
        }
248
256
        def_lldb = lldb;
321
329
                break;
322
330
        case BTERR_READERS:
323
331
                llstrappf(b, n, u8
324
 
                        , _("The database is already opened for read access by %d users.\n  ")
 
332
                        , _pl("The database is already opened for read access by %d user."
 
333
                                , "The database is already opened for read access by %d users."
 
334
                                , rdr_count)
325
335
                        , rdr_count);
326
336
                break;
327
337
        case BTERR_BADPROPS: