コード例 #1
0
ファイル: test_fat.c プロジェクト: mitlab/oggbox
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);
}
コード例 #2
0
ファイル: test_embext.c プロジェクト: hairymnstr/gristle
int main(int argc, char *argv[]) {
  int p = 0;
  int result;
  char buffer[256];
  struct stat st;
  struct ext2context *context;
  printf("Running EXT2 tests...\n\n");
  block_pc_set_image_name("testext.img");
  printf("[%4d] start block device emulation...", p++);
  result = block_init();
  printf("   %d\n", result);
  if(result != 0) {
      exit(0);
  }
  
  printf("[%4d] mount filesystem, FAT32", p++);
  
  result = ext2_mount(0, block_get_volume_size(), 0, &context);

  printf("   %d\n", result);
  
  struct file_ent *fe = ext2_open(context, "/", O_RDONLY, 0777, &result);
  struct file_ent *fe2;
  struct dirent *de;
  
  while((de = ext2_readdir(fe, &result))) {
    snprintf(buffer, sizeof(buffer), "/%s", de->d_name);
    fe2 = ext2_open(context, buffer, O_RDONLY, 0777, &result);
    ext2_fstat(fe2, &st, &result);
    printf("%s %d\n", de->d_name, (int)st.st_size);
    if((st.st_mode & S_IFDIR) && (strcmp(de->d_name, ".") != 0) &&
       (strcmp(de->d_name, "..") != 0)) {
//       printf("Directory contents:\n");
//       printf("fe2->cursor: %d\n", fe2->cursor);
//       printf("fe2->file_sector: %d\n", fe2->file_sector);
      while((de = ext2_readdir(fe2, &result))) {
        printf("  %s [%d]\n", de->d_name, de->d_ino);
      }
    }
    ext2_close(fe2, &result);
  }
  ext2_close(fe, &result);
  
  FILE *fw = fopen("dump.png", "wb");
  fe = ext2_open(context, "/static/test_image.png", O_RDONLY, 0777, &result);
  printf("%p\n", fe);
  printf("fe->inode_number = %d\n", fe->inode_number);
  while((p = ext2_read(fe, &buffer, sizeof(buffer), &result)) == sizeof(buffer)) {
    fwrite(buffer, 1, p, fw);
  }
  if(p > 0) {
    fwrite(buffer, 1, p, fw);
  }
  fclose(fw);
  ext2_close(fe, &result);
  
  printf("\nWrite test...\n\n");
  
  fe = ext2_open(context, "/logs/test.txt", O_WRONLY | O_APPEND, 0777, &result);
  if(fe == NULL) {
      printf("Open for writing failed, errno=%d (%s)\r\n", result, strerror(result));
      exit(-1);
  }
  
  ext2_write(fe, "Hello world\r\n", 13, &result);
  
  ext2_close(fe, &result);
  
  ext2_print_bg1_bitmap(context);
  
  ext2_umount(context);
  
  block_pc_snapshot_all("writenfs.img");
  
  block_halt();
  
  exit(0);
}
コード例 #3
0
ファイル: test_gristle.c プロジェクト: hairymnstr/gristle
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);
}