Skip to content
Snippets Groups Projects
Commit 81f6f40b authored by Adrien Béraud's avatar Adrien Béraud Committed by Emmanuel Lepage Vallée
Browse files

fileutils: allow to set mode on file creation

Refs #76716

Change-Id: I71c5ec0b31dc791af94305a4ff19e62d2e846189
parent bf0c4bca
No related branches found
No related tags found
No related merge requests found
...@@ -234,7 +234,7 @@ loadFile(const std::string& path) ...@@ -234,7 +234,7 @@ loadFile(const std::string& path)
} }
void 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); std::ofstream file(path, std::ios::trunc | std::ios::binary);
if (!file.is_open()) { if (!file.is_open()) {
...@@ -242,6 +242,9 @@ saveFile(const std::string& path, const std::vector<uint8_t>& data) ...@@ -242,6 +242,9 @@ saveFile(const std::string& path, const std::vector<uint8_t>& data)
return; return;
} }
file.write((char*)data.data(), data.size()); file.write((char*)data.data(), data.size());
#ifndef _WIN32
chmod(path.c_str(), mode);
#endif
} }
static size_t static size_t
......
...@@ -45,9 +45,11 @@ ...@@ -45,9 +45,11 @@
#ifndef _WIN32 #ifndef _WIN32
#include <sys/stat.h> // mode_t
#define DIR_SEPARATOR_STR "/" // Directory separator char #define DIR_SEPARATOR_STR "/" // Directory separator char
#define DIR_SEPARATOR_CH '/' // Directory separator string #define DIR_SEPARATOR_CH '/' // Directory separator string
#else #else
#define mode_t unsigned
#define DIR_SEPARATOR_STR "\\" // Directory separator char #define DIR_SEPARATOR_STR "\\" // Directory separator char
#define DIR_SEPARATOR_CH '\\' // Directory separator string #define DIR_SEPARATOR_CH '\\' // Directory separator string
#endif #endif
...@@ -77,7 +79,7 @@ namespace ring { namespace fileutils { ...@@ -77,7 +79,7 @@ namespace ring { namespace fileutils {
std::vector<std::string> readDirectory(const std::string &dir); std::vector<std::string> readDirectory(const std::string &dir);
std::vector<uint8_t> loadFile(const std::string& path); 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 { struct FileHandle {
int fd; int fd;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment