From 81f6f40b3a0ab8b663fcb16ad31b754abf85ce17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Wed, 1 Jul 2015 00:29:59 -0400 Subject: [PATCH] fileutils: allow to set mode on file creation Refs #76716 Change-Id: I71c5ec0b31dc791af94305a4ff19e62d2e846189 --- src/fileutils.cpp | 5 ++++- src/fileutils.h | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/fileutils.cpp b/src/fileutils.cpp index 7b4f7d985b..6d6f21209a 100644 --- a/src/fileutils.cpp +++ b/src/fileutils.cpp @@ -234,7 +234,7 @@ loadFile(const std::string& path) } void -saveFile(const std::string& path, const std::vector<uint8_t>& data) +saveFile(const std::string& path, const std::vector<uint8_t>& data, mode_t mode) { std::ofstream file(path, std::ios::trunc | std::ios::binary); if (!file.is_open()) { @@ -242,6 +242,9 @@ saveFile(const std::string& path, const std::vector<uint8_t>& data) return; } file.write((char*)data.data(), data.size()); +#ifndef _WIN32 + chmod(path.c_str(), mode); +#endif } static size_t diff --git a/src/fileutils.h b/src/fileutils.h index bd8fc64f0e..df16ff56bc 100644 --- a/src/fileutils.h +++ b/src/fileutils.h @@ -45,10 +45,12 @@ #ifndef _WIN32 +#include <sys/stat.h> // mode_t #define DIR_SEPARATOR_STR "/" // Directory separator char -#define DIR_SEPARATOR_CH '/' // Directory separator string +#define DIR_SEPARATOR_CH '/' // Directory separator string #else -#define DIR_SEPARATOR_STR "\\" // Directory separator char +#define mode_t unsigned +#define DIR_SEPARATOR_STR "\\" // Directory separator char #define DIR_SEPARATOR_CH '\\' // Directory separator string #endif @@ -77,7 +79,7 @@ namespace ring { namespace fileutils { std::vector<std::string> readDirectory(const std::string &dir); std::vector<uint8_t> loadFile(const std::string& path); - void saveFile(const std::string& path, const std::vector<uint8_t>& data); + void saveFile(const std::string& path, const std::vector<uint8_t>& data, mode_t mode=755); struct FileHandle { int fd; -- GitLab