aboutsummaryrefslogtreecommitdiff
path: root/mkfs.md/mkfs.c
blob: 37c6e6eddcf0251e3681e42262cbab9176e975f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "../defs.h"
#include "../inode.h"

#include <fcntl.h>

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>

#include <unistd.h>
#include <sys/stat.h>

struct PACKED superblock {
    uint16_t magic;
    uint32_t free_size;
    uint32_t max_size;
};

int main(int argc, char *argv[])
{
    char *fs = NULL;
    int fd = 0;
    struct superblock *sb= NULL;
    inode *rootdir = NULL;
#ifdef _HAS_TRACKED_INODE
    direntry *dentry = NULL;
#endif

    if (argc != 2){
        printf("Uso: %s [file]\n"
               "Necesita proveer un nombre para el filesystem",
               argv[0]);
        return -2;
    }

    fd = open(argv[1], O_WRONLY | O_CREAT, 0777);

    fs = calloc (BLOCKS*BLOCK_SIZE, sizeof(char));
    if (!fs){
        fprintf(stderr, "No hay suficiente memoria para alocar el filesystem\n");
        return (1);
    }
    sb = (struct superblock *) fs;

    sb->magic = 0xF514; /* No hay ganas de hacer aritmetica con lo que esta en el defs.h */
    sb->max_size = BLOCK_SIZE * BLOCKPOOL_BLOCKS;
    sb->free_size = BLOCK_SIZE * BLOCKPOOL_BLOCKS;
	SET_BIT_1((char *)sb + FBB_OFFSET, 0);
    /* El primer elemento del inodepool es el rootdir */
    SET_BIT_1((char*) sb + FIB_OFFSET, 1);
	
	
    rootdir = (inode *) sb + INODEPOOL_OFFSET + sizeof(inode);
    
    rootdir->type = I_DIR;
    rootdir->mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH | S_IFDIR;
    rootdir->uid = getuid();
    rootdir->gid = getgid();
	rootdir->indirect=2;
    rootdir->atime = (uint32_t) time(NULL);
    rootdir->ctime = (uint32_t) time(NULL);
    rootdir->mtime = (uint32_t) time(NULL);

    rootdir->nlinks = 1;

#ifndef _HAS_TRACKED_INODE
    rootdir->size = 0;
#else
    rootdir->size = sizeof(direntry);
    /* Primer bloque */
    /*SET_BIT_1((char *)sb + FBB_OFFSET, 0); */
    dentry = (direntry *) sb + BLOCKPOOL_OFFSET;
    dentry->inode = 0;
    dentry->name[0] = '/';
    dentry->name[1] = '\0';
    dentry->symlink = 0;
#endif

    if ((write(fd, (void *) fs, BLOCKS*BLOCK_SIZE)) == -1){
        perror("write");
        close(fd);
        return -1;
    }
    close(fd);
    free(fs);

    return (0);
}