Пример #1
0
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;
}
Пример #2
0
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;
}
Пример #3
0
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);
}
Пример #4
0
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;
}
Пример #5
0
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;
}