blob: 63080a42d444dd72f1747661b92e29f782d34bd7 [file] [log] [blame]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -05001From 682fb48c137b687477008b68863c2a0b73ed47d1 Mon Sep 17 00:00:00 2001
Patrick Williamsc0f7c042017-02-23 20:41:17 -06002From: Fabio Berton <fabio.berton@ossystems.com.br>
3Date: Fri, 9 Sep 2016 16:00:42 -0300
4Subject: [PATCH] handle read-only files
Patrick Williamsc0f7c042017-02-23 20:41:17 -06005
6Patch from:
7https://github.com/darealshinji/patchelf/commit/40e66392bc4b96e9b4eda496827d26348a503509
8
Brad Bishopc342db32019-05-15 21:57:59 -04009Upstream-Status: Denied [https://github.com/NixOS/patchelf/pull/89]
Patrick Williamsc0f7c042017-02-23 20:41:17 -060010
11Signed-off-by: Fabio Berton <fabio.berton@ossystems.com.br>
Brad Bishopc342db32019-05-15 21:57:59 -040012
Patrick Williamsc0f7c042017-02-23 20:41:17 -060013---
14 src/patchelf.cc | 16 +++++++++++++++-
15 1 file changed, 15 insertions(+), 1 deletion(-)
16
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050017diff --git a/src/patchelf.cc b/src/patchelf.cc
18index fd1e7b7..a941da1 100644
19--- a/src/patchelf.cc
20+++ b/src/patchelf.cc
21@@ -527,9 +527,19 @@ void ElfFile<ElfFileParamNames>::sortShdrs()
Patrick Williamsc0f7c042017-02-23 20:41:17 -060022
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050023 static void writeFile(const std::string & fileName, const FileContents & contents)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060024 {
Patrick Williamsc0f7c042017-02-23 20:41:17 -060025+ struct stat st;
26+ int fd;
27+
Andrew Geisslerd25ed322020-06-27 00:28:28 -050028 debug("writing %s\n", fileName.c_str());
29
30- int fd = open(fileName.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0777);
Patrick Williamsc0f7c042017-02-23 20:41:17 -060031+ if (stat(fileName.c_str(), &st) != 0)
32+ error("stat");
33+
34+ if (chmod(fileName.c_str(), 0600) != 0)
35+ error("chmod");
36+
Andrew Geisslerd25ed322020-06-27 00:28:28 -050037+ fd = open(fileName.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0777);
Patrick Williamsc0f7c042017-02-23 20:41:17 -060038+
39 if (fd == -1)
40 error("open");
41
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050042@@ -543,6 +553,10 @@ static void writeFile(const std::string & fileName, const FileContents & content
Patrick Williamsc0f7c042017-02-23 20:41:17 -060043
44 if (close(fd) != 0)
45 error("close");
46+
47+ if (chmod(fileName.c_str(), st.st_mode) != 0)
48+ error("chmod");
49+
50 }
51
52