Exemplo n.º 1
0
/**
 * Tests having multiple files openned and written at the same time
 */
int multipleOpenFilesTest() {
    int hr = SUCCESS;
    int fd, fd2, fd3, fsize = SD_SECTORSIZE;
    char *fname1 = "simult1";
    char *fname2 = "simult2";
    char *fname3 = "simult3";
    char *buffer = malloc(fsize);
    initBuffer(buffer, fsize);

    FAIL_BRK4(initAndLoadDisk());
    FAIL_BRK4(initFS());

    // Openning the files
    fd = sfs_fopen(fname1);
    FAIL_BRK3((fd == -1), stdout, "Error: fopen for (%s) failed\n", fname1);

    fd2 = sfs_fopen(fname2);
    FAIL_BRK3((fd2 == -1), stdout, "Error: fopen for (%s) failed\n", fname2);

    fd3 = sfs_fopen(fname3);
    FAIL_BRK3((fd3 == -1), stdout, "Error: fopen for (%s) failed\n", fname3);

    // Writing in a different order
    FAIL_BRK3((sfs_fwrite(fd2, buffer, fsize) != fsize), stdout,
            "Error: Write failed\n");
    FAIL_BRK3((sfs_fwrite(fd, buffer, fsize) != fsize), stdout,
            "Error: Write failed\n");
    FAIL_BRK3((sfs_fwrite(fd3, buffer, fsize) != fsize), stdout,
            "Error: Write failed\n");

    // Closing the files in different order
    FAIL_BRK3(sfs_fclose(fd3), stdout, "Error: Closing the file failed\n");
    FAIL_BRK3(sfs_fclose(fd2), stdout, "Error: Closing the file failed\n");
    FAIL_BRK3(sfs_fclose(fd), stdout, "Error: Closing the file failed\n");

    FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: ls failed\n");

    // reload the disk and verify
    FAIL_BRK4(saveAndCloseDisk());
    FAIL_BRK4(initAndLoadDisk());
    FAIL_BRK4(verifyFile(fname1, buffer, fsize));
    FAIL_BRK4(verifyFile(fname2, buffer, fsize));
    FAIL_BRK4(verifyFile(fname3, buffer, fsize));

    Fail:

    SAFE_FREE(buffer);

    saveAndCloseDisk();
    PRINT_RESULTS("Multiple Open Files Test");
    return hr;
}
Exemplo n.º 2
0
/**
 * Checks if the contents of the file match the give data.
 */
int verifyFile(char *fileName, char *data, int fsize) {
    int hr = SUCCESS;
    /* open a file and verify the contents of the file are correct */
    char data2[fsize];
    // open file
    int fd = sfs_fopen(fileName);
    FAIL_BRK3((fd == -1), stdout,
            "Error: Unable to open file %s with intention to read\n", fileName);
    // read file
    FAIL_BRK3((sfs_fread(fd, data2, fsize) != fsize), stdout,
            "Error: failed to read %d bytes to %s, fd:%d\n", fsize, fileName,
            fd);

    FAIL_BRK3(checkBuffers(data, data2, fsize, 0), stdout,
            "Error: contents didn't match\n");

    // TODO: We should not allow reading pass the end of the file...
    //int bread;
    //FAIL_BRK( ((bread = sfs_fread(fd, data2, 1)) == 1), stdout, "Error: successfully reading more data when we didn't expected it %d\n", bread);

    // close file
    FAIL_BRK3(sfs_fclose(fd), stdout, "Error: Unable to close fd: %d\n", fd);

    Fail: return hr;
}
Exemplo n.º 3
0
static int fuse_read(const char *path, char *buf, size_t size, off_t offset,
        struct fuse_file_info *fi)
{
    int fd;
    int res;
    
    char filename[MAXFILENAME];
    
    printf("read\n");
    strcpy(filename, path);
    
    fd = sfs_fopen(filename);
    if (fd == -1)
        return -errno;
    
    if(sfs_fseek(fd, offset) == -1)
        return -errno;
    
    res = sfs_fread(fd, buf, size);
    if (res == -1)
        return -errno;
    
    sfs_fclose(fd);
    return res;
}
Exemplo n.º 4
0
int sfs_remove(char *file) {
	int i, k, j, l;

	for (i=0;i<=MAX_INODES;i++){
                //File exists
                if(!strcmp(rootDirectory.entry[i].name, file)){
                        //Checks if file is open
                        for (k = 0;k<MAXOPENFILES;k++){
                                if (!strcmp((*fileDescriptorTable).entry[k].name, file)){
                                       printf("File is open, closing....\n"); 
					sfs_fclose(k);
					break;
                                }
                        }
			//Free and zero the blocks that the dataptrs attached to the file's INode point to
			for (j = 0;j<NUM_DATA_POINTERS;j++){
				if (rootDirectory.entry[i].inode.dataPointers[j] == 0) break;
				//Frees the block in the freeBlock Array and the dataptr in the rootDirectory inode for the file
				freeBlocks.freeBlocks[rootDirectory.entry[i].inode.dataPointers[j]] = 0;
				freeBlocks.numberFree++;
				rootDirectory.entry[i].inode.dataPointers[j] = 0;
			}		
			rootDirectory.entry[i].inode.size = 0;
			rootDirectory.entry[i].name[0] = '\0';
			write_blocks(1,1,&rootDirectory);
			write_blocks(MAX_BLOCKS-1, 1, &freeBlocks);
			
			//return 0 if successful
			return 0;
		}
	}
	printf("File doesn't exist, can't delete a file that doesn't exist\n");
	return -1; 
}
Exemplo n.º 5
0
static int fuse_create (const char *path, mode_t mode, struct fuse_file_info *fp)
{
    char filename[MAXFILENAME];
    int fd;
    
    printf("create\n");
    strcpy(filename, path);
    fd = sfs_fopen(filename);
    
    sfs_fclose(fd);
    return 0;
}
Exemplo n.º 6
0
static int fuse_truncate(const char *path, off_t size)
{
    char filename[MAXFILENAME];
    int fd;
    
    strcpy(filename, path);
    
    fd = sfs_remove(filename);
    if (fd == -1)
        return -errno;
    
    fd = sfs_fopen(filename);
    sfs_fclose(fd);
    return 0;
}
Exemplo n.º 7
0
static int fuse_open(const char *path, struct fuse_file_info *fi)
{
    int res;
    char filename[MAXFILENAME];
    
    printf("open\n");
    strcpy(filename, path);
    
    res = sfs_fopen(filename);
    if (res == -1)
        return -errno;
    
    sfs_fclose(res);
    return 0;
}
Exemplo n.º 8
0
/**
 * Opens a file, writes some data to it and closes it.
 */
int createSmallFile(char *fileName, char *data, int fsize) {
    int hr = SUCCESS;
    // create one file, write to it, read it back
    // open file
    int fd = sfs_fopen(fileName);
    FAIL_BRK3((fd == -1), stdout,
            "Error: Unable to open file %s with intention to write\n", fileName);
    // write file
    FAIL_BRK3((sfs_fwrite(fd, data, fsize) != fsize), stdout,
            "Error: failed to write %d bytes to %s, fd:%d\n", fsize, fileName,
            fd);
    // close file
    FAIL_BRK3(sfs_fclose(fd), stdout, "Error: Unable to close fd: %d\n", fd);

    Fail: return hr;
}
Exemplo n.º 9
0
static int16_t mbox_mail_numb_put(mbox_mail_name_number_t mail_name_number)
{
    unsigned char file_name[MBOX_MAIL_NAME_NUMBER_FILE_NAME_BUFFER_SIZE];
    sfs_fstream_t* fstream;
    
    // Open mail name number file.
    strncpy_P((char*)file_name, MBOX_MAIL_NAME_NUMBER_FILE_NAME, MBOX_MAIL_NAME_NUMBER_FILE_NAME_BUFFER_SIZE);
    if ((fstream = sfs_fopen(file_name, SFS_OPEN_MODE_RDWR|SFS_OPEN_MODE_BINARY)) == NULL) {
        return EOF;
    }
    
    // Write new mail name number
    sfs_fwrite((const void*)&mail_name_number, MBOX_MAIL_NAME_NUMBER_HEX_SIZE, fstream);
    sfs_fclose(fstream);
    
    return 0;
}
Exemplo n.º 10
0
tileset_t*
tileset_new(const char* filename)
{
	sfs_file_t* file;
	tileset_t*  tileset;

	console_log(2, "loading tileset #%u as `%s`", s_next_tileset_id, filename);

	if ((file = sfs_fopen(g_fs, filename, NULL, "rb")) == NULL)
		goto on_error;
	tileset = tileset_read(file);
	sfs_fclose(file);
	return tileset;

on_error:
	console_log(2, "failed to load tileset #%u", s_next_tileset_id++);
	return NULL;
}
Exemplo n.º 11
0
static mbox_mail_name_number_t mbox_mail_numb_get(void)
{
    uint8_t file_name[MBOX_MAIL_NAME_NUMBER_FILE_NAME_BUFFER_SIZE];
    mbox_mail_name_number_t mail_name_number;
    sfs_fstream_t* fstream;
    
    // Open mail name number file.
    strncpy_P((char*)file_name, MBOX_MAIL_NAME_NUMBER_FILE_NAME, MBOX_MAIL_NAME_NUMBER_FILE_NAME_BUFFER_SIZE);
    if ((fstream = sfs_fopen(file_name, SFS_OPEN_MODE_RD|SFS_OPEN_MODE_BINARY)) == NULL) {
        return EOF;
    }
    
    // read mail name number
    sfs_fread((void*)&mail_name_number, MBOX_MAIL_NAME_NUMBER_HEX_SIZE, fstream);
    sfs_fclose(fstream);
    
    return mail_name_number;
}
Exemplo n.º 12
0
static int8_t mbox_mail_element_get(mbox_mail_element_t** mail_element, sfs_fget_t direction, bool for_reading)
{
    uint8_t opened = true;
    unsigned char file_types[MBOX_MAIL_TYPE_BUFFER_SIZE];
    unsigned char file_name[MBOX_MAIL_NAME_BUFFER_SIZE];
    sfs_fstream_t* mail_stream;
    
    // Return if no mail
    strncpy_P((char*)file_types, MBOX_MAIL_TYPE, MBOX_MAIL_NAME_BUFFER_SIZE);
    if (sfs_fget(file_types, (unsigned char*)file_name, direction) == EOF) {
        return EOF;
    }
    
    // Return if "MBOX_GET_RESTART"
    if (direction == MBOX_GET_RESTART) {
        return EOF;
    }
    
    // Return if unable to open file
    if ((mail_stream = sfs_fopen(file_name, SFS_OPEN_MODE_RD|SFS_OPEN_MODE_BINARY)) == NULL) {
        return EOF;
    }
    
    // Else read file into struct pointed to by argument "mail_element"
    // read mail header
    sfs_fread((void*)(*mail_element), sizeof(mbox_mail_element_t), mail_stream);
    
    // Read mail data
    sfs_fsize_t size = (*mail_element)->mail.size;
    sfs_fread((void*)(*mail_element)->mail.data, size, mail_stream);
    
    // If not opened before, update "opened" flag of file data to indicate mail opened 
    if (((*mail_element)->opened == false) && (for_reading == true)) {
        sfs_fwrite((const void*)&opened, 1, mail_stream);
    }
    
    // Close mail file stream
    sfs_fclose(mail_stream);
    
    // Return OK
    return 0;
}
Exemplo n.º 13
0
int sfs_remove(char *file){
	int index = -1;
	for (int i = 0; i < MAX_FD; i++){
		if (strcmp(file, root[i].fname) == 0){
			index = i;							//matching file with index i
		}
	}

	//If there is no file with that name
	if (index < 0){
		printf("No file to remove\n");
		return 0;

	//If there is a file with that name	
	} else {
		int inode_indx = root[index].inode;
		int block_location;
		if (inode_table[inode_indx].size > 0){	//if there are some blocks to free
			//Number of blocks the file spans across:
			int nmb_of_blocks = (inode_table[inode_indx].size / BLOCK_SIZE) + 1; 
			for (int i = 0; i<nmb_of_blocks; i++){
				block_location = fetch_block(inode_indx,i);
				free_bitmap(block_location);
			}
		}
		//CLOSE IF FILE IS OPEN
		for (int i = 0; i < MAX_FD; i++){		//check if we have a file opened linked to the same inode
			if (inode_indx == fd_table[i].inode) { //if we find an open file with same inode as the matched file
				sfs_fclose(i);
			}
		}
		//REMOVE FROM ROOT DIRECTORY
		root[index] = (dir){"", -1};
		//REINITIALIZE INODE
		inode_table[inode_indx] = (inode){0, 1, 1, 0, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, -1};
		//FLUSH
		flush_to_disk(inode_indx, 0);
		return 0;
	}
}
Exemplo n.º 14
0
static int16_t mbox_mail_numb_create(void)
{
    uint8_t file_name[MBOX_MAIL_NAME_NUMBER_FILE_NAME_BUFFER_SIZE];
    const mbox_mail_name_number_t mail_name_number = 0;
    sfs_fstream_t* fstream;
    
    // Open/create mail name number file.
    strncpy_P((char*)file_name, MBOX_MAIL_NAME_NUMBER_FILE_NAME, MBOX_MAIL_NAME_NUMBER_FILE_NAME_BUFFER_SIZE);
    if ((fstream = sfs_fopen(file_name, SFS_OPEN_MODE_RDWR|SFS_OPEN_MODE_CREATE)) == NULL) {
        return EOF;
    }
    // Write init mail name number (0000)
    int write_count = sfs_fwrite((const void*)&mail_name_number, MBOX_MAIL_NAME_NUMBER_HEX_SIZE, fstream);
    sfs_fclose(fstream);
    
    // Return EOF if write operation failed
    if (write_count != MBOX_MAIL_NAME_NUMBER_HEX_SIZE) {
        return EOF;
    } else {
        return 0;
    }
}
Exemplo n.º 15
0
static int8_t mbox_mail_element_put(mbox_mail_element_t* mail_element)
{
    // Return if mailbox not open
    if (mbox_is_open == false) {
        return EOF;
    }
    
    // Generate filename based on running-number
    unsigned char file_name[MBOX_MAIL_NAME_BUFFER_SIZE];
    strncpy_P((char*)file_name, MBOX_MAIL_NAME_TEMPLATE, MBOX_MAIL_NAME_TEMPLATE_BUFFER_SIZE);
    insi(file_name, (uint16_t)mail_element->number, MBOX_MAIL_NAME_NUMBER_OFFSET, MBOX_MAIL_NAME_NUMBER_SIZE);
    
    // Write data to file
    sfs_fstream_t* fstream;
    sfs_fsize_t size = sizeof(mbox_mail_element_t) + mail_element->mail.size;
    if ((fstream = sfs_fopen(file_name, SFS_OPEN_MODE_WR|SFS_OPEN_MODE_CREATE)) == NULL) {
        return EOF;
    } else {
        sfs_fwrite((const void*)(mail_element), size, fstream);
        sfs_fclose(fstream);
    }
    return 0;
}
Exemplo n.º 16
0
/* The main testing program
 */
int
main(int argc, char **argv)
{
  int i, j, k;
  int chunksize;
  int readsize;
  char *buffer;
  char fixedbuf[1024];
  int fds[MAX_FD];
  char *names[MAX_FD];
  int filesize[MAX_FD];
  int nopen;                    /* Number of files simultaneously open */
  int ncreate;                  /* Number of files created in directory */
  int error_count = 0;
  int tmp;
  
  mksfs(1);                     /* Initialize the file system. */

  /* First we open two files and attempt to write data to them.
   */
  for (i = 0; i < 2; i++) {
    names[i] = rand_name();
    fds[i] = sfs_fopen(names[i]);
    if (fds[i] < 0) {
      fprintf(stderr, "ERROR: creating first test file %s\n", names[i]);
      error_count++;
    }
    tmp = sfs_fopen(names[i]);
    if (tmp >= 0 && tmp != fds[i]) {
      fprintf(stderr, "ERROR: file %s was opened twice\n", names[i]);
      error_count++;
    }
    filesize[i] = (rand() % (MAX_BYTES-MIN_BYTES)) + MIN_BYTES;
  }

  for (i = 0; i < 2; i++) {
    for (j = i + 1; j < 2; j++) {
      if (fds[i] == fds[j]) {
        fprintf(stderr, "Warning: the file descriptors probably shouldn't be the same?\n");
      }
    }
  }

  printf("Two files created with zero length:\n");

  for (i = 0; i < 2; i++) {
    for (j = 0; j < filesize[i]; j += chunksize) {
      if ((filesize[i] - j) < 10) {
        chunksize = filesize[i] - j;
      }
      else {
        chunksize = (rand() % (filesize[i] - j)) + 1;
      }

      if ((buffer = malloc(chunksize)) == NULL) {
        fprintf(stderr, "ABORT: Out of memory!\n");
        exit(-1);
      }
      for (k = 0; k < chunksize; k++) {
        buffer[k] = (char) (j+k);
      }
      // printf("////%d//%d\n", (int)strlen(buffer),chunksize);
      // printf("++++%s\n", buffer);
      tmp = sfs_fwrite(fds[i], buffer, chunksize);
      if (tmp != chunksize) {
        fprintf(stderr, "ERROR: Tried to write %d bytes, but wrote %d\n", 
                chunksize, tmp);
        error_count++;
      }
      free(buffer);
    }
  }

  if (sfs_fclose(fds[1]) != 0) {
    fprintf(stderr, "ERROR: close of handle %d failed\n", fds[1]);
    error_count++;
  }

  /* Sneaky attempt to close already closed file handle. */
  if (sfs_fclose(fds[1]) == 0) {
    fprintf(stderr, "ERROR: close of stale handle %d succeeded\n", fds[1]);
    error_count++;
  }

  printf("File %s now has length %d and %s now has length %d:\n",
         names[0], filesize[0], names[1], filesize[1]);

  /* Just to be cruel - attempt to read from a closed file handle. 
   */
  if (sfs_fread(fds[1], fixedbuf, sizeof(fixedbuf)) > 0) {
    fprintf(stderr, "ERROR: read from a closed file handle?\n");
    error_count++;
  }

  fds[1] = sfs_fopen(names[1]);
  
  sfs_fseek(0, 0);
  sfs_fseek(1, 0);
  
  for (i = 0; i < 2; i++) {
    for (j = 0; j < filesize[i]; j += chunksize) {
      if ((filesize[i] - j) < 10) {
        chunksize = filesize[i] - j;
      }
      else {
        chunksize = (rand() % (filesize[i] - j)) + 1;
      }
      if ((buffer = malloc(chunksize)) == NULL) {
        fprintf(stderr, "ABORT: Out of memory!\n");
        exit(-1);
      }
      readsize = sfs_fread(fds[i], buffer, chunksize);

      if (readsize != chunksize) {
        fprintf(stderr, "ERROR: Requested %d bytes, read %d\n", chunksize, readsize);
        readsize = chunksize;
      }
      for (k = 0; k < readsize; k++) {
        if (buffer[k] != (char)(j+k)) {
          fprintf(stderr, "ERROR: data error at offset %d in file %s (%d,%d)\n",
                  j+k, names[i], buffer[k], (char)(j+k));
          error_count++;
          break;
        }
      }
      free(buffer);
    }
  }

  for (i = 0; i < 2; i++) {
    if (sfs_fclose(fds[i]) != 0) {
      fprintf(stderr, "ERROR: closing file %s\n", names[i]);
      error_count++;
    }
  }

  /* Now try to close the files. Don't
   * care about the return codes, really, but just want to make sure
   * this doesn't cause a problem.
   */
  for (i = 0; i < 2; i++) {
    if (sfs_fclose(fds[i]) == 0) {
      fprintf(stderr, "Warning: closing already closed file %s\n", names[i]);
    }
  }

  /* Now just try to open up a bunch of files.
   */
  ncreate = 0;
  for (i = 0; i < MAX_FD; i++) {
    names[i] = rand_name();
    fds[i] = sfs_fopen(names[i]);
    if (fds[i] < 0) {
      break;
    }
    sfs_fclose(fds[i]);
    ncreate++;
  }

  printf("Created %d files in the root directory\n", ncreate);

  nopen = 0;
  for (i = 0; i < ncreate; i++) {
    fds[i] = sfs_fopen(names[i]);
    if (fds[i] < 0) {
      break;
    }
    nopen++;
  }
  printf("Simultaneously opened %d files\n", nopen);

  for (i = 0; i < nopen; i++) {
    tmp = sfs_fwrite(fds[i], test_str, strlen(test_str));
    if (tmp != strlen(test_str)) {
      fprintf(stderr, "ERROR: Tried to write %d, returned %d\n", 
              (int)strlen(test_str), tmp);
      error_count++;
    }
    if (sfs_fclose(fds[i]) != 0) {
      fprintf(stderr, "ERROR: close of handle %d failed\n", fds[i]);
      error_count++;
    }
  }

  /* Re-open in reverse order */
  for (i = nopen-1; i >= 0; i--) {
    fds[i] = sfs_fopen(names[i]);
    if (fds[i] < 0) {
      fprintf(stderr, "ERROR: can't re-open file %s\n", names[i]);
    }
  }

  /* Now test the file contents.
   */
  for (i = 0; i < nopen; i++) {
      sfs_fseek(fds[i], 0);
  }

  for (j = 0; j < strlen(test_str); j++) {
    for (i = 0; i < nopen; i++) {
      char ch;

      if (sfs_fread(fds[i], &ch, 1) != 1) {
        fprintf(stderr, "ERROR: Failed to read 1 character\n");
        error_count++;
      }
      if (ch != test_str[j]) {
        fprintf(stderr, "ERROR: Read wrong byte from %s at %d (%d,%d)\n", 
                names[i], j, ch, test_str[j]);
        error_count++;
        break;
      }
    }
  }

  /* Now close all of the open file handles.
   */
  for (i = 0; i < nopen; i++) {
    if (sfs_fclose(fds[i]) != 0) {
      fprintf(stderr, "ERROR: close of handle %d failed\n", fds[i]);
      error_count++;
    }
  }
  
  /* Now we try to re-initialize the system.
   */
  mksfs(0);

  for (i = 0; i < nopen; i++) {
    fds[i] = sfs_fopen(names[i]);
    sfs_fseek(fds[i], 0);
    if (fds[i] >= 0) {
      readsize = sfs_fread(fds[i], fixedbuf, sizeof(fixedbuf));
      if (readsize != strlen(test_str)) {
        fprintf(stderr, "ERROR: Read wrong number of bytes\n");
        error_count++;
      }

      for (j = 0; j < strlen(test_str); j++) {
        if (test_str[j] != fixedbuf[j]) {
          fprintf(stderr, "ERROR: Wrong byte in %s at %d (%d,%d)\n", 
                  names[i], j, fixedbuf[j], test_str[j]);
          printf("%d\n", fixedbuf[1]);
          error_count++;
          break;
        }
      }

      if (sfs_fclose(fds[i]) != 0) {
        fprintf(stderr, "ERROR: close of handle %d failed\n", fds[i]);
        error_count++;
      }
    }
  }

  printf("Trying to fill up the disk with repeated writes to %s.\n", names[0]);
  printf("(This may take a while).\n");

  /* Now try opening the first file, and just write a huge bunch of junk.
   * This is just to try to fill up the disk, to see what happens.
   */
  fds[0] = sfs_fopen(names[0]);
  if (fds[0] >= 0) {
    for (i = 0; i < 100000; i++) {
      int x;

      if ((i % 100) == 0) {
        fprintf(stderr, "%d\r", i);
      }

      memset(fixedbuf, (char)i, sizeof(fixedbuf));
      x = sfs_fwrite(fds[0], fixedbuf, sizeof(fixedbuf));
      if (x != sizeof(fixedbuf)) {
        /* Sooner or later, this write should fail. The only thing is that
         * it should fail gracefully, without any catastrophic errors.
         */
        printf("Write failed after %d iterations.\n", i);
        printf("If the emulated disk contains just over %d bytes, this is OK\n",
               (i * (int)sizeof(fixedbuf)));
        break;
      }
    }
    sfs_fclose(fds[0]);
  }
  else {
    fprintf(stderr, "ERROR: re-opening file %s\n", names[0]);
  }

  /* Now, having filled up the disk, try one more time to read the
   * contents of the files we created.
   */
  for (i = 0; i < nopen; i++) {
    fds[i] = sfs_fopen(names[i]);
    sfs_fseek(fds[i], 0);
    if (fds[i] >= 0) {
      readsize = sfs_fread(fds[i], fixedbuf, sizeof(fixedbuf));
      if (readsize < strlen(test_str)) {
        fprintf(stderr, "ERROR: Read wrong number of bytes\n");
        error_count++;
      }

      for (j = 0; j < strlen(test_str); j++) {
        if (test_str[j] != fixedbuf[j]) {
          fprintf(stderr, "ERROR: Wrong byte in %s at position %d (%d,%d)\n", 
                  names[i], j, fixedbuf[j], test_str[j]);
          error_count++;
          break;
        }
      }

      if (sfs_fclose(fds[i]) != 0) {
        fprintf(stderr, "ERROR: close of handle %d failed\n", fds[i]);
        error_count++;
      }
    }
  }

  fprintf(stderr, "Test program exiting with %d errors\n", error_count);
  return (error_count);
}
Exemplo n.º 17
0
/* The main testing program
*/
    int
main(int argc, char **argv)
{
    int i, j, k;
    int chunksize;
    char *buffer;
    char fixedbuf[1024];
    int fds[MAX_FD];
    char *names[MAX_FD];
    int filesize[MAX_FD];
    int nopen;                    /* Number of files simultaneously open */
    int ncreate;                  /* Number of files created in directory */
    int error_count = 0;
    int tmp;

    mksfs(1);                     /* Initialize the file system. */

    /* First we open two files and attempt to write data to them.
    */
    for (i = 0; i < 2; i++) {
        names[i] = rand_name();
        fds[i] = sfs_fopen(names[i]);
        if (fds[i] < 0) {
            fprintf(stderr, "ERROR: creating first test file %s\n", names[i]);
            error_count++;
        }
        tmp = sfs_fopen(names[i]);
        if (tmp >= 0 && tmp != fds[i]) {
            fprintf(stderr, "ERROR: file %s was opened twice\n", names[i]);
            error_count++;
        }
        filesize[i] = (rand() % (MAX_BYTES-MIN_BYTES)) + MIN_BYTES;
    }

    for (i = 0; i < 2; i++) {
        for (j = i + 1; j < 2; j++) {
            if (fds[i] == fds[j]) {
                fprintf(stderr, "Warning: the file descriptors probably shouldn't be the same?\n");
            }
        }
    }

    printf("Two files created with zero length:\n");
    sfs_ls();
    printf("\n");

    for (i = 0; i < 2; i++) {
        for (j = 0; j < filesize[i]; j += chunksize) {
            if ((filesize[i] - j) < 10) {
                chunksize = filesize[i] - j;
            }
            else {
                chunksize = (rand() % (filesize[i] - j)) + 1;
            }

            if ((buffer = malloc(chunksize)) == NULL) {
                fprintf(stderr, "ABORT: Out of memory!\n");
                exit(-1);
            }
            for (k = 0; k < chunksize; k++) {
                buffer[k] = (char) (j+k);
            }
            printf("file: %d, chunksize: %d\n", i, chunksize);
            sfs_ls();
            sfs_fwrite(fds[i], buffer, chunksize);
	    free(buffer);
        }
    }

    sfs_fclose(fds[1]);

    printf("File %s now has length %d and %s now has length %d:\n",
            names[0], filesize[0], names[1], filesize[1]);
    sfs_ls();

    fds[1] = sfs_fopen(names[1]);

    printf("before doing reads\n\n");

    for (i = 0; i < 2; i++) {
        for (j = 0; j < filesize[i]; j += chunksize) {
            if ((filesize[i] - j) < 10) {
                chunksize = filesize[i] - j;
            }
            else {
                chunksize = (rand() % (filesize[i] - j)) + 1;
            }
            if ((buffer = malloc(chunksize)) == NULL) {
                fprintf(stderr, "ABORT: Out of memory!\n");
                exit(-1);
            }
            printf("before sfs_read\n\n");
            sfs_fread(fds[i], buffer, chunksize);
            printf("After sfs_read\n\n");
            /*
            for (k = 0; k < chunksize; k++) {
                if (buffer[k] != (char)(j+k)) {
                    fprintf(stderr, "ERROR: data error at offset %d in file %s (%d,%d)\n",
                            j+k, names[i], buffer[k], (char)(j+k));
                    error_count++;
                    break;
                }
            }
            */
            free(buffer);
            printf("After doing reads %d\n\n", i);
        }
    }
    printf("After doing reads\n\n");

    for (i = 0; i < 2; i++) {
        sfs_fclose(fds[i]);
        if (sfs_remove(names[i]) != 0) {
            fprintf(stderr, "ERROR: deleting file %s\n", names[i]);
            error_count++;
        }
        printf("After deleting file %s:\n", names[i]);
        sfs_ls();
    }

    /* Now try to close and delete the closed and deleted files. Don't
     * care about the return codes, really, but just want to make sure
     * this doesn't cause a problem.
     */
    for (i = 0; i < 2; i++) {
        sfs_fclose(fds[i]);
        if (sfs_remove(names[i]) == 0) {
            fprintf(stderr, "Warning: deleting already deleted file %s\n", names[i]);
        }
        //free(names[i]);
        names[i] = NULL;
    }

    /* Now just try to open up a bunch of files.
    */
    ncreate = 0;
    for (i = 0; i < MAX_FD; i++) {
        names[i] = rand_name();
        fds[i] = sfs_fopen(names[i]);
        if (fds[i] < 0) {
            break;
        }
        sfs_fclose(fds[i]);
        ncreate++;
    }

    printf("Created %d files in the root directory\n", ncreate);

    nopen = 0;
    for (i = 0; i < ncreate; i++) {
        fds[i] = sfs_fopen(names[i]);
        if (fds[i] < 0) {
            break;
        }
        nopen++;
    }
    printf("Simultaneously opened %d files\n", nopen);

    for (i = 0; i < nopen; i++) {
        sfs_fwrite(fds[i], test_str, strlen(test_str));
        sfs_fclose(fds[i]);
    }

    /* Re-open in reverse order */
    for (i = nopen-1; i >= 0; i--) {
        fds[i] = sfs_fopen(names[i]);
        if (fds[i] < 0) {
            fprintf(stderr, "ERROR: can't re-open file %s\n", names[i]);
        }
    }

    /* Now test the file contents.
    */
    for (j = 0; j < strlen(test_str); j++) {
        for (i = 0; i < nopen; i++) {
            char ch;

            sfs_fread(fds[i], &ch, 1);
            if (ch != test_str[j]) {
                fprintf(stderr, "ERROR: Read wrong byte from %s at %d (%d,%d)\n", 
                        names[i], j, ch, test_str[j]);
                error_count++;
                break;
            }
        }
    }

    /* Now close all of the open file handles.
    */
    for (i = 0; i < nopen; i++) {
        sfs_fclose(fds[i]);
    }

    /* Now we try to re-initialize the system.
    */
    mksfs(0);

    for (i = 0; i < nopen; i++) {
        fds[i] = sfs_fopen(names[i]);
        if (fds[i] >= 0) {
            sfs_fread(fds[i], fixedbuf, sizeof(fixedbuf));

            for (j = 0; j < strlen(test_str); j++) {
                if (test_str[j] != fixedbuf[j]) {
                    fprintf(stderr, "ERROR: Wrong byte in %s at %d (%d,%d)\n", 
                            names[i], j, fixedbuf[j], test_str[j]);
                    error_count++;
                    break;
                }
            }

            sfs_fclose(fds[i]);
        }
    }

    for (i = 0; i < ncreate; i++) {
        sfs_remove(names[i]);
        free(names[i]);
        names[i] = NULL;
    }

    //-------- The following part tests sfs_fseek

    printf("Tests sfs_fseek\n");

    // Prepare a file
    char* f_name = rand_name();
    int f_id = sfs_fopen(f_name);
    buffer = malloc(100);

    for(i = 0; i < 10; i++)
    {
        sfs_fwrite(f_id, "0123456789", 10);
    }

    // sfs_fwrite shouldn't change read pointer
    for(i = 0; i < 10; i++)
    {
        sfs_fread(f_id, buffer, 10);
        if(0 != strncmp(buffer, "0123456789", 10))
        {
            fprintf(stderr, "ERROR: should read '0123456789'\n");
            error_count++;
        }
    }

    // sys_seek changes read pointer
    for(i = 0; i < 100; i += 7)
    {
        sfs_fseek(f_id, i);
        sfs_fread(f_id, buffer, 1);
        if (buffer[0] - 48 != i % 10) {
            fprintf(stderr, "ERROR: postion %d shoud be %c\n", i, i % 10);
            error_count++;
        }
    }

    sfs_fseek(f_id, 80);
    sfs_fwrite(f_id, "9876543210", 10);
    sfs_fseek(f_id, 85);
    sfs_fread(f_id, buffer, 10);
    if(0 != strncmp(buffer, "4321001234", 10))
    {
        fprintf(stderr, "ERROR: should read '4321001234'\n");
        error_count++;
    }

    //free(buffer);

    fprintf(stderr, "Test program exiting with %d errors\n", error_count);
    return (error_count);
}
Exemplo n.º 18
0
/**
 * Create a big file, write lots of data to it and close it. Then append another chunk of data to it.
 */
int appendFile() {
    int hr = SUCCESS;
    int fsize1 = 20 * SD_SECTORSIZE;
    int fsize2 = 50 * SD_SECTORSIZE;
    char *buffer = malloc(fsize2 * sizeof(char));
    char *buffer2 = malloc(fsize2 * sizeof(char));

    char *fileName = "foo";

    // create the original file
    FAIL_BRK3(singleBigFile(fileName, fsize1), stdout,
            "Error: Unable to create the initial file\n");

    FAIL_BRK3(refreshDisk(), stdout, "Error: Refresh disk failed\n");

    // open it again
    int fd = sfs_fopen(fileName);
    FAIL_BRK3((fd == -1), stdout, "Error: Unable to reopen the file\n");

    // lseek to the end of the file
    int newPos = sfs_lseek(fd, fsize1 - 1);
    FAIL_BRK3(
            (newPos != fsize1 - 1),
            stdout,
            "Error: Seeking1 to the end of the file failed newPos (%d), fsize1(%d)\n",
            newPos, fsize1);

    initBuffer(buffer, fsize2);
    // write the new data
    int bytesWritten = sfs_fwrite(fd, buffer, fsize2);
    FAIL_BRK3((bytesWritten != fsize2), stdout,
            "Error: Appending write failed\n");

    // close the file
    FAIL_BRK3(sfs_fclose(fd), stdout, "Error: Closing the file failed\n");

    FAIL_BRK3(refreshDisk(), stdout, "Error: Refresh disk failed\n");

    // open it again
    fd = sfs_fopen(fileName);
    FAIL_BRK3((fd == -1), stdout, "Error: Unable to reopen the file\n");

    // again lseek to the place where we started appending
    newPos = sfs_lseek(fd, fsize1 - 1);
    FAIL_BRK3((newPos != fsize1 - 1), stdout,
            "Error: Seeking2 to the end of the file failed\n");

    // read the data
    int bytesRead = sfs_fread(fd, buffer2, fsize2);
    FAIL_BRK3((bytesRead != fsize2), stdout,
            "Error: Reading back the appended part failed\n");

    // make sure it matches
    FAIL_BRK3(checkBuffers(buffer, buffer2, fsize2, 0), stdout,
            "Error: Contents don't match\n");

    // close the file
    FAIL_BRK3(sfs_fclose(fd), stdout, "Error: Closing the file failed\n");

    // finalize
    Fail:

    // clean up
    SAFE_FREE(buffer);
    SAFE_FREE(buffer2);
    return hr;
}
Exemplo n.º 19
0
/**
 * Performs various file operations.
 *
 * @param name name of file to create
 * @param fsize size of file to create
 * @return -1 upon failure, 0 otherwise
 */
int testFile(char* name, int fsize) {
    int fd = 0; /* file handle */
    int bytes, bytesToRead, offset;
    // fix fsize of too small
    if (fsize<=1) fsize=2;
    char *buf = (char *) malloc(sizeof(char) * fsize);
    char *cpy = (char *) malloc(sizeof(char) * fsize);

    LOG(stdout, "Create file...\n");

    fd = sfs_fopen(name);
    FAIL_BRK((fd == -1), stdout, "Error: Open file failed for: %s\n", name);

    initBuffer(buf, fsize);

    /* write to disk */
    FAIL_BRK((sfs_fwrite(fd, buf, fsize) == -1), stdout,
            "Error: Writing %d bytes to file %s failed!\n", fsize, name);

    /* close file */
    FAIL_BRK(sfs_fclose(fd), stdout, "Closing file %s failed!\n", name);

    /* save disk, close it and reload it to make sure changes are flushed to disk */
    FAIL_BRK(refreshDisk(), stdout, "Error: Refreshing disk failed\n");

    /* open file again */
    fd = sfs_fopen(name);
    FAIL_BRK((fd == -1), stdout, "Error: Second open file failed for: %s\n",
            name);

    /* read file */
    int fsize2;
    FAIL_BRK(((fsize2 = sfs_fread(fd, cpy, fsize)) == -1), stdout,
            "Read failed for: %s\n", name);

    FAIL_BRK(
            (fsize != fsize2),
            stdout,
            "Error: The amount we wrote (%d) is different from what we read (%d)\n",
            fsize, fsize2);

    /* check contents */
    FAIL_BRK(checkBuffers(buf, cpy, fsize, 0), stdout,
            "Error: Contents don't match\n");

    /* reset file */
    FAIL_BRK(sfs_fclose(fd), stdout, "Error: Closing file (%d)(%s) failed\n",
            fd, name);
    fd = sfs_fopen(name);
    FAIL_BRK((fd == -1), stdout, "Error: Opening file (%s) failed\n", name);

    free(cpy);
    cpy = (char *) malloc(sizeof(char) * fsize);

    /* check contents using small chunks */
    bytes = 0;
    while (bytes < fsize) {
        bytesToRead = rand() % 9 + 1;

        if (bytes + bytesToRead >= fsize) {
            bytesToRead = fsize - bytes;
        }

        /* read */
        int bytesActuallyRead;
        bytesActuallyRead = sfs_fread(fd, cpy + bytes, bytesToRead);
        FAIL_BRK((bytesActuallyRead == -1), stdout,
                "Error: Read failed for: %s\n", name);
        FAIL_BRK(
                (bytesActuallyRead != bytesToRead),
                stdout,
                "Error: For file (%s) expected to read (%d) but actually read (%d)\n",
                name, bytesToRead, bytesActuallyRead);

        bytes += bytesToRead;
    }

    FAIL_BRK(checkBuffers(buf, cpy, fsize, 0), stdout,
            "Error: Contents don't match\n");

    /* reset file */
    FAIL_BRK(sfs_fclose(fd), stdout, "Error: Closing file (%d) failed\n", fd);
    fd = sfs_fopen(name);
    FAIL_BRK((fd == -1), stdout, "Error: Opening file (%s) failed\n", name);

    free(cpy);
    cpy = (char *) malloc(sizeof(char) * fsize);

    /* check contents from offset */
    offset = rand() % (fsize - 1);
    bytesToRead = rand() % (fsize - offset);

    if (bytesToRead == 0)
        bytesToRead++;

    FAIL_BRK((sfs_lseek(fd, offset) == -1), stdout,
            "Error: lseek failed for: (%s) fd: (%d), offset: (%d)\n", name, fd,
            offset);

    /* read */
    int bytesActuallyRead = 0;
    bytesActuallyRead = sfs_fread(fd, cpy + offset, bytesToRead);
    FAIL_BRK((bytesActuallyRead == -1), stdout, "Error: Read failed for: %s\n",
            name);
    FAIL_BRK(
            (bytesActuallyRead != bytesToRead),
            stdout,
            "Error: For file (%s) expected to read (%d) but actually read (%d)\n",
            name, bytesToRead, bytesActuallyRead);

    FAIL_BRK(checkBuffers(buf, cpy, bytesToRead, offset), stdout,
            "Error: Contents do not match\n");

    FAIL_BRK(sfs_fclose(fd), stdout, "Error: Closing file (%d) failed\n", fd);

    SAFE_FREE(buf);
    SAFE_FREE(cpy);

    return 0; /* passed all tests */

    // Something went wrong...
    Fail:
    
    SAFE_FREE(buf);
    SAFE_FREE(cpy);
    
    return -1;
}
Exemplo n.º 20
0
/**
 * This is a place where you need to put your test. Please insert your test code here and
 * change the name of the test to be representative of what you are testing. You can use this test
 * during the development period to test your code. When you do the final submission here you have to
 * implement a test that tests some interesting functionality of the sfs.
 * @return SUCCESS if the test is passed successfully FAIL otherwise.
 */
int customTest() {
    int hr = SUCCESS;
	int i, j, fd, fd1, fd2, lseek2;
    char *randomBuf;//buffer contain junk data, size: SD_SECTORSIZE
	randomBuf = (char *) malloc(sizeof(char) * SD_SECTORSIZE);

	// For later tests
	char* asciidata = (char*)malloc(257 * sizeof(char)); // 2
	char* morealphabet = (char*)malloc(7*sizeof(char));
	morealphabet[0] = 't';
	morealphabet[1] = 'u';
	morealphabet[2] = 'v';
	morealphabet[3] = 'w';
	morealphabet[4] = 'x';
	morealphabet[5] = 'y';
	morealphabet[6] = 'z';
	char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
	char* fd1read = (char*)malloc(26*sizeof(char));

	// normal test from testfs
    // initialize disk
    FAIL_BRK4(SD_initDisk());
    for (i = 0; i < SD_NUMSECTORS; i++) {
        for (j = 0; j < SD_SECTORSIZE; j++) {
            randomBuf[j] = (char) rand();
        }
		while (SD_write(i, (void*) randomBuf));
    }
	FAIL_BRK4(SD_loadDisk(gsDiskFName));
	refreshDisk();

    // initialize fs, sfs_mkfs
    FAIL_BRK4(initFS());
	refreshDisk();

	// initialize test sfs_mkfs, when mkfs, nothing should appear again.
    FAIL_BRK4(initFS());
	FAIL_BRK4(sfs_mkdir("foo"));
	refreshDisk();
	FAIL_BRK4(sfs_fcd("foo"));
	FAIL_BRK4(sfs_mkdir("bar"));
	FAIL_BRK4(initFS());
	refreshDisk();
	FAIL_BRK4((sfs_fcd("foo") != -1));
	FAIL_BRK4((sfs_fcd("bar") != -1));
	FAIL_BRK4(sfs_fcd("/"));
	FAIL_BRK4((sfs_fcd("foo") != -1));
	FAIL_BRK4((sfs_fcd("bar") != -1));
	FAIL_BRK4(sfs_mkdir("foo"));
	refreshDisk();
	FAIL_BRK4(sfs_fcd("foo"));
	FAIL_BRK4((sfs_fcd("bar") != -1));

	//normal test . and ..
	FAIL_BRK4(initFS());
	refreshDisk();
	FAIL_BRK4((sfs_mkdir(".") != -1));
	FAIL_BRK4(sfs_mkdir(".foo."));
	FAIL_BRK4(sfs_mkdir(".foo"));
	FAIL_BRK4((sfs_mkdir("..") != -1));
	FAIL_BRK4(sfs_mkdir("..foo"));
	FAIL_BRK4(sfs_mkdir("..foo.."));
	FAIL_BRK4((sfs_mkdir("/") != -1));
	FAIL_BRK4(sfs_mkdir("..."));
	FAIL_BRK4(sfs_mkdir("...."));
	FAIL_BRK4((sfs_mkdir("./") != -1));
	FAIL_BRK4((sfs_mkdir("/.") != -1));
	FAIL_BRK4((sfs_mkdir("./.") != -1));

	// ascii code test, make a file containing all ascii code chars to make sure implementation does not use an EOF char for size
	FAIL_BRK4(initFS());
	refreshDisk();
	//257 chars, possible ascii 0 -> 255 + 255 at the end to make sure we test
	// create data

	for (i = 0; i < 256; i++) {
		asciidata[i] = i; // table sequentially
	}
	// now put 255 in again at the end
	asciidata[256] = 255;
	// now fwrite into the a file and fread and compare
	FAIL_BRK4(createSmallFile("asciitable", asciidata, 257));
	refreshDisk();
	FAIL_BRK4(verifyFile("asciitable", asciidata, 257));

	// This test will open the same file twice, write more onto it using one of the file descriptors to lengthen it, and read both to make sure they still match (should test to make sure size is with inodes and not the table
	FAIL_BRK4(initFS());
	refreshDisk();
	FAIL_BRK4(createSmallFile("alphabet", "abcdefghijklmnopqrst", 20));
	// Open twice
	FAIL_BRK4((fd1 = sfs_fopen("alphabet")) == -1);
	FAIL_BRK4((fd2 = sfs_fopen("alphabet")) == -1);
	// lseek to end and write to second one
	FAIL_BRK4((lseek2 = sfs_lseek(fd2, 19)) == -1);
	FAIL_BRK4((sfs_fwrite(fd2, morealphabet, 7)) == -1);
	// Now verify we can read from fd1 and it should be the full alphabet since we wrote to fd2 which is the same file
	FAIL_BRK4((sfs_fread(fd1, fd1read, 26)) == -1);
	FAIL_BRK4((strncmp(alphabet, fd1read, 26)) != 0); // here is the comparison of strings
	FAIL_BRK4((sfs_fclose(fd1)) == -1);
	FAIL_BRK4((sfs_fclose(fd2)) == -1);

	//test dir takes more that one sector

	//test for file
	FAIL_BRK4(initFS());
	refreshDisk();
	FAIL_BRK4((fd = sfs_fopen("foo.bin")) == -1);
	FAIL_BRK4(sfs_fclose(fd));
	FAIL_BRK4((sfs_fcd("foo.bin") != -1));

	//test same name
	FAIL_BRK4(initFS());
	refreshDisk();

	FAIL_BRK4(sfs_mkdir("foo.bin"));
	FAIL_BRK4((fd = sfs_fopen("foo.bin")) != -1);

	FAIL_BRK4((fd = sfs_fopen("bar.bin")) == -1);
	FAIL_BRK4((sfs_mkdir("bar.bin") != -1));

	//test initFS will erase the file descriptor
	FAIL_BRK4(initFS());
	refreshDisk();
	FAIL_BRK4((fd = sfs_fopen("foo.bin")) == -1);
	FAIL_BRK4(initFS());
	FAIL_BRK4((sfs_fclose(fd) != -1));

	//test create nothing
	FAIL_BRK4(initFS());
	refreshDisk();

	FAIL_BRK4((sfs_mkdir("") != -1));
	FAIL_BRK4((fd = sfs_fopen("")) != -1);

	//test open . and ..
	FAIL_BRK4(initFS());
	refreshDisk();

	FAIL_BRK4((fd = sfs_fopen("/")) != -1);
	FAIL_BRK4((fd = sfs_fopen(".")) != -1);
	FAIL_BRK4((fd = sfs_fopen("..")) != -1);

	FAIL_BRK4(sfs_mkdir("foo"));
	FAIL_BRK4((fd = sfs_fopen("foo")) != -1);
	FAIL_BRK4(sfs_fcd("foo"));

	FAIL_BRK4((fd = sfs_fopen("/")) != -1);
	FAIL_BRK4((fd = sfs_fopen(".")) != -1);
	FAIL_BRK4((fd = sfs_fopen("..")) != -1);

	FAIL_BRK4(sfs_fcd("/"));
	FAIL_BRK4((fd = sfs_fopen("/.")) != -1);
	FAIL_BRK4((fd = sfs_fopen("./")) != -1);
	FAIL_BRK4((fd = sfs_fopen("./.")) != -1);
	FAIL_BRK4((fd = sfs_fopen("/..")) != -1);
	FAIL_BRK4((fd = sfs_fopen("../")) != -1);
	FAIL_BRK4((fd = sfs_fopen("../..")) != -1);
	FAIL_BRK4((fd = sfs_fopen("//")) != -1);
	FAIL_BRK4((fd = sfs_fopen(".foo")) == -1);
	FAIL_BRK4((fd = sfs_fopen(".foo.")) == -1);
	FAIL_BRK4((fd = sfs_fopen("..foo")) == -1);
	FAIL_BRK4((fd = sfs_fopen("..foo..")) == -1);
	FAIL_BRK4((fd = sfs_fopen("...")) == -1);
	FAIL_BRK4((fd = sfs_fopen(".....")) == -1);
	
	// test the malloc not been released
	FAIL_BRK4(sfs_mkfs());
	refreshDisk();
	int thememmax;
	struct rusage ru;
	getrusage(RUSAGE_SELF, &ru);
	thememmax=ru.ru_maxrss;
	long int k;
	for(k = 0; k < 100000; ++k)
	{
		FAIL_BRK4(sfs_mkfs());
		FAIL_BRK4(sfs_mkdir("foo"));
		FAIL_BRK4(sfs_fcd("foo"));
		FAIL_BRK4(sfs_ls(f_ls));
		FAIL_BRK4((fd = sfs_fopen("bar")) == -1);
		FAIL_BRK4((sfs_fwrite(fd, randomBuf, SD_SECTORSIZE)) == -1);
		FAIL_BRK4((sfs_lseek(fd, 1)) == -1);
		FAIL_BRK4((sfs_fread(fd, randomBuf, SD_SECTORSIZE - 1)) == -1);
		FAIL_BRK4(sfs_fclose(fd));
	}
	getrusage(RUSAGE_SELF, &ru);
	FAIL_BRK4(100 * thememmax <= ru.ru_maxrss);
/*	
    FAIL_BRK4(createFolder("bar"));
	FAIL_BRK4(sfs_fcd("bar"));
	FAIL_BRK4(createFolder("foo"));	
	FAIL_BRK4(sfs_fcd("foo"));
	FAIL_BRK4(sfs_fcd("/bar/foo"));
//	FAIL_BRK3((sfs_fcd("//bar/foo") != -1), stdout, "Error: sfs_fcd() failed\n");
	FAIL_BRK4(sfs_fcd("/../bar/foo"));
	FAIL_BRK4(sfs_fcd("/../bar/foo/"));
	FAIL_BRK4(sfs_fcd("/"));
	FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");
	FAIL_BRK4(sfs_fcd("/bar"));
	FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n");
	
	//int fopentmp;
	//FAIL_BRK3((fopentmp = sfs_fopen("test.txt") != -1), stdout, "Error: Allowing read from unopened file\n");
	//FAIL_BRK3((sfs_fread(fopentmp, buf, 50) != -1), stdout, "Error: Allowing read from unopened file\n");

    // test if fcd does not allow going to places that does not exist
    FAIL_BRK3((sfs_fcd("bla") != -1), stdout,
            "Error: Allowing cd to folder that does not exist\n");
    FAIL_BRK3((sfs_fcd("x") != -1), stdout,
            "Error: Allowing cd to folder that does not exist\n");
    FAIL_BRK3((sfs_fcd("x/y/x/z") != -1), stdout,
            "Error: Allowing cd to folder that does not exist\n");
*/

    Fail:

    //clean up code goes here
	SAFE_FREE(asciidata);
	SAFE_FREE(morealphabet);
	SAFE_FREE(fd1read);
	SAFE_FREE(randomBuf);
    saveAndCloseDisk();
    PRINT_RESULTS("customTest!");
    return hr;
}
Exemplo n.º 21
0
font_t*
load_font(const char* filename)
{
	image_t*                atlas = NULL;
	int                     atlas_x, atlas_y;
	int                     atlas_size_x, atlas_size_y;
	sfs_file_t*             file;
	font_t*                 font = NULL;
	struct font_glyph*      glyph;
	struct rfn_glyph_header glyph_hdr;
	long                    glyph_start;
	uint8_t*                grayscale;
	image_lock_t*           lock = NULL;
	int                     max_x = 0, max_y = 0;
	int                     min_width = INT_MAX;
	int64_t                 n_glyphs_per_row;
	int                     pixel_size;
	struct rfn_header       rfn;
	uint8_t                 *psrc;
	color_t                 *pdest;

	int i, x, y;
	
	console_log(2, "loading font #%u as `%s`", s_next_font_id, filename);
	
	memset(&rfn, 0, sizeof(struct rfn_header));

	if ((file = sfs_fopen(g_fs, filename, NULL, "rb")) == NULL) goto on_error;
	if (!(font = calloc(1, sizeof(font_t)))) goto on_error;
	if (sfs_fread(&rfn, sizeof(struct rfn_header), 1, file) != 1)
		goto on_error;
	pixel_size = (rfn.version == 1) ? 1 : 4;
	if (!(font->glyphs = calloc(rfn.num_chars, sizeof(struct font_glyph))))
		goto on_error;

	// pass 1: load glyph headers and find largest glyph
	glyph_start = sfs_ftell(file);
	for (i = 0; i < rfn.num_chars; ++i) {
		glyph = &font->glyphs[i];
		if (sfs_fread(&glyph_hdr, sizeof(struct rfn_glyph_header), 1, file) != 1)
			goto on_error;
		sfs_fseek(file, glyph_hdr.width * glyph_hdr.height * pixel_size, SFS_SEEK_CUR);
		max_x = fmax(glyph_hdr.width, max_x);
		max_y = fmax(glyph_hdr.height, max_y);
		min_width = fmin(min_width, glyph_hdr.width);
		glyph->width = glyph_hdr.width;
		glyph->height = glyph_hdr.height;
	}
	font->num_glyphs = rfn.num_chars;
	font->min_width = min_width;
	font->max_width = max_x;
	font->height = max_y;

	// create glyph atlas
	n_glyphs_per_row = ceil(sqrt(rfn.num_chars));
	atlas_size_x = max_x * n_glyphs_per_row;
	atlas_size_y = max_y * n_glyphs_per_row;
	if ((atlas = create_image(atlas_size_x, atlas_size_y)) == NULL)
		goto on_error;

	// pass 2: load glyph data
	sfs_fseek(file, glyph_start, SFS_SEEK_SET);
	if (!(lock = lock_image(atlas))) goto on_error;
	for (i = 0; i < rfn.num_chars; ++i) {
		glyph = &font->glyphs[i];
		if (sfs_fread(&glyph_hdr, sizeof(struct rfn_glyph_header), 1, file) != 1)
			goto on_error;
		atlas_x = i % n_glyphs_per_row * max_x;
		atlas_y = i / n_glyphs_per_row * max_y;
		switch (rfn.version) {
		case 1: // RFN v1: 8-bit grayscale glyphs
			if (!(glyph->image = create_subimage(atlas, atlas_x, atlas_y, glyph_hdr.width, glyph_hdr.height)))
				goto on_error;
			grayscale = malloc(glyph_hdr.width * glyph_hdr.height);
			if (sfs_fread(grayscale, glyph_hdr.width * glyph_hdr.height, 1, file) != 1)
				goto on_error;
			psrc = grayscale;
			pdest = lock->pixels + atlas_x + atlas_y * lock->pitch;
			for (y = 0; y < glyph_hdr.height; ++y) {
				for (x = 0; x < glyph_hdr.width; ++x)
					pdest[x] = color_new(psrc[x], psrc[x], psrc[x], 255);
				pdest += lock->pitch;
				psrc += glyph_hdr.width;
			}
			break;
		case 2: // RFN v2: 32-bit truecolor glyphs
			if (!(glyph->image = read_subimage(file, atlas, atlas_x, atlas_y, glyph_hdr.width, glyph_hdr.height)))
				goto on_error;
			break;
		}
	}
	unlock_image(atlas, lock);
	sfs_fclose(file);
	free_image(atlas);
	
	font->id = s_next_font_id++;
	return ref_font(font);

on_error:
	console_log(2, "failed to load font #%u", s_next_font_id++);
	sfs_fclose(file);
	if (font != NULL) {
		for (i = 0; i < rfn.num_chars; ++i) {
			if (font->glyphs[i].image != NULL) free_image(font->glyphs[i].image);
		}
		free(font->glyphs);
		free(font);
	}
	if (lock != NULL) unlock_image(atlas, lock);
	if (atlas != NULL) free_image(atlas);
	return NULL;
}