Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

85 lines (79 sloc) 2.333 kb
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#include <sstream>
#include "utils.h"
#include "yadfs_exception.h"
std::string check_create_state_directory(const std::string& server) {
// replaces : and / with _
std::string tmp(server);
for (size_t i(0); i<server.size(); ++i)
tmp[i] = (server[i]==':' || server[i]=='/')?'_':server[i];
std::string home_dir(getpwuid(getuid())->pw_dir);
// create ~/.yadfs if it doesn't exist
std::string yadfs_dir(home_dir + "/.yadfs");
int err=(mkdir(yadfs_dir.c_str(), S_IRWXU)?errno:0);
if (EEXIST==err) {
struct stat s;
int err2=stat(yadfs_dir.c_str(), &s);
if (!err2 && S_ISDIR(s.st_mode)) err=0;
}
if (err) {
std::string error_message("Can't create ~/.yadfs directory: ");
error_message += strerror(err);
throw yadfs_exception(error_message);
}
// create state dir for server
std::string state_dir(yadfs_dir + "/" + tmp);
err=(mkdir(state_dir.c_str(), S_IRWXU)?errno:0);
if (EEXIST==err) {
struct stat s;
int err2=stat(state_dir.c_str(), &s);
if (!err2 && S_ISDIR(s.st_mode)) err=0;
}
if (err) {
std::string error_message("Can't create state directory: ");
error_message += strerror(err);
throw yadfs_exception(error_message);
}
return state_dir;
}
static inline bool is_unsafe_in_url(uint8_t c) {
return ((c<=47) || (c>=58 && c<=64) || (c>=91 && c<=96) || (c>=123 && c<=126));
}
std::string url_encode(const std::string& s) {
std::stringstream tmp;
for (size_t c(0); c<s.size(); ++c) {
if (is_unsafe_in_url(s[c])) {
if (s[c]<=0xF) tmp << "%0" << std::hex << (unsigned int) s[c] << std::dec;
else tmp << "%" << std::hex << (unsigned int) s[c] << std::dec;
}
else tmp << s[c];
}
return tmp.str();
}
std::string url_decode(const std::string& s) {
std::stringstream tmp;
for (size_t c(0); c<s.size(); ++c) {
if (s[c]=='%') {
char hi(s[++c]);
char lo(s[++c]);
if (hi>='a' && hi<='f') hi=(hi-'a')+10;
else if (hi>='A' && hi<='F') hi=(hi-'A')+10;
if (lo>='a' && lo<='f') lo=(lo-'a')+10;
else if (lo>='A' && lo<='F') lo=(lo-'A')+10;
tmp << char(lo|(hi<<4));
}
else tmp << s[c];
}
return tmp.str();
}
Jump to Line
Something went wrong with that request. Please try again.