1 /*===========================================================================
2  *
3  *                            PUBLIC DOMAIN NOTICE
4  *               National Center for Biotechnology Information
5  *
6  *  This software/database is a "United States Government Work" under the
7  *  terms of the United States Copyright Act.  It was written as part of
8  *  the author's official duties as a United States Government employee and
9  *  thus cannot be copyrighted.  This software/database is freely available
10  *  to the public for use. The National Library of Medicine and the U.S.
11  *  Government have not placed any restriction on its use or reproduction.
12  *
13  *  Although all reasonable efforts have been taken to ensure the accuracy
14  *  and reliability of the software and data, the NLM and the U.S.
15  *  Government do not and cannot warrant the performance or results that
16  *  may be obtained by using this software or data. The NLM and the U.S.
17  *  Government disclaim all warranties, express or implied, including
18  *  warranties of performance, merchantability or fitness for any particular
19  *  purpose.
20  *
21  *  Please cite the author in any work or product based on this material.
22  *
23  * ===========================================================================
24  *
25  */
26 
27 #include <sra/extern.h>
28 
29 #include <sra/wsradb.h>
30 #include <os-native.h>
31 
32 #define KONST
33 #include "sra-priv.h"
34 #include <sra/types.h>
35 #include <vdb/manager.h>
36 #include <vdb/schema.h>
37 #include <vdb/table.h>
38 #include <vdb/cursor.h>
39 #include <vdb/vdb-priv.h>
40 #include <kdb/manager.h>
41 #include <klib/refcount.h>
42 #include <klib/log.h>
43 #include <klib/rc.h>
44 #include <klib/text.h>
45 
46 #include <stdlib.h>
47 #include <sysalloc.h>
48 
49 #define RC_MODULE (rcSRA)
50 #define RC_TARGET (rcMgr)
51 #define CLASS "SRAMgr"
52 
53 
54 /* MakeUpdate
55  *  create library handle for read/write access
56  *
57  *  "wd" [ IN, NULL OKAY ] - optional working directory for
58  *  accessing the file system. mgr will attach its own reference.
59  *
60  *  NB - not implemented in read-only library,
61  *  and the read-only library may not be mixed with read/write
62  */
SRAMgrMakeUpdate(SRAMgr ** mgrp,struct KDirectory * wd)63 LIB_EXPORT rc_t CC SRAMgrMakeUpdate ( SRAMgr **mgrp, struct KDirectory *wd )
64 {
65     rc_t rc;
66 
67     if ( mgrp == NULL )
68         rc = RC ( rcSRA, rcMgr, rcConstructing, rcParam, rcNull );
69     else
70     {
71         VDBManager *vmgr;
72         rc = VDBManagerMakeUpdate ( & vmgr, wd );
73         if ( rc == 0 )
74         {
75             SRAMgr *mgr;
76             rc = SRAMgrMake ( & mgr, vmgr , NULL );
77             if ( rc == 0 )
78             {
79                 mgr -> read_only = false;
80                 * mgrp = mgr;
81                 return 0;
82             }
83 
84             VDBManagerRelease ( vmgr );
85         }
86 
87         * mgrp = NULL;
88     }
89 
90     return rc;
91 }
92 
93 
94 /* Drop
95  *  drop an existing table
96  *
97  *  "force" [ IN ] - if true, make every attempt to remove table
98  *
99  *  "path" [ IN ]  - NUL terminated table name
100  */
SRAMgrDropTable(SRAMgr * self,bool force,const char * path,...)101 LIB_EXPORT rc_t CC SRAMgrDropTable( SRAMgr *self, bool force, const char *path, ... ) {
102     va_list va;
103     rc_t rc;
104 
105     va_start(va, path);
106     rc = SRAMgrVDropTable(self, force, path, va);
107     va_end(va);
108     return rc;
109 }
110 
SRAMgrVDropTable(SRAMgr * self,bool force,const char * path,va_list args)111 LIB_EXPORT rc_t CC SRAMgrVDropTable( SRAMgr *self, bool force, const char *path, va_list args ) {
112     if (self == NULL)
113         return RC(RC_MODULE, RC_TARGET, rcRemoving, rcSelf, rcNull);
114     if (path == NULL)
115         return RC(RC_MODULE, RC_TARGET, rcRemoving, rcParam, rcNull);
116     return VDBManagerVDrop(self->vmgr, kptTable, path, args);
117 }
118 
SRAMgrVLock(SRAMgr * self,const char * path,va_list args)119 LIB_EXPORT rc_t CC SRAMgrVLock ( SRAMgr *self, const char *path, va_list args ) {
120     if (self == NULL)
121         return RC(RC_MODULE, RC_TARGET, rcRemoving, rcSelf, rcNull);
122     if (path == NULL)
123         return RC(RC_MODULE, RC_TARGET, rcRemoving, rcParam, rcNull);
124     return VDBManagerVLock(self->vmgr, path, args);
125 }
126 
SRAMgrLock(SRAMgr * self,const char * path,...)127 LIB_EXPORT rc_t CC SRAMgrLock ( SRAMgr *self, const char *path, ... ) {
128     va_list va;
129     rc_t rc;
130 
131     va_start(va, path);
132     rc = SRAMgrVLock(self, path, va);
133     va_end(va);
134     return rc;
135 }
136 
SRAMgrVUnlock(SRAMgr * self,const char * path,va_list args)137 LIB_EXPORT rc_t CC SRAMgrVUnlock ( SRAMgr *self, const char *path, va_list args ) {
138     if (self == NULL)
139         return RC(RC_MODULE, RC_TARGET, rcRemoving, rcSelf, rcNull);
140     if (path == NULL)
141         return RC(RC_MODULE, RC_TARGET, rcRemoving, rcParam, rcNull);
142     return VDBManagerVUnlock(self->vmgr, path, args);
143 }
144 
SRAMgrUnlock(SRAMgr * self,const char * path,...)145 LIB_EXPORT rc_t CC SRAMgrUnlock ( SRAMgr *self, const char *path, ... ) {
146     va_list va;
147     rc_t rc;
148 
149     va_start(va, path);
150     rc = SRAMgrVUnlock(self, path, va);
151     va_end(va);
152     return rc;
153 }
154 
155 
156 /* SetMD5Mode
157  *  sets default MD5 file mode for all objects
158  *  opened for update under table
159  */
SRAMgrSetMD5Mode(SRAMgr * self,bool useMD5)160 LIB_EXPORT rc_t CC SRAMgrSetMD5Mode( SRAMgr *self, bool useMD5 ) {
161     if (self == NULL)
162         return RC(RC_MODULE, RC_TARGET, rcUpdating, rcSelf, rcNull);
163     self->mode = useMD5 ? (self->mode | kcmMD5) : (self->mode & ~kcmMD5);
164     return 0;
165 }
166