CoolProp 8.0.0
An open-source fluid property and humid air property database
Macros | Functions
CPfilepaths.cpp File Reference
#include "CoolProp/detail/PlatformDetermination.h"
#include "CoolProp/Exceptions.h"
#include "CoolProp/detail/filepaths.h"
#include "CoolProp/detail/strings.h"
#include "CoolProp/detail/tools.h"
#include <fstream>
#include <algorithm>
#include <atomic>
#include <cerrno>
#include <filesystem>
#include <random>
#include <sstream>
#include <stdexcept>
#include <system_error>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>

Go to the source code of this file.

Macros

#define NOMINMAX
 

Functions

unsigned long long CalculateDirSize (const std::string &path)
 
std::vector< char > get_binary_file_contents (const char *filename)
 Get all the contents of a binary file. More...
 
void write_bytes_atomic (const std::filesystem::path &target, const void *bytes, std::size_t size, bool restrict_perms)
 
void make_dirs (const std::string &file_path)
 Make directory and all required intermediate directories. More...
 
std::string get_separator ()
 Get directory separator. More...
 
std::string get_home_dir ()
 Get the user's home directory; It is believed that is is always a place that files can be written. More...
 
bool path_exists (const std::string &path)
 Return true if path exists. More...
 
std::string join_path (const std::string &one, const std::string &two)
 Return merged path, append separator if string two is empty. More...
 
std::string get_file_contents (const char *filename)
 

Macro Definition Documentation

◆ NOMINMAX

#define NOMINMAX

Definition at line 19 of file CPfilepaths.cpp.

Function Documentation

◆ CalculateDirSize()

unsigned long long CalculateDirSize ( const std::string &  path)

Get the size of a directory in bytes. Uses std::filesystem on modern platforms (Windows, Linux, macOS). Legacy Android/powerpc stub returns 0.

Definition at line 41 of file CPfilepaths.cpp.

◆ get_binary_file_contents()

std::vector< char > get_binary_file_contents ( const char *  filename)

Get all the contents of a binary file.

Definition at line 87 of file CPfilepaths.cpp.

◆ get_file_contents()

std::string get_file_contents ( const char *  filename)

Definition at line 319 of file CPfilepaths.cpp.

◆ get_home_dir()

std::string get_home_dir ( )

Get the user's home directory; It is believed that is is always a place that files can be written.

Definition at line 226 of file CPfilepaths.cpp.

◆ get_separator()

std::string get_separator ( )

Get directory separator.

Definition at line 206 of file CPfilepaths.cpp.

◆ join_path()

std::string join_path ( const std::string &  one,
const std::string &  two 
)

Return merged path, append separator if string two is empty.

Definition at line 299 of file CPfilepaths.cpp.

◆ make_dirs()

void make_dirs ( const std::string &  file_path)

Make directory and all required intermediate directories.

Definition at line 167 of file CPfilepaths.cpp.

◆ path_exists()

bool path_exists ( const std::string &  path)

Return true if path exists.

Definition at line 270 of file CPfilepaths.cpp.

◆ write_bytes_atomic()

void write_bytes_atomic ( const std::filesystem::path &  target,
const void *  bytes,
std::size_t  size,
bool  restrict_perms = false 
)

Atomically write size bytes from bytes to target.

Writes to a sibling <target>.tmp.<process-salt>.<seq> first, then std::filesystem::renames onto the target. The rename is atomic on POSIX same-filesystem and on Win32 (replace-existing semantics), so concurrent writers in different processes / threads either see the previous complete file or one complete writer's payload — never a partial-write file on the visible path.

The temp-path suffix combines a process-unique 64-bit salt (drawn once per process from std::random_device) with a process-local atomic counter, so concurrent writers never collide on their intermediates.

When restrict_perms is true, the temp file is chmod'd to owner-only (0600) before the rename, so the post-rename file inherits the tight permissions without a brief world-readable window. No-op on Windows where the POSIX permission model doesn't apply.

Throws std::runtime_error on open/write/rename failure; the temp file is unlinked before throwing. Assumes target.parent_path() already exists.

Definition at line 132 of file CPfilepaths.cpp.