-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectoryEntry.cc
152 lines (132 loc) · 3.22 KB
/
DirectoryEntry.cc
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
/** Rohit Sindhu [sindh010]
* Aravind Alagiri Ramkumar [alagi005]
* Aparna Mahadevan [mahad028]
*/
#include "DirectoryEntry.h"
#include <stdlib.h>
#include <string.h>
/*
* Constructs an empty DirectoryEntry.
*/
DirectoryEntry::DirectoryEntry()
{
memset(d_name, '\0', 1024);
d_ino = 0;
}
/**
* Constructs a DirectoryEntry for the given inode and name.
* Note that the name is stored internally as a byte[],
* not as a string.
* @param ino the inode number for this DirectoryEntry
* @param name the file name for this DirectoryEntry
*/
DirectoryEntry::DirectoryEntry(short ino, char * name)
{
setIno( ino );
setName( name );
}
/**
* Sets the inode number for this DirectoryEntry
* @param newIno the new inode number
*/
void DirectoryEntry::setIno( short newIno )
{
d_ino = newIno ;
}
/**
* Gets the inode number for this DirectoryEntry
* @return the inode number
*/
short DirectoryEntry::getIno()
{
return d_ino ;
}
/**
* Sets the name for this DirectoryEntry
* @param newName the new name
*/
void DirectoryEntry::setName(char * newName )
{
memset(d_name, '\0', 1024);
strcpy(d_name, newName);
//uu cout << "SetName in dir:" << d_name;
}
/**
* Gets the name for this DirectoryEntry
* @return the name
*/
char * DirectoryEntry::getName()
{
return d_name;
}
/**
* Writes a DirectoryEntry to the specified byte array at the specified
* offset.
* @param buffer the byte array to which the directory entry should be written
* @param offset the offset from the beginning of the buffer to which the
* directory entry should be written
*/
void DirectoryEntry::write(char * buffer , int offset )
{
buffer[offset] = (char)( d_ino >> 8 );
buffer[offset+1] = (char) d_ino;
int nLen = strlen(d_name);
for(int i=0;i<nLen;i++)
{
buffer[offset+2+i] = d_name[i] ;
}
buffer[offset+2+nLen] = '\0';
//har * buf = &buffer[offset+2];
//out << "in DirectoryEntry " << buf << endl;
}
/**
* Reads a DirectoryEntry from the spcified byte array at the specified
* offset.
* @param buffer the byte array from which the directory entry should be read
* @param offset the offset from the beginning of the buffer from which the
* directory entry should be read
*/
void DirectoryEntry::read( char * buffer , int offset )
{
memset(d_name, '\0', 1024);
int hi = buffer[offset] & 0xff;
int lo = buffer[offset+1] & 0xff;
d_ino = (short)( hi << 8 | lo );
int nLen = MAX_FILENAME_LENGTH;//strlen(d_name);
for(int i=0;i<nLen;i++)
{
d_name[i] = buffer[offset+2+i] ;
}
d_name[nLen] = '\0';
}
/**
* Converts a DirectoryEntry to a printable string.
* @return the printable string
*/
char * DirectoryEntry::toString()
{
//Not thread safe - KS
static char buffer[1024];
sprintf(buffer, "DirectoryEntry[%d, %s]", getIno(), getName());
return buffer;
}
void DirectoryEntry::copy(DirectoryEntry & de)
{
de.setIno(getIno());
de.setName(getName());
}
/**
* A test driver for this class.
* @exception java.lang.Exception any exception which may occur.
*/
/* static void main( String[] args ) throws Exception
{
DirectoryEntry root = new DirectoryEntry( (short)1 , "/" ) ;
System.out.println( root.toString() ) ;
}*/
/*
int main(int argc, char ** argv )
{
DirectoryEntry root((short)1 , "/") ;
cout << root.toString() << endl;
}*/