diff --git a/src/IOCoordinator.cpp b/src/IOCoordinator.cpp index dead31eab..425751d15 100755 --- a/src/IOCoordinator.cpp +++ b/src/IOCoordinator.cpp @@ -505,32 +505,24 @@ void IOCoordinator::deleteMetaFile(const bf::path &file) void IOCoordinator::remove(const bf::path &p) { - if (bf::is_regular_file(p) && p.extension() == ".meta") + if (bf::is_directory(p)) { - deleteMetaFile(p); - return; - } - else if (bf::is_regular_file(p)) - { - logger->log(LOG_WARNING, "IOC::unlink(): deleting something that should be here: %s", p.string().c_str()); + bf::directory_iterator dend; + bf::directory_iterator entry(p); + while (entry != dend) + { + remove(p/(*entry)); + ++entry; + } bf::remove(p); return; } - - if (!bf::is_directory(p)) - { - logger->log(LOG_WARNING, "IOC::unlink(): Ignoring something that isn't a directory or a regular file: %s", - p.string().c_str()); - return; - } - bf::directory_iterator dend; - bf::directory_iterator entry(p); - while (entry != dend) - { - remove(p/(*entry)); - ++entry; - } + bf::path possibleMetaFile = p.string() + ".meta"; + if (bf::is_regular_file(possibleMetaFile)) + deleteMetaFile(possibleMetaFile); + else + bf::remove(p); } /* Need to rename this one. The corresponding fcn in IDBFileSystem specifies that it @@ -545,15 +537,13 @@ int IOCoordinator::unlink(const char *path) tell cache they were deleted tell synchronizer to delete them in cloud storage */ + + /* TODO! We need to make sure the input params to IOC fcns don't go up to parent dirs, + ex, if path = '../../../blahblah'. */ bf::path p(metaPath/path); - + try { - if (!bf::exists(p)) - { - errno = ENOENT; - return -1; - } remove(p); } catch (bf::filesystem_error &e) diff --git a/src/PosixTask.cpp b/src/PosixTask.cpp index 3496dad47..b80ef3f98 100644 --- a/src/PosixTask.cpp +++ b/src/PosixTask.cpp @@ -35,16 +35,16 @@ PosixTask::~PosixTask() void PosixTask::handleError(const char *name, int errCode) { SMLogging* logger = SMLogging::get(); - char buf[sizeof(sm_response) + 4]; + char buf[80]; // send an error response if possible sm_response *resp = (sm_response *) buf; resp->returnCode = -1; *((int *) resp->payload) = errCode; - write(*resp, 4); + bool success = write(*resp, 4); - // TODO: construct and log a message - logger->log(LOG_ERR,"%s caught an error: %s.",name,strerror_r(errCode, buf, 80)); + if (!success) + logger->log(LOG_ERR, "%s caught an error: %s.", name, strerror_r(errCode, buf, 80)); } uint PosixTask::getRemainingLength() diff --git a/src/unit_tests.cpp b/src/unit_tests.cpp index 8275c2535..6e0ab30ab 100755 --- a/src/unit_tests.cpp +++ b/src/unit_tests.cpp @@ -407,12 +407,14 @@ bool appendtask() bool unlinktask() { - // make a file and delete it + // make a meta file and delete it const char *filename = "unlinktest1"; - ::unlink(filename); - int fd = ::open(filename, O_CREAT | O_RDWR, 0666); - assert(fd > 0); - scoped_closer f(fd); + IOCoordinator *ioc = IOCoordinator::get(); + bf::path fullPath = ioc->getMetadataPath()/(string(filename) + ".meta"); + bf::remove(fullPath); + + MetadataFile meta(filename); + assert(bf::exists(fullPath)); uint8_t buf[1024]; unlink_cmd *cmd = (unlink_cmd *) buf; @@ -436,7 +438,35 @@ bool unlinktask() assert(resp->returnCode == 0); // confirm it no longer exists - assert(!boost::filesystem::exists(filename)); + assert(!bf::exists(fullPath)); + + // delete it again, make sure we get an error message & reasonable error code + // Interesting. boost::filesystem::remove() doesn't consider it an error if the file doesn't + // exist. Need to look into the reasoning for that, and decide whether IOC + // should return an error anyway. For now, this test below doesn't get + // an error msg. + #if 0 + memset(buf, 0, 1024); + cmd->opcode = UNLINK; + cmd->flen = strlen(filename); + memcpy(&cmd->filename, filename, cmd->flen); + + UnlinkTask u2(clientSock, sizeof(unlink_cmd) + cmd->flen); + ::write(sessionSock, cmd, sizeof(unlink_cmd) + cmd->flen); + u2.run(); + + // verify response + err = ::recv(sessionSock, buf, 1024, MSG_DONTWAIT); + resp = (sm_response *) buf; + assert(err == sizeof(*resp) + 4); + assert(resp->header.type == SM_MSG_START); + assert(resp->header.payloadLen == 8); + assert(resp->header.flags == 0); + assert(resp->returnCode == -1); + err = (*(int *) resp->payload); + assert(err == ENOENT); + #endif + cout << "unlink task OK" << endl; } @@ -696,7 +726,7 @@ bool listdirtask() { listdir_resp_entry *e = (listdir_resp_entry *) &buf[off]; //cout << "len = " << e->flen << endl; - assert(off + e->flen + sizeof(listdir_resp_entry) < 8092); + assert(off + e->flen + sizeof(listdir_resp_entry) < 8192); string file(e->filename, e->flen); assert(files.find((tmpPath/file).string()) != files.end()); fileCounter++; @@ -1196,6 +1226,12 @@ void IOCReadTest1() cout << "IOC read test 1 OK" << endl; } +void IOCUnlink() +{ + cout << "IOCUnlink not implmemented yet" << endl; +} + + int main() { std::size_t sizeKB = 1024; @@ -1238,6 +1274,7 @@ int main() s3storageTest1(); IOCReadTest1(); IOCTruncate(); + IOCUnlink(); return 0; }