Date: Mon, 17 Aug 2009 08:12:26 GMT From: David Forsythe <dforsyth@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 167428 for review Message-ID: <200908170812.n7H8CQlY011410@repoman.freebsd.org>
index | next in thread | raw e-mail
http://perforce.freebsd.org/chv.cgi?CH=167428 Change 167428 by dforsyth@squirrel on 2009/08/17 08:11:54 Modify manifest add functions to return pointer to newly added entry. Speed up plist parse. Affected files ... .. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg.c#45 edit .. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg.h#40 edit .. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_db.c#12 edit .. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_db_hierdb_read.c#6 edit .. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_manifest.c#6 edit .. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_manifest.h#6 edit .. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_manifest_plist.c#7 edit .. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_util.c#18 edit .. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_util.h#15 edit .. //depot/projects/soc2009/dforsyth_libpkg/pkg_info/main.c#32 edit Differences ... ==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg.c#45 (text+ko) ==== @@ -96,7 +96,7 @@ } /* Retrieve pkg identity. In hierdb, this is the directory containing the - * plist. This is a client set identifier, so conflict and dependendency + * plist. This is a client set identifier, so conflict and dependency * checks should not rely on it. */ const char * @@ -382,17 +382,15 @@ pkg_add_pkg_file(struct pkg *p, const char *path, const char *cwd, const char *md5, const char *mode, const char *owner, const char *group) { - int status; - pkg_check_magic(p, __func__); /* TODO: Add some sanity checks in here. */ - status = PKG_OK; pkg_parse_manifest(p); - status |= pkg_manifest_add_file(p->pm, path, md5, cwd, mode, owner, group); + if (pkg_manifest_add_file(p->pm, path, md5, cwd, mode, owner, group) == NULL) + return (PKG_NOT_OK); - return (status); + return (PKG_OK); } int @@ -472,7 +470,9 @@ { pkg_check_magic(p, __func__); pkg_parse_manifest(p); - return (pkg_manifest_add_depend(p->pm, name, origin, version)); + if (pkg_manifest_add_depend(p->pm, name, origin, version) == NULL) + return (PKG_NOT_OK); + return (PKG_OK); } #if 0 ==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg.h#40 (text+ko) ==== @@ -1,6 +1,8 @@ #ifndef __PKG_H__ #define __PKG_H__ +#include <stdio.h> /* For FILE in pkg_dump */ + /* Plist/DB types. Format is 0xXXXXYYYY, where XXXX is manifest type, and YYYY is db type. */ /* HIERDB: Assumes information is stored in files with names with a preceding '+'. ==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_db.c#12 (text+ko) ==== @@ -132,24 +132,23 @@ char **list; struct pkg *entries; - if (db->pkg_count % 10 == 0) { - entries = db->pkg_entries; - list = db->pkg_list; - db->pkg_entries = realloc(entries, sizeof(*entries) * (db->pkg_count + 11)); - db->pkg_list = realloc(list, sizeof(*list) * (db->pkg_count + 11)); - if (db->pkg_entries == NULL || db->pkg_list == NULL) { - printf("need moar memory\n"); - exit(100); - } + + list = NULL; + entries = NULL; + PARALLEL_ARRAY_LIST_REALLOC(db->pkg_entries, entries, db->pkg_list, list, db->pkg_count, 10); + + if (db->pkg_entries == NULL || db->pkg_list == NULL) { + db->pkg_entries = entries; + db->pkg_list = list; + return (PKG_NOT_OK | PKG_MEMORY_ERR); } - - __pkg_init(&db->pkg_entries[db->pkg_count]); - status = pkg_clone(p, &db->pkg_entries[db->pkg_count]); + + __pkg_init(&db->pkg_entries[db->pkg_count - 1]); + status = pkg_clone(p, &db->pkg_entries[db->pkg_count - 1]); /* reuse entries. */ - entries = &db->pkg_entries[db->pkg_count]; - db->pkg_list[db->pkg_count] = entries->ident; - __pkg_set_in_db_ptr(&db->pkg_entries[db->pkg_count], db); - db->pkg_count++; + entries = &db->pkg_entries[db->pkg_count - 1]; + db->pkg_list[db->pkg_count - 1] = entries->ident; + __pkg_set_in_db_ptr(&db->pkg_entries[db->pkg_count - 1], db); db->pkg_list[db->pkg_count] = NULL; return (status); ==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_db_hierdb_read.c#6 (text+ko) ==== @@ -227,23 +227,14 @@ FILE *reqdby_stream; if (!pkg_db_hierdb_file_exists(db, p, REQUIRED_BY_FILE)) - return (PKG_OK); /* REQUIRED_BY doesn't have to exist. */ + return (PKG_OK); /* +REQUIRED_BY doesn't have to exist. */ reqdby_stream = pkg_db_hierdb_open_file_stream_read(db, p, REQUIRED_BY_FILE); - for (count = 0, reqdby = NULL; fgets(line, FILENAME_MAX, reqdby_stream) != NULL; count++) { - if (count % 10 == 0) { - list = reqdby; - reqdby = realloc(list, sizeof(*list) * (count + 11)); - if (reqdby == NULL) { - reqdby = list; - return (PKG_MEMORY_ERR | PKG_NOT_OK); - } - } + for (count = 0, reqdby = NULL; fgets(line, FILENAME_MAX, reqdby_stream) != NULL;) { pkg_util_trim_newline(line); - reqdby[count] = strdup(line); + STRING_LIST_APPEND(reqdby, line, list, count, 10); } - reqdby[count] = NULL; - + fclose(reqdby_stream); __pkg_set_required_by_ptr(p, reqdby); ==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_manifest.c#6 (text+ko) ==== @@ -222,7 +222,7 @@ return ((const char *const *)pm->dirrm_list); } -int +struct pkg_conflict * pkg_manifest_add_conflict(struct pkg_manifest *pm, const char *name, const char *version) { @@ -236,32 +236,29 @@ rage_quit(); } - if (pm->conflict_count % 5 == 0) { - list = pm->conflict_list; - conflicts = pm->conflict_entries; - pm->conflict_list = realloc(list, sizeof(*list) * (pm->conflict_count + 6)); - pm->conflict_entries = realloc(conflicts, sizeof(*conflicts) * (pm->conflict_count + 6)); - if (pm->conflict_list == NULL || pm->conflict_entries == NULL) { - pm->conflict_list = list; - pm->conflict_entries = conflicts; - return (PKG_MEMORY_ERR | PKG_NOT_OK); - } + list = NULL; + conflicts = NULL; + PARALLEL_ARRAY_LIST_REALLOC(pm->conflict_entries, conflicts, pm->conflict_list, list, pm->conflict_count, 5); + if (pm->conflict_entries == NULL || pm->conflict_list == NULL) { + pm->conflict_entries = conflicts; + pm->conflict_list = list; + return (NULL); } status = PKG_OK; - pc = &pm->conflict_entries[pm->conflict_count]; + pc = &pm->conflict_entries[pm->conflict_count - 1]; pkg_conflict_init(pc); status |= pkg_conflict_set_name(pc, name); status |= pkg_conflict_set_version(pc, version); - pm->conflict_list[pm->conflict_count++] = pc->name; + pm->conflict_list[pm->conflict_count - 1] = pc->name; pm->conflict_list[pm->conflict_count] = NULL; - return (status); + return (pc); } -int +struct pkg_depend * pkg_manifest_add_depend(struct pkg_manifest *pm, const char *name, const char *version, const char *origin) { @@ -274,36 +271,33 @@ pkg_error_msg("cant insert conflict without name into manifest.\n"); rage_quit(); } - - if (pm->depend_count % 10 == 0) { - list = pm->depend_list; - depends = pm->depend_entries; - pm->depend_list = realloc(list, sizeof(*list) * (pm->depend_count + 11)); - pm->depend_entries = realloc(depends, sizeof(*depends) * (pm->depend_count + 11)); - if (pm->depend_list == NULL || pm->depend_entries == NULL) { - pm->depend_list = list; - pm->depend_entries = depends; - return (PKG_MEMORY_ERR | PKG_NOT_OK); - } + + list = NULL; + depends = NULL; + PARALLEL_ARRAY_LIST_REALLOC(pm->depend_entries, depends, pm->depend_list, list, pm->depend_count, 10); + if (pm->depend_entries == NULL || pm->depend_list == NULL) { + pm->depend_entries = depends; + pm->depend_list = list; + return (NULL); } status = PKG_OK; - pd = &pm->depend_entries[pm->depend_count]; + pd = &pm->depend_entries[pm->depend_count - 1]; pkg_depend_init(pd); status |= pkg_depend_set_name(pd, name); status |= pkg_depend_set_origin(pd, origin); status |= pkg_depend_set_version(pd, version); - pm->depend_list[pm->depend_count++] = pd->name; + pm->depend_list[pm->depend_count - 1] = pd->name; pm->depend_list[pm->depend_count] = NULL; - return (status); + return (pd); } /* Add a file to a manifest. */ -int +struct pkg_file * pkg_manifest_add_file(struct pkg_manifest *pm, const char *path, const char *md5, const char *cwd, const char *mode, const char *owner, const char *group) { @@ -313,26 +307,22 @@ struct pkg_file *pf; if (path == NULL) { - /* Everything else has defaults. */ - pkg_error_msg(""); + pkg_error_msg("Cannot add file with empty path to manifest.\n"); rage_quit(); } - - if (pm->file_count % 10 == 0) { - list = pm->file_list; - files = pm->file_entries; - pm->file_list = realloc(list, sizeof(*list) * (pm->file_count + 11)); - pm->file_entries = realloc(files, sizeof(*files) * (pm->file_count + 11)); - if (pm->file_list == NULL || pm->file_entries == NULL) { - pm->file_list = list; - pm->file_entries = files; - return (PKG_MEMORY_ERR | PKG_NOT_OK); - } + + list = NULL; + files = NULL; + PARALLEL_ARRAY_LIST_REALLOC(pm->file_entries, files, pm->file_list, list, pm->file_count, 10); + if (pm->file_entries == NULL || pm->file_list == NULL) { + pm->file_entries = files; + pm->file_list = list; + return (NULL); } status = PKG_OK; - pf = &pm->file_entries[pm->file_count]; + pf = &pm->file_entries[pm->file_count - 1]; pkg_file_init(pf); status |= pkg_file_set_path(pf, path); status |= pkg_file_set_md5(pf, md5); @@ -341,10 +331,10 @@ status |= pkg_file_set_owner(pf, owner); status |= pkg_file_set_group(pf, group); - pm->file_list[pm->file_count++] = pf->path; + pm->file_list[pm->file_count - 1] = pf->path; pm->file_list[pm->file_count] = NULL; - return (status); + return (pf); } ==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_manifest.h#6 (text+ko) ==== @@ -125,13 +125,13 @@ int pkg_manifest_add_dirrm_cmd(struct pkg_manifest *pm, const char *dir); -int pkg_manifest_add_conflict(struct pkg_manifest *pm, const char *name, +struct pkg_conflict *pkg_manifest_add_conflict(struct pkg_manifest *pm, const char *name, const char *version); -int pkg_manifest_add_depend(struct pkg_manifest *pm, const char *name, +struct pkg_depend *pkg_manifest_add_depend(struct pkg_manifest *pm, const char *name, const char *version, const char *origin); -int pkg_manifest_add_file(struct pkg_manifest *pm, const char *path, const char *md5, +struct pkg_file *pkg_manifest_add_file(struct pkg_manifest *pm, const char *path, const char *md5, const char *cwd, const char *mode, const char *owner, const char *group); void pkg_manifest_remove_conflict(struct pkg_manifest *pm, const char *name); ==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_manifest_plist.c#7 (text+ko) ==== @@ -63,9 +63,9 @@ char *group; char *mode; - char *last_rel_conflict; - char *last_rel_depend; - char *last_rel_file; + struct pkg_conflict *last_conflict; + struct pkg_depend *last_depend; + struct pkg_file *last_file; }; static void parse_state_set_default(struct plist_parse_state *st); @@ -95,9 +95,9 @@ static void parse_state_reset_last_rel(struct plist_parse_state *st) { - st->last_rel_conflict = NULL; - st->last_rel_depend = NULL; - st->last_rel_file = NULL; + st->last_conflict = NULL; + st->last_depend = NULL; + st->last_file = NULL; } int @@ -212,8 +212,6 @@ char trail[FILENAME_MAX]; /* Last file and depend. */ - struct pkg_file *lpf; - struct pkg_depend *lpd; status = PKG_OK; argc = sscanf(line, "%s %[^\n]", cmd, argument); @@ -343,8 +341,7 @@ status = PKG_NOT_OK; break; } - status = pkg_manifest_add_depend(pm, isolate, NULL, NULL); - status |= pkg_util_strdup(isolate, &st->last_rel_depend); + st->last_depend = pkg_manifest_add_depend(pm, isolate, NULL, NULL); break; case (PM_CONFLICTS): if (strlen(isolate) == 0) { @@ -352,8 +349,7 @@ status = PKG_NOT_OK; break; } - status = pkg_manifest_add_conflict(pm, isolate, NULL); - status |= pkg_util_strdup(isolate, &st->last_rel_conflict); + st->last_conflict = pkg_manifest_add_conflict(pm, isolate, NULL); break; case (PM_COMMENT): if (strchr(argument, (int)':') != NULL) { @@ -363,7 +359,7 @@ } else if (sscanf(argument, "ORIGIN:%s %[^\n]", isolate, trail) >= 1) { status = pkg_util_strdup(isolate, &pm->origin); } else if (sscanf(argument, "DEPORIGIN:%s %[^\n]", isolate, trail) >= 1) { - if (st->last_rel_depend == NULL) { + if (st->last_depend == NULL) { pkg_error_msg("misplaced meta comment: deporigin."); status = PKG_NOT_OK; break; @@ -373,11 +369,10 @@ status = PKG_NOT_OK; break; } - lpd = pkg_manifest_select_depend(pm, st->last_rel_depend); - status = pkg_depend_set_origin(lpd, isolate); + status = pkg_depend_set_origin(st->last_depend, isolate); } else if (sscanf(argument, "MD5:%s %[^\n]", isolate, trail) >= 1) { - if (st->last_rel_file == NULL) { - pkg_error_msg("misplace meta comment: md5."); + if (st->last_file == NULL) { + pkg_error_msg("misplaced meta comment: md5."); status = PKG_NOT_OK; break; } @@ -386,21 +381,23 @@ status = PKG_NOT_OK; break; } - lpf = pkg_manifest_select_file(pm, st->last_rel_file); - status = pkg_file_set_md5(lpf, isolate); + status = pkg_file_set_md5(st->last_file, isolate); break; } /* else { unrecognized comment. } */ } break; case (PM_FILE): pkg_util_trim_newline(line); - status = pkg_manifest_add_file(pm, line, NULL, st->cwd, st->mode, + st->last_file = pkg_manifest_add_file(pm, line, NULL, st->cwd, st->mode, st->owner, st->group); - lpf = pkg_manifest_select_file(pm, line); - if (lpf != NULL) - status |= pkg_file_set_ignored(lpf); - st->ignore_next_file = 0; - status = pkg_util_strdup(line, &st->last_rel_file); + if (st->last_file == NULL) { + pkg_error_msg("plist parse: add file failed.\n"); + return (PKG_NOT_OK); + } + if (st->ignore_next_file != 0) { + status |= pkg_file_set_ignored(st->last_file); + st->ignore_next_file = 0; + } break; case (PM_UNKNOWN): default: ==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_util.c#18 (text+ko) ==== @@ -187,6 +187,8 @@ (group != NULL ? group : BAD_OR_UNKNOWN_VALUE)); fprintf(stream, "\t\t\tMODE: %s\n", (mode != NULL ? mode : BAD_OR_UNKNOWN_VALUE)); + fprintf(stream, "\t\t\tIGNORED: %s\n", + (pkg_pkg_file_ignored(p, file) ? "yes" : "no")); } fprintf(stream, "\tdepends:\n"); ==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_util.h#15 (text+ko) ==== @@ -2,6 +2,8 @@ #define __PKG_UTIL_H__ #include <dirent.h> +#include "pkg_error.h" +#include "pkg.h" void pkg_util_trim_newline(char *to_trim); @@ -17,10 +19,33 @@ void rage_quit(void); -#define APPEND_TO_ARRAY_AND_LIST(struct_type, entries, list, tmp_entries, tmp_list, current_count, increase) \ - tmp_list = list; \ - tmp_entries = entries; \ - list = realloc(tmp_list, sizeof(*tmp_list) * (current_count + increase)); \ - entries = realloc(tmp_entries, sizeof(tmp_entries) * (current_count + increase)); +#define STRING_LIST_APPEND(list, append, tmp_list, list_sz, factor) \ + if (list_sz % factor == 0) { \ + tmp_list = list; \ + list = realloc(tmp_list, sizeof(*tmp_list) * (list_sz + factor + 1)); \ + } \ + if (list != NULL) { \ + list[list_sz++] = strdup(append); \ + list[list_sz] = NULL; \ + } + +#define ARRAY_FACTOR_REALLOC(array, tmp_array, array_sz, factor) \ + if (array_sz % factor == 0) { \ + tmp_array = array; \ + array = realloc(tmp_array, sizeof(*tmp_array) * (array_sz + factor + 1)); \ + } \ + if (array != NULL) \ + array_sz++ + +#define PARALLEL_ARRAY_LIST_REALLOC(array, tmp_array, list, tmp_list, sz, factor) \ + if (sz % factor == 0) { \ + tmp_array = array; \ + tmp_list = list; \ + array = realloc(tmp_array, sizeof(*tmp_array) * (sz + factor + 1)); \ + list = realloc(tmp_list, sizeof(*tmp_list) * (sz + factor + 1)); \ + } \ + if (array != NULL && list != NULL) \ + sz++ + #endif ==== //depot/projects/soc2009/dforsyth_libpkg/pkg_info/main.c#32 (text+ko) ==== @@ -74,6 +74,7 @@ pkg_db_close(db); pkg_db_delete(db); + return (EXIT_SUCCESS); } @@ -306,8 +307,9 @@ klist = pkg_required_by(p); if (klist != NULL) { fprintf(out, "Required by:\n"); - for (; *klist != NULL; buff = *klist++) + for (buff = *klist; *klist != NULL; buff = *++klist) { printf("%s\n", buff); + } } printf("\n"); }help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200908170812.n7H8CQlY011410>
