Exemple #1
0
void mksfs(int fresh){
	int i;
	if (fresh){
		init_fresh_disk(disk_name, BLOCK_SIZE, NUM_BLOCKS);
		
		free_blocks = list_create(NULL);
		
		for (i=2; i<NUM_BLOCKS-1; i++){
			list_append_int(free_blocks, i);
		}
		for (i=0; i<NUM_BLOCKS; i++){
			fat[i][block_index] = unused;
			fat[i][next] = eof;
		}
		for (i=0; i<MAX_NUM_FILES; i++){
			files[i].created = false;
			files[i].fatIndex = eof;
		}
	}
	
	else{
		init_disk(disk_name, BLOCK_SIZE, NUM_BLOCKS);
		read_blocks(0,1, &files);
		read_blocks(1,1, &fat);
		read_blocks(NUM_BLOCKS-1, 1, &free_blocks);
	}
	for(i=0; i<MAX_NUM_FILES; i++){
		fdt[i][open] = false;
		fdt[i][read_ptr] = false;
		fdt[i][write_ptr] = files[i].size;
	}
}
Exemple #2
0
int main(int argc, char *argv[])
{
    if (argc > 1) {
        strncpy(config_file, argv[1], sizeof(config_file) - 1);
        config_file[sizeof(config_file) - 1] = 0;
        if (!strncmp(config_file, "(nd)", 4))
            preset_menu = "dhcp";
    } else if (start_info.mod_len)
        preset_menu = (void*) start_info.mod_start;
    else
        preset_menu = "dhcp --with-configfile";

    mbi.drives_addr = BOOTSEC_LOCATION + (60 * 1024);
    mbi.drives_length = 0;

    mbi.boot_loader_name = (unsigned long) "GNU GRUB " VERSION;
    mbi.mem_lower = (start_info.nr_pages * PAGE_SIZE) / 1024;
    mbi.mem_upper = 0;
    saved_drive = boot_drive;
    saved_partition = install_partition;

    init_disk();

    /* Try to make sure the client part got launched */
    sleep(1);
    cmain();
    printk("cmain returned!\n");
}
Exemple #3
0
int
main(int argc, char *argv[])
{
    int ch;
    cpu_t *cpu = init_cpu();
    char *sys_file;

    while ((ch = getopt(argc, argv, "df:")) != EOF) {
        switch (ch) {
            case 'd':
                cpu->debug = true;
                break;

            case 'f':
                sys_file = strdup(optarg);
                break;
        }
    }

    argc -= optind;
    argv += optind;

    init_disk(cpu->port0, "/tmp/disk0");

    load_cpu(cpu, sys_file);

    while (true)
        run_cpu(cpu);

    free_cpu(cpu);
    return 0;
}
int mksfs(int fresh) {

	int diskinit;

	//Create a new filesystem if the fresh variable is set
	if(fresh==1){
		diskinit = init_fresh_disk(FILESYSTEMNAME, BLOCKSIZE, NUMBEROFBLOCKS);
		if(diskinit != 0){
			//perror("Error creating disk");
			exit(-1);
		}
		else {
			//To keep the root directory in memory
			initDirectory();


			//Initialize SuperBlock
			mySuperBlock.magic = MAGIC;
			mySuperBlock.blockSize = BLOCKSIZE;
			mySuperBlock.fileSystemSize = NUMBEROFBLOCKS;
			mySuperBlock.iNodeTableLength = NUMBEROFINODES;
			mySuperBlock.rootINode = 0;

			//Use a block sized buffer
			unsigned char buffer[512];
			memset(buffer,255,512);
			memcpy(buffer,&mySuperBlock,512);
			write_blocks(0,1,buffer);

			initializeFreeBitMap();
			initializeFDTable();

			//Now for the Inodes: initialize the first one: (for root dir)
			int numRootINode = createINode(RWE, 1, ROOT_UID, ROOT_GID, 1);

			readDirectory();

			return 0;
		}
	}

	//if the free variable is not set, load
	else if (fresh==0) {
		diskinit = init_disk(FILESYSTEMNAME, BLOCKSIZE, NUMBEROFBLOCKS);
		if(diskinit != 0){
			//perror("Error reading disc");
			exit(-1);
		}
		else {
			return 0;
		}
	}

	return -1;
}
//=======================================================================================
//==============================PRIMARY API FUNCTIONS====================================
//=======================================================================================
void mksfs(int fresh){
	buffer = (void*) malloc(BLOCK_SIZE);
	memset(buffer,0,BLOCK_SIZE);
	if (fresh){							//FORMAT VIRTUAL DISK, CREATE NEW FROM SCRATCH
		init_fresh_disk(FILESYSTEM, BLOCK_SIZE, FILESYSTEM_SIZE);
		//=========Storing sb in-memory===========
		sb = (superblock){0, BLOCK_SIZE, FILESYSTEM_SIZE, MAX_FD, 0};
		memcpy(buffer, &sb, sizeof(superblock));

		//========Storing sb on-disk==============
		write_blocks(0, 1, buffer);
		
		//========Initializing Bitmap block=======
		unsigned int bit_row =  4294967295;		//Row consisting of 32 '1' bits
		for (int i = 0; i<256; i++){
			bitmap[i] = bit_row;					//Filling table with 1's (1 means block is free)
		}
		/*We use the first 3 blocks to store the Root Directory mapping table*/
		bitmap[0] = bitmap[0] << 3;
		write_blocks(1, 1, bitmap);
		
		//========Storing inode table on-disk=====
		//inode inode_table[MAX_FD+1];
		for (int i = 0; i <= MAX_FD; i++){
			inode_table[i] = (inode){0, 1, 1, 0, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, -1};
			if (i == 0)	{	//First inode is root dir inode.
				//It spans 3 blocks of data -> first three blocks in the datablock section
				inode_table[i] = (inode){0,1,1,3,{FIRST_DB,FIRST_DB+1,FIRST_DB+2,-1,-1,-1,-1,-1,-1,-1,-1,-1},-1};
			}
			memcpy(buffer, &(inode_table[i]), sizeof(inode));
			write_blocks(i+2, 1, buffer);
		}
		//====Initializing rootdir in-memory======
		for (int i = 0; i < MAX_FD; i++){
			root[i] = (dir){"", -1};
		}
		//====Initializing rootdir on-disk========
		free(buffer);
		buffer = (void*) malloc(BLOCK_SIZE*3);
		memset(buffer,0,BLOCK_SIZE*3);
		memcpy(buffer, &root, 3*BLOCK_SIZE);
		write_blocks(MAX_FD+3, 3, buffer);

		//====Initializing fd table in-memory=====
		for (int i = 0; i < MAX_FD; i++)
			fd_table[i] = (fd){1,-1,-1,-1};			//mark all entries in fd_table as empty (flag = 1 means empty)
	} else {
		init_disk(FILESYSTEM, BLOCK_SIZE, FILESYSTEM_SIZE);
	}
	free(buffer);
}
Exemple #6
0
int mksfs(int fresh)
{
    super_block spr_block;
    char buff[BLKSIZ];
    int i;
    
    // Initialize fd_table
    for(i = 0; i < MAXOPENFILES; i++)
        fd_table[i].status = FREE;
            
    if(fresh)
    {
        // Initialize and write super block to disk
        init_fresh_disk(DISKIMG, BLKSIZ, BLOCKNUM);
        spr_block.magic_number = MAGICNUM;
        spr_block.block_size = BLKSIZ;
        spr_block.fs_size = BLOCKNUM;
        spr_block.inode_tbl_length = INODETBLSIZ;
        spr_block.root_inode = ROOTINODENUM;
        write_blocks(SUPERBLOCKADD, 1,  (void*) &spr_block);
        
        // Setup datablock bitmap
        memset(buff, FREE, BLKSIZ);
        write_blocks(DBBITMAPADD, 1, buff);
        
        // Setup INODE Bitmap and allocate first i-node entry to root directory
        buff[ROOTINODENUM] = USED;
        write_blocks(INBITMAPADD, 1,  buff);
        
        // Allocate fd to root directory
        fd_table[ROOTFD].status = USED;
        fd_table[ROOTFD].inode_num = ROOTINODENUM;
        fd_table[ROOTFD].rw_pointer = 0;
        fd_table[ROOTFD].inode = (inode_struct*) calloc(1, sizeof(inode_struct));
        fd_table[ROOTFD].inode->size = 0;

        write_inode_by_index(fd_table[ROOTFD].inode_num, fd_table[ROOTFD].inode);
    } else 
    {
        init_disk(DISKIMG, BLKSIZ, BLOCKNUM);
        fd_table[ROOTFD].status = USED;
        fd_table[ROOTFD].inode_num = ROOTINODENUM;
        fd_table[ROOTFD].inode = get_inode_by_index(ROOTINODENUM);
        fd_table[ROOTFD].rw_pointer = fd_table[ROOTFD].inode->size;
    }   
    
    return 0;
}
Exemple #7
0
void mksfs(int fresh) {
	if (fresh == 1){
		//Makes a new disk if fresh flag is true
		printf("Initializing disk\n");
		init_fresh_disk(DISK,BLOCK_SIZE,MAX_BLOCKS);	
		fillWithZeroes();	
		//calloc for fdt
		fileDescriptorTable = (FileDescriptorTable*)calloc(1,sizeof(FileDescriptorTable));	
		//Write superblock 
		printf("Writing superblock\n");
		//setupSuperblock(superBlock);
		printf("Setting up superblock\n");
		write_blocks(0,1,&superBlock);
		//Write root directory
		bzero(&rootDirectory,sizeof(Directory));
		printf("Writing root directory\n");
		write_blocks(1,1, &rootDirectory);
		//Write free blocks
		printf("Writing free blocks\n");
		freeBlocks.numberFree = MAX_BLOCKS;
		freeBlocks.freeBlocks[0] = 1; //superblock
		freeBlocks.numberFree--;
		freeBlocks.freeBlocks[1] = 1; //root directory
		freeBlocks.numberFree--;
		freeBlocks.freeBlocks[MAX_BLOCKS-1] = 1; //free_blocks;		
		freeBlocks.numberFree--;
		//Write free blocks to disk
		printf("writing free blocks to disk \n");
		write_blocks(MAX_BLOCKS - 1, 1, &freeBlocks);

		
	}else{
		printf("Loading disk\n");
		//Pull disk data into memory
		init_disk(DISK, BLOCK_SIZE, MAX_BLOCKS);
	}
	//Initializes variable for sfs_getnextfilename;
	sfsGetNextIndex = 0;
	
	//Initializes variable for fileDescriptor table
	(*fileDescriptorTable).firstOpenIndex = 0;
	(*fileDescriptorTable).size = 0;
	//Initializes variable for rootDirectory
	rootDirectory.firstOpenIndex = 2;
	rootDirectory.size = 3;
	return;
}
/**
 * creates the file system
 */
void mksfs(int fresh) {
    // Allocate the local file system
    allocate_necessary_memory();

	//Implement mksfs here
    if (fresh) {
        // We need to construct the file system from scratch

        // Delete the disk file, if it exists
        remove(FILE_SYSTEM_NAME);

        // Initialize a new fresh disk
        init_fresh_disk(FILE_SYSTEM_NAME, DISK_BLOCK_SIZE, DISK_BLOCK_COUNT);

        // Write superblock to disk
        SuperBlock sb = {};
        sb.magic = SB_MAGIC;
        sb.block_size = 512;
        sb.file_system_size = DISK_BLOCK_COUNT;
        sb.i_node_table_length = I_NODE_COUNT;
        sb.root_directory_i_node = 0;
        write_blocks(SUPER_BLOCK_INDEX, 1, &sb);

        // Write the rest of the stuff to disk
        save_local_file_system_to_disk(freeBitMap, iNodeTable, directoryCache);
    } else {
        // File system already exists on disk, so we need to load it from the disk.

        // Initialize an existing disk
        init_disk(FILE_SYSTEM_NAME, DISK_BLOCK_SIZE, DISK_BLOCK_COUNT);

        // Copy iNodes into local memory
        load_i_node_cache_from_disk(iNodeTable);
        // Copy directory cache into memory
        load_directory_cache_from_disk(directoryCache);
        // Copy the free bit map into memory
        load_free_bitmap_from_disk(freeBitMap);
    }

	return;
}
Exemple #9
0
/************************************************
* get_block(blknum,buf)
*    - retrieves one block from the simulated disk
*      if disk file is not yet open, an attempt is
*      made to open it.
*
*    - blknum is the number of the desired block
*       (zero-based count)
*    - buf should point to a block-sized buffer
*
*    - Returns 0 if successful, -1 otherwise
*************************************************/
int
get_block(int blknum,
	  char *buf)
{
  if (blknum >= NUMBLKS || blknum < 0) {
    fprintf(stderr,"get_block: invalid block number: %d\n",blknum);
    return(-1);
  }
  if (diskfd < 0) {
    /* disk data file is not yet open - attempt to open it */
    if (init_disk() != 0) return(-1);
  }
  /* locate specified block */
  if (lseek(diskfd,blknum*BLKSIZE,SEEK_SET) < 0) {
    perror("get_block");
    return(-1);
  }
  /* get the data */
  if (read(diskfd,buf,BLKSIZE) < 0) {
    perror("get_block");
    return(-1);
  }
  return(0);
}
Exemple #10
0
//Creates the file system
int mksfs(int fresh){
	char buf[BLOCKSIZE];
	memset(buf,0, BLOCKSIZE); //sets a block-size buffer  and set it to zero
	
	if(fresh){
		init_fresh_disk(DISK_FILE, BLOCKSIZE, TOTAL_NUM_BLOCKS); 
		
		//initialize super block
		super_block superblock;
		superblock.magic=0xAABB0005;
		superblock.block_size=BLOCKSIZE;
		superblock.file_system_size=TOTAL_NUM_BLOCKS;
		superblock.inode_table_length=NUMBER_INODES;
		superblock.root_dir_inode_number=0;	//set root directory at the first inode 
		//copy super block to buffer
		memcpy((void *)buf,(const void *) &superblock,  sizeof(super_block)); 
		//write the superblock to the first block in the disk
		write_blocks(0,1, buf);
		//initialize inode table
		memset(inodes, 0, sizeof(inodes));
		//Set first inode to root directory inode 
		INODE root_dir_inode={
			.mode=0777, 
			.link_cnt=1, 
			.uid=0, 
			.gid=0,
			.size=0,
			.pointers={14,15,16,17,18,0,0,0,0,0,0,0,0} //pointer to the first data block starting at 14
		};

		inodes[0]=root_dir_inode;
		//write inode into disk
		int inode_number = 0;
		int block_of_inode=inode_number/8+1; 
		char inode_update_buf[BLOCKSIZE];
		memset(inode_update_buf,0, BLOCKSIZE);
		read_blocks(block_of_inode, 1, inode_update_buf);
		memcpy((void *)inode_update_buf+(inode_number%8)*sizeof(INODE),(const void *) &inodes[inode_number], sizeof(INODE));
		write_blocks(block_of_inode, 1, inode_update_buf);	

		//initialize the bitmap
		memset(free_bitmap, 0, sizeof(free_bitmap));
		free_bitmap[0]=255; //superblock and first part of inodes to occupied.
		free_bitmap[1]=255; //inodes table to occupied.
		free_bitmap[2]=224; //directory entry table to occupied. 
		free_bitmap[BLOCKSIZE-1]=1; //last block for the bitmap	
		write_blocks(TOTAL_NUM_BLOCKS-1,1,free_bitmap);//write the bitmap into disk

		//initialize directory table into memory
		memset(dir_table, 0, sizeof(dir_table));

		//Initialize all file's RW pointers to 0 and open to 0 
		memset(file_des_t, 0, sizeof(file_des_t));
		cur_pos=-1;
		return 0;
	}
	else{ 
		//file system already exists
		init_disk(DISK_FILE, BLOCKSIZE, TOTAL_NUM_BLOCKS);
		// set free_bitmap to zeros
		memset(free_bitmap,0, sizeof(free_bitmap));
		//load existing bitmap 
		read_blocks(TOTAL_NUM_BLOCKS-1,1,free_bitmap);
		//load existing superblock
		super_block superblock;
		read_blocks(0,1,&superblock);
		//load existing inode table
		char inodes_buffer[BLOCKS_INODES_TABLE*BLOCKSIZE];
		memset(inodes_buffer,0,sizeof(inodes_buffer));
		read_blocks(1,BLOCKS_INODES_TABLE,inodes_buffer);
		
		int n;
		for(n=0;n<NUMBER_INODES;n++){
			memcpy((void *)&(inodes[n]),(const void *)(inodes_buffer+n*(BLOCKSIZE/8)), BLOCKSIZE/8); 
		}
		
		//load existing directory table
		INODE root_directory=inodes[superblock.root_dir_inode_number];
		int i;
		
		char root_dir_buffer[BLOCKSIZE];
		
		memset(root_dir_buffer,0,BLOCKSIZE);
		
		for(i=0;i<INODE_POINTERS;i++){
			if(root_directory.pointers[i]!=0){
				read_blocks(root_directory.pointers[i],1, root_dir_buffer); 
				int j;
				for(j=0;j<NUM_ENTRIES_PERBLOCK;j++){
					if((j+NUM_ENTRIES_PERBLOCK*i) >= TOTAL_NUM_FILES){
						break;
					}
					memcpy((void *)&(dir_table[j]), (const void *)(root_dir_buffer+j*(sizeof(dir_entry))), sizeof(dir_entry));
				}
				memset(root_dir_buffer,0,BLOCKSIZE);
			}
		}
		//initialize all rw pointers to 0 and open to 0 
		memset(file_des_t, 0, sizeof(file_des_t));
		cur_pos=-1;
		return 0;
	}
}
Exemple #11
0
disk_t *fewf_init(const char *device, const int mode)
{
  unsigned int num_files=0;
  char **filenames= NULL;
  disk_t *disk=NULL;
  struct info_fewf_struct *data;
#if !defined( HAVE_LIBEWF_V2_API ) && defined( HAVE_GLOB_H )
  glob_t globbuf;
#endif
  data=(struct info_fewf_struct *)MALLOC(sizeof(struct info_fewf_struct));
  memset(data, 0, sizeof(struct info_fewf_struct)); 
  data->file_name = strdup(device);
  data->handle=NULL;
  data->mode = mode;

#ifdef DEBUG_EWF
#if defined( HAVE_LIBEWF_V2_API )
  libewf_notify_set_stream( stderr, NULL );
  libewf_notify_set_verbose( 1 );
#else
  libewf_set_notify_values( stderr, 1 );
#endif
#endif

#if defined( HAVE_LIBEWF_V2_API )
  if( libewf_glob(
       data->file_name,
       strlen(data->file_name),
       LIBEWF_FORMAT_UNKNOWN,
       &filenames,
       (int *)&num_files,
       NULL ) != 1 )
  {
      log_error("libewf_glob failed\n");
      free(data);
      return NULL;
  }
#elif defined( HAVE_GLOB_H )
  {
    globbuf.gl_offs = 0;
    glob(data->file_name, GLOB_DOOFFS, NULL, &globbuf);
    if(globbuf.gl_pathc>0)
    {
      filenames=(char **)MALLOC(globbuf.gl_pathc * sizeof(*filenames));
      for (num_files=0; num_files<globbuf.gl_pathc; num_files++) {
	filenames[num_files]=globbuf.gl_pathv[num_files];
      }
    }
  }
  if(filenames==NULL)
  {
    globfree(&globbuf);
    free(data);
    return NULL;
  }
#else
  {
    filenames=(char **)MALLOC(1*sizeof(*filenames));
    filenames[num_files] = data->file_name;
    num_files++;
  }
#endif

  if((mode&TESTDISK_O_RDWR)==TESTDISK_O_RDWR)
  {
#if defined( HAVE_LIBEWF_V2_API )
    if( libewf_handle_initialize(
	  &( data->handle ),
	  NULL ) != 1 )
    {
      log_error("libewf_handle_initialize failed\n");

      libewf_glob_free(
	  filenames,
	  num_files,
	  NULL );
      free(data);
      return NULL;
    }
    if( libewf_handle_open(
	  data->handle,
	  filenames,
	  num_files,
	  LIBEWF_OPEN_READ_WRITE,
	  NULL ) != 1 )
    {
      log_error("libewf_handle_open(%s) failed\n", device);
    }
#else
    data->handle=libewf_open(filenames, num_files, LIBEWF_OPEN_READ_WRITE);
    if(data->handle==NULL)
    {
      log_error("libewf_open(%s) failed\n", device);
    }
#endif /* defined( HAVE_LIBEWF_V2_API ) */
  }
  if(data->handle==NULL)
  {
    data->mode&=~TESTDISK_O_RDWR;
#if defined( HAVE_LIBEWF_V2_API )
    if( libewf_handle_initialize(
	  &( data->handle ),
	  NULL ) != 1 )
    {
      log_error("libewf_handle_initialize failed\n");

      libewf_glob_free(
	  filenames,
	  num_files,
	  NULL );
      free(data);
      return NULL;
    }
    if( libewf_handle_open(
	  data->handle,
	  filenames,
	  num_files,
	  LIBEWF_OPEN_READ,
	  NULL ) != 1 )
    {
      log_error("libewf_handle_open(%s) failed\n", device);

      libewf_handle_free(
	  &( data->handle ),
	  NULL );

      libewf_glob_free(
	  filenames,
	  num_files,
	  NULL );
      free(data);
      return NULL;
    }
#else
    data->handle=libewf_open(filenames, num_files, LIBEWF_OPEN_READ);
    if(data->handle==NULL)
    {
      log_error("libewf_open(%s) failed\n", device);
#if defined( HAVE_GLOB_H )
      globfree(&globbuf);
#endif
      free(filenames);
      free(data);
      return NULL;
    }
#endif /* defined( HAVE_LIBEWF_V2_API ) */
  }

#if defined( HAVE_LIBEWF_V2_API )
  if( libewf_handle_set_header_values_date_format(
       data->handle,
       LIBEWF_DATE_FORMAT_DAYMONTH,
       NULL ) != 1 )
  {
    log_error("%s Unable to set header values date format\n", device);
  }
#else
  if( libewf_parse_header_values( data->handle, LIBEWF_DATE_FORMAT_DAYMONTH) != 1 )
  {
    log_error("%s Unable to parse EWF header values\n", device);
  }
#endif
  disk=(disk_t *)MALLOC(sizeof(*disk));
  init_disk(disk);
  disk->arch=&arch_none;
  disk->device=strdup(device);
  disk->data=data;
  disk->description=fewf_description;
  disk->description_short=fewf_description_short;
  disk->pread_fast=fewf_pread_fast;
  disk->pread=fewf_pread;
  disk->pwrite=(data->mode&TESTDISK_O_RDWR?fewf_pwrite:fewf_nopwrite);
  disk->sync=fewf_sync;
  disk->access_mode=(data->mode&TESTDISK_O_RDWR);
  disk->clean=fewf_clean;
#if defined( HAVE_LIBEWF_V2_API ) || defined( LIBEWF_GET_BYTES_PER_SECTOR_HAVE_TWO_ARGUMENTS )
  {
    uint32_t bytes_per_sector = 0;

#if defined( HAVE_LIBEWF_V2_API )
    if( libewf_handle_get_bytes_per_sector(
         data->handle,
         &bytes_per_sector,
         NULL ) != 1 )
#else
    if( libewf_get_bytes_per_sector(data->handle, &bytes_per_sector)<0)
#endif
    {
      disk->sector_size=DEFAULT_SECTOR_SIZE;
    }
    else
    {
      disk->sector_size=bytes_per_sector;
    }
  }
#else
  disk->sector_size=libewf_get_bytes_per_sector(data->handle);
#endif

//  printf("libewf_get_bytes_per_sector %u\n",disk->sector_size);
  if(disk->sector_size==0)
    disk->sector_size=DEFAULT_SECTOR_SIZE;
  /* Set geometry */
  disk->geom.cylinders=0;
  disk->geom.heads_per_cylinder=1;
  disk->geom.sectors_per_head=1;
  disk->geom.bytes_per_sector=disk->sector_size;
  /* Get disk_real_size */
#if defined( HAVE_LIBEWF_V2_API ) || defined( LIBEWF_GET_MEDIA_SIZE_HAVE_TWO_ARGUMENTS )
  {
    size64_t media_size = 0;

#if defined( HAVE_LIBEWF_V2_API )
    if( libewf_handle_get_media_size(
         data->handle,
         &media_size,
         NULL ) != 1 )
#else
    if(libewf_get_media_size(data->handle, &media_size)<0)
#endif
    {
      disk->disk_real_size=0;
    }
    else
    {
      disk->disk_real_size=media_size;
    }
  }
#else
  disk->disk_real_size=libewf_get_media_size(data->handle);
#endif
  update_disk_car_fields(disk);
#if defined( HAVE_LIBEWF_V2_API )
  libewf_glob_free(
    filenames,
    num_files,
    NULL );
#else
#if defined( HAVE_GLOB_H )
  globfree(&globbuf);
#endif
  free(filenames);
#endif
  return disk;
}
Exemple #12
0
void mksfs(int fresh){
	//Implement mksfs here
	if (fresh == 1){
		//deletes fs if it already exists
		if(access(DISK_FILE, F_OK) != -1){
			unlink(DISK_FILE);			
		}
		//fs creation
		printf("Initalizing sfs\n");
		init_fresh_disk(DISK_FILE, BLOCK_SIZE, MAX_BLOCKS);
        //zero_everything();
		// writes superblock to the first block
        printf("Writing superblocks\n");
        init_superblock();
		write_blocks(0, 1, &sb);
		// write the inode table to the 2nd block
        printf("Writing inode table\n");
		init_inode_table();
        //add_root_dir_inode();
        //add_dummy_file_inode();
		//write_blocks(1, 1, &inode_table);
		// creates free list
		init_free_list();
        // write root directory data to the 3rd block
        printf("Writing root dir\n");		
		init_root_dir();

		//allocate memory for directory inode
		inode_t *inode = malloc(ADJUST((MAX_FILES+1)*sizeof(inode_t)));
		read_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode);
		if(inode == NULL){
			return;
		}
		//set first inode to point to directory
		inode[0].size = DIRECTORY_SIZE*BLOCK_SIZE;
		inode[0].link_cnt = DIRECTORY_SIZE;
		inode[0].exists = 1;
		
		//check to see if we need to use ref_ptr
		//if(DIRECTORY_SIZE > 12){
			//inode[0].ref_ptr = search();
			//setAlloc(inode[0].ref_ptr);
			//unsigned int *buffer = malloc(BLOCK_SIZE);
			//write_blocks(19 + inode[0].ref_ptr, 1, buffer);
			//free(buffer);
		
		//assign the pointers the location of directory files
		int i;
		for(i = 0; i < DIRECTORY_SIZE; i++){
			if(i > 11){
				unsigned int *buffer = malloc(BLOCK_SIZE);
				read_blocks(19+ inode[0].ref_ptr, 1, buffer);
				buffer[i - 12] = 2 + i;
				write_blocks(19+ inode[0].ref_ptr, 1, buffer);
				free(buffer);
			} 
			else{
				inode[0].data_ptrs[i] = 2 + i;
			}
		}
		//update inode and free memory
		write_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode);
		free(inode);		
	}
	
	else if (fresh == 0){
		if(init_disk(DISK_FILE, BLOCK_SIZE, MAX_BLOCKS) != 0){
			printf("Error initializing disk\n");
			return;
		}
	}
	//allocate main memory for filesystem data structures
	int *superblock = malloc(BLOCK_SIZE*SUPERBLOCK_SIZE);

	if(superblock == NULL){
		printf("Error allocating memory for superblock\n");
		return;
	}
	read_blocks(0, SUPERBLOCK_SIZE, superblock);
	//allocate main memory for directory
	root_dir = malloc(ADJUST(sizeof(dir_entry_t)*MAX_FILES));
	
	if(root_dir == NULL){
		printf("Error allocating memory for root directory\n");
		return;
	}
	read_blocks(2, DIRECTORY_SIZE, root_dir);
	//allocate memory for inode table
	inode_table = malloc(ADJUST(sizeof(inode_t)*(MAX_FILES+1)));
	
	if(inode_table == NULL){
		printf("Error allocating memory for inode table");
		return;
	}

	read_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode_table);
	fileCount = 0;
	fd_table = NULL;
	return;		
}
void sfs_init_storage()
{
	CACHE_SWITCH = 0;
	init_disk("storage", 1026, 0);
}
void mksfs(int fresh){
	int freeSpace;
	int FATSize;
	int dirSize;
        int i;
        int j;
	char *buffer1;
	blockSize = BLOCK_SIZE;
	numBlocks = NUM_BLOCKS;
	maximummyfiles = MAX_myfileS;
	buffer1 = (char *) malloc(1024);

	if(fresh != 0){//create a new sfs

		init_disk("s2.dsk", blockSize, numBlocks);
		init_cache(0);
		for(i=0; i<maximummyfiles; i++){
			strcpy(directory[i].name, "empty_myfile");
			directory[i].size = 0;
			directory[i].date = 0;
			directory[i].indFAT = -1;
		}

		for(i=0; i<numBlocks; i++){
			for(j=0; j<2; j++) FAT[i][j] = -2;
			}

		for(i=0; i<numBlocks; i++){
			free_list[i] = '0';
		}
		free_list[0] = '1';
		free_list[1] = '1';
		free_list[numBlocks - 1] = '1';

		//set FDT settings
		for(i=0; i<maximummyfiles; i++){
			FDT[i].dirIndex = -1;
		}

		dirSize = sizeof(entry) * maximummyfiles;
		FATSize = sizeof(int) * numBlocks * 2;
		freeSpace = sizeof(char) * numBlocks;

		while(dirSize>0){
			if(dirSize>1024){
				memcpy(buffer1, directory, 1024);
			}
			else {
				memcpy(buffer1, directory, dirSize);
			}
			write_blocks(0, 1, buffer1);
			dirSize -= 1024;
		}

		while(FATSize>0){
			if(FATSize>1024){
				memcpy(buffer1, FAT, 1024);
			}
			else {
				memcpy(buffer1, FAT, FATSize);
			}
			write_blocks(1, 1, buffer1);
			FATSize -= 1024;
		}

		while(freeSpace>0){
			if(freeSpace>1024){
				memcpy(buffer1, free_list, 1024);
			}
			else {
				memcpy(buffer1, free_list, freeSpace);
			}
			write_blocks(1023, 1, buffer1);
			freeSpace -= 1024;
		}

		printf("SFS created successfully\n");
	}

	else{ // load existing sfs

		for(i=0; i<maximummyfiles; i++){
			FDT[i].dirIndex = -1;
		}

		read_blocks(0, 1, buffer1);
		memcpy(directory,buffer1, 1024);
		read_blocks(1, 1, buffer1);
		memcpy(FAT,buffer1, 1024);
		read_blocks(1023, 1, buffer1);
		memcpy(free_list,buffer1, 1024);
		printf("SFS loading successfull\n");


	}

	
	read_blocks(0, 1, buffer1);

	memcpy(directory,buffer1, 1024);

	read_blocks(1, 1, buffer1);
	memcpy(FAT,buffer1, 1024);

	read_blocks(1023, 1, buffer1);
	memcpy(free_list,buffer1, 1024);
}