Derecho  0.9
Distributed systems toolkit for RDMA
util.hpp
Go to the documentation of this file.
1 #ifndef PERSISTENT_UTIL_HPP
2 #define PERSISTENT_UTIL_HPP
3 
4 #include "../PersistException.hpp"
5 #include <derecho/conf/conf.hpp>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <string>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 
13 #define MAX(a, b) \
14  ({ __typeof__ (a) _a = (a); \
15  __typeof__ (b) _b = (b); \
16  _a > _b ? _a : _b; })
17 
18 #define MIN(a, b) \
19  ({ __typeof__ (a) _a = (a); \
20  __typeof__ (b) _b = (b); \
21  _a < _b ? _a : _b; })
22 
23 #define HIGH__int128(x) (*((uint64_t*)((uint64_t)(&(x)) + 8)))
24 #define LOW__int128(x) (*((uint64_t*)&(x)))
25 
26 //Persistent folder:
27 // #define DEFAULT_FILE_PERSIST_PATH (".plog")
28 // #define DEFAULT_RAMDISK_PATH ("/dev/shm/volatile_t")
29 inline std::string getPersRamdiskPath() {
31  std::stringstream pid_ss;
32  pid_ss << getpid();
33  return path + pid_ss.str();
34 }
35 
36 inline std::string getPersFilePath() {
37  return std::string(derecho::getConfString(CONF_PERS_FILE_PATH));
38 }
39 
40 // verify the existence of a folder
41 // Check if directory exists or not. Create it on absence.
42 // return error if creating failed
43 inline void checkOrCreateDir(const std::string& dirPath) noexcept(false) {
44  struct stat sb;
45  if(stat(dirPath.c_str(), &sb) == 0) {
46  if(!S_ISDIR(sb.st_mode)) {
48  }
49  } else {
50  // create it
51  if(mkdir(dirPath.c_str(), 0700) != 0) {
52  throw PERSIST_EXP_CREATE_PATH(errno);
53  }
54  }
55 }
56 
57 // verify the existence of a regular file
58 inline bool checkRegularFile(const std::string& file) noexcept(false) {
59  struct stat sb;
60  bool bRet = true;
61 
62  if(stat(file.c_str(), &sb) == 0) {
63  if(!S_ISREG(sb.st_mode)) {
65  }
66  } else {
67  bRet = false;
68  }
69  return bRet;
70 }
71 
72 // verify the existence of a sparse file
73 // Check if directory exists or not. Create it on absence.
74 // return error if creating failed
75 inline bool checkOrCreateFileWithSize(const std::string& file, uint64_t size) noexcept(false) {
76  bool bCreate = false;
77  int fd;
78 
79  bCreate = !checkRegularFile(file);
80 
81  fd = open(file.c_str(), O_RDWR | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP | S_IROTH);
82  if(fd < 0) {
83  throw PERSIST_EXP_CREATE_FILE(errno);
84  }
85 
86  if(ftruncate(fd, size) != 0) {
87  throw PERSIST_EXP_TRUNCATE_FILE(errno);
88  }
89  close(fd);
90  return bCreate;
91 }
92 
93 #endif //UTIL_PERSISTENT_HPP
#define PERSIST_EXP_TRUNCATE_FILE(x)
bool checkRegularFile(const std::string &file) noexcept(false)
Definition: util.hpp:58
const std::string & getConfString(const std::string &key)
Definition: conf.cpp:110
#define PERSIST_EXP_INV_PATH
#define PERSIST_EXP_INV_FILE
#define PERSIST_EXP_CREATE_FILE(x)
std::string getPersFilePath()
Definition: util.hpp:36
bool checkOrCreateFileWithSize(const std::string &file, uint64_t size) noexcept(false)
Definition: util.hpp:75
void checkOrCreateDir(const std::string &dirPath) noexcept(false)
Definition: util.hpp:43
#define PERSIST_EXP_CREATE_PATH(x)
std::string getPersRamdiskPath()
Definition: util.hpp:29
#define CONF_PERS_RAMDISK_PATH
Definition: conf.hpp:50
#define CONF_PERS_FILE_PATH
Definition: conf.hpp:49