From 6973ec4786b4cab702c460ed5db45a7c0cd5de72 Mon Sep 17 00:00:00 2001 From: graham.gower Date: Mon, 16 Nov 2009 20:37:34 -0500 Subject: Use the filename arg as a base for the temp file. Clean up function while here. git-svn-id: http://opkg.googlecode.com/svn/trunk@323 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- (limited to 'libopkg') diff --git a/libopkg/pkg.c b/libopkg/pkg.c index b0c2b69..0c0a7d9 100644 --- a/libopkg/pkg.c +++ b/libopkg/pkg.c @@ -315,26 +315,48 @@ void pkg_deinit(pkg_t *pkg) pkg->tags = NULL; } -int pkg_init_from_file(pkg_t *pkg, const char *filename) +int +pkg_init_from_file(pkg_t *pkg, const char *filename) { - int err; - FILE *control_file; + int fd, err = 0; + FILE *control_file; + char *control_path; - err = pkg_init(pkg); - if (err) { return err; } + pkg_init(pkg); - pkg->local_filename = xstrdup(filename); - - control_file = tmpfile(); - err = pkg_extract_control_file_to_stream(pkg, control_file); - if (err) { return err; } + pkg->local_filename = xstrdup(filename); + + sprintf_alloc(&control_path, "%s.control.XXXXXX", filename); + fd = mkstemp(control_path); + if (fd == -1) { + perror_msg("%s: mkstemp(%s)", __FUNCTION__, control_path); + err = -1; + goto err0; + } - rewind(control_file); - pkg_parse_from_stream(pkg, control_file, PFM_ALL); + control_file = fdopen(fd, "rw"); + if (control_file == NULL) { + perror_msg("%s: fdopen", __FUNCTION__, control_path); + close(fd); + err = -1; + goto err1; + } - fclose(control_file); + err = pkg_extract_control_file_to_stream(pkg, control_file); + if (err) + goto err2; - return 0; + rewind(control_file); + pkg_parse_from_stream(pkg, control_file, PFM_ALL); + +err2: + fclose(control_file); +err1: + unlink(control_path); +err0: + free(control_path); + + return err; } /* Merge any new information in newpkg into oldpkg */ -- cgit v0.9.1