#include "cion/common/util/uuid.hh"
#include <aul.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
#include <uuid/uuid.h>
#include <fstream>
namespace {
-const char kUuidFileName[] = ".cion_uuid";
+const char kCionDataDir[] = ".cion";
+const char kUuidFilePath[] = ".cion/cion_uuid";
std::string GetAppDataPath() {
// This API will work only if the caller is an app.
return aul_get_app_data_path();
}
+bool IsExist(const std::string &path)
+{
+ int fd = open(path.c_str(), O_RDONLY);
+ if (fd < 0)
+ return false;
+ struct stat sb;
+ bool result = (fstat(fd, &sb) == 0);
+ close(fd);
+ return result;
+}
+
+bool CreateDir(const std::string& path) {
+ return (mkdir(path.c_str(), 0700) == 0);
+}
+
} // namespace
namespace cion {
if (data_dir.empty())
return {};
- return data_dir + kUuidFileName;
+ return data_dir + kUuidFilePath;
}
bool Uuid::GetUUID() {
if (data_dir.empty())
return false;
+ std::string cion_dir = data_dir + kCionDataDir;
+ if (!IsExist(cion_dir)) {
+ if (!CreateDir(cion_dir))
+ return false;
+ }
+
std::ifstream ifs;
ifs.open(GetUuidPath());
if (!ifs.is_open())