Exemplo n.º 1
0
/**
 *\fn  main (int argc, char * argv[]){
 *\brief si on prend en argumen le chemin d'un fichier et un nom, on renome le fichier avec le nom et on retourne o, sinon on retournt -1;
 **/
int main (int argc, char * argv[]){
  if(argc==3){
    iter iter=decomposition(strdup(argv[1]));
    if(strcmp(iter->name, "FILE:")==0){
      if(iter->next!=NULL){
	iter=iter->next;
	if(strcmp(iter->name,"HOST")==0){
	  return 1; 
	}else{
	  if(iter->next!=NULL){
	    iter=iter->next;
	    disk_id *id=malloc(sizeof(disk_id));
	    error err = start_disk(iter->name, id);
	    if(err.errnb!=-1){
	      if(tfs_rename(argv[1], argv[2])!=-1){
		return 0;
	      }
	    }
	  }
	}
      }
    }
  }
  return -1;
}
Exemplo n.º 2
0
/*
** ./tfs_analyze [name]
** Permet d'obtenir les informations d'un disque.
** Taille, nombre de partitions, taille de chaque partition...
*/
int main(int argc, char *argv[]) {
    if(argc!=1 && argc!=2) {
        fprintf(stderr, "Nombre d'arguments incorrect (%d).\n", argc);
        fprintf(stderr, "./tfs_analyze [name]\n");
        return 1;
    }
    disk_id id;
    char *name;
    name = ((argc==2)?argv[1]:"disk.tfs");
    if(start_disk(name, &id).error_id==1) {
        fprintf(stderr, "Erreur lors du démarrage du disque.\n");
        return 1;
    }
    printf("--- Démarrage du disque. ---\n");
    block b;
    b.block_block = malloc(1024);
    if(read_block(id, b, 0).error_id==1) {
        stop_disk(id);
        fprintf(stderr, "Erreur lors de la lecture du block.\n");
        return 1;
    }
    uint32_t size = read_inblock(0,b);
    uint32_t nb_part = read_inblock(1,b);
    printf("Lecture des informations.\n");
    printf(" > Nom du disque : %s\n", name);
    printf(" > Nombre de blocks : %d\n", size);
    printf(" > Nombre de Partitions : %d\n", nb_part);
    if(nb_part>0) {
        uint32_t i = 0;
        for(i=0; i<nb_part; i++) {
            printf(" > Partition %d : %d blocks\n", i, read_inblock(i+2,b));
            block desc_b;
            desc_b.block_block = malloc(1024);
            if(read_block(id, desc_b, get_description_block(b,i+1)).error_id==1) {
                stop_disk(id);
                fprintf(stderr, "Erreur lors de la lecture d'un block de description.\n");
                return 1;
            }
            if(read_inblock(0,desc_b)>0) {
                printf("    > La partition est formatee.\n");
            }
            else {
                printf("    > La partition n'est pas formatee.\n");
            }
        }
    }
    printf("Lecture terminée.\n");
    if(stop_disk(id).error_id==1) {
        fprintf(stderr, "Erreur lors de l'arrêt du disque.\n");
        return 1;
    }
    else {
        printf("--- Arrêt du disque. ---\n");
    }
    return 0;
}
Exemplo n.º 3
0
/**
 *\fn int main(int argc, char* argv[])
 *\brief Command tfs_analyse Show a little description of a disk, the option -p is for a partition
 *\param argc Integer represents strings number on argv 
 *\param argv Array of string
 *\return  Integer represents the result of the command
 * */
int main(int argc, char* argv[]){
  error e;
  int i;
  int used_space=0;
  if(argc >= 2){
    disk_id* disk = malloc(sizeof(disk_id));
    e = start_disk(argv[argc-1],disk);
    if(e.errnb!=-1){
			switch(argc){
				case 2 : 
					fprintf(stdout,"\n\t\t\t\t\tDisk Configuration : %s\n",argv[argc-1]);
					if((*disk).nbPart>0){
						fprintf(stdout,"Partition number : %d\n\n\tList of partitions : \n",(*disk).nbPart);
						for(i=0; i<(*disk).nbPart;i++){
							used_space += (*disk).tabPart[i].taille+1;
							fprintf(stdout,"Partition %d : \t%dB\n",i,(*disk).tabPart[i].taille);
						}
					}
					int free_space = (*disk).nbBlock - used_space;
					fprintf(stdout,"\nDisk : %s\n\t\tBlocks number : %d\tUsed blocks : %d\t\tFree blocks : %d\n",argv[argc-1],(*disk).nbBlock,used_space,free_space);
					break;
				case 4 :
					if(strcmp(argv[1],"-p")==0){
						int pnum = atoi(argv[argc-2]);
						if((*disk).nbPart>0 && pnum<(*disk).nbPart && pnum>-1 ){
							fprintf(stdout,"\n\t\t\t\t\tPartition %d Configuration on %s : \n",pnum,argv[argc-1]);
							Part p = (*disk).tabPart[pnum];
							fprintf(stdout,"Size of partition : %d B\n\n",p.taille);
							fprintf(stdout,"\tNumber of the description block of the partition on disk : Block N° %d\n\n",p.num_first_block);
							fprintf(stdout,"\tSize of free blocks in partition : %dB\n\n",p.free_block_count);
							fprintf(stdout,"\tNumber of the first free block in the partition : Block N° %d\n\n",p.first_free_block);
							fprintf(stdout,"\t\tSize of the files Table in partiton : %dB\n",p.file_table_size);
							fprintf(stdout,"\t\tMaximum number of file in partition : %d\n",p.max_file_count);
							fprintf(stdout,"\t\tNumber of free file in partition : %d\n",p.free_file_count);
							fprintf(stdout,"\t\tNumber of the first free file in partition : %d\n\n",p.first_free_file);
						}
					}else{
						fprintf(stderr,"tfs_analyse : Wrong arguments for partition option. Do you mean -p instead of %s\n",argv[1]);
						exit(-1);
					}
					break;
			}
			stop_disk(*disk);
			exit(0);
		}else{
			exit(-1);
		}
  }else{
    fprintf(stderr,"tfs_analyse : Wrong arguments number\n");
    exit(-1);
  }
}
Exemplo n.º 4
0
int main(int argc, char **argv) 
{
	FILE* file;
	error err;
	disk_id disk;
	block bk;
	char* name;
	
	if(argc < 1 || argc > 2)
	{
		fprintf(stderr, ERROR_TFS_ANALYSE);
		fprintf(stderr, ERROR_NUMBBER_ARGUMENT);
		exit(EXIT_FAILURE);
	}

	name = argc == 2 ? argv[1] : "disk.tfs";
	
	while(access(name, F_OK) == -1)
	{
		printf("%s no exists, new disk name :\n", name);
		scanf("%s", name);
	}	
	
	if((err = start_disk(name, &disk)).flag == FLAG_ERROR)
	{
		fprintf(stderr, err.desc);
		exit(EXIT_FAILURE);
	}
	
	memset(bk.desc, uint32_to_little_endian(0), TTTFS_VOLUME_BLOCK_SIZE);
	
	if((err = read_block(disk, &bk, 0)).flag == FLAG_ERROR)
	{
		fprintf(stderr, err.desc);
		
		if((err = stop_disk(disk)).flag == FLAG_ERROR)
			fprintf(stderr, err.desc);
			
		exit(EXIT_FAILURE);
	}
	
	display(disk, bk);
	
	if((err = stop_disk(disk)).flag == FLAG_ERROR)
	{
		fprintf(stderr, err.desc);
		exit(EXIT_FAILURE);
	}
	
	exit(EXIT_SUCCESS);
}
Exemplo n.º 5
0
void
start_fs(const char *diskname, const char *partname)
{
	int rc;
	oskit_blkio_t *part;

	osenv_process_lock();
	rc = start_disk(diskname, partname, 0, &part);
	osenv_process_unlock();
	if (rc) {
		printf("start_disk() failed:  ernno 0x%x\n", rc);
		exit(rc);
	}

	start_fs_on_blkio(part);
}
Exemplo n.º 6
0
int main(int argc, char* argv[]){
    int temp;
    if (argc == 3 || argc == 4){
        // Verification du mode
        if (strcmp(argv[1],"-s") != 0){
            fprintf(stderr, "Error : wrong option mode.\n");
            return -1;
        }
        
        // Initialisation
        int size = atoi(argv[2]);
        char* name = malloc(sizeof(char));
        if (argc == 4){
            strcpy(name,argv[3]);
        }
        else{
            strcpy(name,"disk.tfs");
        }
        
        // Création du disque
        disk_id* id = malloc(sizeof(disk_id));
        temp=open(name,O_CREAT,0777);
        close(temp);
        error e = start_disk(name,id);
        if (e.val == 0){
            block *block0;
            block0=malloc(sizeof(block));
            uint32_t d = itoui(size);
            memcpy(block0->octets,&d,sizeof(uint32_t));
            
            //memcpy(&d,block0->octets,sizeof(uint32_t));
            write_block(id,block0,0);
            sync_disk(id);
            printf("Disk %s created ! - Size : %d \n", name, size);
        }
        else{
            fprintf(stderr, "Error while creating disk.\n");
            return -1;
        }
    }
    else{
        fprintf(stderr, "Error : wrong number of arguments.\n");
        return -1;
    }
    
    return 0;
}
Exemplo n.º 7
0
int verif_path(char* path){
	if(verif_chemin(path) == 0){
		// l'indice 0 correspond au FILE 
		// l'indice 1 correspond au vide
		// l'indice 2 correspond au disk
		char* name = bout_chemin(path,2);
		disk_id disk; 
		if(exist_disk(name)==0){
			error rep = start_disk(name,&disk);
			if(rep.error_id==0){
				uint32_t numero_partition = atoi(bout_chemin(path,3))+1;
				if(numero_partition<0){
					fprintf(stderr,"Le numero de la partition ne peut pas etre negatif");
					return 1;
				} 
				block zero; 
				zero.block_block = malloc(1024);
				read_block(disk,zero,0);
				if(numero_partition <= nombre_partition(zero)){
					block description;
					description.block_block = malloc(1024);
					read_block(disk,description,get_description_block(zero,numero_partition));			
					char* str = malloc(strlen(path)-7);
					bout_chemin_final(path,str);
					uint32_t racine = get_description_block(zero,numero_partition)+get_racine(description);	
					block racine_block; 	
					racine_block.block_block = malloc(1024);
					read_block(disk,racine_block,racine);	
					//directory dir;  		
					
					
					
				}
		
				// disk.tfs/0/baba  compte_nombre_slash -2 l'indice du bout de chemin commence a 2 jusqua nombre de slash
				// si l'indice de la boucle est egale au nombre de slash verifier si le nom du fichier existe dans le fic courant 
				// structure directory creer une entree avec un tableau de d'entree le nombre d'entree
				// -verifie a la racine si le le name que j aurai a lindice 4 existe 
				// ********* a continuer ******
			}
			return 1;	
		}
		return 1;	
	}
	return 1;
}
Exemplo n.º 8
0
static OSKIT_COMDECL
fs_file_open(oskit_file_t *f, oskit_oflags_t iflags,
	     struct oskit_openfile **out_openfile)
{
        struct fs_file  *fsf = (struct fs_file *) f;
	char		*name, disk[8], part[8];
	oskit_blkio_t   *bio;

#if VERBOSITY > 1
	printf(__FUNCTION__": asked to open `%s'\n", fsf->component);
#endif
        memset(disk, 0, sizeof(disk));
        memset(part, 0, sizeof(part));

	/*
	 * Look at the component name and see if its recognizable as
	 * a linux device name. If it is, separate it from the partition
	 * and use start_disk to get it fired off.
	 */
	name = fsf->component;

	if (*name == 'r') {
		/* All we got is char devices, and linux does not like "r" */
		name++;
	}

	/*
	 * Look for sd, wd, or hd, followed by a single digit.
	 */
	if (strncmp("sd", name, 2) &&
	    strncmp("wd", name, 2) &&
	    strncmp("hd", name, 2))
		return OSKIT_EINVAL;

	if (!isdigit(name[2]))
		return OSKIT_EINVAL;

	/*
	 * Okay, got a valid disk name. Don't worry about what the partition
	 * looks like since it will be checked someplace else.
	 */
	strncpy(disk, name, 3);
	strncpy(part, &name[3], sizeof(part));

	/*
	 * Use the startup library. We get back a blkio.
	 */
	if (start_disk(disk, part,
		       (iflags & OSKIT_O_ACCMODE) == OSKIT_O_RDONLY, &bio)) {
		printf("devdir fs_file_open: "
		       "Could not start disk '%s'\n", name);
		return OSKIT_E_FAIL;
	}
	fsf->bio = bio;
	fsf->blksize = oskit_blkio_getblocksize(bio);

#if VERBOSITY > 1
	printf("devdir fs_file_open: "
	       "Opened `%s' - bio:%p blksize:%d\n", name, bio, fsf->blksize);
#endif
	/*
	 * Open succeeds, but we do not implement peropen state, so just
	 * return status. The absio interface will be used.
	 */
	*out_openfile = 0;
	return 0;
}
Exemplo n.º 9
0
int main(int argc, char* argv[])
{
	char* nom;
	int npartition = 0;
	int i;
	if(argc < 3)
	{
			fprintf(stderr, "Erreur: le nombre d'argument est invalide \n");
			exit(-1);
	}
	for(i=1;i<argc-1;i++)
	{
		if(!strcmp(argv[i], "-p"))
		{
			if(atoi(argv[i+1]) ==0)
			{
				fprintf(stderr, "Erreur: L'argument -p doit être suivi par un nombre\n");
				exit(-1);
			}
			npartition++;
		}
	}
	
	int partition[npartition];
	//printf("npar = %d \n", npartition);
	int k=0;
	for(i=1;i<argc-1;i++)
	{
		if(!strcmp(argv[i], "-p"))
		{
			if(!atoi(argv[i+1]) || atoi(argv[i+1]) < 0)
			{
				fprintf(stderr, "Erreur: L'argument -p doit être suivi par un nombre positif \n");
				exit(-1);
			}
			partition[k] = atoi(argv[i+1]);
			k++;
		}
	}
	
	for(i = 0; i<npartition;i++)
	{
		if(partition[i] < 0)
		{
			fprintf(stderr, "Erreur: La taille des partitions ne peut pas être négative\n");
				exit(-1);
		}
	}
	if(atoi(argv[argc-1]) == 0) // Si le dernier argument n'est pas un nombre alors on a donné le nom du disque
	{						// en parametre
		nom = argv[argc-1];
	}
	else
	{
		nom = "disk.tfs";
	}

	/*
	d = malloc(sizeof(disk_id_s));
	b = malloc(BLOCK_SIZE);
	f = malloc(BLOCK_SIZE);
	printf("%d", size);
	strncpy(f,toLittleEndian(size),4);
	if(argc == 4)
		nom = argv[3];
	else
		nom = "disk.tfs";
	start_disk(nom, &d);
	write_block(d, b, size-1);
	write_block(d, f, 0);
	printf("Creation du disque %s de taille %d blocs (%d octets) \n", nom, size, size*BLOCK_SIZE);*/

	int offset = 4;
	disk_id d;
	d=malloc(sizeof(disk_id_s));
	
	block first_block = malloc(1024);

	char* n_little = toLittleEndian(npartition); 

	//printf("%s", nom);
	start_disk(nom, &d);
	read_block(d, first_block, 0);


	int tmp = 0;
	for(i=0;i<npartition;i++)
	{
		tmp+= partition[i];
	}
	char* sub = blocksub(first_block,0);
	int taille_disk = data_to_int(sub, 4);
	//printf("-> %d\n", taille_disk);
	if(tmp > taille_disk-1)
	{
		fprintf(stderr, "Erreur: La taille totale des partitions dépasse la taille du disque \n");
		exit(-1);
	}
	ajouter_infos(first_block,offset,n_little, sizeof(n_little));
	offset += 4;

	write_block(d, first_block,0);

	for(i = 0; i<npartition;i++)
	{
		printf("Creation de la partition : %d de taille %d bloc(s)\n", i, partition[i]);
		char* p = toLittleEndian(partition[i]);
		ajouter_infos(first_block,offset,p, sizeof(p));
		offset +=4;
	}
	stop_disk(d);
	return 0;
}
Exemplo n.º 10
0
int main(int argc, char* argv[]){
    uint32_t temp;
    int a,i,nb,npart,mf,first,pemplacement=0;
    char* name;
    disk_id* id;
    error e;
    block *block0;
    // block* block_navigation = malloc(sizeof(block));
    //block *block_file_table;
    TTTFS_File_Table_Entry* entry_root = malloc(sizeof(TTTFS_File_Table_Entry));
    
    if (argc < 5){
        fprintf(stderr, "Error : wrong number of arguments.\n");
        return -1;
    }
    
    name = malloc(sizeof(char));
    strcpy(name,"disk.tfs");
    nb = argc;
    
    // Si le nombre d'argument est pair, i.e le nom du disque est indiqué
    if (argc % 2 == 0) {
        strcpy(name,argv[argc-1]);
        nb--;
    }
    //recuperation du numero de la partition
    if (strcmp(argv[1],"-p") != 0){
        fprintf(stderr, "Error : wrong option mode \"-p\" .\n");
        return -1;
    }
    npart=atoi(argv[2]);
    //recuperation du nombre de file demandé
    if (strcmp(argv[3],"-mf") != 0){
        fprintf(stderr, "Error : wrong option mode \"-mf\".\n");
        return -1;
    }
    mf=atoi(argv[4]);
    
    //démarage du disque
    id = malloc(sizeof(disk_id));
    e = start_disk(name,id);
    if (e.val != 0){
        fprintf(stderr, "Error while reading disk.\n");
        return -1;
    }
    
    //recuperation du block 0
    block0 = malloc(sizeof(block));
    read_block(id,block0,0);
    
    //verification de l'existance de la partition
    memcpy(&temp,(block0->octets)+(sizeof(uint32_t)),sizeof(uint32_t));
    if ((a=uitoi(temp))< npart) {
        fprintf(stderr, "Non-existing partition number .\n");
        return -1;
    }
    
    //recuperation du decalage de la partition
    for (i=0; i<npart; i++) {
        memcpy(&temp,(block0->octets)+((2+i)*sizeof(uint32_t)),sizeof(uint32_t));
        pemplacement+=uitoi(temp);
    }
    pemplacement++;
    //printf("decalage pour part: %i \n",pemplacement);
    
    //mise à jour de l'entête de la partition
    memcpy(&temp,(block0->octets) + ((npart+2)*sizeof(uint32_t)),sizeof(uint32_t));
    a=uitoi(temp);
    //printf("a:%i\n",a);
    
    first = 2+(mf/FILE_TABLE_BLOCK_SIZE);
    if (a-first<0) {// file count trop grand
        fprintf(stderr, "Error file_count too big .\n");
        return -1;
    }
    
    else if(a-first<mf){//file count trop grand mais tiens dans la memoire
        fprintf(stderr, "file_count of %i too big for this partition .\n",mf);
        mf=a-first;
        printf("changing file_count to %i\n",mf);
    }
    
    //printf("cas n : \n\t mf/ftb:%i \n\t mf:%i \n\t first:%i \n\t a:%i\n",mf/FILE_TABLE_BLOCK_SIZE+1,mf,first,a);
    read_block(id,block0,pemplacement);
    //first = 2+(mf/FILE_TABLE_BLOCK_SIZE);
    a-=first;
    //printf("a : %i\n",a);
    
    temp = itoui(a);//nombre de blocks libres
    memcpy((block0->octets) + (3*sizeof(uint32_t)),&temp,sizeof(uint32_t));
    temp = itoui(first); // premier block libre
    memcpy((block0->octets) + (4*sizeof(uint32_t)),&temp,sizeof(uint32_t));
    // le nombre de fichiers supportables
    if (a<mf) {// verification du nombre de fichier max
        temp = itoui(a);
        
    }
    else{
        temp = itoui(mf);
        //printf("temp: %i\n",temp);
    }
    memcpy((block0->octets) + (5*sizeof(uint32_t)),&temp,sizeof(uint32_t));
    // le nombre de fichiers actuellement libres
    if (a<mf) {// verification du nombre de fichier max
        temp = itoui(a);
    }
    else{
        temp = itoui(mf);
    }
    memcpy((block0->octets) + (6*sizeof(uint32_t)),&temp,sizeof(uint32_t));
    temp = itoui(0); // le numero du premier fichier libre du volume
    memcpy((block0->octets) + (7*sizeof(uint32_t)),&temp,sizeof(uint32_t));
    // Next free file
    
    int j;
    block *partition_sub_block = malloc(sizeof(block));
    for (j = first; j < a; j++){
        if (j == a-1){
            a = itoui(j);
        }
        else{
            a = itoui(j+1);
        }
        read_block(id,partition_sub_block,j);
        memcpy((partition_sub_block->octets) + (TTTFS_VOLUME_BLOCK_SIZE-sizeof(uint32_t)),&a,sizeof(uint32_t));
        write_block(id,partition_sub_block,j);
    }
    write_block(id,block0,pemplacement);
    
    for (i = 0; i < (mf/FILE_TABLE_BLOCK_SIZE); i++){
        if (i == (mf/FILE_TABLE_BLOCK_SIZE)-1){
            a = itoui(i);
        }
        else{
            a = itoui(i+1);
        }
        // Pour chaque bloc...
        read_block(id,partition_sub_block,pemplacement+1+i);
        for (j = 0; j < FILE_TABLE_BLOCK_SIZE; j++){
            memcpy((partition_sub_block->octets) + ((j+1)*sizeof(uint32_t))-sizeof(uint32_t),&a,sizeof(uint32_t));
        }
        write_block(id,partition_sub_block,pemplacement+1+i);
    }
    
    
    // ---------------- Construction du répertoire racine
    
    // On récuppère le numéro de bloc qui sera occupé par la racine
    read_block(id,block0,pemplacement);
    memcpy(&temp,(block0->octets)+(4*sizeof(uint32_t)),sizeof(uint32_t));
    first = uitoi(temp);
    
    // Création de l'entrée
    //block_file_table = malloc(sizeof(block));
    //read_block(id,block_file_table,pemplacement+1);
    entry_root->size = TFS_DIRECTORIES_SIZE*2;
    entry_root->type = 0;
    entry_root->sub_type = 1;
    entry_root->tfs_direct[0] = first;
    for (i = 1; i < 10; i++){
        entry_root->tfs_direct[i] = 0;
    }
    entry_root->tfs_indirect1 = 0;
    entry_root->tfs_indirect2 = 0;
    
    // Remplissage
    if (fill_block(id,pemplacement).val != 0){
        fprintf(stderr, "Error while filling block.\n");
        return -1;
    }
    if (fill_entry(id,pemplacement,entry_root).val != 0){
        fprintf(stderr, "Error while writing on File Table.\n");
        return -1;
    }
    //write_block(id,block_file_table,pemplacement+1);
    char* buf_d = malloc(sizeof(uint32_t)+28);
    read_block(id,block0,first);
    temp = itoui(0);
    memcpy(buf_d,&temp,sizeof(uint32_t));
    strncpy(&buf_d[sizeof(uint32_t)],".\0",28);
    memcpy((block0->octets),buf_d,sizeof(uint32_t)+28);
    strncpy(&buf_d[sizeof(uint32_t)],"..\0",28);
    memcpy((block0->octets)+(sizeof(uint32_t)+28),buf_d,sizeof(uint32_t)+28);
    write_block(id,block0,first);
    
    sync_disk(id);
    printf("Partition %i formated !\n",npart);
    return 0;
}
Exemplo n.º 11
0
int main(int argc, char *argv[]) {

	if (argc > 2 && strcmp(argv[1], "-s") == 0) {

		int size;
		int valReturn = sscanf(argv[2], "%d", &size);

		if (valReturn != 1 || size < 2) {
			fprintf(stderr, "tfs_create -s need 1 argument :"
					" the size enter :  %d  is not CORRECT \n", size);
			return 1;

		} else {
			error er;
			disk_id *disk = malloc(sizeof(*disk));
			if(disk==NULL){
				er.val=1;
				er.message="ERROR MALLOC DISK in TFS_CREATE";
				testerror(er);
			}

			char* nameFile;

			if (argc > 3) {  //the user give a name to the file
				nameFile = argv[3];
			} else {
				nameFile = "disk.tfs";
			}

			er = createFile(nameFile);
			testerror(er);
			er = start_disk(nameFile, disk);
			testerror(er);

			int sizeInOctal = size * 1024;

			/*
			 int i=0;
			 for(i; i<sizeInOctal;i++){
			 fputc(45,r);
			 }

			 ftruncate(fileno(r) sizeInOctal);
			 deplace le curseur a la position max-1
			 */
			fseek(disk->fichier, (sizeInOctal - 1), SEEK_CUR);
			fputc(0, disk->fichier); //ecris un octet en position max
			//Il faut reouvrir le fichier en mode binaire apres avoir fait un fputc
			fseek(disk->fichier, 0, SEEK_CUR);
			stop_disk(*disk);

			er = start_disk(nameFile, disk);
			testerror(er);
			block *b;
			b = initBlock();
			free(b->valeur[0]);
			b->valeur[0] = valueToNombre32bits(size);
			//b->valeur[1] = valueToNombre32bits(1);// there is 1 partition at first
			//b->valeur[2] = valueToNombre32bits(size-1);
			er=write_block(*disk, *b, 0);
			testerror(er);

			/*
			 read_block(*disk,*b,0);
			 printf("dans fonction afficahge de block apres read\n");
			 printBlock(b);
			 */
			er=stop_disk(*disk);
			testerror(er);
			freeBlock(b);
			freeDisk(disk);
			return 0;
		}
	} else {
		fprintf(stderr,
				"tfs_create need at minimum 1 argument :\n -s "
						"follow by the size ( a POSITIVE NUMBER)of "
						"the new tfs\n [name] is optional,  it is the name of the tfs\n");
		return 1;
	}
	return 0;
}
Exemplo n.º 12
0
/**
 *\fn main(int argc, char *argv[]){
 *\brief on initialise une partition, c'est a dire on remplie son premier block avec toutes les informations nécessaires, puis on construit le file table, avec le chainage des fichiers libres, puis on construit le dossier racine(on ajoute aussi son entrée dans la table des fichiers), et enfin on initialise le chainage des blocks libres.
 **/
int main(int argc, char *argv[]){
  int i; // i pour les for
  if(argc==6){
    if(strcmp(argv[1], "-p")==0){
      if(strcmp(argv[3], "-mf")==0){
	disk_id *id=malloc(sizeof(disk_id));
	error err = start_disk(argv[argc-1], id);
	if(err.errnb==0){
	  int partition =atoi(argv[2]); //la première partition à l'id 0
	  if(0<=partition && partition<=id->nbPart){
	    int file_count=atoi(argv[4]);
	    int taille=id->tabPart[partition].taille;
	    int taille_descripteur=((file_count-1)/16)+1;
	    if(0<file_count && taille>(1+taille_descripteur+file_count)){ //on vérifie qu'il y a bien assez de blocs dans la partition pour y mettre file_count fichiers.
	      //tout les tests sont faits, on commence a initialiser le premier bloc de la partition.
	      block b;
	      int l;
	      for(l=0;l<1024;l++){
		b.buff[l]='\0';
	      }	     	     
	      fill_block(&b, MAGIC_NUMBER, 0);
	      fill_block(&b, SIZEBLOCK, 4);
	      fill_block(&b, taille, 8);
	      fill_block(&b,taille-2-taille_descripteur , 12);
	      fill_block(&b, taille_descripteur+2, 16);
	      fill_block(&b, file_count, 20);
	      fill_block(&b, file_count-1, 24);
	      fill_block(&b,1, 28); //Il y a un fichier 0, le dossier racine.
	      
	      //trouver l'indice du premier block de la partition.
	      uint32_t id_first=1;
	      for(i=0;i<partition; i++){
		id_first+=id->tabPart[i].taille;
	      }
	      write_block(*id, b,int_to_little(id_first)); 
	 	      
	      //initialisation file table
	      block file_table;
	      int k;
	      for(k=0;k<1024;k++){
		file_table.buff[k]='\0';
	      }
	      printf("1 %d \n", taille_descripteur);
	      printf("2 %d \n", id_first);
	      for(i=1; i<=taille_descripteur; i++){
		if(i==1){
		  fill_block(&file_table, 64, 0);
		  fill_block(&file_table, 1, 4);
		  fill_block(&file_table, 0, 8);
		  fill_block(&file_table, taille_descripteur+1, 12);
		}else{
		  fill_block(&file_table, 16*(i-1)+1, 60);
		}
		int j;
		for(j=1; j<=15; j++){
		  if((16*(i-1)+j)<file_count){
		    fill_block(&file_table, (i-1)*16+j+1, j*64+60);
		  }else{
		    if(16*(i-1)+j==file_count){
		       fill_block(&file_table, (i-1)*16+j, j*64+60);
		    }

		  }
		}
		write_physical_block(*id, file_table,i+id_first);
		for(k=0;k<1024;k++){
		  file_table.buff[k]='\0';
		}
	      }
	      //initialisation du dossier racine
	      block racine;
	      for(k=0;k<1024;k++){
		racine.buff[k]='\0';
	      }
	      fill_block(&racine,0,0);
	      racine.buff[4]='.';
	      racine.buff[5]='0';
	      fill_block(&racine,0,32);
	      racine.buff[36]='.';
	      racine.buff[37]='.';
	      racine.buff[38]='0';
	      write_block(*id, racine,int_to_little(id_first+taille_descripteur+1));

	      //initialisation des blocs restants (chainage)
	      block reste;
	      for(i= taille_descripteur+2; i<taille;i++){
		int k;
                for(k=0;k<1024;k++){
                  reste.buff[k]='\0';
                }
		if(i!=(taille-1)){
		  fill_block(&reste,i+1, 1020);
		} else{
		  fill_block(&reste,i, 1020);
		}
		write_block(*id, reste,int_to_little(id_first+i));
		fill_block(&reste,0, 1020);
	      }
	      
 	    }else{
	      printf("tfs format : wrong arguments, too much files : %d \n", file_count);
	    }
	  }else{
	    printf("tfs format : wrong arguments, no partition %d \n", partition);
	  }
	}
	stop_disk(*id);
      }else{
	printf("tfs format : wrong arguments, miss -mf \n");
      }
    }else{
      printf("tfs format : wrong arguments, miss -p \n");
    }
  }else{
    printf("tfs format : wrong arguments number %d \n", (argc-1));

  }
  return 0;
}