Check return value of chdir and getcwd

Prevents compiler warnings when building with -D_FORTIFY_SOURCE=2

Signed-off-by: Allan McRae <allan@archlinux.org>
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Allan McRae 2010-06-27 20:32:11 +10:00 committed by Dan McGee
parent 41724cbcde
commit 5a3aae02fe
3 changed files with 28 additions and 11 deletions

View File

@ -556,6 +556,7 @@ static int commit_single_pkg(pmpkg_t *newpkg, int pkg_current, int pkg_count,
struct archive *archive; struct archive *archive;
struct archive_entry *entry; struct archive_entry *entry;
char cwd[PATH_MAX] = ""; char cwd[PATH_MAX] = "";
int restore_cwd = 0;
_alpm_log(PM_LOG_DEBUG, "extracting files\n"); _alpm_log(PM_LOG_DEBUG, "extracting files\n");
@ -579,11 +580,16 @@ static int commit_single_pkg(pmpkg_t *newpkg, int pkg_current, int pkg_count,
/* save the cwd so we can restore it later */ /* save the cwd so we can restore it later */
if(getcwd(cwd, PATH_MAX) == NULL) { if(getcwd(cwd, PATH_MAX) == NULL) {
_alpm_log(PM_LOG_ERROR, _("could not get current working directory\n")); _alpm_log(PM_LOG_ERROR, _("could not get current working directory\n"));
cwd[0] = 0; } else {
restore_cwd = 1;
} }
/* libarchive requires this for extracting hard links */ /* libarchive requires this for extracting hard links */
chdir(handle->root); if(chdir(handle->root) != 0) {
_alpm_log(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), handle->root, strerror(errno));
ret = -1;
goto cleanup;
}
/* call PROGRESS once with 0 percent, as we sort-of skip that here */ /* call PROGRESS once with 0 percent, as we sort-of skip that here */
if(is_upgrade) { if(is_upgrade) {
@ -629,9 +635,9 @@ static int commit_single_pkg(pmpkg_t *newpkg, int pkg_current, int pkg_count,
} }
archive_read_finish(archive); archive_read_finish(archive);
/* restore the old cwd is we have it */ /* restore the old cwd if we have it */
if(strlen(cwd)) { if(restore_cwd && chdir(cwd) != 0) {
chdir(cwd); _alpm_log(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
} }
if(errors) { if(errors) {

View File

@ -372,8 +372,8 @@ int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int
cleanup: cleanup:
umask(oldmask); umask(oldmask);
archive_read_finish(_archive); archive_read_finish(_archive);
if(restore_cwd) { if(restore_cwd && chdir(cwd) != 0) {
chdir(cwd); _alpm_log(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
} }
return(ret); return(ret);
} }
@ -542,8 +542,8 @@ int _alpm_run_chroot(const char *root, const char *cmd)
} }
cleanup: cleanup:
if(restore_cwd) { if(restore_cwd && chdir(cwd) != 0) {
chdir(cwd); _alpm_log(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
} }
return(retval); return(retval);

View File

@ -676,6 +676,7 @@ int download_with_xfercommand(const char *url, const char *localpath,
struct stat st; struct stat st;
char *parsedcmd,*tempcmd; char *parsedcmd,*tempcmd;
char cwd[PATH_MAX]; char cwd[PATH_MAX];
int restore_cwd = 0;
char *destfile, *tempfile, *filename; char *destfile, *tempfile, *filename;
if(!config->xfercommand) { if(!config->xfercommand) {
@ -708,8 +709,14 @@ int download_with_xfercommand(const char *url, const char *localpath,
parsedcmd = strreplace(tempcmd, "%u", url); parsedcmd = strreplace(tempcmd, "%u", url);
free(tempcmd); free(tempcmd);
/* save the cwd so we can restore it later */
if(getcwd(cwd, PATH_MAX) == NULL) {
pm_printf(PM_LOG_ERROR, _("could not get current working directory\n"));
} else {
restore_cwd = 1;
}
/* cwd to the download directory */ /* cwd to the download directory */
getcwd(cwd, PATH_MAX);
if(chdir(localpath)) { if(chdir(localpath)) {
pm_printf(PM_LOG_WARNING, _("could not chdir to download directory %s\n"), localpath); pm_printf(PM_LOG_WARNING, _("could not chdir to download directory %s\n"), localpath);
ret = -1; ret = -1;
@ -736,7 +743,11 @@ int download_with_xfercommand(const char *url, const char *localpath,
} }
cleanup: cleanup:
chdir(cwd); /* restore the old cwd if we have it */
if(restore_cwd && chdir(cwd) != 0) {
pm_printf(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
}
if(ret == -1) { if(ret == -1) {
/* hack to let an user the time to cancel a download */ /* hack to let an user the time to cancel a download */
sleep(2); sleep(2);