Example #1
0
void rename_over_test(const char *mountpt)
{
	int i;
	char a[100];
	char b[100];
	char c[100];
	
	sprintf(a,"%s/a",mountpt);
	sprintf(b,"%s/b",mountpt);
	sprintf(c,"%s/c",mountpt);
	
	yaffs_StartUp();
	
	yaffs_mount(mountpt);
	
	printf("Existing files\n");
	dumpDirFollow(mountpt);
	
	
	
	i = yaffs_open(c,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
	printf("File c handle is %d\n",i);
	yaffs_close(i);
	i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR,  S_IREAD | S_IWRITE); 
	yaffs_close(i);
	i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR,  S_IREAD | S_IWRITE);
	yaffs_close(i);
	yaffs_rename(a,b); // rename over
	yaffs_rename(b,a); // rename back again (not renaimng over)
	yaffs_rename(a,b); // rename back again (not renaimng over)
	
	
	yaffs_unmount(mountpt);
	
}
Example #2
0
static int yWriteFile(const char *fname, unsigned sz32)
{
	int h;
	int r;
	int i;
	unsigned checksum = 0;
	
	printf("Writing file %s\n",fname);

	h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);

	if(h < 0){
		printf("could not open file %s\n",fname);
		return h;
	}

        xx[0] = sz32;
        checksum ^= xx[0];

	if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
		goto WRITE_ERROR;
	}
		
	while(sz32> 0){
        	for(i = 0; i < XX_SIZE; i++){
        	  xx[i] = sz32 + i;
        	  checksum ^= xx[i];
                }

		if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){
			goto WRITE_ERROR;
		}
		sz32--;
	}

	xx[0] = checksum;

	if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
		goto WRITE_ERROR;
	}
	
	
	yaffs_close(h);
	return 0;

WRITE_ERROR:
	printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END));
	yaffs_close(h);
	return -1;
	
}
Example #3
0
static void UpdateCounter(const char *name, unsigned *val,  int initialise)
{
  int inh=-1;
  int outh=-1;
  unsigned x[2];
  int nread = 0;
  int nwritten = 0;
  
  x[0] = x[1] = 0;
  
  if(initialise){
    x[0] = 0; 
    x[1] = 1;
  } else {
    inh = yaffs_open(name,O_RDONLY, S_IREAD | S_IWRITE);
    if(inh >= 0){
      nread = yaffs_read(inh,x,sizeof(x));
      yaffs_close(inh);
    }

    if(nread != sizeof(x) ||
       x[0] + 1 != x[1]){
      printf("Error reading counter %s handle %d, x[0] %u x[1] %u last error %d\n",
              name, inh, x[0], x[1],yaffsfs_GetLastError());
      FatalError();
              
    }
    x[0]++;
    x[1]++;
  }
  
  outh = yaffs_open(fullTempCounterName, O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
  if(outh >= 0){
    nwritten = yaffs_write(outh,x,sizeof(x));
    yaffs_close(outh);
    yaffs_rename(fullTempCounterName,name);
  }
  
  if(nwritten != sizeof(x)){
      printf("Error writing counter %s handle %d, x[0] %u x[1] %u\n",
              name, inh, x[0], x[1]);
      FatalError();
  }
  
  *val = x[0];
  
  printf("##\n"
         "## Set counter %s to %u\n"
         "##\n", name,x[0]);
}
Example #4
0
void fill_empty_files_test(const char *mountpt)
{
	int i;
	yaffs_StartUp();
	char name[100];
	int result = 0;
	
	int d,f;

	for(i = 0; i < 5; i++)
	{
		yaffs_mount(mountpt);
		for(d = 0; result >= 0 && d < 1000; d++){
			sprintf(name,"%s/%d",mountpt,d);
			result= yaffs_mkdir(name,0);
			printf("creating directory %s result %d\n",name,result);
			
			for(f = 0; result >= 0 && f < 100; f++){
				sprintf(name,"%s/%d/%d",mountpt,d,f);
				result= yaffs_open(name,O_CREAT, 0);
				yaffs_close(result);
				printf("creating file %s result %d\n",name,result);
			}
		}
		yaffs_unmount(mountpt);
	}
	
}
Example #5
0
int dump_file_data(char *fn)
{
	int h;
	int i = 0;
	int ok = 1;
	unsigned char b;
	
	h = yaffs_open(fn, O_RDWR,0);

				
	printf("%s\n",fn);
	while(yaffs_read(h,&b,1)> 0)
	{
		printf("%02x",b);
		i++;
		if(i > 32) 
		{
		   printf("\n");
		   i = 0;;
		 }
	}
	printf("\n");
	yaffs_close(h);
	return ok;
}
Example #6
0
int overwrite_test(const char *path)
{
   char aname[100];
   char bname[100];
   int i;
   int j;   
   int a;
   int b;
   yaffs_StartUp();
   
   yaffs_mount(path);
   
   sprintf(aname,"%s%s",path,"/a");
   sprintf(bname,"%s%s",path,"/b");
   
   b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
   for(j= 0; j < 500; j++){
   	yaffs_write(b,bname,100);
	a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
   	for(i = 0; i < rand() % 20000; i++)
   		yaffs_write(a,&a,sizeof(a));
	yaffs_close(a);
   }
   
   return 0;
   
}
Example #7
0
void read_a_file(char *fn)
{
	int h;
	int i = 0;
	unsigned char b;

	h = yaffs_open(fn, O_RDWR,0);
	if(h<0)
	{
		printf("File not found\n");
		return;
	}

	while(yaffs_read(h,&b,1)> 0)
	{
		printf("%02x ",b);
		i++;
		if(i > 32)
		{
		   printf("\n");
		   i = 0;;
		 }
	}
	printf("\n");
	yaffs_close(h);
}
int test_yaffs_chmod_EROFS(void)
{
	int error=0;
	int output;
	if (yaffs_close(yaffs_open(FILE_PATH,O_CREAT | O_RDWR, FILE_MODE))==-1){
		print_message("failed to create file\n",1);
		return -1;
	}
	EROFS_setup();

	output = yaffs_chmod(FILE_PATH,S_IREAD|S_IWRITE);

	if (output<0){
		error=yaffs_get_error();
		if (abs(error)==EROFS){
			return 1;
		} else {
			print_message("different error than expected\n",2);
			return -1;
		}
	} else {
		print_message("chmoded with EROFS setup (which is a bad thing)\n",2);
		return -1;
	}

}
int test_yaffs_rename_file_to_dir(void)
{
    int output=0;

    if (0 !=  yaffs_access(FILE_PATH,0)) {
        output = test_yaffs_open();
        if (output < 0) {
            print_message("failed to create file\n",2);
            return -1;
        } else {
            output = yaffs_close(output);
            if (output < 0) {
                print_message("failed to close file\n",2);
                return -1;
            }
        }
    }
    output = yaffs_rename( "/yaffs2/foo" , RENAME_DIR_PATH);
    if (output<0) {
        print_message("failed to rename a file over an empty directory\n",2);
        return -1;
    } else {
        return 1;
    }

}
int  test_yaffs_rename_ENOTEMPTY(void)
{

	int output=0;
	int error_code =0;

	if (yaffs_mkdir(DIR_PATH,O_CREAT | O_RDWR)==-1){
		print_message("failed to create dir\n",1);
		return -1;
	}
	if (yaffs_mkdir(DIR_PATH2,O_CREAT | O_RDWR)==-1){
		print_message("failed to create dir2\n",1);
		return -1;
	}
	if (yaffs_close(yaffs_open(DIR_PATH2_FILE,O_CREAT | O_RDWR, FILE_MODE))==-1){
		print_message("failed to create file\n",1);
		return -1;
	}
	output = yaffs_rename( DIR_PATH,DIR_PATH2 );
	if (output<0){ 
		error_code=yaffs_get_error();
		if (abs(error_code)==ENOTEMPTY){
			return 1;
		} else {
			print_message("different error than expected\n",2);
			return -1;
		}
		print_message("could not rename a directory over a nonempty directory (which is a bad thing)\n",2);
		return -1;
	} 
		
	return 1;

}
int  test_yaffs_rename_full_dir_over_dir(void)
{

	int output=0;
	int error_code =0;

	if (yaffs_mkdir(DIR_PATH,O_CREAT | O_RDWR)==-1){
		print_message("failed to create dir\n",1);
		return -1;
	}
	if (yaffs_mkdir(DIR_PATH2,O_CREAT | O_RDWR)==-1){
		print_message("failed to create dir2\n",1);
		return -1;
	}
	if (yaffs_close(yaffs_open(DIR_PATH2_FILE,O_CREAT | O_RDWR, FILE_MODE))==-1){
		print_message("failed to create file\n",1);
		return -1;
	}
	output = yaffs_rename( DIR_PATH2,DIR_PATH );
	if (output<0){ 
		print_message("could not rename a directory over a empty directory (which is a bad thing)\n",2);
		return -1;
	} 
		
	return 1;

}
int test_yaffs_fsync_EROFS(void)
{
	int output = 0;
	int error_code = 0;
	if (yaffs_close(yaffs_open(FILE_PATH,O_CREAT | O_RDWR, FILE_MODE))==-1){
		print_message("failed to create file\n",1);
		return -1;
	}
	EROFS_setup();
	handle = yaffs_open(FILE_PATH,O_CREAT  ,S_IREAD  );
	if (handle<0){
		print_message("failed to open file\n",2);
		return -1;
	}
	output = yaffs_fsync(handle);
	if (output==-1){
		error_code=yaffs_get_error();

		if (abs(error_code)==EROFS){
			return 1;
		} else {
			print_message("different error than expected\n",2);
			return -1;
		}
	} else {
		print_message("file synced with EROFS set.(which is a bad thing)\n",2);
		return -1;
	}
}
Example #13
0
void handle_test(const char *mountpt)
{
	int i;
	int h;
	int cycle;
	char a[100];

	sprintf(a,"%s/aaa",mountpt);
	
	yaffs_StartUp();
	
	yaffs_mount(mountpt);

        for(cycle = 0; cycle < 5; cycle++){
        printf("Start cycle %d\n",cycle);
 	i = 0;
	do {
        h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
	printf("%d  handle %d\n",i,h);
	i++;
	} while(h >= 0);
	
	while(i >= -1) {
	 yaffs_close(i);
	 i--;
        }
        }
	
	yaffs_unmount(mountpt);
}
Example #14
0
void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
{
   int h1;
   int i;
   int offs;
   
   h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
   
   for(i = 0; i < 100000; i+= 10000)
   {
   	write_10k(h1);
   }
   
   offs = yaffs_lseek(h1,0,SEEK_CUR);
   if( offs != 100000)
   {
   	printf("Could not write file\n");
   }
   
   yaffs_unlink(fdel);
   for(i = 0; i < 100000; i+= 10000)
   {
   	write_10k(h1);
   }
   
   offs = yaffs_lseek(h1,0,SEEK_CUR);
   if( offs != 200000)
   {
   	printf("Could not write file\n");
   }
   
   yaffs_close(h1);
   yaffs_unlink(fdel1);
   
}
int test_yaffs_rmdir_ENOTDIR(void)
{
	int output=0;
	int error_code =0;
	if (yaffs_close(yaffs_open(FILE_PATH,O_CREAT | O_RDWR, FILE_MODE))==-1){
		print_message("failed to create file\n",1);
		return -1;
	}
	if (0 !=  yaffs_access(DIR_PATH,0)) {
		output = yaffs_mkdir(DIR_PATH,S_IWRITE | S_IREAD);
		if (output < 0) {
			print_message("failed to create directory\n",2);
			return -1;
		}
	}
	output = yaffs_rmdir("/yaffs2/test_dir/foo/dir/");
	if (output<0){ 
		error_code=yaffs_get_error();
		if (abs(error_code)==ENOTDIR){
			return 1;
		} else {
			print_message("returned error does not match the the expected error\n",2);
			return -1;
		}
	} else{
		print_message("removed /yaffs2/ directory (which is a bad thing)\n",2);
		return -1;
	}	
}
Example #16
0
void make_a_file(char *yaffsName, char bval, int sizeOfFile)
{
	int outh;
	int i;
	unsigned char buffer[100];

	outh = yaffs_open(yaffsName,
				O_CREAT | O_RDWR | O_TRUNC,
				S_IREAD | S_IWRITE);
	if (outh < 0) {
		printf("Error opening file: %d. %s\n", outh, yaffs_error_str());
		return;
	}

	memset(buffer, bval, 100);

	do {
		i = sizeOfFile;
		if (i > 100)
			i = 100;
		sizeOfFile -= i;

		yaffs_write(outh, buffer, i);

	} while (sizeOfFile > 0);


	yaffs_close(outh);
}
Example #17
0
int test_yaffs_open_EACCES(void)
{
	int error_code=0;
	int output =-1;
	if (yaffs_access(FILE_PATH,0)!=0){
		output=yaffs_close(test_yaffs_open());
		if (output<0){
			print_message("failed to open file\n",2);
			return -1;
		}
	}

	output=yaffs_chmod(FILE_PATH,S_IREAD);
	if (output<0){
		print_message("failed to chmod file\n",2);
		return -1;
	}
	handle=yaffs_open(FILE_PATH, O_TRUNC| O_RDWR,FILE_MODE );
	if (handle <0){
		error_code=yaffs_get_error();
		if (abs(error_code)==EACCES){
			return 1;
		}
		else {
			print_message("different error than expected\n",2);
			return -1;
		}
	}
	else {
		print_message("file opened with read and write permissions, chmoded to read only.(which is a bad thing)\n",2);
		return -1;
	}
}
Example #18
0
void fill_disk(const char *path,int nfiles)
{
	int h;
	int n;
	int result;
	int f;

        static char xx[600];	
	char str[50];
	
	for(n = 0; n < nfiles; n++)
	{
		sprintf(str,"%s/%d",path,n);
		
		h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
		
		printf("writing file %s handle %d ",str, h);
		
		while ((result = yaffs_write(h,xx,600)) == 600)
		{
			f = yaffs_freespace(path);
		}
		result = yaffs_close(h);
		printf(" close %d\n",result);
	}
}
Example #19
0
/*==============================================================================
 * - cat()
 *
 * - print a file context
 */
int cat(int argc, char *argv[])
{
    int fd;
    int read_byte;
    char file_context[1024];
    char path_name[PATH_LEN_MAX];

    CHECK_ARG_NUM(2, "please type file name");

    _make_abs_path(path_name, argv[1]);

    /*
     * open file
     */
    fd = yaffs_open(path_name, O_RDONLY, 0);
    if ( fd ==  -1) {
        serial_printf("cannot open file '%s'", path_name);
        return CMD_ERROR;
    }

    /*
     * read and print file context
     */
    read_byte = yaffs_read(fd, file_context, 1023);
    while (read_byte > 0) {
        file_context[read_byte] = '\0';
        serial_printf(file_context);

        read_byte = yaffs_read(fd, file_context, 1023);
    }

    yaffs_close(fd);
    return CMD_OK;
}
Example #20
0
int check_pattern_file(char *fn)
{
	int h;
	int marker;
	int i;
	int size;
	int ok = 1;
	
	h = yaffs_open(fn, O_RDWR,0);
	size = yaffs_lseek(h,0,SEEK_END);
		
	for(i = 0; i < size; i+=256)
	{
		yaffs_lseek(h,i,SEEK_SET);
		yaffs_read(h,&marker,sizeof(marker));
		ok = (marker == ~i);
		if(!ok)
		{
		   printf("pattern check failed on file %s, size %d at position %d. Got %x instead of %x\n",
					fn,size,i,marker,~i);
		}
	}
	yaffs_close(h);
	return ok;
}
Example #21
0
void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2)
{
	int h;
	
	int iterations;
	
	h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
		
	iterations = (syze1 + strlen(fn) -1)/ strlen(fn);
	while (iterations > 0)
	{
		yaffs_write(h,fn,strlen(fn));
		iterations--;
	}
	
	yaffs_ftruncate(h,reSyze);
	
	yaffs_lseek(h,0,SEEK_SET);
	iterations = (syze2 + strlen(fn) -1)/ strlen(fn);
	while (iterations > 0)
	{
		yaffs_write(h,fn,strlen(fn));
		iterations--;
	}
	
	yaffs_close (h);
}
Example #22
0
void verify_file_of_size(const char *fn,int syze)
{
	int h;
	int result;
	
	char xx[200];
	char yy[200];
	int l;
	
	int iterations = (syze + strlen(fn) -1)/ strlen(fn);
	
	h = yaffs_open(fn, O_RDONLY, S_IREAD | S_IWRITE);
		
	while (iterations > 0)
	{
		sprintf(xx,"%s %8d",fn,iterations);
		l = strlen(xx);
		
		result = yaffs_read(h,yy,l);
		yy[l] = 0;
		
		if(strcmp(xx,yy)){
			printf("=====>>>>> verification of file %s failed near position %lld\n",fn,(long long)yaffs_lseek(h,0,SEEK_CUR));
		}
		iterations--;
	}
	yaffs_close (h);
}
Example #23
0
void verify_200k_file(const char *fn)
{
   int h1;
   int i;
   char x[11];
   const char *s="0123456789";
   int errCount = 0;
   
   h1 = yaffs_open(fn, O_RDONLY, 0);
   
   for(i = 0; i < 200000 && errCount < 10; i+= 10)
   {
   	yaffs_read(h1,x,10);
	if(strncmp(x,s,10) != 0)
	{
		printf("File %s verification failed at %d\n",fn,i);
		errCount++;
	}
   }
   if(errCount >= 10)
   	printf("Too many errors... aborted\n");
      
   yaffs_close(h1);	   
	
}
int test_yaffs_fchmod_EROFS_clean(void)
{
	int output =-1;
	if (handle >= 0) {
		output= yaffs_close(handle);
	}
	return (EROFS_clean() && output);
}
int test_yaffs_open_EEXIST_clean(void)
{
	if (handle >=0){
		return yaffs_close(handle);
	} else {
		return 1;	/* the file failed to open so there is no need to close it*/
	}
}
Example #26
0
/*==============================================================================
 * - cp()
 *
 * - copy a file form here to there
 */
int cp(int argc, char *argv[])
{
    int fd_in, fd_out;
    int read_byte, write_byte;
    char file_context[1024];

    char old_path_name[PATH_LEN_MAX];
    char new_path_name[PATH_LEN_MAX];

    CHECK_ARG_NUM(3, "too few argument");

    _make_abs_path(old_path_name, argv[1]);
    fd_in = yaffs_open(old_path_name, O_RDONLY, 0);
    CHECK_YAFFS_RETVAL(fd_in, "can't open file '%s'%s\n", old_path_name, "");

    _make_abs_path(new_path_name, argv[2]);
    fd_out = yaffs_open(new_path_name, O_CREAT|O_RDWR|O_TRUNC, S_IREAD|S_IWRITE);
    if (fd_out == -1) {
        yaffs_close(fd_in);
        serial_printf("can't create file '%s'", new_path_name);
        return CMD_ERROR;
    }

    /*
     * read from fd_in and write to fd_out
     */
    read_byte = yaffs_read(fd_in, file_context, 1024);
    while (read_byte > 0) {
        write_byte = yaffs_write(fd_out, file_context, read_byte);
        if (write_byte != read_byte) {
            serial_printf("copy file failed!");
            break;
        }

        read_byte = yaffs_read(fd_in, file_context, 1024);
    }

    yaffs_close(fd_in);
    yaffs_close(fd_out);

    if (read_byte > 0) {
        return CMD_ERROR;
    } else {
        return CMD_OK;
    }
}
int test_yaffs_ftruncate_EROFS_clean(void)
{
	int output=1;
	if (handle >= 0) {
		output= yaffs_close(handle);
	}
	return (EROFS_clean() && output);
}
Example #28
0
void V5M_SaveImageFile(char *str, char *buf, UINT32 len)
{
	int h;

	yaffs_unlink(str);
	h = yaffs_open(str,  O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
	yaffs_write(h, buf, (unsigned int)len);
	yaffs_close(h);
}
Example #29
0
void SaveImageFile(char *str, char *buf, int len)
{
	int h;

	yaffs_unlink(str);
	h = yaffs_open(str,  O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
	yaffs_write(h, buf, len);
	yaffs_close(h);
}
int test_yaffs_access(void)
{

    if (-1==yaffs_close(yaffs_open(FILE_PATH,O_CREAT | O_RDWR, FILE_MODE))) {
        print_message("failed to create file\n",1);
        return -1;
    }
    return yaffs_access(FILE_PATH,0);
}