File System Simulation

Hi all,

I have been having issues with creating a file system simulation that consists of a class Sdrive and a class Filesys. The Sdrive basically simulates the disk drive, and it consists of numberofblocks many blocks where each block has blocksize many bytes. Filesys basically just does dynamic file management.

Now I am having trouble with my Filesys class, as I am getting a segmentation fault and I don't know why. It occurring at line 133 in ffsynch when I try to update the ROOT with the new updated information, and it's occurring when I call fssynch from the default constructor (according to my debugger).

Hoping you guys can help me out, I am stumped!

For the sake of not posting pages of code, I'm just putting the interface of Sdrive(I know this works correctly as I've tested it) and the two problematic functions of Filesys that is causing the error. Also posting my testing main function below the classes.

If you need to see any of the other functions, because you may thing an error could be occurring there or need further information I would gladly provide it. I really just want to figure out why this isn't working!

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

class Sdisk
{
public:
    Sdisk(string disk_name); //default constructor called when file already exists
    Sdisk(string disk_name, int number_of_blocks, int block_size); //called when creating a new while from scratch
    int getblock(int blocknumber, string& buffer); //retrieves block from disk and stores this information on buffer
    int putblock(int blocknumber, string buffer); //writes at specified blocknumber from buffer
    int getblocksize();
    int getblocknumber();
private:
    string diskname;        // file name of software-disk
    int numberofblocks;     // number of blocks on disk
    int blocksize;          // block size in bytes
};  

#ifndef FILESYS_H
#define FILESYS_H

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "sdrive.h"

using namespace std;

class Filesys: public Sdisk
{
public:
    Filesys(string filename);
    int fsclose();
    int fssynch();
    int newfile(string file);
    int rmfile(string file);
    int getfirstblock(string file);
    int addblock(string file, string buffer);
    int delblock(string file, int blocknumber);
    int readblock(string file, int blocknumber, string& buffer);
    int writeblock(string file, int blocknumber, string buffer);
    int nextblock(string file, int blocknumber);
    
private:
    int rootsize; //max number of entries in ROOT
    int fatsize; //number of blocks in FAT
    vector<string> filenames; //filenames in ROOT
    vector<int> firstblocks; //first blocks in ROOT
    vector<int> fat;
    
};

Filesys::Filesys(string filename) : Sdisk(filename)
{
    cout << getblocknumber() << endl;
    rootsize = (getblocksize()/12);
    fatsize = (8*getblocknumber()/getblocksize()) + 1;
    cout << "Rootsize " << rootsize << endl;
    cout << "Fatsize " << fatsize << endl; 
    
    string buffer;
    getblock(1, buffer);
    if(buffer[0] == '#'){
        //file does not exist, so make it
        //build the initial root
        cout << "Building initial root" << endl;
        for (int i = 0; i < rootsize; i++){
            filenames.push_back("XXXXX");
            firstblocks.push_back(0);
        }
        
        //build the FAT
        cout << "Building FAT" << endl;
        fat.push_back(2 + fatsize);
        for(int i = 1; i <= 1 + fatsize; i++){
            fat.push_back(1);
        }
        for(int i = 2 + fatsize; i < getblocknumber(); i++){
            fat[i] = i + 1;
            //cout << fat[i] << endl;
        }
        fat[fat.size() - 1] = 0;
    }
    else{
        //since file exists, read in root and fat
        cout << "File exists, reading in ROOT and FAT" << endl;
        string buffer;
        istringstream readroot(buffer);
        readroot.str(buffer);
        
        //read the root directory
        for(int i = 0; i < rootsize; i++){
            readroot >> filenames[i] >> firstblocks[i];
        }
        
        
        //read the FAT
        string fatbuffer;
        for(int i = 0; i < fatsize; i++){
            string s;
            getblock(i + 2, s);
            fatbuffer += s;
        }
        
        istringstream fatstream;
        fatstream.str(fatbuffer); //load buffer into fatstream
        
        for(int i = 0; i < getblocknumber(); i++){
            int temp;
            fatstream >> temp;
            fat.push_back(temp);
        }
        
        //cout << fatbuffer << endl;
    }
    
    fssynch();
}

int Filesys::fssynch()
{
    //write updated root to disk
    ostringstream rootstream;
    
    for(int i = 0; i < rootsize; ++i){
        rootstream << filenames[i] << " " << firstblocks[i] << " ";  
//debugger says the seg fault occurs here, why's that??s
    }
    string root = rootstream.str();
    for(long j = root.size(); j < getblocksize(); j++){
        root += "#";
    }
    putblock(1, root);
    
    //write the FAT onto the disk
    
    ostringstream fatstream;
    
    for(int i = 0; i < getblocknumber(); i++){
        fatstream << fat[i] << " ";
    }
    string fatstring = fatstream.str();
    for(int i = 0; i < getblocksize(); i++){
        fatstring += "#";
    }
    
    //writing the substring of the FAT
    for(int i = 0, blocks = 2; i < fatsize; i++){
        string temp = fatstring.substr(i * getblocksize(), getblocknumber());
        putblock(i + blocks, temp);
    }
    return 1; //successful!
}


//testing main function
#include <iostream>
#include <string>
#include "sdrive.h"
#include "filesys.h"

using namespace std;


int main()
{
    Sdisk disk1("disk1",256,128);
    Filesys fsys("disk1"); //debugger also says the segmentation fault is here
    fsys.newfile("file1");
    fsys.newfile("file2");
                 
    string bfile;
                 
    for (int i=1; i<=1024; i++)
    {
        bfile+="1";
    }
    
    vector<string> blocks=block(bfile,128);
                 
    int blocknumber=0;
                 
    for (int i=0; i<=blocks.size(); i++)
    {
        blocknumber=fsys.addblock("file1",blocks[i]);
    }
                 
    fsys.delblock("file1",fsys.getfirstblock("file1"));
                 
    for (int i=1; i<=2048; i++)
    {
        bfile+="2";
    }
                 
    for (int i=0; i<=blocks.size(); i++)
    {
        blocknumber=fsys.addblock("file2",blocks[i]);
    }
                 
    fsys.delblock("file2",blocknumber);
}
Last edited on
Topic archived. No new replies allowed.