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 <vdb/extern.h>
28 
29 #define TRACK_REFERENCES 0
30 /* should match dbmgr-cmn.c */
31 
32 #include "libvdb.vers.h"
33 
34 #define KONST const
35 #include "dbmgr-priv.h"
36 #undef KONST
37 
38 #include "schema-priv.h"
39 #include "linker-priv.h"
40 
41 #include <vdb/manager.h>
42 #include <vdb/schema.h>
43 #include <kdb/kdb-priv.h> /* KDBManagerMakeReadWithVFSManager */
44 #include <kdb/manager.h>
45 #include <kfs/directory.h>
46 #include <klib/text.h>
47 #include <klib/rc.h>
48 #include <sysalloc.h>
49 
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include <assert.h>
53 
54 /*--------------------------------------------------------------------------
55  * VDBManager
56  *  opaque handle to library
57  */
58 
VDBManagerMakeWithVFSManager(const VDBManager ** mgrp,const KDirectory * wd,struct VFSManager * vmgr)59 LIB_EXPORT rc_t CC VDBManagerMakeWithVFSManager(const VDBManager ** mgrp,
60     const KDirectory *wd, struct VFSManager *vmgr)
61 {
62     return VDBManagerMakeReadWithVFSManager(mgrp, wd, NULL);
63 }
64 
65 /* MakeRead
66  * MakeReadWithVFSManager
67  *  create library handle for specific use
68  *  NB - only one of the functions will be implemented
69  *
70  *  "wd" [ IN, NULL OKAY ] - optional working directory for
71  *  accessing the file system. mgr will attach its own reference.
72  */
VDBManagerMakeRead(const VDBManager ** mgrp,const KDirectory * wd)73 LIB_EXPORT rc_t CC VDBManagerMakeRead ( const VDBManager **mgrp, const KDirectory *wd )
74 {
75     return VDBManagerMakeReadWithVFSManager(mgrp, wd, NULL);
76 }
77 
VDBManagerMakeReadWithVFSManager(const VDBManager ** mgrp,const KDirectory * wd,struct VFSManager * vmgr)78 LIB_EXPORT rc_t CC VDBManagerMakeReadWithVFSManager ( const VDBManager ** mgrp,
79     const KDirectory *wd, struct VFSManager *vmgr )
80 {
81     rc_t rc;
82 
83     if ( mgrp == NULL )
84         rc = RC ( rcVDB, rcMgr, rcConstructing, rcParam, rcNull );
85     else
86     {
87         VDBManager *mgr = calloc ( 1, sizeof * mgr );
88         if ( mgr == NULL )
89             rc = RC ( rcVDB, rcMgr, rcConstructing, rcMemory, rcExhausted );
90         else
91         {
92             rc = KDBManagerMakeReadWithVFSManager ( & mgr -> kmgr, wd, vmgr );
93             if ( rc == 0 )
94             {
95                 rc = VSchemaMakeIntrinsic ( & mgr -> schema );
96                 if ( rc == 0 )
97                 {
98                     rc = VLinkerMakeIntrinsic ( & mgr -> linker );
99                     if ( rc == 0 )
100                     {
101                         rc = VDBManagerConfigPaths ( mgr, false );
102                         if ( rc == 0 )
103                         {
104                             mgr -> user = NULL;
105                             mgr -> user_whack = NULL;
106                             KRefcountInit ( & mgr -> refcount, 1, "VDBManager", "make-read", "vmgr" );
107                             * mgrp = mgr;
108                             return 0;
109                         }
110 
111                         VLinkerRelease ( mgr -> linker );
112                     }
113 
114                     VSchemaRelease ( mgr -> schema );
115                 }
116 
117                 KDBManagerRelease ( mgr -> kmgr );
118             }
119 
120             free ( mgr );
121         }
122 
123         * mgrp = NULL;
124     }
125     return rc;
126 }
127 
128 
129 /* MakeRsrc
130  *  common make, regardless of library
131  */
VDBManagerMakeRsrc(VDBManager ** mgr,struct VFSManager * vfs)132 LIB_EXPORT rc_t CC VDBManagerMakeRsrc ( VDBManager ** mgr, struct VFSManager * vfs )
133 {
134     return VDBManagerMakeReadWithVFSManager ( ( const VDBManager** ) mgr, NULL, vfs );
135 }
136 
137 
138 /* Version
139  *  returns the library version
140  */
VDBManagerVersion(const VDBManager * self,uint32_t * version)141 LIB_EXPORT rc_t CC VDBManagerVersion ( const VDBManager *self, uint32_t *version )
142 {
143     if ( version == NULL )
144         return RC ( rcVDB, rcMgr, rcAccessing, rcParam, rcNull );
145     if ( self == NULL )
146         return RC ( rcVDB, rcMgr, rcAccessing, rcSelf, rcNull );
147 
148     * version = LIBVDB_VERS;
149     return 0;
150 }
151 
152 
153 /* RunPeriodicTasks
154  *  executes periodic tasks, such as cache flushing
155  */
VDBManagerRunPeriodicTasks(const VDBManager * self)156 LIB_EXPORT rc_t CC VDBManagerRunPeriodicTasks ( const VDBManager *self )
157 {
158     if ( self != NULL )
159         return KDBManagerRunPeriodicTasks ( self -> kmgr );
160 
161     return RC ( rcVDB, rcMgr, rcExecuting, rcSelf, rcNull );
162 }
163