示例#1
0
int sfs_initialize(int erase)
{
if (erase == 1)
    {
    new_filesystem();
    put_super_blk();
    put_inode_table();

    // open root folder /

    int fd = sfs_open("/");
    if (fd >= 0) {
            printf("%s %d\n", "Root folder / is opened. File descriptor:", fd);
    }
    return 0;

    }

    else
    {
    get_inode_table_from_disk();
    return 1;
    }

return -1;
}
示例#2
0
int ext_read_block(const char *filename, int block_id, void *buf)
{
	char *blk_path = (char *)malloc(sizeof(char) * strlen(filename) + 7);
	int fd = 0;
	get_block_file_path(blk_path, filename, block_id);
	fd = sfs_open("/", blk_path);
	sfs_read(fd, buf, DFS_BLOCK_SIZE);
	sfs_close(fd);
	return 0;
}
示例#3
0
int ext_write_block(const char *filename, int block_id, void *buf)
{
	char *blk_path = (char *)malloc(sizeof(char) * strlen(filename) + 7);
	int fd = 0;
	get_block_file_path(blk_path, filename, block_id);
	int dirbid = sfs_mkdir("/");
	fd = sfs_open("/", blk_path);
	int a = sfs_write(fd, buf, DFS_BLOCK_SIZE);
	printf("successfully write %s to file %d\n", blk_path, fd);
	sfs_print_info();
	sfs_close(fd);
	return 0;
}
示例#4
0
文件: test.c 项目: aragorn/wisebot
static int LOAD (test_input_t *t)
{
	t->sfs = sfs_create(0, t->fd, 0);
	if(t->sfs == NULL) {
		return FAIL;
	}

	if(sfs_open(t->sfs, t->load_option) == FAIL) {
		error("TEST>can't load sfs");
		return FAIL;
	}

	return SUCCESS;
}
int sfs_getsize(char *pathName)
{
	int index,i,size = 0;

	/* Setting index equal to the value that is retried from the findIndex_file
	 * function. Also in the findIndex_file the path name is passed through so
	 * that the pathName can be validated also.
	 */
	index = findIndex_file(pathName);

	// Returns -1 if pathName entered is not valid
	if(index == -1)
	{
		return -1;
	}	

	// Returns -2 if the directory is invalid
	if(index == -2)
	{
		return -2;
	}
	
	// Return -3 if the directory can not be found
	if(index == -3)
	{
		return -3;
	}
	int open_size = sfs_open(pathName);
	

	// Used to increment the the size of the current file accordingly
	for(i=0; i < 8; i++)
	{	
		size += InodeTable[i][index] > 0;
	}

	return 128*size;
}
示例#6
0
main()
{
  int i;
  int retval;  /* used to hold return values of file system calls */

  /* do forever:
     1) print a list of available commands
     2) read a command
     3) read arguments for the command
     4) perform the requested operation
     5) display the results of the operation
  */
  while(1) {
    /* print a list of available commands */
    printf("\n");
    printf("o: open a file\n");
    printf("r: read from a file\n");
    printf("w: write to a file\n");
    printf("R: read from a directory\n");
    printf("c: close a file\n");
    printf("m: create (make) a new file\n");
    printf("d: delete a file\n");
    printf("s: get the size of a file\n");
    printf("t: get the type of a file\n");
    printf("i: initialize the file system\n");
    printf("q: quit - exit this program\n");
    /* read in the next command */
    printf("\nCommand? ");
    if (gets(command_buffer) == NULL) break;
    /* determine which command was requested */
    switch(command_buffer[0]) {
    case 'o':
      /* Open a file */
      printf("Enter full path name of file to open: ");
      scanf(INPUT_BUF_FORMAT,data_buffer_1);
      retval = sfs_open(data_buffer_1);
      if (retval >= 0) {
	printf("Open succeeded.  File Descriptor number is %d\n",retval);
      }
      else {
	printf("Error.  Return value was %d\n",retval);
      }
      break;
    case 'r':
      /* Read from a file */
      printf("Enter file descriptor number: ");
      scanf("%d",&p1);
      printf("Enter read start location: ");
      scanf("%d",&p2);
      printf("Enter number of bytes to read: ");
      scanf("%d",&p3);
      retval = sfs_read(p1,p2,p3,io_buffer);
      if (retval > 0) {
	printf("Read succeeded.\n");
	printf("The following data was read (only printable ASCII will display)\n");
	for(i=0;i<p3;i++) {
	  putchar(io_buffer[i]);
	}
	printf("\n");
      }
      else {
	printf("Error.  Return value was %d\n",retval);
      }
      break;
    case 'w':
      /* Write to a file */

      printf("Enter file descriptor number: ");
      scanf("%d",&p1);
      printf("Enter write start location: ");
      scanf("%d",&p2);
      printf("Enter number of bytes to write: ");
      scanf("%d",&p3);
      printf("This program allows only non-white-space, printable ASCII characters to be written to a file.\n");
      printf("Enter %d characters to be written: ",p3);
      scanf(IO_BUF_FORMAT,io_buffer);
      retval = sfs_write(p1,p2,p3,io_buffer);
      if (retval > 0) {
	printf("Write succeeded.\n");
	printf("Wrote %s to the disk\n",io_buffer);
      }
      else {
	printf("Error.  Return value was %d\n",retval);
      }
      break;
    case 'R':
      /* Read from a directory */
      printf("Enter file descriptor number: ");
      scanf("%d",&p1);
      retval = sfs_readdir(p1,io_buffer);
      if (retval > 0) {
	printf("sfs_readdir succeeded.\n");
	printf("Directory entry is: %s\n",io_buffer);
      }
      else if (retval == 0) {
	printf("sfs_readdir succeeded.\n");
	printf("No more entries in this directory\n");
      }
      else {
	printf("Error.  Return value was %d\n",retval);
      }
      break;
    case 'c':
      /* Close a file */
      printf("Enter file descriptor number: ");
      scanf("%d",&p1);
      retval = sfs_close(p1);
      if (retval > 0) {
	printf("sfs_close succeeded.\n");
      }
      else {
	printf("Error.  Return value was %d\n",retval);
      }
      break;
    case 'm':
      /* Create a new file */
      printf("Enter full path name of new file: ");
      scanf(INPUT_BUF_FORMAT,data_buffer_1);
      printf("Enter 0 for regular file, 1 for directory: ");
      scanf("%d",&p1);
      retval = sfs_create(data_buffer_1,p1);
      if (retval > 0) {
	printf("sfs_create succeeded.\n");
      }
      else {
	printf("Error.  Return value was %d\n",retval);
      }
      break;
    case 'd':
      /* Delete a file */
      printf("Enter full path name of file to delete: ");
      scanf(INPUT_BUF_FORMAT,data_buffer_1);
      retval = sfs_delete(data_buffer_1);
      if (retval > 0) {
	printf("sfs_delete succeeded.\n");
      }
      else {
	printf("Error.  Return value was %d\n",retval);
      }
      break;
    case 's':
      /* Get the size of a file */
      printf("Enter full path name of file: ");
      scanf(INPUT_BUF_FORMAT,data_buffer_1);
      retval = sfs_getsize(data_buffer_1);
      if (retval >= 0) {
	printf("sfs_getsize succeeded.\n");
	printf("size = %d\n",retval);
      }
      else {
	printf("Error.  Return value was %d\n",retval);
      }
      break;
    case 't':
      /* Get the type of a file */
      printf("Enter full path name of file: ");
      scanf(INPUT_BUF_FORMAT,data_buffer_1);
      retval = sfs_gettype(data_buffer_1);
      if (retval >= 0) {
	printf("sfs_gettype succeeded.\n");
	if (retval == 0) {
	  printf("file type is REGULAR\n");
	}
	else if (retval == 1) {
	  printf("file type is DIRECTORY\n");
	}
	else {
	  printf("file has unknown type %d\n",retval);
	}
      }
      else {
	printf("Error.  Return value was %d\n",retval);
      }
      break;
    case 'i':
      /* Initialize the file system */
      printf("Enter 1 to erase disk while initializing, 0 otherwise: ");
      scanf("%d",&p1);
      retval = sfs_initialize(p1);
      if (retval > 0) {
	printf("sfs_initialize succeeded.\n");
      }
      else {
	printf("Error.  Return value was %d\n",retval);
      }
      break;
    case 'q':
      /* Quit this program */
      break;
    default:
      printf("Unknown command: %s\n",command_buffer);
      break;
    }
    if (command_buffer[0] == 'q') break;
    /* cleanup the newline that remains after reading command parameter(s) */
    gets(command_buffer);
  }
}
示例#7
0
int test_case_1(char **argv, int op_type)
{
	if (send_file_request(argv, "local_file", 1) == -1) 
	{
		return 1;
	}
	sleep(5);
	int ret = 0;
	int fd;
	FILE *local_fp = fopen("local_file", "rb");
	char *buf = (char *) malloc(sizeof(char) * DFS_BLOCK_SIZE);
	char *buf_local = (char *) malloc(sizeof(char) * DFS_BLOCK_SIZE);
	
	sfs_reloadfs("d1/local_fs");
	fd = sfs_open("/", "local_file_blk_0");
	sfs_read(fd, buf, DFS_BLOCK_SIZE);
	sfs_close(fd);
	fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp);
	if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1;
	if (ret == 0)
	{
		fseek(local_fp, 2 * DFS_BLOCK_SIZE, SEEK_SET); 
		fd = sfs_open("/", "local_file_blk_2");
		sfs_read(fd, buf, DFS_BLOCK_SIZE); 
		sfs_close(fd);
		fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp);
		if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1;
	}
	if (ret == 0)
	{
		fseek(local_fp, 4 * DFS_BLOCK_SIZE, SEEK_SET);
		fd = sfs_open("/", "local_file_blk_4");
		sfs_read(fd, buf, DFS_BLOCK_SIZE); 
		sfs_close(fd);
		fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp);
		if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1;
	}
	if (ret == 0)
	{
		fseek(local_fp, 6 * DFS_BLOCK_SIZE, SEEK_SET);
		fd = sfs_open("/", "local_file_blk_6");
		sfs_read(fd, buf, DFS_BLOCK_SIZE); 
		sfs_close(fd);
		fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp);
		if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1;
	}
	sfs_close_storage();
	
	sfs_reloadfs("d2/local_fs");
	fd = sfs_open("/", "local_file_blk_1");
	sfs_read(fd, buf, DFS_BLOCK_SIZE);
	sfs_close(fd);
	fseek(local_fp, 1 * DFS_BLOCK_SIZE, SEEK_SET);
	fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp);
	if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1;
	if (ret == 0)
	{
		fseek(local_fp, 3 * DFS_BLOCK_SIZE, SEEK_SET);
		fd = sfs_open("/", "local_file_blk_3");
		sfs_read(fd, buf, DFS_BLOCK_SIZE); 
		sfs_close(fd);
		fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp);
		if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1;
	}
	if (ret == 0)
	{
		fseek(local_fp, 5 * DFS_BLOCK_SIZE, SEEK_SET);
		fd = sfs_open("/", "local_file_blk_5");
		sfs_read(fd, buf, DFS_BLOCK_SIZE); 
		sfs_close(fd);
		fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp);
		if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1;
	}
	if (ret == 0)
	{
		fseek(local_fp, 7 * DFS_BLOCK_SIZE, SEEK_SET);
		fd = sfs_open("/", "local_file_blk_7");
		sfs_read(fd, buf, DFS_BLOCK_SIZE); 
		sfs_close(fd);
		fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp);
		if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1;
	}
	sfs_close_storage();
	fclose(local_fp);
	free(buf);
	free(buf_local);
	return ret;
}
示例#8
0
int main(int argc, char *argv[]) {
    
    if ( argc == 2 ) {
	port = strtol(argv[1], &endptr, 0);
	if ( *endptr ) {
	    fprintf(stderr, "Invalid port number.\n");
	    exit(EXIT_FAILURE);
	}
    }
    else if ( argc < 2 ) {
            port = ecPort;
    }

    else {
	fprintf(stderr, "Invalid arguments\n");
	exit(EXIT_FAILURE);
    }

    fileSystemInit();
    connection_setup();

    if ( (connS = accept(listS, NULL, NULL) ) < 0 ) {
	    fprintf(stderr, "Error!!\n");
	    exit(EXIT_FAILURE);
    }
    header = (char *)malloc(2 * sizeof(int));
    elreadmos = (char *)malloc(10000);
    myLStable = (char *)malloc(100);
    while ( 1 ) {
	recieveing(connS, recEI, 2 * sizeof(int));
	memcpy(&recvReq, recEI, sizeof(int));
	memcpy(&recvSize, recEI +sizeof(int), sizeof(int));
	switch (recvReq) {

		case 1:
			printf("root directory\n");
			LStableAdd = sfs_ls();
			strcpy(myLStable, LStableAdd);
			size = strlen(myLStable);
			DataSend = (char *)malloc(size);
			memcpy(header, &lsCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int)); 
			memcpy(DataSend, myLStable, size);
			Sending(connS, header, (2 * sizeof(int)));
			Sending(connS, DataSend, size);
			free(DataSend);
			break;
		case 2:
			printf("open file\n");
			recData = (char *)malloc(recvSize);
			recieveing(connS, recData, recvSize);
			memcpy(openReq, recData, recvSize);
			openReq[recvSize] = 0;
			remID = sfs_open(openReq);
			DataSend = (char *)malloc(sizeof(int));
			size = sizeof(int);
			memcpy(header, &opCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int));
			memcpy(DataSend, &remID, size);
			Sending(connS, header, (2 * sizeof(int)));
			Sending(connS, DataSend, size);
			free(recData);
			free(DataSend);
			break;
		case 3:
			printf("close file\n");
			recData = (char *)malloc(recvSize);
			recieveing(connS, recData, recvSize);
			memcpy(&elremote, recData, sizeof(int));
			sfs_close(elremote);
			memcpy(header, &closeCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int)); 
			Sending(connS, header, (2 * sizeof(int)));
                        free(recData);
			break;
		case 4:
			printf("write file\n");
			recData = (char *)malloc(recvSize);
			elWritBuf = (char *)malloc(recvSize);
			recieveing(connS, recData, recvSize - 8);
			memcpy(elWritBuf, recData, recvSize -8);
			elWritBuf[recvSize - 8] = 0;
			recieveing(connS, recData, sizeof(int));
			memcpy(&remID, recData, sizeof(int));
			recieveing(connS, recData, sizeof(int));
			memcpy(&my_len, recData, sizeof(int));
                        sfs_write(remID, elWritBuf, my_len);
			size = 0;
                        memcpy(header, &writCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int)); 
			Sending(connS, header, (2 * sizeof(int)));
                        free(recData);
			free(elWritBuf);
                        break;

		case 5:

			printf("read file\n");
			recData = (char *)malloc(recvSize);
                        recieveing(connS, recData, sizeof(int));
			memcpy(&remID, recData, sizeof(int));
			recieveing(connS, recData, sizeof(int));
			memcpy(&my_len, recData, sizeof(int));
                        sfs_read(remID, elreadmos, my_len);
                        size = strlen(elreadmos);
                        memcpy(header, &readCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int));
			Sending(connS, header, (2 * sizeof(int)));
                        DataSend = (char *)malloc(size);
			memcpy(DataSend, elreadmos, size);
			DataSend[size] = 0;
			puts(DataSend);
                        Sending(connS, DataSend, size);
                        break;

		case 6:
			printf("remove file\n");
			recData = (char *)malloc(recvSize);
                        recieveing(connS, recData, recvSize);
			memcpy(remReq, recData, recvSize);
			remReq[recvSize] = 0;
			printf("file to remove = %s\n",remReq);
                        if (sfs_remove(remReq) == -1){
				size = strlen("Error: file doesnot exist\n");
				DataSend = (char *)malloc(size);
				strcpy(DataSend, "Error: file doesnt exist\n");
			}

			else{
				size = strlen("file removed successfully\n");
				DataSend = (char *)malloc(size);
				strcpy(DataSend, "file remove successfull\n");

			}
                        memcpy(header, &remCom, sizeof(int));
			memcpy(header+sizeof(int), &size, sizeof(int)); 
			Sending(connS, header, (2 * sizeof(int)));
                        Sending(connS, DataSend, size);
                        free(recData);
			free(DataSend);
                        break;
               default:
			printf("Invalid input\n");
                        break;
	}

    }

}
示例#9
0
/* The main testing program
*/
    int
main(int argc, char **argv)
{
    int i, j, k;
    int chunksize;
    char *buffer;
    int fds[MAX_FD];
    char *names[MAX_FD];
    int filesize[MAX_FD];
    int error_count = 0;
    int tmp;

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

    /* First we open five files and attempt to write data to them.
    */
    for (i = 0; i < 5; i++) {
        names[i] = rand_name();
        fds[i] = sfs_open(names[i]);
        if (fds[i] < 0) {
            fprintf(stderr, "ERROR: creating first test file %s\n", names[i]);
            error_count++;
        }
        tmp = sfs_open(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 < 5; 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("Five files created with zero length:\n");
    sfs_ls();
    printf("\n");

    for (i = 0; i < 5; 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);
            }
            sfs_write(fds[i], buffer, chunksize);
            free(buffer);
        }
    }

    for (i = 0; i < 5; i++)
      sfs_close(fds[i]);

    sfs_ls();
	
    for (i = 0; i < 5; i++)
      fds[i] = sfs_open(names[i]);

    printf("Reopened the files again.. the read/write pointers should be set to front\n");

    for (i = 0; i < 5; 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);
            }
            sfs_read(fds[i], buffer, chunksize);
            puts(buffer);
            for (k = 0; k < chunksize; k++) {
                if (buffer[k] != (char)(j+k)) {
                    fprintf(stderr, "ERROR: data error at offset %d in file %s (%x,%x)\n",
                            j+k, names[i], buffer[k], (char)(j+k));
                    error_count++;
                    break;
                }
            }
            free(buffer);
        }
    }

    fprintf(stderr, "Test program exiting with %d errors\n", error_count);
    
    return (error_count);
}