Example #1
0
    void testRandomAccess()
    {
        loadStore();
        const int BLK_SIZE = 1 * 1024;
        const int BLKS = 2048;
        unsigned char *b = (unsigned char *) malloc(
                               sizeof(unsigned char) * BLK_SIZE);
        CPPUNIT_ASSERT(b);
        //==== Sequential Setup Phase ====
        for (int i = 0; i < BLKS; ++i) {
            f_write(i % 256, b, BLK_SIZE, i * BLK_SIZE);
        }
        for (int i = 0; i < BLKS; ++i) {
            c_read(i % 256, b, BLK_SIZE, i * BLK_SIZE);
        }

        //==== Random Write Test ====
        std::vector<int> idxs;
        for (int i = 0; i < BLKS; ++i) {
            idxs.push_back(i);
        }
        std::random_shuffle(idxs.begin(), idxs.end());
        for (int i = 0; i < BLKS; ++i) {
            f_write(i % 256, b, BLK_SIZE, idxs.at(i) * BLK_SIZE);
        }
        for (int i = 0; i < BLKS; ++i) {
            c_read(i % 256, b, BLK_SIZE, idxs.at(i) * BLK_SIZE);
        }
        closeStore();
        deleteStore();
        free(b);
    }
int check_match()
{
	USER usr;
	int i=0,count,match;
	c_lseek(user_list_fd,-USR_LEN,SEEK_END);
	c_read(user_list_fd,&usr,USR_LEN);
	count=usr.userid;

	for(;i<=count;i++)
	{
		c_lseek(user_list_fd,i*USR_LEN,SEEK_SET);
		c_read(user_list_fd,&usr,USR_LEN);
		match=strcmp(usr.name,user_temp_p.name);
		if(match==0)//找到了
		{
			match=strcmp(usr.password,user_temp_p.password);
			if(match==0)
			{
				return 1;
				break;
			}
			else return 0;
		}
	}
	return 0;
}
Example #3
0
void DCTRD::readprf(void)
	{
	unsigned size;
    int i;
    KETTA_PREFINFO* ketta_prfix;	     /* suf. seot. info kettal*/
    FSxCHAR* prffixes;

	/* prefiksite massiiv */

	size = (unsigned)(file_info.prfix - file_info.pref); // pref mass. pikkus
    prffixes = (FSxCHAR *) malloc(sizeof(char) * size);  // see malloc on ok
	if (prffixes==NULL)
        {
        throw(VEAD(ERR_MORFI_PS6N,ERR_NOMEM,__FILE__,__LINE__,"$Revision: 521 $"));
        }
	if(c_read(file_info.pref, prffixes, size) == false)
        {
        free(prffixes);
        throw(VEAD(ERR_MORFI_PS6N,ERR_ROTTEN,__FILE__,__LINE__,"$Revision: 521 $"));
        }
    assert((size%dctsizeofFSxCHAR)==0);
    prefiksiteLoend.Start(FSxvrdle, size, prffixes); // prefiksiteLoend'i asi on free(prffixes)

	// pref seot. info

	size = (unsigned)(file_info.taandsl - file_info.prfix);     // pref. infot sisald. mass. pikkus
    if (sizeof(KETTA_PREFINFO) * prefiksiteLoend.len != size)   // ei saa olla
        {
        throw(VEAD(ERR_MORFI_PS6N,ERR_MINGIJAMA,__FILE__,__LINE__,"$Revision: 521 $"));
        }
    ketta_prfix = (KETTA_PREFINFO*)malloc(sizeof(KETTA_PREFINFO)*prefiksiteLoend.len); // see malloc on OK
	if (ketta_prfix==NULL)
        {
        throw(VEAD(ERR_MORFI_PS6N,ERR_NOMEM,__FILE__,__LINE__,"$Revision: 521 $"));
        }
	if(c_read(file_info.prfix, ketta_prfix, size) == false)
        {
        free(ketta_prfix);
        throw(VEAD(ERR_MORFI_PS6N,ERR_ROTTEN,__FILE__,__LINE__,"$Revision: 521 $"));
        }
    prfix = (PREFINFO *) malloc(sizeof(PREFINFO) * prefiksiteLoend.len); // see malloc on ok
	if (prfix == NULL)
        {
        free(ketta_prfix);
        throw(VEAD(ERR_MORFI_PS6N,ERR_NOMEM,__FILE__,__LINE__,"$Revision: 521 $"));
        }
    for (i=0; i < prefiksiteLoend.len; i++) /* kopeeri prefiksiga seotud info paremale kujule*/
        {
        prfix[i].sl = ketta_prfix[i].sl;
        prfix[i].piiriKr6nksud = (ketta_prfix[i].piiriKr6nksud0 & 0xFF) | ((ketta_prfix[i].piiriKr6nksud1 & 0xFF)<<8);
        prfix[i].lisaKr6nksud = (ketta_prfix[i].lisaKr6nksud0 & 0xFF) | ((ketta_prfix[i].lisaKr6nksud1 & 0xFF)<<8);
        }
    free(ketta_prfix);
	}
Example #4
0
 void testSimpleReadWrite()
 {
     loadStore();
     unsigned char *b = (unsigned char *) malloc(
                            sizeof(unsigned char) * 1024 * 1024);
     f_write(17, b, 5 * 1024, 34);
     c_read(17, b, 5 * 1024, 34);
     CPPUNIT_ASSERT(sbdi_fsync(sbdi, SIV_KEYS) == SBDI_SUCCESS);
     closeStore();
     loadStore();
     c_read(17, b, 5 * 1024, 34);
     closeStore();
     deleteStore();
     free(b);
 }
int login()//return 1 success
{
	USER usr;
	int i=usr.userid;

	printf("a login request\n");

	c_lseek(user_list_fd,i*USR_LEN,SEEK_SET);
	c_read(user_list_fd,&usr,USR_LEN);
	int check;
	check= strcmp(usr.password,user_temp_p.password);
	if(check==0)//验证成功,将status置为1,写回文件。可以考虑建一个在线用户的数据结构;
	{
		usr.status=1;
		c_lseek(user_list_fd,-USR_LEN,SEEK_CUR);
		c_write(user_list_fd,&usr,USR_LEN);
		c_send(sockfd,(USER*)usr,USR_LEN,0);
		return 1;
	}
	else
	{
		printf("user:%d login error\n",usr.userid);
		usr.status=0;
		c_send(sockfd,(USER*)usr,USR_LEN,0);
        return 0;
	}
	
}
int user_transations()
{
	USER usr;
	memset(&usr,0,USR_LEN);
	int i=user_temp_p.userid;
	int is_exist;
	int is_match;
	//user结构体status最初被初始化为0,只有上线以后才会变为1
	//所以首先检查status的状态
	//如果为0,且用户名存在,则表示用户名密码错
	//如果为0,且用户名不存在,则表示用户要注册
	//如果为1,则表示用户想改密码
	if(user_temp_p.status==0)
	{
		
		is_exist=check_exist();
		if(is_exist==0)
			registration();
		else
			login();
	}
	else 
	{
		c_lseek(user_list_fd,i*USR_LEN,SEEK_SET);
		c_read(user_list_fd,&usr,USR_LEN);
		memset(usr.password,0,15);
		strncpy(usr.password,user_temp_p.password,strlen(user_temp_p.password));
		c_lseek(user_list_fd,i*USR_LEN,SEEK_SET);
		c_write(user_list_fd,&usr,USR_LEN);
		//modify password;
	}
}
Example #7
0
File: fread.c Project: olebole/iraf
/* FREAD -- Read a binary block of data from the input file.  To be consistent
** with UNIX we must read until nelem chars have been accumulated or until
** EOF is seen.  Hence, if reading from a record structured device such as a
** terminal, the read will not be terminated by end of record (newline).
** If the number of bytes (C chars) requested does not fill an integral number
** of XCHARS additional bytes will be read to fill out the last XCHAR.
*/
int
fread (
  char	*bp,			/* output buffer		*/
  int	szelem,			/* nbytes per element		*/
  int	nelem,			/* nelems to read		*/
  FILE	*fp
)
{
	register int nread, n;
	int	nbytes;
	XINT	fd = fileno (fp);
	char	*op = bp;


	fd     = fileno (fp);
	nbytes = nelem * szelem;
	nread  = 0;

	if (fp == stdin)
	    (void) fflush (stdout);
	if (szelem <= 0)
	    return (0);

	for (op = bp;  nread < nbytes;  op += n) {
	    iferr (n = c_read (fd, op, nbytes-nread)) {
		fp->_fflags |= _FERR;
		break;
	    } else if (n == EOF) {
int init()
{
	int c_init();

	user_list_fd=c_open("./user_list",O_RDWR|O_CREAT|0660);

	USER usr;
	memset(&usr,0,sizeof(USER));
	c_lseek(user_list_fd,0,SEEK_SET);
	c_write(user_list_fd,(char *)&usr,USR_LEN);
	
	int on = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
       perror("setsockopt error");

	c_bind(sockfd,(struct sockaddr*)&server,ADDR_LEN);
	listen(sockfd,7);//两个队列之和为7

	CHIS ch_his;

	bzero(&ch_his,HSTR_LEN);
	c_lseek(mainfd,0,SEEK_SET);
	c_write(mainfd,&ch_his,HSTR_LEN);
	c_lseek(mainfd,-HSTR_LEN,SEEK_END);
	c_read(mainfd,&ch_his,HSTR_LEN);
	count=ch_his.count;//initialize the value by the last count in the local file

	printf("initialization finished\n");
	return 0;
}
int chis_transations()
{
	int i=chis_temp_p.count;
	int max;
	CHIS chis;
	memset(&chis,0,HSTR_LEN);
	c_lseek(recordfd,-HSTR_LEN,SEEK_END);
	c_read(recordfd,&chis,HSTR_LEN);
	max=chis.count;//get the count of all the history msg
	//send the message from i to the latest msg to cli
	for(i++;i<=max;i++)
	{
		c_lseek(recordfd,HSTR_LEN*i,SEEK_SET);
		c_read(recordfd,&chis,HSTR_LEN);
		c_send(conn,&chis,HSTR_LEN,0);//send to conn,conn is the socket for r/w
	}
	return 0;
}
int check_exist()
{ //i的遍历上线在user_list_fd所指文件的最后一个usr结构体的id
	USER usr;
	int i=0,count,exist;
	c_lseek(user_list_fd,-USR_LEN,SEEK_END);
	c_read(user_list_fd,&usr,USR_LEN);
	count=usr.userid;

	for(;i<=count;i++)
	{
		c_lseek(user_list_fd,i*USR_LEN,SEEK_SET);
		c_read(user_list_fd,&usr,USR_LEN);
		exist=strcmp(usr.name,user_temp_p.name);
		if(exist==0)//success
		{
			return 1;
			break;
		}
	}
	return 0;

}
Example #11
0
void
v_serviceFillNewGroups(
    v_service service)
{
    c_set newGroups;
    C_STRUCT(v_event) ge;
    v_group g, oldGroup;
    c_iter oldGroups;
    v_kernel kernel;

    assert(service != NULL);
    assert(C_TYPECHECK(service, v_service));

    kernel = v_objectKernel(service);
    newGroups = (c_voidp)c_setNew(v_kernelType(kernel, K_GROUP));

    if (newGroups != NULL) {
        addAllGroups(newGroups, kernel->groupSet);
        v_observerLock(v_observer(service));
        g = v_group(c_read(newGroups)); /* need a group for the event */

        if(v_observer(service)->eventData != NULL){
            oldGroups = ospl_c_select((c_set)v_observer(service)->eventData, 0);
            oldGroup = v_group(c_iterTakeFirst(oldGroups));

            while(oldGroup){
                newGroups = c_setInsert(newGroups, oldGroup);
                c_free(oldGroup);
                oldGroup = v_group(c_iterTakeFirst(oldGroups));
            }
            c_iterFree(oldGroups);
        }
        /* just for safety, when assertion are compiled out, free the prev set */
        c_free((c_object)v_observer(service)->eventData);
        v_observer(service)->eventData = (c_voidp)newGroups;

        ge.kind = V_EVENT_NEW_GROUP;
        ge.source = v_publicHandle(v_public(kernel));
        ge.userData = g;
        v_observerNotify(v_observer(service), &ge, NULL);
        v_observerUnlock(v_observer(service));
        c_free(g);
    }
}
Example #12
0
int GetCDLabel(void){
    Window    wd;
    Field     fd;
    char      cdLabel[15];
    int       col=22, row=4, width=21, height=15;
    int       i,xkey;
    Control   lb;
    BYTE      Unit;
    strncpy(cdLabel,"    ",4);
    wd = w_open(col, row, width, height);
    w_umessage(wd,cdDriver);
    w_lmessage(wd,"ESC for New Driver");
    lb = c_add_listbox(wd," Drive  Label ",2,1,17,13,LISTBOX_SORT);
    for (Unit=0; Unit<=LastUnit; Unit++){
      (void)ReadPVD(Unit);
      if (CD=='I')
         strncpy(&cdLabel[4],iso->volLabel,11);
      else if (CD=='H')
         strncpy(&cdLabel[4],hsf->volLabel,11);
      else
         strncpy(&cdLabel[4],"Unknown Fmt",11);
      i=Unit;
      i/=10;
      cdLabel[1] = '0'+i;
      i=Unit;
      i%=10;
      cdLabel[2] = '0'+i;
      c_add_item(lb,cdLabel,-1,ENABLE);

   }
    xkey=c_dialog(wd);
    if (xkey == _ESC){
       w_close(wd);
       return -1;
    }
    else{
       Unit = c_read(lb,C_STATE);
       w_close(wd);
       return Unit;
    }
}
Example #13
0
void DCTRD::readfgrs(void)
	{
	//int  res;
	unsigned size;

	/* l�pugruppide vormijadad */

	size = (unsigned)(file_info.suf - file_info.fgr); /* l�pugruppide vormijadade mass. pikkus */
	homo_form = size/gr_size;
    fgr = (char *)malloc(sizeof(char) * size); // see malloc on ok
	if (fgr==NULL)
        {
        throw(VEAD(ERR_MORFI_PS6N,ERR_NOMEM,__FILE__,__LINE__, "$Revision: 521 $"));
        }
	if(c_read(file_info.fgr, fgr, size) == false)
        {
        free(fgr);
        fgr=NULL;
        throw(VEAD(ERR_MORFI_PS6N,ERR_MINGIJAMA,__FILE__,__LINE__, "$Revision: 521 $"));
        }
	}
Example #14
0
void DCTRD::readends(void)
	{
	unsigned size;
    FSxCHAR *endings;

	// l�ppude tabel

	size = (unsigned)(file_info.groups - file_info.ends);  // l�ppude mass. pikkuse
    endings = (FSxCHAR *) malloc(sizeof(char) * size); // see malloc on OK
	if (endings==NULL)
        {
        throw(VEAD(ERR_MORFI_PS6N,ERR_ROTTEN,__FILE__,__LINE__,"$Revision: 521 $"));
        }
	if(c_read(file_info.ends, endings, size) == false) // ok, sealt ei tule throw()-d
        {
        free(endings);
        endings=NULL;
        throw(VEAD(ERR_MORFI_PS6N,ERR_ROTTEN,__FILE__,__LINE__,"$Revision: 521 $"));
        }
    // pistame need loendiklassi sisse
    l6ppudeLoend.Start(FSxvrdle, size, endings);
	}
Example #15
0
void DCTRD::readfms(void)
	{
	unsigned size;
    FSxCHAR *formings;
	// vormide tabel

	size = (unsigned)(file_info.fgr - file_info.forms); // vormide mass. pikkus
    formings = (FSxCHAR *) malloc(sizeof(char) * size); // see malloc on OK
	if(formings==NULL)
        {
        throw(VEAD(ERR_MORFI_PS6N,ERR_NOMEM,__FILE__,__LINE__, "$Revision: 521 $"));
        }
	if(c_read(file_info.forms, formings, size) == false)
        {
        free(formings);
        formings=NULL;
        throw(VEAD(ERR_MORFI_PS6N,ERR_ROTTEN,__FILE__,__LINE__, "$Revision: 521 $"));
        }
    assert( (size%sizeof(FSxCHAR)) == 0 );
    // pistame need loendiklassi sisse
    vormideLoend.Start(FSxvrdle, size, formings);
	}
int registration()
{
	USER usr;
	int id;
	c_lseek(user_list_fd,-USR_LEN,SEEK_END);
	c_read(user_list_fd,&usr,USR_LEN);
	id=usr.userid;
	const char *name;
	const char *password;
	memset(&usr,0,USR_LEN);
	name=&(user_temp_p.name);
	password=&(user_temp_p.password);
	strncpy(usr.name,name,10);
	strncpy(usr.password,password,15);
	usr.status=0;
	usr.userid= id+1;

	c_lseek(user_list_fd,0,SEEK_END);
	c_write(user_list_fd,&usr,USR_LEN);
	//返回一则成功信息。
	//c_send(sockfd,....)
}
Example #17
0
void DCTRD::readloe(int i)
	{
    unsigned size;
    FSxCHAR* tmpPtr;

	size = (unsigned)(file_info.loend[i+1] - file_info.loend[i]);  //loendi mass. pikkus
	if (size == 0L) // loendit polegi 
		{
		return;
		}
    tmpPtr=(FSxCHAR *)(malloc(size)); // see malloc on OK
    if(tmpPtr==NULL) // reserveerime loendile m�lu
        {
        throw(VEAD(ERR_MORFI_PS6N,ERR_NOMEM,__FILE__,__LINE__,"$Revision: 521 $"));
        }
	if(c_read(file_info.loend[i], tmpPtr, size) == false) // t�mbame loendi sisse
        {
        free(tmpPtr);
        throw(VEAD(ERR_MORFI_PS6N,ERR_ROTTEN,__FILE__,__LINE__,"$Revision: 521 $"));
        }
    // dctLoend[i] asi on free(tmpPtr)
    dctLoend[i].Start(FSxvrdle, size, tmpPtr);
	}