所以我的头衔说明了一切。我必须为我的操作系统类创建文件系统模拟。我仍然不太喜欢C,这就是我寻求帮助的原因。而让我失望的一件事就是我必须创建一个存储区,该存储区是一个2 ^ 16块的数组,每块256字节;块只是一个简单的字节序列,直到它与结构重叠(当然使用联合):目录或文件元数据,索引节点或数据节点;还有一种节点:
这就是我所做的
#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如果我设置正确,但我认为这是我应该如何设置节点内存来创建一个2 ^ 16块,每块256字节的数组。任何帮助将不胜感激。
答案 0 :(得分:1)
由于空间维度是固定的,你应该能够像这样声明内存:
my_field