Example #1
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);
}
Example #2
0
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);
}