2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $XConsortium: user_base.cc /main/8 1996/08/15 14:13:12 cde-hal $
26 * Copyright (c) 1992 HAL Computer Systems International, Ltd.
27 * All rights reserved. Unpublished -- rights reserved under
28 * the Copyright Laws of the United States. USE OF A COPYRIGHT
29 * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
32 * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33 * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
34 * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35 * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
38 * RESTRICTED RIGHTS LEGEND
39 * Use, duplication, or disclosure by the Government is subject
40 * to the restrictions as set forth in subparagraph (c)(l)(ii)
41 * of the Rights in Technical Data and Computer Software clause
42 * at DFARS 252.227-7013.
44 * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
52 #include "oliasdb/user_base.h"
53 #include "oliasdb/mark.h"
54 #include "utility/debug.h"
55 #include "misc/unique_id.h"
57 #define LOCK_DIR "lock"
58 #define AT_LOCK "at_lock"
59 #define W_LOCK "w_lock"
60 #define ACCESS_INFO "access_info"
62 extern void_ptr_array g_store_array;
63 extern Boolean g_transaction_on;
65 extern int g_mode_8_3;
67 user_base::user_base( const char* spec_path, rw_flag_t rw) :
68 spec_name(spec_path), backup_file(0), rw_flag(rw), first_desc_ptr(0),
73 f_obj_dict = new object_dict;
75 char* path = getenv("DTINFO_USER_DB") ;
78 char* home = getenv("HOME");
79 char* lang = getenv("LANG");
82 throw(stringException("HOME unspecified"));
84 throw(stringException("LANG unknown"));
86 path = form("%s/.dt/dtinfo/%s", home, lang);
89 char* name = getenv("USER");
94 strcpy(base_path, path);
95 strcpy(base_name, name);
96 strcpy(base_desc, "");
103 user_base::user_base( const char* base_dir,
106 const char* spec_path,
109 base(0, 0, 0, base_dir, base_nm, base_ds, ""), first_desc_ptr(0),
110 backup_file(0), rw_flag(rw), checking_status(SUCC),
115 f_obj_dict = new object_dict;
120 user_base::checking_status_t user_base::check_mode()
125 case user_base::READ:
127 if ( exist_dir(base_path) == false ) {
128 return user_base::FAIL;
132 case user_base::WRITE:
134 if ( check_and_create_dir(base_path) == false ) {
135 return user_base::FAIL;
142 catch (systemException&, e)
144 return user_base::FAIL;
148 if ( exist_file(SCHEMA_FILE, base_path) == true )
149 return user_base::SUCC;
151 if ( exist_file(form("%s.%s", base_name, SCHEMA_FILE_SUFFIX), base_path) == true )
152 return user_base::SUCC;
154 return user_base::NO_BASE;
157 user_base::checking_status_t user_base::check_lock()
159 char lock_dir[PATHSIZ];
161 sprintf(lock_dir, "%s/%s", base_path, LOCK_DIR);
163 if ( check_and_create_dir(lock_dir) == false ) {
164 MESSAGE(cerr, form("no write permission to %s", lock_dir));
165 return user_base::CREATE_LOCKFILE_FAIL;
168 strcpy(atomic_lock_path, form("%s/%s", lock_dir, AT_LOCK));
169 strcpy(write_lock_path, form("%s/%s", lock_dir, W_LOCK));
170 strcpy(ai_path, form("%s/%s", lock_dir, ACCESS_INFO));
175 case user_base::READ:
178 read_lock(atomic_lock_path, write_lock_path,
179 ai_path, access_info("read"), offset, ai_info
183 debug(cerr, ai_info);
186 return user_base::RLOCK_FAIL;
188 return user_base::SUCC;
191 case user_base::WRITE:
194 write_lock(atomic_lock_path, write_lock_path,
195 ai_path, access_info("write"), ai_info
199 debug(cerr, ai_info);
202 // int x = strlen(ai_info);
203 // if ( x >= 5 && strncmp(ai_info + x - 5, "read", 4) == 0 )
205 // rw_flag = user_base::READ;
206 // MESSAGE(cerr, "write locking failed, try read mode");
207 // int ok = check_lock();
214 return user_base::WLOCK_FAIL;
216 return user_base::SUCC;
226 void user_base::_init()
228 set_mode(HEALTH, true);
230 checking_status = check_mode();
232 MESSAGE(cerr, "Initial mode");
233 debug(cerr, checking_status);
237 switch ( checking_status ) {
239 case user_base::NO_BASE:
241 if ( disk_space(base_path) < 10*KB ) {
242 checking_status = user_base::FAIL;
257 catch (beginTransException &,e)
259 // cases: can't open log or write size info to log
260 checking_status = user_base::FAIL;
264 catch (commitTransException &,e)
266 // cases: bad log file, can't write to store, etc.
267 checking_status = user_base::FAIL;
271 // cases: can't do define()
272 catch (mmdbException &,e)
274 checking_status = user_base::FAIL;
275 ubase_trans.rollback();
283 checking_status = check_mode();
286 case user_base::SUCC:
291 first_desc_ptr = f_obj_dict -> init_a_base(base_path, base_name);
296 catch (mmdbException &,e) {
298 fprintf(stderr, "mmdbException caught @ %s line:%d.\n",
303 set_mode(HEALTH, false);
304 checking_status = user_base::FAIL;
311 set_mode(HEALTH, false);
316 if ( checking_status == user_base::SUCC ) {
317 checking_status = check_lock();
318 debug(cerr, checking_status);
319 if ( checking_status != user_base::SUCC ) {
320 set_mode(HEALTH, false);
327 MESSAGE(cerr, "Final mode"); debug(cerr, checking_status);
329 switch ( checking_status ) {
331 case user_base::SUCC:
332 set_mode(HEALTH, true);
333 MESSAGE(cerr, form("userbase \"%s\" available", base_name));
337 set_mode(HEALTH, false);
344 user_base::~user_base()
346 if ( checking_status == user_base::SUCC &&
347 exist_dir(form("%s/%s", base_path, LOCK_DIR)) == true )
352 case user_base::READ:
353 read_unlock(atomic_lock_path, ai_path, offset);
355 case user_base::WRITE:
356 write_unlock(atomic_lock_path, write_lock_path, ai_path);
366 void user_base::clean_up()
368 abs_storage* store_ptr = 0;
369 desc* desc_ptr = first_desc_ptr;
372 store_ptr = desc_ptr -> get_store();
377 MESSAGE(cerr, "removing: ");
378 MESSAGE(cerr, store_ptr -> my_name());
382 catch (mmdbException &,e)
386 desc_ptr = desc_ptr -> get_next_desc();
389 if ( exist_file(SCHEMA_FILE, base_path) == true )
390 del_file(SCHEMA_FILE, base_path);
393 Boolean user_base::define()
395 if ( check_and_create_dir(base_path) == false ) {
396 throw(stringException(form("can't create %s", base_path)));
399 char spec_file_path[PATHSIZ];
401 //char* x = getenv("DTINFO_LIB");
402 char* x = getenv("DTINFO_MARKSPECPATH");
405 strcpy(spec_file_path, spec_name);
408 strcpy(spec_file_path, form("%s/%s", x, spec_name));
410 if (exist_file(spec_file_path) == false) {
412 debug(cerr, spec_file_path);
414 throw(stringException(
415 form("missing %s. can't define user_base", spec_file_path)
421 char unique_nm[PATHSIZ];
422 strcpy(unique_nm, form("%s.%s", base_name, unique_id()));
426 f_obj_dict -> init_a_base(spec_file_path, base_path, base_name);