View | Details | Raw Unified | Return to bug 12135
Collapse All | Expand All

(-)scrollkeeper-0.3.14.orig/libs/database.c (-20 / +134 lines)
Lines 17-22 Link Here
17
 */
17
 */
18
 
18
 
19
#include <config.h>
19
#include <config.h>
20
#include <limits.h>
20
#include <stdlib.h>
21
#include <stdlib.h>
21
#include <string.h>
22
#include <string.h>
22
#include <stddef.h>
23
#include <stddef.h>
Lines 27-35 Link Here
27
#include <dirent.h>
28
#include <dirent.h>
28
#include <scrollkeeper.h>
29
#include <scrollkeeper.h>
29
30
30
#define PATHLEN		256
31
32
33
/*
31
/*
34
 * Create a directory.  Send errors to appropriate places (STDOUT and log 
32
 * Create a directory.  Send errors to appropriate places (STDOUT and log 
35
 * file) according to command-line flags.
33
 * file) according to command-line flags.
Lines 55-87 Link Here
55
 */
53
 */
56
int sk_mkdir_with_parents(char *fullpath, mode_t options, char outputprefs)
54
int sk_mkdir_with_parents(char *fullpath, mode_t options, char outputprefs)
57
{
55
{
58
    char path[1024];
56
    char *path;
59
    char slash[]="/";
57
    char slash[]="/";
60
    char delim[]="/";
58
    char delim[]="/";
61
    char *token, *pathcopy;
59
    char *token, *pathcopy;
62
    struct stat buf;
60
    struct stat buf;
61
    int path_max = 4096;
62
#ifdef PATH_MAX
63
    path_max = PATH_MAX;
64
#else
65
    path_max = pathconf (full_path, _PC_PATH_MAX);
66
    if (path_max <= 0) path_max = 4096;
67
#endif
63
68
64
    pathcopy = strdup(fullpath); /* Copy b/c strtok edits the string it operates on */
69
    pathcopy = strdup(fullpath); /* Copy b/c strtok edits the string it operates on */
70
71
    if (!pathcopy) {
72
        sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", 
73
                  _("Could not init vars : %s\n"),strerror(errno));
74
	return 2;
75
    }
76
    
77
    path = (char *) malloc (path_max);
78
    if (!path) {
79
        sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", 
80
                  _("Could not init vars : %s\n"),strerror(errno));
81
	free (pathcopy);
82
	return 2;
83
    }
84
65
    path[0] = '\0';              /* Initialize with end of string null character */
85
    path[0] = '\0';              /* Initialize with end of string null character */
66
    if (pathcopy[0] == slash[0]) sprintf(path, "/"); /* preserve any starting slash */
86
    if (pathcopy[0] == slash[0]) sprintf(path, "/"); /* preserve any starting slash */
67
     
87
     
68
    token = strtok (pathcopy, delim);
88
    token = strtok (pathcopy, delim);
69
    delim[0]=slash[0];
89
    delim[0]=slash[0];
70
    while(token != NULL) {
90
    while(token != NULL) {
91
71
        if (strlen(path) == 0 || ((strlen(path) == 1) && (path[0] == slash[0]))) {
92
        if (strlen(path) == 0 || ((strlen(path) == 1) && (path[0] == slash[0]))) {
72
                sprintf(path, "%s%s", path, token);
93
		strncat (path, token, path_max);
73
        } else {
94
        } else {
74
                sprintf(path, "%s/%s", path, token);
95
		strncat (path, "/", path_max);
96
		strncat (path, token, path_max);
75
        }
97
        }
98
76
        if (stat(path, &buf) == -1) {
99
        if (stat(path, &buf) == -1) {
77
            if (sk_mkdir(path, options, outputprefs) != 0) {
100
            if (sk_mkdir(path, options, outputprefs) != 0) {
101
		free (pathcopy);
102
		free (path);
78
                return 1;
103
                return 1;
79
                }
104
                }
80
            }
105
            }
106
81
        delim[0]=slash[0];
107
        delim[0]=slash[0];
82
        token = strtok (NULL, delim);
108
        token = strtok (NULL, delim);
83
        }
109
        }
84
110
111
    free (pathcopy);
112
    free (path);
85
    return 0;
113
    return 0;
86
}
114
}
87
115
Lines 93-104 Link Here
93
int create_database_directory(char *scrollkeeper_dir, char *scrollkeeper_data_dir, char outputprefs)
121
int create_database_directory(char *scrollkeeper_dir, char *scrollkeeper_data_dir, char outputprefs)
94
{
122
{
95
    DIR *dir;
123
    DIR *dir;
96
    char source_path[PATHLEN], target_path[PATHLEN]; 
124
    char *source_path = NULL, *target_path = NULL; 
97
    struct dirent *dir_ent;
125
    struct dirent *dir_ent;
98
    struct stat buf;
126
    struct stat buf;
99
    int empty;
127
    int empty;
100
    char *data_dir, dirname[PATHLEN];
128
    char *data_dir, *dirname = NULL;
101
        
129
        
130
    int path_max = 4096;
131
#ifdef PATH_MAX
132
    path_max = PATH_MAX;
133
#else
134
    path_max = pathconf (source_path, _PC_PATH_MAX);
135
    if (path_max <= 0) path_max = 4096;
136
#endif
137
138
    source_path = (char *) malloc (path_max);
139
    if (!source_path) {
140
        sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", 
141
                  _("Could not init vars : %s\n"),strerror(errno));
142
	return 2;
143
    }
144
    
145
    target_path = (char *) malloc (path_max);
146
    if (!target_path) {
147
        sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", 
148
                  _("Could not init vars : %s\n"),strerror(errno));
149
	free (source_path);
150
	return 2;
151
    }
152
    
153
    dirname = (char *) malloc (path_max);
154
    if (!dirname) {
155
        sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", 
156
                  _("Could not init vars : %s\n"),strerror(errno));
157
	free (source_path);
158
	free (target_path);
159
	return 2;
160
    }
161
    
102
    /* check if it's empty */
162
    /* check if it's empty */
103
    
163
    
104
    empty = 1;
164
    empty = 1;
Lines 106-111 Link Here
106
    if (dir == NULL) {
166
    if (dir == NULL) {
107
    	if (sk_mkdir_with_parents(scrollkeeper_dir, S_IRUSR|S_IWUSR|S_IRGRP|
167
    	if (sk_mkdir_with_parents(scrollkeeper_dir, S_IRUSR|S_IWUSR|S_IRGRP|
108
                            S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH, outputprefs) != 0) {
168
                            S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH, outputprefs) != 0) {
169
	    free (source_path);
170
	    free (target_path);
171
	    free (dirname);
109
            return 1;
172
            return 1;
110
        }
173
        }
111
	dir = opendir(scrollkeeper_dir);
174
	dir = opendir(scrollkeeper_dir);
Lines 121-131 Link Here
121
    }
184
    }
122
    closedir(dir);
185
    closedir(dir);
123
    
186
    
124
    if (!empty)
187
    if (!empty) {
188
	free (source_path);
189
	free (target_path);
190
	free (dirname);
125
        return 0;
191
        return 0;
192
    }
126
        
193
        
127
    data_dir = malloc((strlen(scrollkeeper_data_dir)+strlen("/Templates")+1)*
194
    data_dir = malloc((strlen(scrollkeeper_data_dir)+strlen("/Templates")+1)*
128
    			sizeof(char));
195
    			sizeof(char));
196
    if (!data_dir) {
197
        sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", 
198
                  _("Could not init vars : %s\n"),strerror(errno));
199
	free (source_path);
200
	free (target_path);
201
	free (dirname);
202
        return 0;
203
    }
204
    
129
    check_ptr(data_dir, "scrollkeeper-install");
205
    check_ptr(data_dir, "scrollkeeper-install");
130
    sprintf(data_dir, "%s/Templates", scrollkeeper_data_dir);
206
    sprintf(data_dir, "%s/Templates", scrollkeeper_data_dir);
131
    
207
    
Lines 138-175 Link Here
138
        if (dir_ent->d_name[0] == '.')
214
        if (dir_ent->d_name[0] == '.')
139
	    continue;
215
	    continue;
140
	    
216
	    
141
	snprintf(source_path, PATHLEN, "%s/%s", data_dir, dir_ent->d_name);	
217
	snprintf(source_path, path_max, "%s/%s", data_dir, dir_ent->d_name);	
142
    
218
    
143
        lstat(source_path, &buf);
219
        lstat(source_path, &buf);
144
    
220
    
145
        if (S_ISDIR(buf.st_mode)) /* copy the directory */
221
        if (S_ISDIR(buf.st_mode)) /* copy the directory */
146
	{
222
	{
147
	    char source_file[PATHLEN], target_file[PATHLEN];
223
	    char *source_file, *target_file;
224
225
	    source_file = (char *)malloc(path_max);
226
	    if (!source_file) {
227
    		sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", 
228
                	_("Could not init vars : %s\n"),strerror(errno));
229
		free (source_path);
230
		free (target_path);
231
		free (dirname);
232
		return 2;
233
	    }
148
	
234
	
149
	    snprintf(dirname, PATHLEN, "%s/%s", scrollkeeper_dir, dir_ent->d_name);
235
	    target_file = (char *)malloc(path_max);
236
	    if (!target_file) {
237
    		sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", 
238
                	_("Could not init vars : %s\n"),strerror(errno));
239
		free (source_file);
240
		free (source_path);
241
		free (target_path);
242
		free (dirname);
243
		return 2;
244
	    }
245
	
246
	    snprintf(dirname, path_max, "%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);
247
	    mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH);
151
	    
248
	    
152
	    snprintf(source_file, PATHLEN, "%s/scrollkeeper_cl.xml", source_path);
249
	    snprintf(source_file, path_max, "%s/scrollkeeper_cl.xml", source_path);
153
	    snprintf(target_file, PATHLEN, "%s/%s/scrollkeeper_cl.xml", 
250
	    snprintf(target_file, path_max, "%s/%s/scrollkeeper_cl.xml", 
154
	    		scrollkeeper_dir, dir_ent->d_name);
251
	    		scrollkeeper_dir, dir_ent->d_name);
155
	    copy_file(source_file, target_file);
252
	    copy_file(source_file, target_file);
156
	    snprintf(target_file, PATHLEN, "%s/%s/scrollkeeper_extended_cl.xml", 
253
	    snprintf(target_file, path_max, "%s/%s/scrollkeeper_extended_cl.xml", 
157
	    		scrollkeeper_dir, dir_ent->d_name);
254
	    		scrollkeeper_dir, dir_ent->d_name);
158
	    copy_file(source_file, target_file);
255
	    copy_file(source_file, target_file);
256
	    free (source_file);
257
	    free (target_file);
159
	}
258
	}
160
	else /* link the directory */
259
	else /* link the directory */
161
	{
260
	{
162
	    char *target_locale;
261
	    char *target_locale;
163
	    char aux_path[PATHLEN];
262
	    char *aux_path;
263
264
	    aux_path = (char *)malloc(path_max);
265
	    if (!aux_path) {
266
    		sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", 
267
                	_("Could not init vars : %s\n"),strerror(errno));
268
		free (source_path);
269
		free (target_path);
270
		free (dirname);
271
		return 2;
272
	    }
164
273
165
	    realpath(source_path, aux_path);
274
	    realpath(source_path, aux_path);
166
	    target_locale = strrchr(aux_path, '/');
275
	    target_locale = strrchr(aux_path, '/');
167
	    target_locale++;
276
	    target_locale++;
168
	    	    
277
	    	    
169
	    snprintf(source_path, PATHLEN, "%s/%s", scrollkeeper_dir, dir_ent->d_name);
278
	    snprintf(source_path, path_max, "%s/%s", scrollkeeper_dir, dir_ent->d_name);
170
	    snprintf(target_path, PATHLEN, "%s", target_locale);
279
	    snprintf(target_path, path_max, "%s", target_locale);
171
	    	   
280
	    	   
172
	    symlink(target_path, source_path); 
281
	    symlink(target_path, source_path); 
282
	    free (aux_path);
173
	}
283
	}
174
    }
284
    }
175
    
285
    
Lines 178-188 Link Here
178
    
288
    
179
    /* create TOC and index directory */
289
    /* create TOC and index directory */
180
    
290
    
181
    snprintf(dirname, PATHLEN, "%s/TOC", scrollkeeper_dir);
291
    snprintf(dirname, path_max, "%s/TOC", scrollkeeper_dir);
182
    mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH);
292
    mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH);
183
293
184
    snprintf(dirname, PATHLEN, "%s/index", scrollkeeper_dir);
294
    snprintf(dirname, path_max, "%s/index", scrollkeeper_dir);
185
    mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH);
295
    mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH);
296
297
    free (source_path);
298
    free (target_path);
299
    free (dirname);
186
    return 0;
300
    return 0;
187
}
301
}
188
302

Return to bug 12135