Example #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;
}
Example #2
0
static int fuse_write(const char *path, const char *buf, size_t size,
        off_t offset, struct fuse_file_info *fi)
{
    int fd;
    int res;
    
    char filename[MAXFILENAME];
    
    printf("write\n");
    strcpy(filename, path);
    
    fd = sfs_fopen(filename);
    if (fd == -1) 
        return -errno;
    
    if(sfs_fseek(fd, offset) == -1)
        return -errno;
    
    res = sfs_fwrite(fd, buf, size);
    if (res == -1)
        return -errno;
    
    sfs_fclose(fd);
    return res;
}
Example #3
0
void remove_inode_from_directory(char* name)
{
    int i, num_entries;
    int dir_size = fd_table[ROOTFD].inode->size;
    char* buf = (char*)malloc(dir_size);
    directory_map* dir_entry = (directory_map*)buf;
    
    fd_table[ROOTFD].rw_pointer = 0;
    sfs_fread(ROOTFD, buf, dir_size);
    
    num_entries = dir_size/sizeof(directory_map);
    
    for(i = 0; i < num_entries; i++) {
        if(strcmp(dir_entry->file_name, name) == 0) {
            memmove(dir_entry, &dir_entry[1], sizeof(directory_map)*(num_entries-i-1));
            
            // Truncate root directory inode
            fd_table[ROOTFD].rw_pointer = 0;
            fd_table[ROOTFD].inode->size = 0;
            free_blk_pointers(&(fd_table[ROOTFD].inode->blk_pointers));
            memset(fd_table[ROOTFD].inode, 0, sizeof(inode_struct));
            sfs_fwrite(ROOTFD, buf, dir_size-sizeof(directory_map));
            write_inode_by_index(fd_table[ROOTFD].inode_num, fd_table[ROOTFD].inode);
            free(buf);
            return;
        }
        dir_entry++;
    }
}
Example #4
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;
}
Example #5
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;
}
Example #6
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;
}
Example #7
0
int sfs_fopen(char *name) 
{
    int inode_num, fd, i; 
    inode_struct* inode;
    directory_map new_file;
    
    inode_num = get_inode_num_by_name(name);
    
    // Check if file is already open
    for(i = 1; i < MAXOPENFILES; i++) {
        if( (fd_table[i].status == USED) && (fd_table[i].inode_num == inode_num)) return i;
    }
    
    fd = get_free_fd();
    if(fd == -1) return -1;
        
    if(inode_num == -1) // File does not exist, create file 
    {
        // Allocate i-node
        inode_num = get_free_inode_num();
        inode = calloc(1, sizeof(inode_struct));
        
        // Add new file to directory
        new_file.inode_num = inode_num;
        strcpy(new_file.file_name, name);
        
        // Seek to end of directory and write new file entry
        fd_table[ROOTFD].rw_pointer = fd_table[ROOTFD].inode->size;
        sfs_fwrite(ROOTFD, (char*)&new_file, sizeof(directory_map));
        write_inode_by_index(fd_table[ROOTFD].inode_num, fd_table[ROOTFD].inode);
    } else {
        inode = get_inode_by_index(inode_num);
    } 
    
    fd_table[fd].inode_num = inode_num;
    fd_table[fd].inode = inode;
    fd_table[fd].rw_pointer = inode->size;
    
    return fd;
}
Example #8
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;
    }
}
Example #9
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;
}
Example #10
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);
}
/* 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);
}
Example #12
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;
}
Example #13
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;
}
Example #14
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;
}