From ee3094148f257521e65193c825b804fe871cc395 Mon Sep 17 00:00:00 2001 From: "mooredan@suncup.net" Date: Tue, 19 Oct 2010 01:40:18 +0000 Subject: [PATCH] Fix issue #56 : ghost folders rmdir a non-empty directory now fails as it should git-svn-id: http://s3fs.googlecode.com/svn/trunk@201 df820570-a93a-0410-bd06-b72b767a4274 --- s3fs/s3fs.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/s3fs/s3fs.cpp b/s3fs/s3fs.cpp index 30a63be..612935e 100644 --- a/s3fs/s3fs.cpp +++ b/s3fs/s3fs.cpp @@ -1073,8 +1073,47 @@ s3fs_unlink(const char *path) { static int s3fs_rmdir(const char *path) { - cout << "unlink[path=" << path << "]" << endl; + cout << "rmdir[path=" << path << "]" << endl; + + // need to check if the directory is empty + { + string responseText; + string resource = urlEncode(service_path + bucket); + string query = "delimiter=/&prefix="; + if (strcmp(path, "/") != 0) + query += urlEncode(string(path).substr(1) + "/"); + + query += "&max-keys=50"; + + string url = host + resource + "?"+ query; + + auto_curl curl; + string my_url = prepare_url(url.c_str()); + curl_easy_setopt(curl, CURLOPT_URL, my_url.c_str()); + curl_easy_setopt(curl, CURLOPT_FAILONERROR, true); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseText); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); + + auto_curl_slist headers; + string date = get_date(); + headers.append("Date: "+date); + headers.append("ContentType: "); + headers.append("Authorization: AWS "+AWSAccessKeyId+":"+calc_signature("GET", "", date, headers.get(), resource + "/")); + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers.get()); + + VERIFY(my_curl_easy_perform(curl.get())); + + cout << endl << responseText << endl; + if (responseText.find ("") != std::string::npos || responseText.find ("") != std::string::npos ) { + // directory is not empty + cout << "[path=" << path << "] not empty" << endl; + return -ENOTEMPTY; + } + } + // delete the directory string resource = urlEncode(service_path + bucket + path); string url = host + resource;