ReturnStatus FileMetaDAO::updateFile(const std::string &path, const FileMeta &meta, FileMeta *pMeta, int *pDelta) { Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId); NameSpace *DataNS = pDataChannel->m_DataNS; int rt = 0; Args fd; ReturnStatus rs; rs = isfile(path); if (!rs.success()) { return rs; } fd = DataNS->Open(path.c_str(), O_RDWR); if (-1 == rt) { ERROR_LOG("path %s, open() error, %s.", path.c_str(), strerror(errno)); // already check path errors in isfile() return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } // check file version FileAttr attr; rt = DataNS->readn(&fd, &attr, sizeof(attr)); if (sizeof(attr) != rt) { ERROR_LOG("readn() error"); DataNS->Close(&fd); return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } if (meta.m_Attr.m_Version != attr.m_Version + 1) { ERROR_LOG("version outdated, current version %" PRIu64 ", " "received version %" PRIu64, attr.m_Version, meta.m_Attr.m_Version); DataNS->Close(&fd); pMeta->m_Attr = attr; return ReturnStatus(MU_FAILED, VERSION_OUTDATED); } // write metadata rs = writeFileMeta(&fd, meta); DataNS->Close(&fd);; // return file size delta *pDelta = meta.m_Attr.m_Size - attr.m_Size; return rs; }
ReturnStatus FileMetaDAO::movFile(const std::string &srcPath, const std::string &destPath) { Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId); NameSpace *DataNS = pDataChannel->m_DataNS; int rt = 0; int error = 0; ReturnStatus rs; // check src path rs = isfile(srcPath); if (!rs.success()) { if (IS_DIRECTORY == rs.errorCode) { return ReturnStatus(MU_FAILED, SRC_PATH_IS_DIRECTORY); } else if (PATH_INVALID == rs.errorCode) { return ReturnStatus(MU_FAILED, SRC_PATH_INVALID); } else if (PATH_NOT_EXIST == rs.errorCode) { return ReturnStatus(MU_FAILED, SRC_PATH_NOT_EXIST); } return rs; } // check dest path rs = isdir(prefix(destPath)); if (!rs.success()) { if (MU_UNKNOWN_ERROR != rs.errorCode) { return ReturnStatus(MU_FAILED, DEST_PATH_INVALID); } return rs; } rt = DataNS->Move(srcPath.c_str(), destPath.c_str()); if (-1 == rt) { error = errno; ERROR_LOG("src path %s, dest path %s, rename() error, %s.", srcPath.c_str(), destPath.c_str(), strerror(error)); if (EISDIR == error) { return ReturnStatus(MU_FAILED, DEST_PATH_IS_DIRECTORY); } else { return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } } return ReturnStatus(MU_SUCCESS); }
bool WrapperOwner::ok(JSContext* cx, const ReturnStatus& status, ObjectOpResult& result) { if (status.type() == ReturnStatus::TReturnObjectOpResult) return result.fail(status.get_ReturnObjectOpResult().code()); if (!ok(cx, status)) return false; return result.succeed(); }
ReturnStatus FileMetaDAO::delFile(const std::string &path, int *pDelta) { Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId); NameSpace *DataNS = pDataChannel->m_DataNS; int rt = 0; Args fd; ReturnStatus rs; rs = isfile(path); if (!rs.success()) { return rs; } // read file size fd = DataNS->Open(path.c_str(), O_RDONLY); if (false == fd.valid) { ERROR_LOG("path %s, open() error, %s", path.c_str(), strerror(errno)); return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } FileAttr attr; rt = DataNS->readn(&fd, &attr, sizeof(attr)); DataNS->Close(&fd); if (sizeof(attr) != rt) { ERROR_LOG("path %s, readn() error", path.c_str()); return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } *pDelta = attr.m_Size; // do delete action rt = DataNS->Unlink(path.c_str()); if (-1 == rt) { ERROR_LOG("path %s, unlink() error, %s.", path.c_str(), strerror(errno)); return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } return ReturnStatus(MU_SUCCESS); }
int CheckIdleUserTask::checkIdleUser(MUWorkItem *pItem) { std::auto_ptr<CheckIdleUserItem> pRItem = std::auto_ptr<CheckIdleUserItem>( dynamic_cast<CheckIdleUserItem *>(pItem)); ReturnStatus rs = pItem->getReturnStatus(); if (!rs.success()) { ERROR_LOG("check idle user failed"); } return 0; }
ReturnStatus FileMetaDAO::delDir(const std::string &path, uint64_t *pDelta) { assert(pDelta); ReturnStatus rs; uint64_t delta = 0; rs = rmdirRecursive(path, pDelta); if (!rs.success()) { ERROR_LOG("path %s, rmdirRecursive() error", path.c_str()); } return rs; }
bool WrapperOwner::ok(JSContext* cx, const ReturnStatus& status) { if (status.type() == ReturnStatus::TReturnSuccess) return true; if (status.type() == ReturnStatus::TReturnStopIteration) return JS_ThrowStopIteration(cx); RootedValue exn(cx); if (!fromVariant(cx, status.get_ReturnException().exn(), &exn)) return false; JS_SetPendingException(cx, exn); return false; }
nsresult WrapperOwner::instanceOf(JSObject* obj, const nsID* id, bool* bp) { ObjectId objId = idOf(obj); JSIID iid; ConvertID(*id, &iid); ReturnStatus status; if (!SendInstanceOf(objId, iid, &status, bp)) return NS_ERROR_UNEXPECTED; if (status.type() != ReturnStatus::TReturnSuccess) return NS_ERROR_UNEXPECTED; return NS_OK; }
bool WrapperOwner::ok(JSContext* cx, const ReturnStatus& status) { if (status.type() == ReturnStatus::TReturnSuccess) return true; if (status.type() == ReturnStatus::TReturnDeadCPOW) { JS_ReportErrorASCII(cx, "operation not possible on dead CPOW"); return false; } RootedValue exn(cx); if (!fromVariant(cx, status.get_ReturnException().exn(), &exn)) return false; JS_SetPendingException(cx, exn); return false; }
ReturnStatus FileMetaDAO::getFile(const std::string &path, FileMeta *pMeta) { assert(pMeta); Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId); NameSpace *DataNS = pDataChannel->m_DataNS; // clear possible data pMeta->m_BlockList.clear(); ReturnStatus rs; rs = isfile(path); if (!rs.success()) { return rs; } int rt = 0; Args fd; fd = DataNS->Open(path.c_str(), O_RDONLY); if (false == fd.valid) { ERROR_LOG("path %s, open() error, %s.", path.c_str(), strerror(errno)); return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } rs = readFileMeta(&fd, pMeta); DataNS->Close(&fd); if (!rs.success()) { ERROR_LOG("path %s, readFileMeta() error", path.c_str()); } return rs; }
ReturnStatus FileMetaDAO::createFile( const std::string &path, const FileMeta &meta, FileMeta *pMeta, int *pDelta) { int rt = 0; int error = 0; Args fd; Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId); NameSpace *DataNS = pDataChannel->m_DataNS; fd = DataNS->Open(path.c_str(), O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR); if (false == fd.valid) { error = errno; ERROR_LOG("path %s, open() error, %s.", path.c_str(), strerror(error)); if (EEXIST == error) { ERROR_LOG("path %s, file exist", path.c_str()); // is a regular file? ReturnStatus rs = isfile(path); if (!rs.success()) { return rs; } fd = DataNS->Open(path.c_str(), O_RDONLY); if (false == fd.valid) { ERROR_LOG("path %s, open() error, %s", path.c_str(), strerror(errno)); return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } // check file version FileAttr attr; rt = DataNS->readn(&fd, &attr, sizeof(attr)); DataNS->Close(&fd); if (sizeof(attr) != rt) { return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } pMeta->m_Attr.m_Version = attr.m_Version; return ReturnStatus(MU_FAILED, PATH_EXIST); } else if (ENOENT == error || ENOTDIR == error) { ERROR_LOG("path %s, path invalid", path.c_str()); return ReturnStatus(MU_FAILED, PATH_INVALID); } else { return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } } ReturnStatus rs = writeFileMeta(&fd, meta); DataNS->Close(&fd); // return file size delta *pDelta = meta.m_Attr.m_Size; return rs; }
ReturnStatus FileMetaDAO::getDir2(const std::string &path, std::list<EDEntry> *pEntryList) { assert(pEntryList); Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId); NameSpace *DataNS = pDataChannel->m_DataNS; int rt = 0; int error = 0; std::string entryName; std::string npath; Args st; rt = DataNS->OpenDir(path.c_str(), &st); if (false == st.valid) { error = errno; ERROR_LOG("path %s, opendir() error, %s.", path.c_str(), strerror(errno)); if (ENOTDIR == error) { return ReturnStatus(MU_FAILED, NOT_DIRECTORY); } else if (ENOENT == error) { return checkPrefix(path); } } FileMeta meta; ReturnStatus rs; Dirent dirent; while(DataNS->ReadDirNext(&st, &dirent)){ entryName = dirent.filetype; npath = path + PATH_SEPARATOR_STRING + entryName; // omit "." and ".." // omit user info file in user root dir if (entryName == DENTRY_CURRENT_DIR || entryName == DENTRY_PARENT_DIR || entryName == USER_INFO_FILE_NAME) { continue; } EDEntry ent; ent.m_Name = entryName; if (MU_DIRECTORY == dirent.filetype) { ent.m_Type = MU_DIRECTORY; rs = statDir(npath, &meta); if (!rs.success()) { ERROR_LOG("path %s, statDir() error", npath.c_str()); return rs; } ent.m_Mode = meta.m_Attr.m_Mode; ent.m_CTime = meta.m_Attr.m_CTime; ent.m_MTime = meta.m_Attr.m_MTime; ent.m_Size = meta.m_Attr.m_Size; } else { // clear data of last entry meta.m_BlockList.clear(); rs = getFile(npath, &meta); if (!rs.success()) { ERROR_LOG("path %s, getFile() error", npath.c_str()); return rs; } ent.m_Mode = meta.m_Attr.m_Mode; ent.m_CTime = meta.m_Attr.m_CTime; ent.m_MTime = meta.m_Attr.m_MTime; ent.m_Size = meta.m_Attr.m_Size; ent.m_Version = meta.m_Attr.m_Version; ent.m_Type = meta.m_Attr.m_Type; ent.m_BlockList = meta.m_BlockList; } pEntryList->push_back(ent); } return ReturnStatus(MU_SUCCESS); }
ReturnStatus FileMetaDAO::rmdirRecursive(const std::string &path, uint64_t *pDelta) { Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId); NameSpace *DataNS = pDataChannel->m_DataNS; int rt = 0; ReturnStatus rs; int error = 0; std::string entryName; std::string npath; Args st; rt = DataNS->OpenDir(path.c_str(), &st); if (rt < 0) { error = errno; ERROR_LOG("path %s, opendir() error, %s.", path.c_str(), strerror(errno)); if (ENOTDIR == error) { ERROR_LOG("path %s, opendir() error, no directory"); return ReturnStatus(MU_FAILED, NOT_DIRECTORY); } else if (ENOENT == error) { return checkPrefix(path); } } // delete its children Dirent dirent; while(DataNS->ReadDirNext(&st, &dirent)){ entryName = dirent.filename; // omit "." and ".." // omit user info file in user root dir if (entryName == DENTRY_CURRENT_DIR || entryName == DENTRY_PARENT_DIR || entryName == USER_INFO_FILE_NAME) { continue; } npath = path + PATH_SEPARATOR_STRING + entryName; if (MU_DIRECTORY == dirent.filetype) { // directory, call myself to delete it rs = rmdirRecursive(npath, pDelta); if (!rs.success()) { ERROR_LOG("path %s, rmdirRecursive() error", npath.c_str()); return rs; } } else { // add file size to changed user quota addFileSizeToDelta(npath, pDelta); // delete it directly rt = DataNS->Unlink(npath.c_str()); if (-1 == rt) { ERROR_LOG("path %s, unlink() error, %s.", npath.c_str(), strerror(errno)); return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } } } // delete path // do not delete user root if (path.substr(path.length() - 1) == ROOT_PATH) { return ReturnStatus(MU_SUCCESS); } rt = DataNS->RmDir(path.c_str()); if (-1 == rt) { ERROR_LOG("path %s, rmdir() error, %s.", path.c_str(), strerror(errno)); return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } return ReturnStatus(MU_SUCCESS); }