--- scrollkeeper-0.3.14.orig/libs/database.c 2002-06-02 10:03:10 +0400 +++ scrollkeeper-0.3.14.orig/libs/database.c 2007-06-25 19:03:43 +0400 @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -27,9 +28,6 @@ #include #include -#define PATHLEN 256 - - /* * Create a directory. Send errors to appropriate places (STDOUT and log * file) according to command-line flags. @@ -55,33 +53,63 @@ */ int sk_mkdir_with_parents(char *fullpath, mode_t options, char outputprefs) { - char path[1024]; + char *path; char slash[]="/"; char delim[]="/"; char *token, *pathcopy; struct stat buf; + int path_max = 4096; +#ifdef PATH_MAX + path_max = PATH_MAX; +#else + path_max = pathconf (full_path, _PC_PATH_MAX); + if (path_max <= 0) path_max = 4096; +#endif pathcopy = strdup(fullpath); /* Copy b/c strtok edits the string it operates on */ + + if (!pathcopy) { + sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", + _("Could not init vars : %s\n"),strerror(errno)); + return 2; + } + + path = (char *) malloc (path_max); + if (!path) { + sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", + _("Could not init vars : %s\n"),strerror(errno)); + free (pathcopy); + return 2; + } + path[0] = '\0'; /* Initialize with end of string null character */ if (pathcopy[0] == slash[0]) sprintf(path, "/"); /* preserve any starting slash */ token = strtok (pathcopy, delim); delim[0]=slash[0]; while(token != NULL) { + if (strlen(path) == 0 || ((strlen(path) == 1) && (path[0] == slash[0]))) { - sprintf(path, "%s%s", path, token); + strncat (path, token, path_max); } else { - sprintf(path, "%s/%s", path, token); + strncat (path, "/", path_max); + strncat (path, token, path_max); } + if (stat(path, &buf) == -1) { if (sk_mkdir(path, options, outputprefs) != 0) { + free (pathcopy); + free (path); return 1; } } + delim[0]=slash[0]; token = strtok (NULL, delim); } + free (pathcopy); + free (path); return 0; } @@ -93,12 +121,44 @@ int create_database_directory(char *scrollkeeper_dir, char *scrollkeeper_data_dir, char outputprefs) { DIR *dir; - char source_path[PATHLEN], target_path[PATHLEN]; + char *source_path = NULL, *target_path = NULL; struct dirent *dir_ent; struct stat buf; int empty; - char *data_dir, dirname[PATHLEN]; + char *data_dir, *dirname = NULL; + int path_max = 4096; +#ifdef PATH_MAX + path_max = PATH_MAX; +#else + path_max = pathconf (source_path, _PC_PATH_MAX); + if (path_max <= 0) path_max = 4096; +#endif + + source_path = (char *) malloc (path_max); + if (!source_path) { + sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", + _("Could not init vars : %s\n"),strerror(errno)); + return 2; + } + + target_path = (char *) malloc (path_max); + if (!target_path) { + sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", + _("Could not init vars : %s\n"),strerror(errno)); + free (source_path); + return 2; + } + + dirname = (char *) malloc (path_max); + if (!dirname) { + sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", + _("Could not init vars : %s\n"),strerror(errno)); + free (source_path); + free (target_path); + return 2; + } + /* check if it's empty */ empty = 1; @@ -106,6 +166,9 @@ if (dir == NULL) { if (sk_mkdir_with_parents(scrollkeeper_dir, S_IRUSR|S_IWUSR|S_IRGRP| S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH, outputprefs) != 0) { + free (source_path); + free (target_path); + free (dirname); return 1; } dir = opendir(scrollkeeper_dir); @@ -121,11 +184,24 @@ } closedir(dir); - if (!empty) + if (!empty) { + free (source_path); + free (target_path); + free (dirname); return 0; + } data_dir = malloc((strlen(scrollkeeper_data_dir)+strlen("/Templates")+1)* sizeof(char)); + if (!data_dir) { + sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", + _("Could not init vars : %s\n"),strerror(errno)); + free (source_path); + free (target_path); + free (dirname); + return 0; + } + check_ptr(data_dir, "scrollkeeper-install"); sprintf(data_dir, "%s/Templates", scrollkeeper_data_dir); @@ -138,38 +214,72 @@ if (dir_ent->d_name[0] == '.') continue; - snprintf(source_path, PATHLEN, "%s/%s", data_dir, dir_ent->d_name); + snprintf(source_path, path_max, "%s/%s", data_dir, dir_ent->d_name); lstat(source_path, &buf); if (S_ISDIR(buf.st_mode)) /* copy the directory */ { - char source_file[PATHLEN], target_file[PATHLEN]; + char *source_file, *target_file; + + source_file = (char *)malloc(path_max); + if (!source_file) { + sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", + _("Could not init vars : %s\n"),strerror(errno)); + free (source_path); + free (target_path); + free (dirname); + return 2; + } - snprintf(dirname, PATHLEN, "%s/%s", scrollkeeper_dir, dir_ent->d_name); + target_file = (char *)malloc(path_max); + if (!target_file) { + sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", + _("Could not init vars : %s\n"),strerror(errno)); + free (source_file); + free (source_path); + free (target_path); + free (dirname); + return 2; + } + + snprintf(dirname, path_max, "%s/%s", scrollkeeper_dir, dir_ent->d_name); mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH); - snprintf(source_file, PATHLEN, "%s/scrollkeeper_cl.xml", source_path); - snprintf(target_file, PATHLEN, "%s/%s/scrollkeeper_cl.xml", + snprintf(source_file, path_max, "%s/scrollkeeper_cl.xml", source_path); + snprintf(target_file, path_max, "%s/%s/scrollkeeper_cl.xml", scrollkeeper_dir, dir_ent->d_name); copy_file(source_file, target_file); - snprintf(target_file, PATHLEN, "%s/%s/scrollkeeper_extended_cl.xml", + snprintf(target_file, path_max, "%s/%s/scrollkeeper_extended_cl.xml", scrollkeeper_dir, dir_ent->d_name); copy_file(source_file, target_file); + free (source_file); + free (target_file); } else /* link the directory */ { char *target_locale; - char aux_path[PATHLEN]; + char *aux_path; + + aux_path = (char *)malloc(path_max); + if (!aux_path) { + sk_message(outputprefs, SKOUT_DEFAULT, SKOUT_QUIET, "", + _("Could not init vars : %s\n"),strerror(errno)); + free (source_path); + free (target_path); + free (dirname); + return 2; + } realpath(source_path, aux_path); target_locale = strrchr(aux_path, '/'); target_locale++; - snprintf(source_path, PATHLEN, "%s/%s", scrollkeeper_dir, dir_ent->d_name); - snprintf(target_path, PATHLEN, "%s", target_locale); + snprintf(source_path, path_max, "%s/%s", scrollkeeper_dir, dir_ent->d_name); + snprintf(target_path, path_max, "%s", target_locale); symlink(target_path, source_path); + free (aux_path); } } @@ -178,11 +288,15 @@ /* create TOC and index directory */ - snprintf(dirname, PATHLEN, "%s/TOC", scrollkeeper_dir); + snprintf(dirname, path_max, "%s/TOC", scrollkeeper_dir); mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH); - snprintf(dirname, PATHLEN, "%s/index", scrollkeeper_dir); + snprintf(dirname, path_max, "%s/index", scrollkeeper_dir); mkdir(dirname, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH|S_IXUSR|S_IXGRP|S_IXOTH); + + free (source_path); + free (target_path); + free (dirname); return 0; }