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 <sysalloc.h>
28 #include <kapp/main.h> /* KMain */
29 
30 #include <klib/text.h>
31 #include <klib/log.h> /* LOGERR */
32 #include <klib/out.h> /* OUTMSG */
33 #include <klib/refcount.h>
34 #include <klib/rc.h>
35 #include <klib/time.h>
36 
37 #include <kfs/directory.h>
38 #include <kfs/file.h>
39 
40 #include <xfs/model.h>
41 #include <xfs/node.h>
42 #include <xfs/tree.h>
43 #include <xfs/access.h>
44 #include <xfs/xfs.h>
45 
46 #include <stdio.h>
47 #include <string.h>
48 
49 /******************************************************************************/
50 
51 
52 #ifdef JOJOBA
53 
54 static
55 void
SLEPOY(int Sec)56 SLEPOY ( int Sec )
57 {
58 
59 printf ( "Sleeping %d seconds\n", Sec );
60 KSleepMs ( Sec * 1000 );
61 
62 printf ( "    DONE [ Sleeping %d seconds ]\n", Sec );
63 
64 }
65 
66 #endif /* JOJOBA */
67 
68 XFS_EXTERN rc_t CC XFS_InitAll_MHR ( const char * ConfigFile );
69 XFS_EXTERN rc_t CC XFS_DisposeAll_MHR ();
70 
71 static
72 rc_t
MakeModel(struct XFSModel ** Model,const char * ProjectId,bool ReadOnly)73 MakeModel (
74             struct XFSModel ** Model,
75             const char * ProjectId,
76             bool ReadOnly
77 )
78 {
79     rc_t RCt;
80     struct XFSModel * Mod;
81     struct XFSModelNode * ModNod;
82 
83     RCt = 0;
84     Mod = NULL;
85 
86     RCt = XFSModelFromScratch ( & Mod, NULL );
87     if ( RCt == 0 ) {
88         RCt = XFSModelAddRootNode ( Mod, "gap-project" );
89         if ( RCt == 0 ) {
90             ModNod = ( struct XFSModelNode * ) XFSModelRootNode ( Mod );
91             if ( ModNod == NULL ) {
92                 RCt = XFS_RC ( rcInvalid );
93             }
94             else {
95                 RCt = XFSModelNodeSetProperty (
96                                         ModNod,
97                                         XFS_MODEL_MODE,
98                                         ( ReadOnly
99                                                 ? XFS_MODEL_MODE_RO
100                                                 : XFS_MODEL_MODE_RW
101                                         )
102                                         );
103                 if ( RCt == 0 ) {
104                     RCt = XFSModelNodeSetProperty (
105                                             ModNod,
106                                             XFS_MODEL_PROJECTID,
107                                             ProjectId
108                                             );
109                     if ( RCt == 0 ) {
110                         * Model = Mod;
111                     }
112                 }
113 
114             }
115         }
116     }
117 
118     return RCt;
119 }
120 
121 
122 static
run(const char * ProjectId,const char * MountPoint,bool ReadOnly,bool Daemonize)123 rc_t run (
124         const char * ProjectId,
125         const char * MountPoint,
126         bool ReadOnly,
127         bool Daemonize
128 )
129 {
130     rc_t RCt;
131     struct XFSModel * TheModel;
132     struct XFSTree * TheTree;
133     struct XFSControl * TheControl;
134 
135     RCt = 0;
136     TheModel = NULL;
137     TheTree = NULL;
138     TheControl = NULL;
139 
140     OUTMSG ( ( "<<--- run()\n" ) );
141 
142     XFS_InitAll_MHR ( NULL );
143 
144     RCt = MakeModel ( & TheModel, ProjectId, ReadOnly );
145 
146     printf ( "HA(XFSModelMake)[RC=%d]\n", RCt );
147 
148     if ( RCt == 0 ) {
149         RCt = XFSTreeMake ( TheModel, & TheTree );
150 
151         printf ( "HA(XFSTreeMake)[RC=%d]\n", RCt );
152 
153         if ( RCt == 0 ) {
154             printf ( "HA(XFSControlMake)[RC=%d]\n", RCt );
155 
156             RCt = XFSControlMake ( & TheControl, TheTree );
157             printf ( "HE(XFSControlMake)[0x%p][RC=%d]\n", ( void * ) TheControl, RCt );
158 
159             if ( RCt == 0 ) {
160 
161                 XFSControlSetMountPoint ( TheControl, MountPoint );
162                 XFSControlSetLabel ( TheControl, "Olaffsen" );
163                 XFSControlSetLogFile ( TheControl, NULL );
164 
165                 if ( ! Daemonize ) {
166                     XFSControlSetArg ( TheControl, "-f", "-f" );
167                 }
168 
169                 printf ( "HA(XFSStart)\n" );
170                 RCt = XFSStart ( TheControl );
171 
172                 printf ( "HE(XFSStart)[RC=%d]\n", RCt );
173                 if ( RCt == 0 ) {
174                     printf ( "HA(XFSStop)\n" );
175                     RCt = XFSStop ( TheControl );
176                     printf ( "HE(XFSStop)[RC=%d]\n", RCt );
177                 }
178                 else {
179                     printf ( "Can not start FUSE\n" );
180                 }
181             }
182 
183             XFSControlDispose ( TheControl );
184 
185             XFSTreeRelease ( TheTree );
186         }
187 
188         XFSModelRelease ( TheModel );
189     }
190 
191     XFS_DisposeAll_MHR ();
192 
193     OUTMSG ( ( "--->> run()\n" ) );
194 
195     return RCt;
196 }
197 
198 /*  Here I will temporarily parce arguments, and will attach
199  *  toolkit ones later ... test program :)
200  */
201 char ProgramName[333];
202 char ProjectId [33];
203 int ProjectIdInt = 0;
204 char MountPoint[333];
205 bool ReadOnly = true;
206 bool Daemonize = false;
207 bool LogToFile = false;
208 char LogFile [ 777 ];
209 
210 #define RO_TAG   "ro"
211 #define RW_TAG   "rw"
212 #define DM_TAG   "-d"
213 
214 #define LF_TAG   "-l"
215 
216 static
217 void
RightUsage()218 RightUsage()
219 {
220     printf("\ndbGaP mount tool demo program. Will mount and show content of cart files\n");
221     printf("\nUsage: %s [%s|%s] [%s log_file] [%s] project_id mount_point\n\n\
222 Where:\n\
223     project_id - usually integer greater that zero and less than twelve\n\
224     %s - mount in read only mode\n\
225     %s - mount in read-write mode\n\
226     %s - run mounter as daemon\n\
227     mount_point - point to mount\n\
228     log_file - file to log logs\n\
229 \n\n", ProgramName, RO_TAG, RW_TAG, LF_TAG, DM_TAG, RO_TAG, RW_TAG, DM_TAG );
230 }   /* RightUsage() */
231 
232 static
233 bool
ParseArgs(int argc,char ** argv)234 ParseArgs ( int argc, char ** argv )
235 {
236     const char * PPU;
237     const char * Arg;
238     int llp;
239 
240     Arg = NULL;
241     llp = 0;
242     * ProgramName = 0;
243     * ProjectId = 0;
244     ProjectIdInt = 0;
245     * MountPoint = 0;
246 
247     ReadOnly = true;
248     Daemonize = false;
249 
250         /* Herer wer arer extractingr programr namer
251          */
252     PPU = strrchr ( * argv, '/' );
253     if ( PPU == NULL ) {
254         PPU = * argv;
255     }
256     else {
257         PPU ++;
258     }
259     strcpy ( ProgramName, PPU );
260 
261         /* Herer shouldr ber atr leastr oner argumentr - projectr idr
262          */
263     if ( argc <= 2 ) {
264         printf ( "ERROR : too few arguments\n" );
265         return false;
266     }
267 
268     llp = 1;
269     Arg = * ( argv + llp );
270 
271         /* firstr paramr couldr ber "ro|rw" orr "-d"
272          */
273     if ( strcmp ( Arg, RO_TAG ) == 0 ) {
274         ReadOnly = true;
275 
276         llp ++;
277     }
278     else {
279         ReadOnly = false;
280         if ( strcmp ( Arg, RW_TAG ) == 0 ) {
281 
282             llp ++;
283         }
284     }
285 
286         /* secondr paramr "-d" orr projectr idr
287          */
288     if ( argc <= llp ) {
289         printf ( "ERROR : too few arguments\n" );
290         return false;
291     }
292     Arg = * ( argv + llp );
293     if ( strcmp ( Arg, DM_TAG ) == 0 ) {
294         Daemonize = true;
295 
296         llp ++;
297     }
298 
299         /* andr nowr itr isr projectr idr
300          */
301     if ( argc <= llp ) {
302         printf ( "ERROR : too few arguments\n" );
303         return false;
304     }
305     Arg = * ( argv + llp );
306     strcpy ( ProjectId, Arg );
307         /* checkr thatr integerr
308          */
309     ProjectIdInt = atol ( ProjectId );
310     if ( ProjectIdInt <= 0 ) {
311         printf ( "ERROR : invalid project_id '%s'\n", ProjectId );
312         return false;
313     }
314     llp ++;
315 
316 
317         /* mountr pointr ifr existsr
318          */
319     if ( llp < argc ) {
320         Arg = * ( argv + llp );
321         strcpy ( MountPoint, Arg );
322         llp ++;
323     }
324 
325     printf ( "PrI [%d]\n", ProjectIdInt );
326     printf ( "MnP [%s]\n", MountPoint );
327 
328 
329     return true;
330 }   /* ParseArgs() */
331 
332 
333 const char UsageDefaultName[] = "Henri Fuseli";
UsageSummary(const char * progname)334 rc_t CC UsageSummary (const char* progname) { return 0; }
Usage(const Args * args)335 rc_t CC Usage ( const Args * args ) { return 0; }
336 
KMain(int argc,char * argv[])337 rc_t CC KMain(int argc, char *argv[]) {
338 
339     // KLogLevelSet ( klogInfo );
340     KLogLevelSet ( klogDebug );
341     // XFSLogInit ( "log.log" );
342 
343     if ( ! ParseArgs ( argc, argv ) ) {
344         RightUsage();
345         return 1;
346     }
347 
348     return run (
349                 ProjectId,
350                 MountPoint,
351                 ReadOnly,
352                 Daemonize
353                 );
354 }
355