2016-11-13 71 views
-3

所以我的標題說明了一切。我必須爲我的操作系統類創建一個文件系統模擬。我對C的態度還不是很好,這就是我尋求幫助的原因。其中一個讓我感到不滿的是我必須創建一個存儲,它是一個由2^16個數據塊組成的數組,每個數據塊有256個字節;一個塊只是一個普通的字節序列,直到它被一個結構覆蓋(當然使用聯合):目錄或文件元數據,索引節點或數據節點;還有一種類型的節點:如何創建一個由256字節組成的2^16塊數組

這是我做

#include "project1task3.h" 

int main() 
{ 

    createFileSystem(); 
} 
/* 
* create the file system 
* (i.e., allocate and initializing all structures and auxiliary data; 
* create the superblock) 
* 
* 
* */ 
void createFileSystem() 
{ 
    // setting up file system "superblock" 
    FS_NODE fileSystem; 
    NODE typeNode; 
    NODE* p; // pointer that will point to file or director 
    //typeNode.type = DIR; 
    int i; 
    int j; 
    size_t nodeSize; 
    // allocate space for the fixed sie and the variable part (union) 
    nodeSize = sizeof(FS_NODE) + sizeof(NODE); 
    if ((memory = malloc(nodeSize)) == NULL) 
    { 
     for(i = 0; i < MEM;i++) 
     { 
      for(j = 0; j < BLOCK_SIZE;j++) 
      { 
       //not sure if this is how it should be 
       memory->content.index[j] = 0; 

      } 
     } 
    } 




    strcpy(fileSystem.name,"\\"); 
    fileSystem.creat_t = 0; // set creation time to 0 
    fileSystem.access = 400; //the access should not allow for deletion or name change, 400 will allow only for read in 
    fileSystem.owner = 000;// no permissions yet basically none 
    fileSystem.size = 0; 
    fileSystem.block_ref = BLOCK_SIZE; 
    fileSystem.access_t =0; 
    fileSystem.mod_t = 0; 
    /* 
    *A file-descriptor node holds meta-data information about a file or a directory 
    * such as size, access rights, creation time, access time, and modification time, along with a pointer to actual content of the file or director 
    * */ 
    typeNode.content.fd.creat_t =0; 
    typeNode.content.fd.access = 400; 
    typeNode.content.fd.size=0; 
    typeNode.content.fd.mod_t = 0; 
    typeNode.content.fd.access_t = 0; 
    //if((memory= malloc(sizeof *memory + MEM)) != NULL) 

    /* 
    * 
    * In case of a node with the type opf directory, the size indicates the number of files in the directory, 
    * and the block reference points to the index block that holds indices to all files in the directory. 
    * Assume that a directory may hold directly up to 127 files or directories; each index of the index block points to a file or a directory descriptor. 
    * Of course, each of the subdirectries may hold another 127 files or directories, and so on. 
    * 
    * 
    * */ 

    /* 
     * * 
     * 
     * In case of a file, the size in the file descriptor indicates the actual size of the file. 
     * The block reference either ponts to a single data block 
     * if the size of the file is less than 254, or to an index block with an array of references to data blocks for file larger than 254. 
     * Assume that the maximum size of a file is 127 * 254 (i.e., maximum allowed for a one-level indexing). 
     * 
     * 
     * 
     * */ 



} 
/* 
* 
* create a file (i.e., allocate one block for meta-level information; set the size to 0, the times to the current time, and the access rights to some default) 
create of a directory (just like a file creation, but with a different type) 
delete a file (return blocks and clean up your supporting structures; e.g., reset the bits in the bit vector) 
delete a directory (delete the files from the directory, and then delete the directory; clean up) 
obtain file information (type - file or directory, size, times, access right, owner) 
* 
* 
* */ 
void createAFile() 
{ 
} 

void createDirectory() 
{ 
} 


void deleteFile() 
{ 
} 

void obtainFileInfo() 
{ 

} 

這是頭

#include <time.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <sys/stat.h> 
#include <string.h> 
#define BLOCK_SIZE 256 
#define MAX_NAME_LENGTH 128 
#define DATA_SIZE 254 
#define INDEX_SIZE 127 
#define MEM 65536 //2^16 

typedef char data_t; 
typedef unsigned short index_t; 

typedef enum 
{ 
    DIR, 
    FILEE, 
    INDEX, 
    DATA 
} NODE_TYPE; 

typedef struct fs_node 
{ 
    char name[MAX_NAME_LENGTH]; 
    time_t creat_t; // creation time 
    time_t access_t; // last access 
    time_t mod_t; // last modification 
    mode_t access; // access rights for the file 
    unsigned short owner; // owner ID 
    unsigned short size; 
    index_t block_ref; // reference to the data or index block 
} FS_NODE; 

typedef struct node 
{ 
    NODE_TYPE type; 
    union 
    { 
     FS_NODE fd; 
     data_t data[DATA_SIZE]; 
     index_t index[INDEX_SIZE]; 
    } content; 
} NODE; 

// storage blocks 

NODE *memory; // allocate 2^16 blocks (in init 
void createFileSystem(); 
void createAFile(); 
void createDirectory(); 
void deleteFile(); 
void obtainFileInfo(); 

IDK的,如果我把它的權利,但我認爲這是我如何應設置節點內存以創建每個塊包含256個字節的2^16塊數組。任何幫助,將不勝感激。

+0

不知道這個問題是在這裏。你有什麼問題? –

回答

1

由於空間的尺寸是固定的,你應該能夠只需要聲明的記憶是這樣的:

char space[65536][256]; 
相關問題