Commit 3818e3c42c1ac77dac0c571165dabfe2da05ff2b

Christian Weisgerber 2020-11-01T16:28:46

convert all remaining instances of chmod(2) to fchmod(2) ok stsp

diff --git a/lib/object_create.c b/lib/object_create.c
index a1f3f64..71e76e0 100644
--- a/lib/object_create.c
+++ b/lib/object_create.c
@@ -78,6 +78,11 @@ create_object_file(struct got_object_id *id, FILE *content,
 			goto done;
 	}
 
+	if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
+		err = got_error_from_errno2("fchmod", tmppath);
+		goto done;
+	}
+
 	err = got_deflate_to_file(&tmplen, content, tmpfile);
 	if (err)
 		goto done;
@@ -92,11 +97,6 @@ create_object_file(struct got_object_id *id, FILE *content,
 	}
 	free(tmppath);
 	tmppath = NULL;
-
-	if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
-		err = got_error_from_errno2("chmod", objpath);
-		goto done;
-	}
 done:
 	free(objpath);
 	if (tmppath) {
diff --git a/lib/reference.c b/lib/reference.c
index 2b33b7d..6df30b6 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -1140,17 +1140,17 @@ got_ref_write(struct got_reference *ref, struct got_repository *repo)
 		sb.st_mode = GOT_DEFAULT_FILE_MODE;
 	}
 
+	if (fchmod(fileno(f), sb.st_mode) != 0) {
+		err = got_error_from_errno2("fchmod", tmppath);
+		goto done;
+	}
+
 	if (rename(tmppath, path) != 0) {
 		err = got_error_from_errno3("rename", tmppath, path);
 		goto done;
 	}
 	free(tmppath);
 	tmppath = NULL;
-
-	if (chmod(path, sb.st_mode) != 0) {
-		err = got_error_from_errno2("chmod", path);
-		goto done;
-	}
 done:
 	if (ref->lf == NULL && lf)
 		unlock_err = got_lockfile_unlock(lf);
@@ -1276,23 +1276,22 @@ delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
 			goto done;
 		}
 
-		if (stat(packed_refs_path, &sb) != 0) {
+		if (fstat(fileno(f), &sb) != 0) {
 			if (errno != ENOENT) {
-				err = got_error_from_errno2("stat",
+				err = got_error_from_errno2("fstat",
 				    packed_refs_path);
 				goto done;
 			}
 			sb.st_mode = GOT_DEFAULT_FILE_MODE;
 		}
 
-		if (rename(tmppath, packed_refs_path) != 0) {
-			err = got_error_from_errno3("rename", tmppath,
-			    packed_refs_path);
+		if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
+			err = got_error_from_errno2("fchmod", tmppath);
 			goto done;
 		}
 
-		if (chmod(packed_refs_path, sb.st_mode) != 0) {
-			err = got_error_from_errno2("chmod",
+		if (rename(tmppath, packed_refs_path) != 0) {
+			err = got_error_from_errno3("rename", tmppath,
 			    packed_refs_path);
 			goto done;
 		}
diff --git a/lib/worktree.c b/lib/worktree.c
index 54a1c38..3d3b938 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -968,6 +968,11 @@ install_symlink_conflict(const char *deriv_target,
 	if (err)
 		goto done;
 
+	if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
+		err = got_error_from_errno2("fchmod", path);
+		goto done;
+	}
+
 	if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
 	    GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
 	    deriv_target ? deriv_target : "(symlink was deleted)",
@@ -989,10 +994,6 @@ install_symlink_conflict(const char *deriv_target,
 		err = got_error_from_errno3("rename", path, ondisk_path);
 		goto done;
 	}
-	if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
-		err = got_error_from_errno2("chmod", ondisk_path);
-		goto done;
-	}
 done:
 	if (f != NULL && fclose(f) == EOF && err == NULL)
 		err = got_error_from_errno2("fclose", path);
@@ -1511,6 +1512,12 @@ install_blob(struct got_worktree *worktree, const char *ondisk_path,
 			return got_error_from_errno2("open", ondisk_path);
 	}
 
+	if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
+		err = got_error_from_errno2("fchmod",
+		    update ? tmppath : ondisk_path);
+		goto done;
+	}
+
 	if (progress_cb) {
 		if (restoring_missing_file)
 			err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
@@ -1559,12 +1566,6 @@ install_blob(struct got_worktree *worktree, const char *ondisk_path,
 		}
 	}
 
-	if (chmod(ondisk_path,
-	    get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
-		err = got_error_from_errno2("chmod", ondisk_path);
-		goto done;
-	}
-
 done:
 	if (fd != -1 && close(fd) != 0 && err == NULL)
 		err = got_error_from_errno("close");
@@ -4332,8 +4333,8 @@ create_patched_content(char **path_outfile, int reverse_patch,
 			goto done;
 
 		if (!S_ISLNK(sb2.st_mode)) {
-			if (chmod(*path_outfile, sb2.st_mode) == -1) {
-				err = got_error_from_errno2("chmod", path2);
+			if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
+				err = got_error_from_errno2("fchmod", path2);
 				goto done;
 			}
 		}