osd_file::error osd_get_full_path(std::string &dst, std::string const &path) { try { #if defined(WIN32) std::vector<char> path_buffer(MAX_PATH); if (::_fullpath(&path_buffer[0], path.c_str(), MAX_PATH)) { dst = &path_buffer[0]; return osd_file::error::NONE; } else { return osd_file::error::FAILURE; } #else std::unique_ptr<char, void (*)(void *)> canonical(::realpath(path.c_str(), nullptr), &std::free); if (canonical) { dst = canonical.get(); return osd_file::error::NONE; } std::vector<char> path_buffer(PATH_MAX); if (::realpath(path.c_str(), &path_buffer[0])) { dst = &path_buffer[0]; return osd_file::error::NONE; } else if (path[0] == PATHSEPCH) { dst = path; return osd_file::error::NONE; } else { while (!::getcwd(&path_buffer[0], path_buffer.size())) { if (errno != ERANGE) return errno_to_file_error(errno); else path_buffer.resize(path_buffer.size() * 2); } dst.assign(&path_buffer[0]).push_back(PATHSEPCH); dst.append(path); return osd_file::error::NONE; } #endif } catch (...) { return osd_file::error::OUT_OF_MEMORY; } }
void temp_fstream::open (std::ios_base::openmode mode) { close(); const char* tmpdir = getenv("TMPDIR"); size_t tmpdir_len = tmpdir ? std::strlen(tmpdir) : 0; if (tmpdir_len == 0 || tmpdir_len > 4096) { // no $TMPDIR or it's excessively long => fall back to /tmp tmpdir = "/tmp"; tmpdir_len = 4; } std::vector<char> path_buffer(tmpdir_len + 18); char* path = &path_buffer[0]; std::strcpy(path, tmpdir); std::strcpy(path + tmpdir_len, "/git-crypt.XXXXXX"); mode_t old_umask = umask(0077); int fd = mkstemp(path); if (fd == -1) { int mkstemp_errno = errno; umask(old_umask); throw System_error("mkstemp", "", mkstemp_errno); } umask(old_umask); std::fstream::open(path, mode); if (!std::fstream::is_open()) { unlink(path); ::close(fd); throw System_error("std::fstream::open", path, 0); } unlink(path); ::close(fd); }
std::string absolutePath(const std::string& relative_path) { #ifdef WIN32 // Entirely derived from LLVM's FileManager source (see getCanonicalName). llvm::SmallString<PATH_MAX> path_buffer(relative_path); llvm::sys::fs::make_absolute(path_buffer); llvm::sys::path::native(path_buffer); llvm::sys::path::remove_dots(path_buffer, true); return path_buffer.str().str(); #else char resolved_path[PATH_MAX]; realpath(relative_path.c_str(), resolved_path); return std::string(resolved_path); #endif }
// "/some/path" => "/some/path0000012" bool zk::create_seq(const string& path, string& seqfile) { scoped_lock lk(m_); string path_buffer(path.size() + 16, '\0'); int rc = zoo_create(zh_, path.c_str(), NULL, 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL | ZOO_SEQUENCE, &path_buffer[0], path.size() + 16); seqfile = ""; if (rc != ZOK) { LOG(ERROR) << "failed to create: " << path << " - " << zerror(rc); return false; } else { seqfile = path_buffer; DLOG(INFO) << __func__ << " " << seqfile; return true; } }