diff --git a/src/curl.cpp b/src/curl.cpp index 6394c12..a15ffde 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -40,6 +40,7 @@ #include #include "curl.h" +#include "string_util.h" using namespace std; @@ -48,6 +49,21 @@ std::map curl_times; std::map curl_progress; std::string curl_ca_bundle; +class auto_curl_slist { + public: + auto_curl_slist() : slist(0) { } + ~auto_curl_slist() { curl_slist_free_all(slist); } + + struct curl_slist* get() const { return slist; } + + void append(const string& s) { + slist = curl_slist_append(slist, s.c_str()); + } + + private: + struct curl_slist* slist; +}; + CURL *create_curl_handle(void) { time_t now; CURL *curl_handle; @@ -88,6 +104,36 @@ void destroy_curl_handle(CURL *curl_handle) { return; } +int curl_delete(const char *path) { + int result; + string date; + string url; + string my_url; + string resource; + auto_curl_slist headers; + CURL *curl = NULL; + + resource = urlEncode(service_path + bucket + path); + url = host + resource; + date = get_date(); + + headers.append("Date: " + date); + headers.append("Content-Type: "); + if(public_bucket.substr(0,1) != "1") + headers.append("Authorization: AWS " + AWSAccessKeyId + ":" + + calc_signature("DELETE", "", date, headers.get(), resource)); + + my_url = prepare_url(url.c_str()); + curl = create_curl_handle(); + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers.get()); + curl_easy_setopt(curl, CURLOPT_URL, my_url.c_str()); + result = my_curl_easy_perform(curl); + destroy_curl_handle(curl); + + return result; +} + /** * @return fuse return code */ diff --git a/src/curl.h b/src/curl.h index 84891c3..5d327d9 100644 --- a/src/curl.h +++ b/src/curl.h @@ -21,12 +21,18 @@ extern time_t readwrite_timeout; extern bool debug; extern std::string program_name; extern std::string ssl_verify_hostname; +extern std::string AWSAccessKeyId; extern std::string AWSSecretAccessKey; +extern std::string service_path; +extern std::string host; +extern std::string bucket; +extern std::string public_bucket; static const EVP_MD* evp_md = EVP_sha1(); CURL *create_curl_handle(void); void destroy_curl_handle(CURL *curl_handle); +int curl_delete(const char *path); int my_curl_easy_perform(CURL* curl, BodyStruct* body = NULL, FILE* f = 0); size_t WriteMemoryCallback(void *ptr, size_t blockSize, size_t numBlocks, void *data); size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp); diff --git a/src/s3fs.cpp b/src/s3fs.cpp index 9ae6bda..19eee59 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -1716,48 +1716,20 @@ static int s3fs_mkdir(const char *path, mode_t mode) { return 0; } -// aka rm static int s3fs_unlink(const char *path) { int result; - string date; - string url; - string my_url; - string resource; char *s3_realpath; - auto_curl_slist headers; - CURL *curl = NULL; if(foreground) - cout << "unlink[path=" << path << "]" << endl; + printf("unlink[path=%s]\n", path); s3_realpath = get_realpath(path); - resource = urlEncode(service_path + bucket + s3_realpath); - url = host + resource; - date = get_date(); - headers.append("Date: " + date); - headers.append("Content-Type: "); - if(public_bucket.substr(0,1) != "1") - headers.append("Authorization: AWS " + AWSAccessKeyId + ":" + - calc_signature("DELETE", "", date, headers.get(), resource)); - - my_url = prepare_url(url.c_str()); - curl = create_curl_handle(); - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers.get()); - curl_easy_setopt(curl, CURLOPT_URL, my_url.c_str()); - - result = my_curl_easy_perform(curl); - - destroy_curl_handle(curl); + result = curl_delete(s3_realpath); free(s3_realpath); - - if(result != 0) - return result; - delete_stat_cache_entry(path); - return 0; + return result; } static int directory_empty(const char *path) { @@ -1823,57 +1795,24 @@ static int directory_empty(const char *path) { } static int s3fs_rmdir(const char *path) { - CURL *curl = NULL; - CURL *curl_handle = NULL; int result; char *s3_realpath; - struct BodyStruct body; if(foreground) printf("s3fs_rmdir [path=%s]\n", path); s3_realpath = get_realpath(path); - body.text = (char *)malloc(1); - body.size = 0; // directory must be empty if(directory_empty(path) != 0) return -ENOTEMPTY; - // delete the directory - string resource = urlEncode(service_path + bucket + s3_realpath); - string url = host + resource; + result = curl_delete(s3_realpath); - curl_handle = create_curl_handle(); - curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "DELETE"); - - auto_curl_slist headers; - string date = get_date(); - headers.append("Date: " + date); - headers.append("Content-Type: "); - if (public_bucket.substr(0,1) != "1") { - headers.append("Authorization: AWS " + AWSAccessKeyId + ":" + - calc_signature("DELETE", "", date, headers.get(), resource)); - } - curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers.get()); - - string my_url = prepare_url(url.c_str()); - curl_easy_setopt(curl_handle, CURLOPT_URL, my_url.c_str()); - - result = my_curl_easy_perform(curl_handle); - - // delete cache entry delete_stat_cache_entry(path); - - if(body.text) free(body.text); free(s3_realpath); - destroy_curl_handle(curl); - destroy_curl_handle(curl_handle); - if(result != 0) - return result; - - return 0; + return result; } static int s3fs_symlink(const char *from, const char *to) { diff --git a/src/s3fs.h b/src/s3fs.h index 746e0a3..c2bef4d 100644 --- a/src/s3fs.h +++ b/src/s3fs.h @@ -35,13 +35,13 @@ bool debug = 0; bool foreground = 0; bool nomultipart = false; bool service_validated = false; -static std::string host = "http://s3.amazonaws.com"; -static std::string service_path = "/"; +std::string host = "http://s3.amazonaws.com"; +std::string service_path = "/"; std::string bucket = ""; std::string mount_prefix = ""; static std::string mountpoint; std::string program_name; -static std::string AWSAccessKeyId; +std::string AWSAccessKeyId; std::string AWSSecretAccessKey; static mode_t root_mode = 0; static std::string passwd_file = ""; @@ -52,7 +52,7 @@ unsigned long max_stat_cache_size = 10000; static std::string use_cache; static std::string use_rrs; std::string ssl_verify_hostname = "1"; -static std::string public_bucket; +std::string public_bucket; extern pthread_mutex_t stat_cache_lock; extern pthread_mutex_t curl_handles_lock;