-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKernel.cc
1631 lines (1425 loc) · 41.7 KB
/
Kernel.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Rohit Sindhu [sindh010]
* Aravind Alagiri Ramkumar [alagi005]
* Aparna Mahadevan [mahad028]
*/
#include "Kernel.h"
#include <stdlib.h>
#include <string.h>
#include <cstdlib>
/**
* Prints a system error message. The actual text written
* to stderr is the
* given string, followed by a colon, a space, the message
* text, and a newline. It is customary to give the name of
* the program as the argument to perror.
* @param s the program name
*/
//char * Kernel::sys_errlist[32];
char Kernel::PROGRAM_NAME[512];
ProcessContext Kernel::process;
FileSystem * Kernel::openFileSystems;
int Kernel::processCount;
FileDescriptor ** Kernel::openFiles;
int Kernel::MAX_OPEN_FILES;
char * g_sys_errlist[32] = {
NULL,
"Not owner",
"No such file or directory",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
"Bad file number",
NULL,
NULL,
NULL,
"Permission denied",
NULL,
NULL,
NULL,
"File exists",
"Cross-device link",
NULL,
"Not a directory",
"Is a directory",
"Invalid argument",
"File table overflow",
"Too many open files",
NULL,
NULL,
"File too large",
"No space left on device",
NULL,
"Read-only file system",
"Too many links"
};
void Kernel::perror(const char * s )
{
bool support_error = false;
char error[512];
memset(error, '\0', 512);
if ((process.errno > 0 ) && ( process.errno < sys_nerr ))
{
support_error = true;
strcpy(error, g_sys_errlist[process.errno]);
}
if ( support_error == false )
{
cout << s << ": unknown errno " << process.errno << endl;
}
else
{
cout << s << ": " << error << endl;
}
}
/**
* Set the value of errno for the current process.
* <p>
* Simulates the unix variable:
* <pre>
* extern int errno ;
* </pre>
* @see getErrno
*/
void Kernel::setErrno( int newErrno )
{
process.errno = newErrno ;
}
/**
* Get the value of errno for the current process.
* <p>
* Simulates the unix variable:
* <pre>
* extern int errno ;
* </pre>
* @see setErrno
*/
int Kernel::getErrno()
{
return process.errno ;
}
/**
* Closes the specified file descriptor.
* <p>
* Simulates the unix system call:
* <pre>
* int close(int fd);
* </pre>
* @param fd the file descriptor of the file to close
* @return Zero if the file is closed; -1 if the file descriptor
* is invalid.
*/
int Kernel::close(int fd)
{
// check fd
int status = check_fd( fd ) ;
if( status < 0 )
{
return status;
}
// remove the file descriptor from the kernel's list of open files
for( int i = 0 ; i < MAX_OPEN_FILES ; i ++ )
{
if(openFiles[i] == process.openFiles[fd])
{
delete openFiles[i];
openFiles[i] = NULL;
break ;
}
}
// ??? is it an error if we didn't find the open file?
// remove the file descriptor from the list.
process.openFiles[fd] = NULL;
return 0 ;
}
/**
* Creates a file or directory with the specified mode.
* <p>
* Creates a new file or prepares to rewrite an existing file.
* If the file does not exist, it is given the mode specified.
* If the file does exist, it is truncated to length zero.
* The file is opened for writing and its file descriptor is
* returned.
* <p>
* Simulates the unix system call:
* <pre>
* int creat(const char *pathname, mode_t mode);
* </pre>
* @param pathname the name of the file or directory to create
* @param mode the file or directory protection mode for the new file
* @return the file descriptor (a non-negative integer); -1 if
* a needed directory is not searchable, if the file does not
* exist and the directory in which it is to be created is not
* writable, if the file does exist and is unwritable, if the
* file is a directory, or if there are already too many open
* files.
* @exception java.lang.Exception if any underlying action causes
* an exception to be thrown
*/
int Kernel::creat( char * pathname , short mode )
{
// get the full path
char * fullPath = getFullPath(pathname);
char dirname[1024];
memset(dirname, '\0', 1024);
strcpy(dirname, "/" );
FileSystem * fileSystem = openFileSystems;
IndexNode currIndexNode;
IndexNode prevIndexNode;
IndexNode emptyIndexNode;
getRootIndexNode()->copy(currIndexNode);
//out << "CurIndexNode " << currIndexNode.toString() << endl;
// cout << currIndexNode.toString();
short indexNodeNumber = FileSystem::ROOT_INDEX_NODE_NUMBER ;
char * token = NULL;
token = strtok(fullPath, "/");
char name[512];// = "." ; // start at root node
memset(name, '\0', 512);
strcpy(name, "."); //may be not needed.
while(1)
{
if(token != NULL)
{
memset(name, '\0', 512);
strcpy(name, token);
// cout << name << endl;
// check to see if the current node is a directory
// cout <<currIndexNode.toString() << endl;
if((currIndexNode.getMode()&S_IFMT) != S_IFDIR)
{
// return (ENOTDIR) if a needed directory is not a directory
process.errno = ENOTDIR ;
return -1 ;
}
// get the next inode corresponding to the token
currIndexNode.copy(prevIndexNode);
//Init CurIndexNode
emptyIndexNode.copy(currIndexNode);
//prevIndexNode = currIndexNode ;
// currIndexNode = new IndexNode() ;
indexNodeNumber = findNextIndexNode(openFileSystems, prevIndexNode, name, currIndexNode);
}
else
{
break;
}
token = strtok(NULL, "/");
if(token != NULL)
{
strcat(dirname, name);
strcat(dirname, "/");
}
}
// ??? we need to set some fields in the file descriptor
int flags = O_WRONLY ; // ???
FileDescriptor * fileDescriptor = NULL;
if ( indexNodeNumber < 0 )
{
// file does not exist. We check to see if we can create it.
// check to see if the prevIndexNode (a directory) is writeable
// ??? tbd
// return (EACCES) if the file does not exist and the directory
// in which it is to be created is not writable
currIndexNode.setMode(mode) ;
currIndexNode.setNlink((short)1);
// allocate the next available inode from the file system
short newInode = fileSystem->allocateIndexNode() ;
if( newInode == -1 )
{
return -1 ;
}
//Do writeIndex first -KS
fileSystem->writeIndexNode(&currIndexNode , newInode);
// cout << "new file des " << currIndexNode.toString() << endl;
fileDescriptor = new FileDescriptor(fileSystem, currIndexNode , flags);
// assign inode for the new file
fileDescriptor->setIndexNodeNumber( newInode ) ;
// System.out.println( "newInode = " + newInode ) ;
// open the directory
// ??? it would be nice if we had an "open" that took an inode
// instead of a name for the dir
// System.out.println( "dirname = " + dirname.toString() ) ;
// cout << "dir name and name " << dirname << " " << name << endl;
int dir = open(dirname , O_RDWR);
if( dir < 0 )
{
perror(PROGRAM_NAME);
cout << PROGRAM_NAME << ": unable to open directory for writing" << endl;
return -1;
}
// scan past the directory entries less than the current entry
// and insert the new element immediately following
// cout << "New dir name " << name << endl;
int status = 0;
DirectoryEntry newDirectoryEntry(newInode , name);
DirectoryEntry currentDirectoryEntry;
while(true)
{
// read an entry from the directory
status = readdir(dir, currentDirectoryEntry);
if(status < 0)
{
cout << PROGRAM_NAME << ": error reading directory in creat";
exit( EXIT_FAILURE ) ;
}
else if( status == 0 )
{
// if no entry read, write the new item at the current
// location and break
writedir( dir , newDirectoryEntry) ;
break ;
}
else
{
// if current item > new item, write the new item in
// place of the old one and break
if(strcmp(currentDirectoryEntry.getName(),newDirectoryEntry.getName()) > 0)
{
int seek_status = lseek(dir , -DirectoryEntry::DIRECTORY_ENTRY_SIZE, 1);
// cout << "lseek status" << seek_status << endl;
if(seek_status < 0)
{
cout << PROGRAM_NAME << ": error during seek in creat";
exit( EXIT_FAILURE );
}
writedir(dir, newDirectoryEntry);
break ;
}
}
}
// copy the rest of the directory entries out to the file
DirectoryEntry nextDirectoryEntry;
while(status>0)
{
// read next item
status = readdir(dir , nextDirectoryEntry);
if(status>0)
{
// in its place
int seek_status = lseek(dir, -DirectoryEntry::DIRECTORY_ENTRY_SIZE, 1);
if( seek_status < 0 )
{
cout << PROGRAM_NAME << ": error during seek in creat" ;
exit( EXIT_FAILURE ) ;
}
}
// write current item
// cout << currentDirectoryEntry.toString() << endl;
writedir( dir , currentDirectoryEntry );
// cout << "Old : " << currentDirectoryEntry.toString() << endl;
// current item = next item
nextDirectoryEntry.copy(currentDirectoryEntry);
// cout << "Next : " << nextDirectoryEntry.toString() << endl;
}
// close the directory
close(dir) ;
}
else
{
// file does exist ( indexNodeNumber >= 0 )
// if it's a directory, we can't truncate it
if((currIndexNode.getMode() & S_IFMT ) == S_IFDIR)
{
// return (EISDIR) if the file is a directory
process.errno = EISDIR ;
return -1 ;
}
// check to see if the file is writeable by the user
// ??? tbd
// return (EACCES) if the file does exist and is unwritable
// free any blocks currently allocated to the file
int blockSize = fileSystem->getBlockSize();
int blocks = (currIndexNode.getSize() + blockSize-1) / blockSize;
for( int i = 0 ; i < blocks ; i ++ )
{
int address = currIndexNode.getBlockAddress(i, (void *)fileSystem) ;
if( address != FileSystem::NOT_A_BLOCK )
{
fileSystem->freeBlock(address);
currIndexNode.setBlockAddress(i , FileSystem::NOT_A_BLOCK, (void*) fileSystem);
}
}
// update the inode to size 0
currIndexNode.setSize(0);
// write the inode to the file system.
fileSystem->writeIndexNode(&currIndexNode, indexNodeNumber);
// set up the file descriptor
fileDescriptor = new FileDescriptor(fileSystem , currIndexNode, flags);
// assign inode for the new file
fileDescriptor->setIndexNodeNumber(indexNodeNumber);
}
return open(fileDescriptor) ;
}
/**
* Terminate the current "process". Any open files will be closed.
* <p>
* Simulates the unix system call:
* <pre>
* exit(int status);
* </pre>
* <p>
* Note: If this is the last process to terminate, this method
* calls finalize().
* @param status the exit status
* @exception java.lang.Exception if any underlying
* Exception is thrown
*/
void Kernel::exit( int status )
{
// cout << "exit" <<endl;
// close anything that might be open for the current process
for( int i = 0 ; i < process.getOpenFilesCount() ; i ++ )
{
if( process.openFiles[i] != NULL )
{
close( i ) ;
}
}
// terminate the process
processCount -- ;
// if this is the last process to end, call finalize
if( processCount <= 0 )
{
finalize( status ) ;
}
}
/**
* Set the current file pointer for a file.
* The current file position is updated based on the values of
* offset and whence. If whence is 0, the new position is
* offset bytes from the beginning of the file. If whence is
* 1, the new position is the current position plus the value
* of offset. If whence is 2, the new position is the size
* of the file plus the offset value. Note that offset may be
* negative if whence is 1 or 2, as long as the resulting
* position is not less than zero. It is valid to position
* past the end of the file, but it is not valid to read
* past the end of the file.
* <p>
* Simulates the unix system call:
* <pre>
* lseek( int filedes , int offset , int whence );
* </pre>
* @param fd the file descriptor
* @param offset the offset
* @param whence 0 = from beginning of file; 1 = from
* current position ; 2 = from end of file
*/
int Kernel::lseek( int fd , int offset , int whence )
{
// check fd
int status = check_fd( fd ) ;
if( status < 0 )
return status ;
FileDescriptor * file = process.openFiles[fd] ;
int newOffset ;
if( whence == 0 )
newOffset = offset ;
else if( whence == 1 )
newOffset = file->getOffset() + offset ;
else if ( whence == 2 )
newOffset = file->getSize() + offset ;
else
{
// bad whence value
process.errno = EINVAL ;
return -1 ;
}
if( newOffset < 0 )
{
// bad offset value
process.errno = EINVAL ;
return -1 ;
}
file->setOffset( newOffset ) ;
return newOffset ;
}
/**
* Opens a file or directory for reading, writing, or
* both reading and writing.
* <p>
* The file is positioned at the beginning (byte 0).
* The returned file descriptor must be used for subsequent
* calls for other input and output functions on the file.
* <p>
* Simulates the unix system call:
* <pre>
* int open(const char *pathname, int flags );
* </pre>
* @param pathname the name of the file or directory to create
* @param flags the flags to use when opening the file: O_RDONLY,
* O_WRONLY, or O_RDWR.
* @return the file descriptor (a non-negative integer); -1 if
* the file does not exist, if one of the necessary directories
* does not exist or is unreadable, if the file is not readable
* (resp. writable), or if too many files are open.
* @exception java.lang.Exception if any underlying action causes
* an exception to be thrown
*/
int Kernel::open( char * pathname , int flags )
{
// get the full path name
char * fullPath = getFullPath( pathname ) ;
IndexNode indexNode;
short indexNodeNumber = findIndexNode(fullPath , indexNode);
if( indexNodeNumber < 0 )
{
return -1 ;
}
// ??? return (Exxx) if the file is not readable
// and was opened O_RDONLY or O_RDWR
// ??? return (Exxx) if the file is not writable
// and was opened O_WRONLY or O_RDWR
// set up the file descriptor
FileDescriptor * fileDescriptor = new FileDescriptor(
openFileSystems, indexNode , flags ) ;
fileDescriptor->setIndexNodeNumber( indexNodeNumber ) ;
return open( fileDescriptor ) ;
}
/**
* Open a file using a FileDescriptor. The open and create
* methods build a file descriptor and then invoke this method
* to complete the open process.
* <p>
* This is a convenience method for the simulator kernel.
* @param fileDescriptor the file descriptor
* @return the file descriptor index in the process open file
* list assigned to this open file
*/
int Kernel::open(FileDescriptor * fileDescriptor )
{
// scan the kernel open file list for a slot
// and add our new file descriptor
int kfd = -1;
for(int i=0;i<MAX_OPEN_FILES;i++)
{
if(openFiles[i] == NULL)
{
kfd = i;
openFiles[kfd] = fileDescriptor;
break;
}
}
if(kfd == -1)
{
// return (ENFILE) if there are already too many open files
process.errno = ENFILE;
delete fileDescriptor;
return -1;
}
// scan the list of open files for a slot
// and add our new file descriptor
int fd = -1;
for(int i=0;i<process.getMaxOpenFiles();i++)
{
if(process.openFiles[i] == NULL)
{
fd = i;
process.openFiles[fd] = fileDescriptor;
break;
}
}
if(fd == -1)
{
// remove the file from the kernel list
openFiles[kfd] = NULL;
// return (EMFILE) if there isn't room left
process.errno = EMFILE;
delete fileDescriptor;
return -1 ;
}
// return the index of the file descriptor for now open file
return fd ;
}
/**
* Read bytes from a file.
* <p>
* Simulates the unix system call:
* <pre>
* int read(int fd, void *buf, size_t count);
* </pre>
* @param fd the file descriptor of a file open for reading
* @param buf an array of bytes into which bytes are read
* @param count the number of bytes to read from the file
* @return the number of bytes actually read; or -1 if an error occurs.
* @exception java.lang.Exception if any underlying action causes
* Exception to be thrown
*/
int Kernel::read( int fd , char * buf , int count )
{
// check fd
int status = check_fd_for_read( fd ) ;
if( status < 0 )
return status ;
FileDescriptor * file = process.openFiles[fd] ;
int offset = file->getOffset() ;
int size = file->getSize() ;
int blockSize = file->getBlockSize() ;
char * bytes = file->getBytes() ;
int readCount = 0 ;
for( int i = 0 ; i < count ; i ++ )
{
// if we read to the end of the file, stop reading
if( offset >= size )
break ;
// if this is the first time through the loop,
// or if we're at the beginning of a block, load the data block
if( ( i == 0 ) || ( ( offset % blockSize ) == 0 ) )
{
status = file->readBlock( (short)( offset / blockSize ) ) ;
if( status < 0 ) {
cout << "Error in read block" << status << endl;
return status ;
}
}
// copy a byte from the file buffer to the read buffer
buf[i] = bytes[ offset % blockSize ] ;
offset ++ ;
readCount ++ ;
}
// update the offset
file->setOffset( offset ) ;
// return the count of bytes read
return readCount ;
}
/**
* Reads a directory entry from a file descriptor for an open directory.
* <p>
* Simulates the unix system call:
* <pre>
* int readdir(unsigned int fd, struct dirent *dirp ) ;
* </pre>
* Note that count is ignored in the unix call.
* @param fd the file descriptor for the directory being read
* @param dirp the directory entry into which data should be copied
* @return number of bytes read; 0 if end of directory; -1 if the file
* descriptor is invalid, or if the file is not opened for read access.
* @exception java.lang.Exception if any underlying action causes
* Exception to be thrown
*/
int Kernel::readdir(int fd, DirectoryEntry& dirp)
{
// check fd
int status = check_fd_for_read( fd ) ;
if( status < 0 )
{
return status ;
}
FileDescriptor * file = process.openFiles[fd] ;
// check to see if the file is a directory
if((file->getMode() & S_IFMT) != S_IFDIR)
{
// return (ENOTDIR) if a needed directory is not a directory
process.errno = ENOTDIR ;
return -1 ;
}
// cout << "offset " << file->getOffset() << " size " << file->getSize() << endl;
// return 0 if at end of directory
// cout << "getOffset()" << file->getOffset() << " getSize " << file->getSize();
if( file->getOffset() >= file->getSize() )
{
// cout << "we could see 48 offset not happen in java"<< endl;
return 0 ;
}
// read a block, if needed
status = file->readBlock((short)(file->getOffset() / file->getBlockSize()));
if( status < 0 )
{
return status;
}
// read bytes from the block into the DirectoryEntry
//OKS Probelm here
dirp.read(file->getBytes(),file->getOffset() % file->getBlockSize());
// cout << dirp.toString() << endl;
int newOffset = file->getOffset() + DirectoryEntry::DIRECTORY_ENTRY_SIZE;
file->setOffset(newOffset) ;
// cout << "new offset " << newOffset << endl;
// return the size of a DirectoryEntry
return DirectoryEntry::DIRECTORY_ENTRY_SIZE ;
}
/**
* Obtain information for an open file.
* <p>
* Simulates the unix system call:
* <pre>
* int fstat(int filedes, struct stat *buf);
* </pre>
* @exception java.lang.Exception if any underlying action causes
* Exception to be thrown
*/
int Kernel::fstat( int fd , Stat &buf )
{
// check fd
int status = check_fd( fd ) ;
if( status < 0 )
return status ;
FileDescriptor * fileDescriptor = process.openFiles[fd] ;
short deviceNumber = fileDescriptor->getDeviceNumber() ;
short indexNodeNumber = fileDescriptor->getIndexNodeNumber() ;
IndexNode indexNode;
fileDescriptor->getIndexNode()->copy(indexNode);
// copy information to buf
buf.st_dev = deviceNumber ;
buf.st_ino = indexNodeNumber ;
buf.copyIndexNode( indexNode ) ;
return 0 ;
}
/**
* Obtain information about a named file.
* <p>
* Simulates the unix system call:
* <pre>
* int stat(const char *name, struct stat *buf);
* </pre>
* @exception java.lang.Exception if any underlying action causes
* Exception to be thrown
*/
int Kernel::stat(char * name , Stat & buf )
{
// a buffer for reading directory entries
DirectoryEntry directoryEntry;
// get the full path
char * fullPath = getFullPath( name ) ;
// cout << "Stat Full Path: " << fullPath << endl;
// find the index node
IndexNode indexNode;
short indexNodeNumber = findIndexNode(fullPath , indexNode ) ;
if( indexNodeNumber < 0 )
{
// return ENOENT
process.errno = ENOENT ;
return -1 ;
}
// copy information to buf
buf.st_dev = ROOT_FILE_SYSTEM ;
buf.st_ino = indexNodeNumber ;
buf.copyIndexNode( indexNode ) ;
return 0 ;
}
/**
* First commits inodes to buffers, and then buffers to disk.
* <p>
* Simulates unix system call:
* <pre>
* int sync(void);
* </pre>
*/
void Kernel::sync()
{
// write out superblock if updated
// write out free list blocks if updated
// write out inode blocks if updated
// write out data blocks if updated
// at present, all changes to inodes, data blocks,
// and free list blocks
// are written as they go, so this method does nothing.
}
/**
* Write bytes to a file.
* <p>
* Simulates the unix system call:
* <pre>
* int write(int fd, const void *buf, size_t count);
* </pre>
* @exception java.lang.Exception if any underlying action causes
* Exception to be thrown
*/
int Kernel::write(int fd, char * buf, int count)
{
// cout << "Kernel::write Count : " << count << endl;
// check fd
int status = check_fd_for_write( fd ) ;
if( status < 0 )
{
return status;
}
FileDescriptor * file = process.openFiles[fd] ;
// return (ENOSPC) if the device containing the file system
// referred to by fd has not room for the data
int offset = file->getOffset() ;
int size = file->getSize() ;
int blockSize = file->getBlockSize() ;
char * bytes = file->getBytes() ;
int writeCount = 0 ;
for(int i=0;i<count;i++)
{
// if this is the first time through the loop,
// or if we're at the beginning of a block,
// load or allocate a data block
if( ( i == 0 ) || ( ( offset % blockSize ) == 0 ) )
{
status = file->readBlock( (short)( offset / blockSize ) ) ;
if( status < 0 )
{
return status;
}
}
// copy a byte from the write buffer to the file buffer
bytes[offset % blockSize] = buf[i];
offset++;
// if we get to the end of a block, write it out
if((offset % blockSize ) == 0)
{
status = file->writeBlock((short)((offset-1)/blockSize));
if( status < 0 )
{
return status ;
}
// update the file size if it grew
if( offset > size )
{
file->setSize( offset ) ;
size = offset ;
}
}
writeCount ++ ;
}
// write the last block if we wrote anything to it
if((offset % blockSize)>0)
{
status = file->writeBlock((short)((offset-1)/blockSize));
if( status < 0 )
{
// cout << "here " << endl;
return status;
}
}
// update the file size if it grew
if(offset > size)
{
file->setSize(offset);
}
// update the offset
file->setOffset(offset);
// return the count of bytes written
return writeCount ;
}
/**
* Writes a directory entry from a file descriptor for an
* open directory.
* <p>
* Simulates the unix system call:
* <pre>
* int readdir(unsigned int fd, struct dirent *dirp ) ;
* </pre>
* Note that count is ignored in the unix call.
* @param fd the file descriptor for the directory being read
* @param dirp the directory entry into which data should be copied
* @return number of bytes read; 0 if end of directory; -1 if the file
* descriptor is invalid, or if the file is not opened for read access.
* @exception java.lang.Exception if any underlying action causes
* Exception to be thrown
*/
int Kernel::writedir( int fd , DirectoryEntry& dirp )
{
// check fd
int status = check_fd_for_write( fd ) ;
if( status < 0 )
return status ;
FileDescriptor * file = process.openFiles[fd] ;
// check to see if the file is a directory
if( ( file->getMode() & S_IFMT ) != S_IFDIR )
{
// return (ENOTDIR) if a needed directory is not a directory
process.errno = ENOTDIR ;
return -1 ;
}
short blockSize = file->getBlockSize() ;
// allocate or read a block
status = file->readBlock( (short)( file->getOffset() / blockSize ) ) ;
if( status < 0 )
{
return status ;
}
// cout << "dir written " << dirp.toString() << " offset" << file->getOffset() << endl;
// write bytes from the DirectoryEntry into the block
dirp.write( file->getBytes() , file->getOffset() % blockSize ) ;
// write the updated block
status = file->writeBlock( (short)( file->getOffset() / blockSize ) ) ;
if( status < 0 )
{
return status ;
}
//cout <<"From dir write " << dirp.toString() << endl;
// update the file size
file->setOffset(file->getOffset()+DirectoryEntry::DIRECTORY_ENTRY_SIZE);
if(file->getOffset() > file->getSize())
{
file->setSize(file->getOffset());
}
// return the size of a DirectoryEntry
return DirectoryEntry::DIRECTORY_ENTRY_SIZE ;
}
/*
to be done:
int access(const char *pathname, int mode);
int link(const char *oldpath, const char *newpath);
int unlink(const char *pathname);
int rename(const char *oldpath, const char *newpath);
int symlink(const char *oldpath, const char *newpath);
int lstat(const char *file_name, struct stat *buf);
int chmod(const char *path, mode_t mode);
int fchmod(int fildes, mode_t mode);
int chown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);
int utime(const char *filename, struct utimbuf *buf);
int readlink(const char *path, char *buf, size_t bufsiz);
int chdir(const char *path);
mode_t umask(mode_t mask);
*/
/**
* This is an internal variable for the simulator which always