cleanup HTTP DELETE operations to use the same curl interface

git-svn-id: http://s3fs.googlecode.com/svn/trunk@381 df820570-a93a-0410-bd06-b72b767a4274
This commit is contained in:
ben.lemasurier@gmail.com 2011-08-31 22:20:20 +00:00
parent 9fb05fba4f
commit 79ee801b94
4 changed files with 61 additions and 70 deletions

View File

@ -40,6 +40,7 @@
#include <map>
#include "curl.h"
#include "string_util.h"
using namespace std;
@ -48,6 +49,21 @@ std::map<CURL*, time_t> curl_times;
std::map<CURL*, progress_t> 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
*/

View File

@ -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);

View File

@ -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) {

View File

@ -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;