diff options
author | graham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358> | 2009-11-16 20:37:34 (EST) |
---|---|---|
committer | graham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358> | 2009-11-16 20:37:34 (EST) |
commit | 6973ec4786b4cab702c460ed5db45a7c0cd5de72 (patch) | |
tree | d4e66a30fa82b344f9cb436873ae8545449954a1 | |
parent | bc43fa87c22eef3c77947711a3a0b049dfabf4ea (diff) |
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
-rw-r--r-- | libopkg/pkg.c | 50 |
1 files changed, 36 insertions, 14 deletions
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 */ |