Example #1
0
int main(int argc, char *argv[])
{
	struct block_device *bldev;
	FatVol vol;
	FatFile file;
	char *rootpath = argc > 2 ? argv[2] : "/";

	bldev = block_device_file_new(argc > 1 ? argv[1] : "fat32.img", "r+");
	assert(bldev != NULL);

	assert(fat_vol_init(bldev, &vol) == 0);
	fprintf(stderr, "Fat type is FAT%d\n", vol.type);

	fat_mkdir(&vol, "Directory1");
	fat_mkdir(&vol, "Directory2");
	fat_mkdir(&vol, "Directory3");
	assert(fat_chdir(&vol, "Directory1") == 0);
	fat_mkdir(&vol, "Directory1");
	fat_mkdir(&vol, "Directory2");
	fat_mkdir(&vol, "Directory3");
	if(fat_create(&vol, "Message file with a long name.txt", O_WRONLY, &file) == 0) {
		for(int i = 0; i < 100; i++) {
			char message[80];
			sprintf(message, "Here is a message %d\n", i);
			assert(fat_write(&file, message, strlen(message)) == (int)strlen(message));
		}
	}
	assert(fat_chdir(&vol, "..") == 0);
	assert(fat_open(&vol, ".", O_RDONLY, &file) == 0);
	print_tree(&vol, &file, rootpath[0] == '/' ? rootpath + 1 : rootpath);

	block_device_file_destroy(bldev);
}
int fs_remove(char *file_name) {
  //printf("Função não implementada: fs_remove\n");

  int i;

  i =0;
  while(i < 128 && !(!strcmp(file_name, dir[i].name) && dir[i].used == 'T')){
    i++;
  }
  if ( i == 128){
    printf("Arquivo nao encontrado\n");
    return 0;
  }
  int marca_dir =i;

  dir[marca_dir].used = 'F';
  dir[marca_dir].size = 0;


  i = dir[marca_dir].first_block;
  dir[marca_dir].first_block = -1;

  //loop para correr blocos do arquivo
  while(i > 2){
    int aux = dir[i].first_block;
    fat[i] = 1;
    i = aux;
  }

  fat_write();
  //retorno arquivo removido com sucesso
  return 1;
}
int fs_format() {
    //printf("Função não implementada: fs_format\n");
    int i;
    /*inicializando a FAT*/
    for(i = 0; i < 65536; i++) {
        if(i < 32) {
            fat[i] = 3;
        } else {
            if(i > 32) {
                fat[i] = 1;
            } else {
                fat[i] = 4;
            }
        }
    }

    /*Formata o diretório*/
    for (i = 0; i< 128; i++) {
        dir[i].used = '0';
    }

    /*Salva a FAT e seta a variável que informa se a FAT está formatada ou não*/
    fat_write();
    formatado = 1;
    return 1;
}
int fs_create(char* file_name) {
    //printf("Função não implementada: fs_create\n");
    int i, pos = -1, pos_fat = -1;

    if(formatado == 0) {
        printf("Disco não formatado\n");
        return 0;
    }


    //verifica o nome do arquivo, para que nao exista arquivos com nomes duplicados
    //posicao guarda a primeira posicao livre encontrada no diretorio
    for(i = 0; i < 128; i++) {
        if(dir[i].used == '1' && strcmp(dir[i].name,file_name) == 0) {
            printf("Arquivo ja existente\n");
            return 0;
        } else {
            if(dir[i].used == '0' && pos == -1) {
                pos = i;
            }
        }
    }

    //se a posicao e invalida, nao há espaco no diretorio
    if(pos == -1) {
        printf("\nNão há espaço no diretório\n");
        return 0;
    }

    //procura a primeira posiçao livre na fat para alocar o arquivo
    for(i = 33; i < 65536; i++) {
        if (fat[i] == 1) {
            pos_fat = i;
            fat[i] = 2;
            break;
        }
    }

    //se a posicao for invalida nao há espaço livre
    if(pos_fat == -1) {
        printf("Fat cheia\n");
        return 0;
    }

    dir[pos].first_block = pos_fat;
    dir[pos].used = '1';
    strcpy(dir[pos].name, file_name);
    dir[pos].size = 0;
    arquivo[pos_fat].aberto = -1; //cria o arquivo e seta ele como fechado

    fat_write();

    return pos;
}
Example #5
0
int main(int argc, char *argv[]) {
  int p = 0;
  int rerrno = 0;
  FILE *fp;
  int len;
  uint8_t *d;
//   int v;
  printf("Running FAT tests...\n\n");
  printf("[%4d] start block device emulation...", p++);
  printf("   %d\n", block_init());
  
  printf("[%4d] mount filesystem, FAT32", p++);
  printf("   %d\n", fat_mount(0, PART_TYPE_FAT32));

//   p = test_open(p);

  int fd;
  
//   printf("Open\n");
//   fd = fat_open("/newfile.txt", O_WRONLY | O_CREAT, 0777, &rerrno);
//   printf("fd = %d, errno=%d (%s)\n", fd, rerrno, strerror(rerrno));
//   if(fd > -1) {
//     printf("Write\n");
//     fat_write(fd, "Hello World\n", 12, &rerrno);
//     printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
//     printf("Close\n");
//     fat_close(fd, &rerrno);
//     printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
//   }
  
  printf("Open\n");
  fd = fat_open("/newfile.png", O_WRONLY | O_CREAT, 0777, &rerrno);
  printf("fd = %d, errno=%d (%s)\n", fd, rerrno, strerror(rerrno));
  if(fd > -1) {
    fp = fopen("/home/nathan/gowrong_draft1.png", "rb");
    fseek(fp, 0, SEEK_END);
    len = ftell(fp);
    d = malloc(len);
    fseek(fp, 0, SEEK_SET);
    fread(d, 1, len, fp);
    fclose(fp);
    printf("Write PNG\n");
    fat_write(fd, d, len, &rerrno);
    printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
    printf("Close\n");
    fat_close(fd, &rerrno);
    printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
  }
  
  block_pc_snapshot_all("writenfs.img");
  exit(0);
}
Example #6
0
static void create_test(Volume *v, const char *pn)
{
	Channel *c;
	char buf[2048];
	int res;
	res = fat_open_pr(&v->root_dentry, pn, strlen(pn), O_RDWR | O_CREAT | O_EXCL, 0, &c);
	printf("Open: %i (%s)\n", res, strerror(-res));
	for (res = 0; res < sizeof(buf); res++) buf[res] = (res % 10) + '0';
	res = fat_write(c, buf, sizeof(buf));
	printf("Write: %i (%s)\n", res, strerror(-res));
	res = fat_close(c);
	printf("Close: %i (%s)\n", res, strerror(-res));
}
int fs_create(char* file_name) {
  //printf("Função não implementada: fs_create\n");

  int i;
  //procura por arquivo existente
  for ( i = 0; i < 128; i++){
    if(!strcmp(file_name, dir[i].name) && dir[i].used == 'T'){
      printf("Arquivo ja existe.\n");
      return 0;
    }
  }
  i =0;
  while( i < 128 && dir[i].used != 'F'){
    i++;
  }
  if(i == 128){
    printf("Disco cheio\n");
    return 0;
  }
  int marca_dir = i;


  i = 33;
  while( (i < bl_size()/8) && fat[i]== 1){
    i++;
  }
  if(i == (bl_size()/8)){
    printf("Fat cheio\n");
    return 0;
  }
  int marca_fat = i;

  //insere valores na primeira posicao vazia encontrada
  dir[marca_dir].used = 'T';
  dir[marca_dir].first_block = marca_fat;
  strcpy(dir[marca_dir].name, file_name);
  dir[marca_dir].size = 0;

  //marca fat como usado
  fat[marca_fat] = 2;
  fat_write();
  //retorno arquivo criado com sucesso
  return 1;
}
int fs_format() {
  //printf("Função não implementada: fs_format\n");

  int i;
  // inicia fat
  fat[32] = 4;
  for ( i = 0 ; i < 32 ; i++){
    fat[i] = 3;
  }
  for (i = 33; i < 65536; i++){
    fat[i]= 1;
  }
  // marca dir como nao usado
  for( i = 0 ; i < 128; i++){
    dir[i].used = 'F'; // 'F' para false
  }
  fat_write();
  return 0;
}
Example #9
0
int main()
{
	int res;
	Volume *v;
	nls_init();
	res = fat_mount("../img/floppy.img", &v);
	printf("Mount: %i (%s)\n", res, strerror(-res));
	//for (res = 0; res < 100000; res++)
	//unlink_test(v, "\\grub\\menu.lst");
	//rmdir_test(v, "\\faccio un provolone");
	rename_test(v, "\\grub\\rentest", "\\puppo");
		//read_test(v, "\\grub\\menu.lst");
		//lfnfind_test(v, "\\grub", "*.*");
		//create_test(v, "\\faccio una prova.txt");
		//mkdir_test(v, "\\faccio un provolone");
		//dosfind_test(v, "\\autoexec.bat\\*.*");
	res = fat_unmount(v);
	printf("Unmount: %i (%s)\n", res, strerror(-res));
	#if 0
	//res = fat_open(v, "\\autoexec.bat", NULL, O_RDWR, 0, &c);
	res = fat_open(v, "\\faccio una prova.txt", NULL, O_RDWR | O_CREAT | O_EXCL, 0, &c);
	printf("Open: %i (%s)\n", res, strerror(-res));
	#if 1 /* write */
	res = fat_lseek(c, 0, SEEK_SET);
	printf("Seek: %i (%s)\n", res, strerror(-res));
	for (k = 0; k < sizeof(buf); k++) buf[k] = (k % 10) + '0';
	res = fat_write(c, buf, sizeof(buf));
	printf("Write: %i (%s)\n", res, strerror(-res));
	#elif 0 /* read */
	res = fat_read(c, buf, sizeof(buf));
	printf("Read: %i (%s)\n", res, strerror(-res));
	for (k = 0; k < res; k++) fputc(buf[k], stdout);
	#elif 0 /* truncate */
	res = fat_ftruncate(c, 65432);
	printf("Truncate: %i (%s)\n", res, strerror(-res));
	#endif
	res = fat_close(c);
	printf("Close: %i (%s)\n", res, strerror(-res));
	res = fat_unmount(v);
	printf("Unmount: %i (%s)\n", res, strerror(-res));
	#endif
	return 0;
}
Example #10
0
int main(void)
{
	struct mmc_port spi2;
	struct block_mbr_partition part;
	struct fat_vol_handle vol;
	struct fat_file_handle file;

	stm32_setup();

	mmc_init(SPI2, GPIOA, GPIO3, &spi2);
	mbr_partition_init(&part, (struct block_device *)&spi2, 0);

	assert(fat_vol_init((struct block_device *)&part, &vol) == 0);
	printf("Fat type is FAT%d\n", vol.type);

	time_counter = 0;
	char dirname[20];
	char filename[20];
	char buffer[2000];
	for(int i = 0; i < 100; i++) {
		sprintf(dirname, "Dir%d", i);
		fat_mkdir(&vol, dirname);
		assert(fat_chdir(&vol, dirname) == 0);
		for(int j = 0; j < 100; j++) {
			sprintf(filename, "File%d", j);
			assert(fat_create(&vol, filename, O_WRONLY, &file) == 0);
			assert(fat_write(&file, buffer, sizeof(buffer)) == sizeof(buffer));
		}
		assert(fat_chdir(&vol, "..") == 0);
	}
	asm("bkpt");

	assert(fat_open(&vol, ".", 0, &file) == 0);
	print_tree(&vol, &file, 0);

	while (1) {
	}

	return 0;
}
int fs_remove(char *file_name) {
    //printf("Função não implementada: fs_remove\n");
    int flag = 0, i, fat_aux, pos;

    if(formatado == 0) {
        printf("Disco não formatado\n");
        return 0;
    }

    for (i = 0; i < 128; i++) {
        if(dir[i].used != '0' && strcmp(dir[i].name,file_name) == 0) {
            flag = 1;
            break;
        }
    }

    if (flag == 0) {
        printf("Arquivo inexistente\n");
        return 0;
    }
    arquivo[i].aberto = -2; //com o arquivo removido deve setar -2 para inexistente
    dir[i].used = '0';
    dir[i].size = 0;
    pos = i;
    i = dir[i].first_block;
    dir[pos].first_block = -1;

    while(i != 2) {
        fat_aux = fat[i];
        fat[i] = 1;
        i = fat_aux;
    }

    fat_write();
    return 1;
}
Example #12
0
int main (){
	//s32 i, j;
	//s32 len, status;
	FATTIE * fp;
	u8 * str = (u8 *)"Test string to demonstrate that\nsdcard filesystem works properly\n";

	init_platform();

	while(*cd_ptr != 0);

	sd_trivial_delay(1000000);

	spi_dev_status |= SD_FS_CARD_DETECT;

	switch(init_fat_filesystem()) {
	case 0:
		xil_printf("microSD initialized successfully\n\r");
		break;

	default:
		xil_printf("Exiting\n\r");
		return -1;
	}

	fp = fat_open((u8 *)"TESTERIN.SGF");

	if(fp == NULL) {
		xil_printf("Couldn't open file\n\r");
		goto end;
	}

	// Pass in the sgf config structure into init_sgf_file() for writing the header of SGF
	init_sgf_file(fp);


	/* Read from memory */

	// Just for testing the function out
	Move one, two, three, four;
	one.stoneColor = BLACK_STONE;
	one.stoneX = 3;
	one.stoneY = 3;

	two.stoneColor = WHITE_STONE;
	two.stoneX = 15;
	two.stoneY = 15;

	three.stoneColor = BLACK_STONE;
	three.stoneX = 15;
	three.stoneY = 3;

	four.stoneColor = WHITE_STONE;
	four.stoneX = 3;
	four.stoneY = 15;

	// This is where you append the newly placed stone in the SGF file (filename)
	// Obviously, you can just declare one Move struct and reuse it using a while loop until stoneColor == 0 like below
	write_sgf_move(fp, one.stoneX, one.stoneY, one.stoneColor);
	write_sgf_move(fp, two.stoneX, two.stoneY, two.stoneColor);
	write_sgf_move(fp, three.stoneX, three.stoneY, three.stoneColor);
	write_sgf_move(fp, four.stoneX, four.stoneY, four.stoneColor);

	/*
	Move tmp;
	unsigned int offset = 0;

	tmp = *(some_memory_address + offset);

	while(tmp.stoneColor != NO_STONE) {
		write_sgf_move(filename, tmp.stoneX, tmp.stoneY, tmp.stoneColor);
		offset += 3;    // The structs are stacked perfectly into the memory, so add 3 bytes.
		tmp = *(some_memory_address + offset);
	}
	*/

	/* Write a simple ")" to close off the main branch in SGF */
	// You may want to read extra Move from the memory and check if the stoneColor == 0;
	end_sgf_file(fp);

	fat_close(fp);

	fp = fat_open((u8 *)"ABCDEFGH.TXT");

	fat_write(fp, str, strlen(str));

	fat_close(fp);

	end: sd_eject_card();


	return 0;
}
Example #13
0
int main(int argc, char *argv[]) {
    int p = 0;
    int rerrno = 0;
    int result;
    int parts;
    uint8_t temp[512];
    struct partition *part_list;

    if(argc < 2) {
        printf("Please specify a disk image to work on.\n");
        exit(-2);
    }

    block_pc_set_image_name(argv[1]);
//   int v;
    printf("Running FAT tests...\n\n");
    printf("[%4d] start block device emulation...", p++);
    printf("   %d\n", block_init());

    printf("[%4d] mount filesystem, FAT32", p++);

    result = fat_mount(0, block_get_volume_size(), PART_TYPE_FAT32);

    printf("   %d\n", result);

    if(result != 0) {
        // mounting failed.
        // try listing the partitions
        block_read(0, temp);
        parts = read_partition_table(temp, block_get_volume_size(), &part_list);

        printf("Found %d valid partitions.\n", parts);

        if(parts > 0) {
            result = fat_mount(part_list[0].start, part_list[0].length, part_list[0].type);
        }
        if(result != 0) {
            printf("Mount failed\n");
            exit(-2);
        }

    }

    printf("Part type = %02X\n", fatfs.type);
    //   p = test_open(p);

    int fd;
    int i;
    char block_o_data[1024];
    uint32_t temp_uint = 0xDEADBEEF;
    memset(block_o_data, 0x42, 1024);
//   printf("Open\n");
//   fd = fat_open("/newfile.txt", O_WRONLY | O_CREAT, 0777, &rerrno);
//   printf("fd = %d, errno=%d (%s)\n", fd, rerrno, strerror(rerrno));
//   if(fd > -1) {
//     printf("Write\n");
//     fat_write(fd, "Hello World\n", 12, &rerrno);
//     printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
//     printf("Close\n");
//     fat_close(fd, &rerrno);
//     printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
//   }

//   printf("Open\n");
//   fd = fat_open("/newfile.png", O_WRONLY | O_CREAT, 0777, &rerrno);
//   printf("fd = %d, errno=%d (%s)\n", fd, rerrno, strerror(rerrno));
//   if(fd > -1) {
//     fp = fopen("gowrong_draft1.png", "rb");
//     fseek(fp, 0, SEEK_END);
//     len = ftell(fp);
//     d = malloc(len);
//     fseek(fp, 0, SEEK_SET);
//     fread(d, 1, len, fp);
//     fclose(fp);
//     printf("Write PNG\n");
//     fat_write(fd, d, len, &rerrno);
//     printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
//     printf("Close\n");
//     fat_close(fd, &rerrno);
//     printf("errno=%d (%s)\n", rerrno, strerror(rerrno));
//   }

    printf("errno = (%d) %s\n", rerrno, strerror(rerrno));
    result = fat_mkdir("/foo", 0777, &rerrno);
    printf("mkdir /foo: %d (%d) %s\n", result, rerrno, strerror(rerrno));

    result = fat_mkdir("/foo/bar", 0777, &rerrno);
    printf("mkdir /foo/bar: %d (%d) %s\n", result, rerrno, strerror(rerrno));

    result = fat_mkdir("/web", 0777, &rerrno);
    printf("mkdir /web: %d (%d) %s\n", result, rerrno, strerror(rerrno));

    if((fd = fat_open("/foo/bar/file.html", O_WRONLY | O_CREAT, 0777, &rerrno)) == -1) {
        printf("Couldn't open file (%d) %s\n", rerrno, strerror(rerrno));
        exit(-1);
    }

    for(i=0; i<20; i++) {
//     printf("fd.cluster = %d\n", file_num[fd].full_first_cluster);
        if(fat_write(fd, block_o_data, 1024, &rerrno) == -1) {
            printf("Error writing to new file (%d) %s\n", rerrno, strerror(rerrno));
        }
    }

    if(fat_close(fd, &rerrno)) {
        printf("Error closing file (%d) %s\n", rerrno, strerror(rerrno));
    }

    printf("Open directory\n");
    if((fd = fat_open("/foo/bar", O_RDONLY, 0777, &rerrno)) < 0) {
        printf("Failed to open directory (%d) %s\n", rerrno, strerror(rerrno));
        exit(-1);
    }
    struct dirent de;

    while(!fat_get_next_dirent(fd, &de, &rerrno)) {
        printf("%s\n", de.d_name);
    }
    printf("Directory read failed. (%d) %s\n", rerrno, strerror(rerrno));

    if(fat_close(fd, &rerrno)) {
        printf("Error closing directory, (%d) %s\n", rerrno, strerror(rerrno));
    }

    if(fat_open("/web/version.txt", O_RDONLY, 0777, &rerrno) < 0) {
        printf("Error opening missing file (%d) %s\n", rerrno, strerror(rerrno));
    } else {
        printf("success! opened non existent file for reading.\n");
    }

    printf("Trying to write a big file.\n");

    if((fd = fat_open("big_file.bin", O_WRONLY | O_CREAT, 0777, &rerrno))) {
        printf("Error opening a file for writing.\n");
    }

    for(i=0; i<1024 * 1024 * 10; i++) {
        if((i & 0xfff) == 0)
            printf("Written %d bytes\n", i * 4);
        fat_write(fd, &temp_uint, 4, &rerrno);
    }
    fat_close(fd, &rerrno);

//   result = fat_rmdir("/foo/bar", &rerrno);
//   printf("rmdir /foo/bar: %d (%d) %s\n", result, rerrno, strerror(rerrno));
//
//   result = fat_rmdir("/foo", &rerrno);
//   printf("rmdir /foo: %d (%d) %s\n", result, rerrno, strerror(rerrno));

    block_pc_snapshot_all("writenfs.img");
    exit(0);
}
Example #14
0
int fat_request(DWORD Function, void *Params)
{
  int Res;
  switch (Function)
  {
    case FD32_READ:
    {
      fd32_read_t *X = (fd32_read_t *) Params;
      tFile       *F;
      if (X->Size < sizeof(fd32_read_t)) return EINVAL;
      F = (tFile *) X->DeviceId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_read(F, X->Buffer, X->BufferBytes);
    }
    case FD32_WRITE:
    {
      #ifdef FATWRITE
      fd32_write_t *X = (fd32_write_t *) Params;
      tFile        *F;
      if (X->Size < sizeof(fd32_write_t)) return EINVAL;
      F = (tFile *) X->DeviceId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_write(F, X->Buffer, X->BufferBytes);
      #else
      return EROFS;
      #endif
    }
    case FD32_LSEEK:
    {
      fd32_lseek_t *X = (fd32_lseek_t *) Params;
      if (X->Size < sizeof(fd32_lseek_t)) return EINVAL;
      if (((tFile *) X->DeviceId)->FilSig != FAT_FILSIG) return EBADF;
      return fat_lseek((tFile *) X->DeviceId, &X->Offset, X->Origin);
    }
    case FD32_OPENFILE:
    {
      fd32_openfile_t *X = (fd32_openfile_t *) Params;
      tVolume         *V;
      if (X->Size < sizeof(fd32_openfile_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_open(V, X->FileName, X->Mode, X->Attr, X->AliasHint, (tFile **) &X->FileId);
    }
    case FD32_CLOSE:
    {
      fd32_close_t *X = (fd32_close_t *) Params;
      tFile        *F;
      if (X->Size < sizeof(fd32_close_t)) return EINVAL;
      F = (tFile *) X->DeviceId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_close(F);
    }
    case FD32_READDIR:
    {
      fd32_readdir_t *X = (fd32_readdir_t *) Params;
      tFile          *F;
      if (X->Size < sizeof(fd32_readdir_t)) return EINVAL;
      F = (tFile *) X->DirId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_readdir(F, (fd32_fs_lfnfind_t *) X->Entry);
    }
    case FD32_FFLUSH:
    {
      #ifdef FATWRITE
      fd32_fflush_t *X = (fd32_fflush_t *) Params;
      tFile         *F;
      if (X->Size < sizeof(fd32_fflush_t)) return EINVAL;
      F = (tFile *) X->DeviceId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_fflush(F);
      #else
      return EROFS;
      #endif
    }
    case FD32_OPEN:
    {
      fd32_open_t *X = (fd32_open_t *) Params;
      if (X->Size < sizeof(fd32_open_t)) return EINVAL;
      if (((tFile *) X->DeviceId)->FilSig != FAT_FILSIG) return EBADF;
      return ++((tFile *) X->DeviceId)->References;
    }
    case FD32_GETATTR:
    {
      fd32_getattr_t *X = (fd32_getattr_t *) Params;
      if (X->Size < sizeof(fd32_getattr_t)) return EINVAL;
      if (((tFile *) X->FileId)->FilSig != FAT_FILSIG) return EBADF;
      return fat_get_attr((tFile *) X->FileId, (fd32_fs_attr_t *) X->Attr);
    }
    case FD32_SETATTR:
    {
      #ifdef FATWRITE
      fd32_setattr_t *X = (fd32_setattr_t *) Params;
      tFile          *F;
      if (X->Size < sizeof(fd32_setattr_t)) return EINVAL;
      F = (tFile *) X->FileId;
      if (F->FilSig != FAT_FILSIG) return EBADF;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(F->V)) < 0) return Res;
      #endif
      return fat_set_attr(F, (fd32_fs_attr_t *) X->Attr);
      #else
      return EROFS;
      #endif
    }
    case FD32_REOPENDIR:
    {
      fd32_reopendir_t *X = (fd32_reopendir_t *) Params;
      tVolume          *V;
      if (X->Size < sizeof(fd32_reopendir_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_reopendir(V, (tFindRes *) X->DtaReserved, (tFile **) &X->DirId);
    }
    case FD32_UNLINK:
    {
      #ifdef FATWRITE
      fd32_unlink_t *X = (fd32_unlink_t *) Params;
      tVolume       *V;
      if (X->Size < sizeof(fd32_unlink_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_unlink(V, X->FileName, X->Flags);
      #else
      return EROFS;
      #endif
    }
    case FD32_RENAME:
    {
      #ifdef FATWRITE
      fd32_rename_t *X = (fd32_rename_t *) Params;
      tVolume       *V;
      if (X->Size < sizeof(fd32_rename_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_rename(V, X->OldName, X->NewName);
      #else
      return EROFS;
      #endif
    }
    case FD32_MKDIR:
    {
      #ifdef FATWRITE
      fd32_mkdir_t *X = (fd32_mkdir_t *) Params;
      tVolume      *V;
      if (X->Size < sizeof(fd32_mkdir_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_mkdir(V, X->DirName);
      #else
      return FD32_EROFS;
      #endif
    }
    case FD32_RMDIR:
    {
      #ifdef FATWRITE
      fd32_rmdir_t *X = (fd32_rmdir_t *) Params;
      tVolume      *V;
      if (X->Size < sizeof(fd32_rmdir_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_rmdir(V, X->DirName);
      #else
      return FD32_EROFS;
      #endif
    }
    case FD32_MOUNT:
    {
      fd32_mount_t *X = (fd32_mount_t *) Params;
      if (X->Size < sizeof(fd32_mount_t)) return EINVAL;
      return fat_mount(X->hDev, (tVolume **) &X->FsDev);
    }
    case FD32_UNMOUNT:
    {
      fd32_unmount_t *X = (fd32_unmount_t *) Params;
      tVolume        *V;
      if (X->Size < sizeof(fd32_unmount_t)) return EINVAL;
      V = (tVolume *) X->DeviceId;
      if (V->VolSig != FAT_VOLSIG) return ENODEV;
      #ifdef FATREMOVABLE
      if ((Res = fat_mediachange(V)) < 0) return Res;
      #endif
      return fat_unmount(V);
    }
    case FD32_PARTCHECK:
    {
      fd32_partcheck_t *X = (fd32_partcheck_t *) Params;
      if (X->Size < sizeof(fd32_partcheck_t)) return EINVAL;
      return fat_partcheck(X->PartId);
    }
    case FD32_GETFSINFO:
    {
      fd32_getfsinfo_t *X = (fd32_getfsinfo_t *) Params;
      if (X->Size < sizeof(fd32_getfsinfo_t)) return EINVAL;
      return fat_get_fsinfo((fd32_fs_info_t *) X->FSInfo);
    }
    case FD32_GETFSFREE:
      return fat_get_fsfree((fd32_getfsfree_t *) Params);
  }
  return EINVAL;
}
Example #15
0
int __write (int handle, const U8 *buf, U32 len) {
  /* Low level file write function. */
  FALLOC alloc;
  IOB *fcb;
  BOOL wralloc;
  U32 size;

  START_LOCK (int);

  if (buf == NULL) {
    /* Invalid buffer, return error. */
    RETURN (-1);
  }
  fcb = &_iob[handle];

  if (fcb->drive == DRV_MCARD) {
    /* Write data to Flash Memory Card. */
    if (fat_write (fcb, buf, len) == __FALSE) {
      /* Error, data not written to file. */
      RETURN (-1);
    }
    RETURN (0);
  }

  wralloc = __FALSE;
  if (len & 3) {
    wralloc = __TRUE;
  }
  while (len) {
    size = fcb->_ftop - fcb->_fbot - sizeof (FALLOC);
    if (size > len) {
      size = len;
    }
    len -= size;
    fs_WriteBlock (((DEVCONF *)fcb->DevCfg)[fcb->_fblock].bStart + fcb->_fbot,
                   (void *)buf, size, fcb);
    buf += size;
    fcb->_fbot += size;

    if (fs_BlockFull (fcb) == __TRUE) {
      /* Current Flash Block is full, allocate next one */
      if (fs_AllocBlock (fcb) == __FALSE) {
        RETURN (-1);
      }
      /* Do not write alloc record for empty segment. */
      if (len == 0) {
        goto ex;
      }
    }
  }
  if (wralloc == __FALSE) {
    /* Set flag 'Write Allocation Requested'. */
    fcb->flags |= _IOWALLOC;
  }
  else {
    /* If buffer is not full, write also allocation record. */
    alloc.end    = fcb->_fbot;
    alloc.fileID = fcb->fileID;
    alloc.index  = fcb->_fidx++;
    fs_WriteBlock (((DEVCONF *)fcb->DevCfg)[fcb->_fblock].bStart + fcb->_ftop,
                   &alloc, sizeof (FALLOC), fcb);
    /* Always write on 4-byte boundaries. */
    fcb->_ftop -= sizeof (FALLOC);
    fcb->_fbot = (fcb->_fbot + 3) & ~3;
ex: fcb->flags &= ~_IOWALLOC;
  }
  RETURN (0);

  END_LOCK;
}