Пример #1
0
ReturnStatus
FileMetaDAO::isdir(const std::string &path)
{
	Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId);
	NameSpace *DataNS = pDataChannel->m_DataNS;

	
    int rt = 0;

	FileAttr st;
    rt = DataNS->Stat(path.c_str(), &st);

    if (0 == rt) {
        if (MU_DIRECTORY == st.m_Type) {
            return ReturnStatus(MU_SUCCESS);

        } else {
            ERROR_LOG("path %s, not directory", path.c_str());
            return ReturnStatus(MU_FAILED, NOT_DIRECTORY);
        }
    }

    int error = errno;

    ERROR_LOG("path %s, stat() error, %s", path.c_str(), strerror(error));

    if (ENOENT == error || ENOTDIR == error) {
        return checkPrefix(path);

    } else {
        return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR);
    }
}
Пример #2
0
bool MultiNameSpace::writeToNameServer(PortWriter& cmd,
                                       PortReader& reply,
                                       const ContactStyle& style) {
    NameSpace *ns = HELPER(this).getOne();
    if (!ns) return false;
    return ns->writeToNameServer(cmd,reply,style);
}
Пример #3
0
bool MultiNameSpace::disconnectPortToPortPersistently(const Contact& src,
                                                      const Contact& dest,
                                                      ContactStyle style) {
    NameSpace *ns = HELPER(this).getOne();
    if (!ns) return false;
    return ns->disconnectPortToPortPersistently(src,dest,style);
}
Пример #4
0
bool MultiNameSpace::disconnectTopicFromPort(const Contact& src,
                                             const Contact& dest,
                                             ContactStyle style) {
    NameSpace *ns = HELPER(this).getOne();
    if (!ns) return false;
    return ns->disconnectTopicFromPort(src,dest,style);
}
Пример #5
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;
}
Пример #6
0
 void scan() {
     // reset flags
     _localOnly = true;
     _usesCentralServer = false;
     _serverAllocatesPortNumbers = true;
     // now scan each namespace
     for (int i=0; i<(int)spaces.size(); i++) {
         NameSpace *ns = spaces[i];
         if (!ns) continue;
         // if any namespace is nonlocal, combination is nonlocal
         if (!ns->localOnly()) _localOnly = false;
         // if any namespace uses a central server, combination also does
         if (ns->usesCentralServer()) _usesCentralServer = true;
         // if any namespace doesn't allocate port numbers, combination
         // cannot be relied on to do so either
         if (!ns->serverAllocatesPortNumbers()) {
             _serverAllocatesPortNumbers = false;
         }
         // if any namespace lacks informed connections, combination
         // cannot be relied on to be informed either
         if (!ns->connectionHasNameOfEndpoints()) {
             _connectionHasNameOfEndpoints = false;
         }
     }
 }
Пример #7
0
ReturnStatus
FileMetaDAO::checkPrefix(const std::string &path)
{
	Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId);
	NameSpace *DataNS = pDataChannel->m_DataNS;
	
    int rt = 0;

	FileAttr st;
    rt = DataNS->Stat(prefix(path).c_str(), &st);

    if (0 == rt) {

        if (st.m_Type == MU_DIRECTORY) {//如果path是个存在的目录
            // prefix path is existed and refers to a directory
            return ReturnStatus(MU_FAILED, PATH_NOT_EXIST);

        } else {//如果path是个存在的文件
            return ReturnStatus(MU_FAILED, PATH_INVALID);
        }

    } else {
        if (errno == ENOENT || errno == ENOTDIR) {//如果这个路径不存在
            return ReturnStatus(MU_FAILED, PATH_INVALID);

        } else {//
            return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR);
        }
    }

}
Пример #8
0
ReturnStatus
FileMetaDAO::putDir(const std::string &path)
{
	Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId);
	NameSpace *DataNS = pDataChannel->m_DataNS;
	
    int rt = 0;

    rt = DataNS->MkDir(path.c_str(),
                 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);

    if (0 == rt) {
        return ReturnStatus(MU_SUCCESS);
    }

    // error occurred

    int error = errno;

    ERROR_LOG("path %s, mkdir() error, %s.", path.c_str(), strerror(error));

    if (EEXIST == error) {
        ERROR_LOG("path %s, path exist", path.c_str());
        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);
    }

    return ReturnStatus(MU_SUCCESS);  // make compiler happy
}
Пример #9
0
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);
}
Пример #10
0
ReturnStatus
FileMetaDAO::getDir(const std::string &path, std::list<PDEntry> *pEntryList)
{
    assert(pEntryList);

    Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId);
	NameSpace *DataNS = pDataChannel->m_DataNS;

    int rt = 0;
    int error = 0;
    std::string entryName;
    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);
        }
    }

    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;
        }

        PDEntry ent;
        ent.m_Name = entryName;

        if (MU_DIRECTORY == dirent.filetype) {
            ent.m_Type = MU_DIRECTORY;

        } else {
            ent.m_Type = MU_REGULAR_FILE;
        }

        pEntryList->push_back(ent);
    }


    return ReturnStatus(MU_SUCCESS);
}
Пример #11
0
/**
 * Parses the keyword "class" from the given token stack
 */
int CodeParser::parseKeyword_Class( Scope* scope, TokenStack* stack ) {
	Class* classdef;
	const char* name;
	Token* t;

	// validate incoming scope
	if( scope->getScopeType() != NAMESPACE_SCOPE ) {
		SYNTAX_ERROR( "Classes cannot be defined in this scope", stack->last() );
		return -1;
	}

	// the next token must be the identifier
	if( !( name = state->getIdentifierName( stack, "class" ) ) ) {
		return -2;
	}

	// get the name space
	NameSpace* ns = (NameSpace*)scope;
	printf( "class '%s' in namespace '%s' modifiers = %s\n", name, ns->getName(), toBinary( modifiers ) );

	// TODO check for inheritance! multiple inheritance?
	// - class MyChild extends MyParent { ... }
	// - class StrobeLamp extends Lamp, Strobe { ... }

	// create the class
	classdef = new Class( name, ns->getName(), ns, modifiers );
	this->modifiers = 0;

	// add the name space to the state
	if( !ns->addClass( classdef ) ) {
		char* error = new char[256];
		sprintf( error, "Class '%s' already exists in namespace '%s'\n", name, ns->getName() );
		SYNTAX_ERROR( error, stack->last() );
		delete error;
		return -4;
	}

	// the next element must be a code block
	if( !stack->hasNext() || !( t = stack->peek() ) || ( t->getType() != tok::CODE_BLOCK ) ) {
		SYNTAX_ERROR( "unexpected token", t );
		return -5;
	}

	// process the code block
	int errorCode = parse( classdef, ((ComplexToken*)t)->getChildren() );

	// register the class
	state->addClass( classdef );

	// reset the class-level counters
	state->resetCounters();
	return errorCode;
}
Пример #12
0
 Contact queryName(const ConstString& name) {
     activate();
     for (int i=0; i<(int)spaces.size(); i++) {
         NameSpace *ns = spaces[i];
         if (!ns) continue;
         if (ns->getNameServerName()==name) {
             return ns->getNameServerContact();
         }
         Contact result = ns->queryName(name);
         if (result.isValid()) return result;
     }
     return Contact();
 }
bool ApexResourceProvider::checkResource(ResID nameSpace, const char* name)
{
	/* Return true is named resource has known non-null pointer */
	uint32_t nsIndex = getNSIndex(nameSpace);
	if (nsIndex < mNameSpaces.size())
	{
		NameSpace* ns = mNameSpaces[nsIndex];
		ResID id = ns->getOrCreateID(name, mResources[ns->getID()].name);
		PX_ASSERT(id < mResources.size());
		return checkResource(id);
	}
	return false;
}
Пример #14
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);
}
Пример #15
0
 Contact queryName(const char *name) {
     activate();
     for (int i=0; i<(int)spaces.size(); i++) {
         NameSpace *ns = spaces[i];
         if (!ns) continue;
         if (ns->getNameServerName()==name) {
             return ns->getNameServerContact();
         }
         //printf("Query from %s\n", spaces[i]->getNameServerName().c_str());
         Contact result = ns->queryName(name);
         //printf("Got %s\n", result.toString().c_str());
         if (result.isValid()) return result;
     }
     return Contact();
 }
Пример #16
0
 Contact whereDelegate()
 {
     if (!space) {
         return Contact();
     }
     return space->getNameServerContact();
 }
Пример #17
0
 void scan() {
     _localOnly = true;
     _usesCentralServer = false;
     _serverAllocatesPortNumbers = true;
     for (int i=0; i<(int)spaces.size(); i++) {
         NameSpace *ns = spaces[i];
         if (!ns) continue;
         if (!ns->localOnly()) _localOnly = false;
         if (ns->usesCentralServer()) _usesCentralServer = true;
         if (!ns->serverAllocatesPortNumbers()) {
             _serverAllocatesPortNumbers = false;
         }
         if (!ns->connectionHasNameOfEndpoints()) {
             _connectionHasNameOfEndpoints = false;
         }
     }
 }
Пример #18
0
 Contact queryName(const ConstString& name) {
     activate();
     // try query against each namespace in order
     for (int i=0; i<(int)spaces.size(); i++) {
         NameSpace *ns = spaces[i];
         if (!ns) continue;
         if (ns->getNameServerName()==name) {
             // optimization: return cached server address for 
             // port names that match name of namespace
             return ns->getNameServerContact();
         }
         Contact result = ns->queryName(name);
         // return a result once we get one, skipping any remaining
         // namespaces
         if (result.isValid()) return result;
     }
     return Contact();
 }
ResID ApexResourceProvider::createResource(ResID nameSpace, const char* name, bool refCount)
{
	uint32_t nsIndex = getNSIndex(nameSpace);
	if (nsIndex < mNameSpaces.size())
	{
		NameSpace* ns = mNameSpaces[nsIndex];
		ResID id = ns->getOrCreateID(name, mResources[ns->getID()].name);
		PX_ASSERT(id < mResources.size());
		if (id < mResources.size() && refCount)
		{
			mResources[id].refCount++;
		}
		return id;
	}
	else
	{
		return INVALID_RESOURCE_ID;
	}
}
Пример #20
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;
}
Пример #21
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;
}
Пример #22
0
ReturnStatus
FileMetaDAO::statDir(const std::string &path, FileMeta *pMeta)
{
    assert(pMeta);

    Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId);
	NameSpace *DataNS = pDataChannel->m_DataNS;

    int rt = 0;
    int error = 0;

	FileAttr st;
	rt = DataNS->Stat(path.c_str(), &st);

    if (-1 == rt) {
        error = errno;
        ERROR_LOG("path %s, stat() error, %s.", path.c_str(), strerror(error));

        if (ENOENT == error || ENOTDIR == error) {
            return checkPrefix(path);

        } else {
            return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR);
        }
    }

    if (st.m_Type != MU_DIRECTORY) {
        ERROR_LOG("path %s, not directory", path.c_str());
        return ReturnStatus(MU_FAILED, NOT_DIRECTORY);
    }

    pMeta->m_Attr.m_Mode = st.m_Mode;
    pMeta->m_Attr.m_CTime = st.m_CTime;
    pMeta->m_Attr.m_MTime = st.m_MTime;
    pMeta->m_Attr.m_Size = st.m_Size;

    return ReturnStatus(MU_SUCCESS);
}
Пример #23
0
Value *MultiNameSpace::getProperty(const ConstString& name, const ConstString& key) {
    NameSpace *ns = HELPER(this).getOne();
    if (!ns) return NULL;
    return ns->getProperty(name,key);
}
Пример #24
0
bool MultiNameSpace::setProperty(const ConstString& name, const ConstString& key,
                                const Value& value) {
    NameSpace *ns = HELPER(this).getOne();
    if (!ns) return false;
    return ns->setProperty(name,key,value);
}
Пример #25
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);
}
Пример #26
0
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);
}
Пример #27
0
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);
    
}
Пример #28
0
ReturnStatus
FileMetaDAO::writeFileMeta(Args *fd, const FileMeta &meta)
{
	Channel* pDataChannel = ChannelManager::getInstance()->Mapping(m_BucketId);
	NameSpace *DataNS = pDataChannel->m_DataNS;

    int rt = 0;

    rt = DataNS->Lseek(fd, 0, SEEK_SET);

    if (-1 == rt) {
        ERROR_LOG("lseek() error, %s.", strerror(errno));
        return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR);
    }

    int blocks = meta.m_BlockList.size();

    int attrLen = sizeof(FileAttr);

    char *pBuf = new char[
        attrLen + FIXED_BLOCK_CHECKSUM_LEN * blocks];
    int bufIdx = 0;

    memcpy(pBuf + bufIdx, &(meta.m_Attr), attrLen);
    bufIdx += attrLen;

    for (std::list<BlockMeta>::const_iterator it = meta.m_BlockList.begin();
         it != meta.m_BlockList.end(); ++it) {
        memcpy(pBuf + bufIdx,
               it->m_Checksum.c_str(),
               FIXED_BLOCK_CHECKSUM_LEN);
        bufIdx += FIXED_BLOCK_CHECKSUM_LEN;
    }

    rt = DataNS->writen(fd, pBuf, bufIdx);

    delete [] pBuf;
    pBuf = NULL;

    if (bufIdx != rt) {
        ERROR_LOG("writen() error");
        return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR);
    }

    //int iovcnt = blocks + 1;
    //struct iovec iov[iovcnt];
    //int iovIdx = 0;

    //iov[iovIdx].iov_base = const_cast<FileAttr *>(&(meta.m_Attr));
    //iov[iovIdx].iov_len = sizeof(meta.m_Attr);
    //++iovIdx;

    //for (std::list<BlockMeta>::const_iterator it = meta.m_BlockList.begin();
    //it != meta.m_BlockList.end(); ++it) {
    //iov[iovIdx].iov_base = const_cast<char *>(it->m_Checksum.c_str());
    //iov[iovIdx].iov_len = FIXED_BLOCK_CHECKSUM_LEN;
    //++iovIdx;
    //}

    //rt = ::writev(fd, iov, iovcnt);

    //if (sizeof(meta.m_Attr) + blocks * FIXED_BLOCK_CHECKSUM_LEN != rt) {
    //ERROR_LOG("writev() error, %s.", strerror(errno));
    //return ReturnStatus(MU_FAILED, MU_UNKNOWN_ERROR);
    //}

    return ReturnStatus(MU_SUCCESS);
}
Пример #29
0
bool SubscriberOnSql::welcome(const ConstString& port, int activity) {
    mutex.wait();

    NameSpace *ns = getDelegate();
    if (ns) {
        NestedContact nc(port);
        if (nc.getNestedName().size()>0) {
            NameStore *store = getStore();
            if (store!=NULL) {
                Contact node = store->query(nc.getNodeName());
                Contact me = store->query(port);
                if (node.isValid() && me.isValid()) {
                    if (activity>0) {
                        ns->registerAdvanced(me,store);
                    } else {
                        ns->unregisterAdvanced(port,store);
                    }
                }
            }
        }
    }

    char *msg = NULL;
    char *query;
    if (activity>0) {
        query = sqlite3_mprintf("INSERT OR IGNORE INTO live (name,stamp) VALUES(%Q,DATETIME('now'))", 
                                port.c_str());
    } else {
        // Port not responding.  Mark as non-live.
        if  (activity==0) {
            query = sqlite3_mprintf("DELETE FROM live WHERE name=%Q AND stamp < DATETIME('now','-30 seconds')", 
                                    port.c_str());
        } else {
            // activity = -1 -- definite dodo
            query = sqlite3_mprintf("DELETE FROM live WHERE name=%Q", 
                                    port.c_str());
        }
    }
    if (verbose) {
        printf("Query: %s\n", query);
    }
    bool ok = true;
    int result = sqlite3_exec(SQLDB(implementation), query, 
                              NULL, NULL, &msg);
    if (result!=SQLITE_OK) {
        ok = false;
        if (msg!=NULL) {
            fprintf(stderr,"Error: %s\n", msg);
            sqlite3_free(msg);
        }
    }
    sqlite3_free(query);
    mutex.post();

    if (activity>0) {
        hookup(port);
    } else if (activity<0) {
        breakdown(port);
    }
    return ok;
}
Пример #30
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;
}