table: astro_object - added union for char designation.
[libastrodb.git] / src / library.c
blob310972b506e39e9617a6233a6d17ed4bc5d226a1
1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2008 Liam Girdwood
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <errno.h>
26 #include <libastrodb/library.h>
27 #include <libastrodb/adbstdio.h>
29 #define PATH_SIZE 1024
31 enum astrodb_msg_level adb_msg_level = ADB_MSG_INFO;
33 static char *dirs[] = {
34 "/I",
35 "/II",
36 "/III",
37 "/IV",
38 "/V",
39 "/VI",
40 "/VII",
41 "/VIII",
42 "/IX",
45 static int create_lib_local_dirs(char *location)
47 char dir[PATH_SIZE];
48 struct stat stat_info;
49 int i = 0, ret = 0;
51 /* create <path>/lnc/ */
52 bzero(dir, PATH_SIZE);
53 strncat(dir, location, PATH_SIZE);
54 if (stat(location, &stat_info) < 0) {
55 if ((ret = mkdir(dir, S_IRWXU | S_IRWXG)) < 0) {
56 return ret;
60 /* create sub dirs */
61 for (i = 0; i < astrodb_size(dirs); i++) {
62 bzero(dir, PATH_SIZE);
63 strncat(dir, location, PATH_SIZE);
64 strncat(dir, dirs[i], PATH_SIZE);
65 if (stat(dir, &stat_info) < 0) {
66 if ((ret = mkdir(dir, S_IRWXU | S_IRWXG)) < 0) {
67 return ret;
72 return ret;
75 /*! \fn astrodb_library* astrodb_open_library(char* remote, char* local)
76 * \param local Local library repository location
77 * \param remote Remote repository location
78 * \returns A astrodb_library object or NULL on failure
80 * Initialises a CDS library structure on disk at location specified.
82 * This typically only needs to be called once per program.
85 struct astrodb_library *astrodb_open_library(char *remote, char *local)
87 struct astrodb_library *lib = NULL;
88 int err;
90 if (local == NULL)
91 return NULL;
93 lib = (struct astrodb_library *) malloc(sizeof(struct astrodb_library));
94 if (lib == NULL)
95 return NULL;
96 bzero(lib, sizeof(struct astrodb_library));
98 err = create_lib_local_dirs(local);
99 if (err < 0) {
100 astrodb_error("failed to create local lib dir %s %d\n",
101 local, err);
102 goto err;
106 lib->local = strdup(local);
107 if (lib->local == NULL)
108 goto err;
109 lib->remote = strdup(remote);
110 if (lib->remote == NULL)
111 goto err;
112 return lib;
114 err:
115 free(lib->local);
116 free(lib->remote);
117 free(lib);
118 return NULL;
122 /*! \fn void astrodb_close_library(astrodb_library* lib)
123 * \param lib Library to free
125 * Free's all library resources.
127 void astrodb_close_library(struct astrodb_library * lib)
129 free(lib->remote);
130 free(lib->local);
131 free(lib);
134 void astrodb_set_msg_level(enum astrodb_msg_level level)
136 adb_msg_level = level;