~diegoturcios/filesystem/TurciosFuse

« back to all changes in this revision

Viewing changes to src/mkfs.c

  • Committer: Diego Turcios
  • Date: 2012-02-27 14:52:55 UTC
  • Revision ID: diegoturciostc@gmail.com-20120227145255-xejxhusygpi0ldqc
Testiando
;

q

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define _XOPEN_SOURCE 500  //ESTO ES NECESARIO PARA EL PWRITE Y PREAD
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <unistd.h>
 
5
#include <sys/types.h>
 
6
#include <sys/stat.h>
 
7
#include <fcntl.h>
 
8
#include <string.h>
 
9
#include <ctype.h>
 
10
 
 
11
#include "estructura_bloques.h"
 
12
 
 
13
int escribir_bloque(int fd, int lba, void *buffer)
 
14
{
 
15
    return pwrite(fd, buffer, SIZE_BLOCK, lba*SIZE_BLOCK); //funcion para escribir son para linux
 
16
}
 
17
 
 
18
int leer_bloque(int fd, int lba, void *buffer)
 
19
{
 
20
    return pread(fd, buffer, SIZE_BLOCK, lba*SIZE_BLOCK);// funcion para leer son para linux
 
21
}
 
22
 
 
23
#include <getopt.h> /*Este include es para parsiar argumentos de la consola */
 
24
 
 
25
unsigned long parsiarint(char *s)
 
26
{
 
27
    unsigned long valor= strtol(s,&s,0); /*convierte la parte inicial de la cadena nptr en un valor entero largo de acuerdo con la base dada */
 
28
    if(toupper(*s)== 'G')
 
29
        valor*=(1024*1024*1024);
 
30
    if(toupper(*s)=='M')
 
31
        valor*=(1024*1024*1024);
 
32
    if(toupper(*s)=='K')
 
33
        valor*=(1024);
 
34
 
 
35
    return valor;
 
36
}
 
37
 
 
38
void mensaje()
 
39
{
 
40
    printf("uso: mkfs-turcios [--crear <tamano (g|m|k)>] <image-file>\n");
 
41
    exit(1);
 
42
}
 
43
 
 
44
int main(int argc, char **argv)
 
45
{
 
46
    char *path;
 
47
    int i;
 
48
    int c;
 
49
    int fd, numero_bloques;
 
50
    struct stat sb;
 
51
    unsigned long size_disco=0;
 
52
    void *buffer=calloc(SIZE_BLOCK,1);
 
53
 
 
54
    static struct option opciones[]={
 
55
                                      {"crear",required_argument, NULL, 'c'},
 
56
                                      {0,0,0,0}
 
57
                                    };/*El Struct getgetopt.h*/
 
58
 
 
59
    while((c=getopt_long_only(argc, argv, "", opciones, NULL)) !=-1)
 
60
 
 
61
    switch(c)
 
62
    {
 
63
        case 'c': size_disco=parsiarint(optarg);
 
64
            break;
 
65
        default: printf("Erro opcion erronea");
 
66
    }
 
67
 
 
68
    if(optind >=argc)
 
69
    {
 
70
        mensaje();
 
71
    }
 
72
    path=argv[optind];
 
73
 
 
74
    /* Ver que tan grande es la imagen o crear una nueva */
 
75
    if(size_disco==0)
 
76
    {
 
77
        if((fd=open(path,O_WRONLY,0777)<0 ||fstat(fd,&sb)< 0))/* O_WRONLY viene de fcntl.h  y el fstat() de stat.h*/
 
78
        {
 
79
            perror("Error al abrir el archivo\n"),
 
80
            exit(1);
 
81
        }
 
82
        numero_bloques=sb.st_size/SIZE_BLOCK;  /* Size of file, in bytes.*/
 
83
    }
 
84
 
 
85
    else
 
86
    {
 
87
        numero_bloques=size_disco/SIZE_BLOCK;
 
88
        if((fd=open(path,O_WRONLY|O_CREAT|O_TRUNC))<0)
 
89
        {
 
90
              perror("Error al crear el archivo"),exit(1);
 
91
        }
 
92
        for(i=0;i<numero_bloques;i++)
 
93
        {
 
94
              if(write(fd,buffer,SIZE_BLOCK)<0)
 
95
              {
 
96
                    perror("Error al escribir al archivo"),exit(1);
 
97
               }
 
98
        }
 
99
               lseek(fd,0,SEEK_SET);
 
100
     }
 
101
 
 
102
     /* Escribir superbloque */
 
103
 
 
104
    struct super_bloque *sp=buffer;
 
105
    sp->magic_number=MAGIC;
 
106
    sp->size_bloques=SIZE_BLOCK;
 
107
    sp->total_bloques=numero_bloques;
 
108
    sp->bloque_root=3;
 
109
    sp->bloques_libres=numero_bloques-5;
 
110
    sp->primerbloque_mapabits=3;
 
111
    sp->sizebloques_mapabits=2;
 
112
 
 
113
    escribir_bloque(fd,0,buffer);
 
114
 
 
115
 
 
116
    /* Aqui tiene que ir el mapa de bits */
 
117
 
 
118
 
 
119
    const int cantidad_estructdirectorios=SIZE_BLOCK/sizeof(struct directorio);
 
120
    struct directorio *folder=buffer;
 
121
 
 
122
    for(i=0;i<cantidad_estructdirectorios;i++)
 
123
        folder[i].valid=0;
 
124
 
 
125
    escribir_bloque(fd,1+2,buffer);
 
126
 
 
127
    close(fd);
 
128
 
 
129
    return 0;
 
130
 
 
131
 
 
132
 
 
133
}
 
134
 
 
135