1 | /* copyright (C) 2001 Sun Microsystems, Inc.*/ |
---|
2 | |
---|
3 | /* |
---|
4 | * This library is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU Lesser General Public |
---|
6 | * License as published by the Free Software Foundation; either |
---|
7 | * version 2.1 of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This library is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | * Lesser General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU Lesser General Public |
---|
15 | * License along with this library; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | */ |
---|
18 | |
---|
19 | #include <config.h> |
---|
20 | #include <stdlib.h> |
---|
21 | #include <string.h> |
---|
22 | #include <stddef.h> |
---|
23 | #include <unistd.h> |
---|
24 | #include <sys/stat.h> |
---|
25 | #include <errno.h> |
---|
26 | #include <libintl.h> |
---|
27 | #include <dirent.h> |
---|
28 | #include <scrollkeeper.h> |
---|
29 | |
---|
30 | #define PATHLEN 256 |
---|
31 | |
---|
32 | |
---|
33 | /* |
---|
34 | * Create a directory. Send errors to appropriate places (STDOUT and log |
---|
35 | * file) according to command-line flags. |
---|
36 | */ |
---|
37 | static int sk_mkdir(char *path, mode_t options, char outputprefs) |
---|
38 | { |
---|
39 | if (mkdir(path, options) != 0) { |
---|
40 | sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", |
---|
41 | _("Could not create directory %s : %s\n"),path,strerror(errno)); |
---|
42 | return 1; |
---|
43 | } |
---|
44 | return 0; |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | /* |
---|
49 | * Create a directory and any parent directories as necessary. Send errors |
---|
50 | * to appropriate places (STDOUT and log file) according to command-line |
---|
51 | * flags. |
---|
52 | * |
---|
53 | * This function uses strtok, which is pretty lame. It would be nice to |
---|
54 | * replace it with something which doesn't write over its input variables. |
---|
55 | */ |
---|
56 | int sk_mkdir_with_parents(char *fullpath, mode_t options, char outputprefs) |
---|
57 | { |
---|
58 | char path[1024]; |
---|
59 | char slash[]="/"; |
---|
60 | char delim[]="/"; |
---|
61 | char *token, *pathcopy; |
---|
62 | struct stat buf; |
---|
63 | |
---|
64 | pathcopy = strdup(fullpath); /* Copy b/c strtok edits the string it operates on */ |
---|
65 | path[0] = '\0'; /* Initialize with end of string null character */ |
---|
66 | if (pathcopy[0] == slash[0]) sprintf(path, "/"); /* preserve any starting slash */ |
---|
67 | |
---|
68 | token = strtok (pathcopy, delim); |
---|
69 | delim[0]=slash[0]; |
---|
70 | while(token != NULL) { |
---|
71 | if (strlen(path) == 0 || ((strlen(path) == 1) && (path[0] == slash[0]))) { |
---|
72 | sprintf(path, "%s%s", path, token); |
---|
73 | } else { |
---|
74 | sprintf(path, "%s/%s", path, token); |
---|
75 | } |
---|
76 | if (stat(path, &buf) == -1) { |
---|
77 | if (sk_mkdir(path, options, outputprefs) != 0) { |
---|
78 | return 1; |
---|
79 | } |
---|
80 | } |
---|
81 | delim[0]=slash[0]; |
---|
82 | token = strtok (NULL, delim); |
---|
83 | } |
---|
84 | |
---|
85 | return 0; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | /* check if the database directory exists, if not create it |
---|
90 | with the locale directories having similar structure in |
---|
91 | $localstatedir as in $datadir/Templates |
---|
92 | */ |
---|
93 | int create_database_directory(char *scrollkeeper_dir, char *scrollkeeper_data_dir, char outputprefs) |
---|
94 | { |
---|
95 | DIR *dir; |
---|
96 | char source_path[PATHLEN], target_path[PATHLEN]; |
---|
97 | struct dirent *dir_ent; |
---|
98 | struct stat buf; |
---|
99 | int empty; |
---|
100 | char *data_dir, dirname[PATHLEN]; |
---|
101 | |
---|
102 | /* check if it's empty */ |
---|
103 | |
---|
104 | empty = 1; |
---|
105 | dir = opendir(scrollkeeper_dir); |
---|
106 | if (dir == NULL) { |
---|
107 | if (sk_mkdir_with_parents(scrollkeeper_dir, S_IRUSR|S_IWUSR|S_IRGRP| |
---|
108 | S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH, outputprefs) != 0) { |
---|
109 | return 1; |
---|
110 | } |
---|
111 | dir = opendir(scrollkeeper_dir); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | while((dir_ent = readdir(dir)) != NULL && empty) |
---|
116 | { |
---|
117 | if (dir_ent->d_name[0] == '.') |
---|
118 | continue; |
---|
119 | |
---|
120 | empty = 0; |
---|
121 | } |
---|
122 | closedir(dir); |
---|
123 | |
---|
124 | if (!empty) |
---|
125 | return 0; |
---|
126 | |
---|
127 | data_dir = malloc((strlen(scrollkeeper_data_dir)+strlen("/Templates")+1)* |
---|
128 | sizeof(char)); |
---|
129 | check_ptr(data_dir, "scrollkeeper-install"); |
---|
130 | sprintf(data_dir, "%s/Templates", scrollkeeper_data_dir); |
---|
131 | |
---|
132 | /* create locale directories and symlinks */ |
---|
133 | |
---|
134 | dir = opendir(data_dir); |
---|
135 | |
---|
136 | while((dir_ent = readdir(dir)) != NULL) |
---|
137 | { |
---|
138 | if (dir_ent->d_name[0] == '.') |
---|
139 | continue; |
---|
140 | |
---|
141 | snprintf(source_path, PATHLEN, "%s/%s", data_dir, dir_ent->d_name); |
---|
142 | |
---|
143 | lstat(source_path, &buf); |
---|
144 | |
---|
145 | if (S_ISDIR(buf.st_mode)) /* copy the directory */ |
---|
146 | { |
---|
147 | char source_file[PATHLEN], target_file[PATHLEN]; |
---|
148 | |
---|
149 | snprintf(dirname, PATHLEN, "%s/%s", scrollkeeper_dir, dir_ent->d_name); |
---|
150 | mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH); |
---|
151 | |
---|
152 | snprintf(source_file, PATHLEN, "%s/scrollkeeper_cl.xml", source_path); |
---|
153 | snprintf(target_file, PATHLEN, "%s/%s/scrollkeeper_cl.xml", |
---|
154 | scrollkeeper_dir, dir_ent->d_name); |
---|
155 | copy_file(source_file, target_file); |
---|
156 | snprintf(target_file, PATHLEN, "%s/%s/scrollkeeper_extended_cl.xml", |
---|
157 | scrollkeeper_dir, dir_ent->d_name); |
---|
158 | copy_file(source_file, target_file); |
---|
159 | } |
---|
160 | else /* link the directory */ |
---|
161 | { |
---|
162 | char *target_locale; |
---|
163 | char aux_path[PATHLEN]; |
---|
164 | |
---|
165 | realpath(source_path, aux_path); |
---|
166 | target_locale = strrchr(aux_path, '/'); |
---|
167 | target_locale++; |
---|
168 | |
---|
169 | snprintf(source_path, PATHLEN, "%s/%s", scrollkeeper_dir, dir_ent->d_name); |
---|
170 | snprintf(target_path, PATHLEN, "%s", target_locale); |
---|
171 | |
---|
172 | symlink(target_path, source_path); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | closedir(dir); |
---|
177 | free(data_dir); |
---|
178 | |
---|
179 | /* create TOC and index directory */ |
---|
180 | |
---|
181 | snprintf(dirname, PATHLEN, "%s/TOC", scrollkeeper_dir); |
---|
182 | mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH); |
---|
183 | |
---|
184 | snprintf(dirname, PATHLEN, "%s/index", scrollkeeper_dir); |
---|
185 | mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH); |
---|
186 | return 0; |
---|
187 | } |
---|
188 | |
---|