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; }
void FileMetaDAO::addFileSizeToDelta(const std::string &path, uint64_t *pDelta) { Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId); NameSpace *DataNS = pDataChannel->m_DataNS; int rt = 0; Args fd; // 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 ; } //int fd = rt; 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 ; } *pDelta += attr.m_Size; }
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); }
ReturnStatus FileMetaDAO::readFileMeta(Args *fd, FileMeta *pMeta) { /* assert(pMeta); Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId); NameSpace *DataNS = pDataChannel->m_DataNS; int rt = 0; rt = DataNS->readn(fd, &(pMeta->m_Attr), sizeof(pMeta->m_Attr)); if (sizeof(pMeta->m_Attr) != rt) { ERROR_LOG("read attr, readn() error"); return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } int blocks = pMeta->m_Attr.m_Size / FIXED_BLOCK_SIZE; if (pMeta->m_Attr.m_Size % FIXED_BLOCK_SIZE != 0) { ++blocks; } char *pBlockList = new char[blocks * FIXED_BLOCK_CHECKSUM_LEN]; rt = DataNS->readn(fd, pBlockList, blocks * FIXED_BLOCK_CHECKSUM_LEN); if (blocks * FIXED_BLOCK_CHECKSUM_LEN != rt) { ERROR_LOG("read block list, readn() error"); delete [] pBlockList; return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } BlockMeta bmeta; for (int i = 0; i < blocks; ++i) { bmeta.m_Checksum = std::string(pBlockList + i * FIXED_BLOCK_CHECKSUM_LEN, FIXED_BLOCK_CHECKSUM_LEN); pMeta->m_BlockList.push_back(bmeta); } delete [] pBlockList; return ReturnStatus(MU_SUCCESS); */ //---------------- assert(pMeta); Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId); NameSpace *DataNS = pDataChannel->m_DataNS; int rt = 0; //TODO int max_blocks = 512;//max file size is 256MB char *buffer = new char[max_blocks * FIXED_BLOCK_CHECKSUM_LEN]; rt = DataNS->readn(fd, buffer, max_blocks * FIXED_BLOCK_CHECKSUM_LEN); memcpy(&(pMeta->m_Attr), buffer, sizeof(pMeta->m_Attr)); int blocks = pMeta->m_Attr.m_Size / FIXED_BLOCK_SIZE; if (pMeta->m_Attr.m_Size % FIXED_BLOCK_SIZE != 0) { ++blocks; } if ((sizeof(pMeta->m_Attr) + blocks * FIXED_BLOCK_CHECKSUM_LEN) != rt) { ERROR_LOG("read file meta, readn() error"); delete [] buffer; return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR); } BlockMeta bmeta; for(int i = 0; i < blocks; ++i){ bmeta.m_Checksum = std::string(buffer + sizeof(pMeta->m_Attr) + i * FIXED_BLOCK_CHECKSUM_LEN, FIXED_BLOCK_CHECKSUM_LEN); pMeta->m_BlockList.push_back(bmeta); } delete [] buffer; return ReturnStatus(MU_SUCCESS); }
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; }