‫Hello and welcome to this lecture and this lecture we're going to implement f open, but only in the
‫call virtual filesystem layer.
‫We will not be implementing the F open for the fact 16 filesystem.
‫Yet by the end of this lecture, you can expect generic F open to be complete and it will call our fat
‫16 open function, but our fat 16 open function will just return zero.
‫It will not actually open any files.
‫OK, let's begin.
‫So first things first.
‫What is our f open function for?
‫OK, well you've most likely opened a file in C before.
‫That's exactly what this function will do.
‫It is responsible for locating the correct file system to open a Farlam.
‫It does this because it knows the drive.
‫No, I know in the drive no.
‫We can locate the correct drive.
‫Once we know the correct drive, we can we can locate the correct file system.
‫Once we have the correct file system, we can call.
‫It's open function, aifs, open function at this point, the open function in the implemented file
‫system will be called, it will now be responsible for finding the file, checking if it exists, opening
‫up the file and then creating some private data for it and returning that private data.
‫That's why the return type is a void pointer.
‫However, in this lecture we're only implementing the core functionality.
‫We will not implement the fat 16 functionality for open in this lecture.
‫OK, so I hope that explains what F Open is in our implementation.
‫OK, so the very first thing we're going to do, we're going to go and address equals zero and we're
‫going to return address.
‫Now we're going to go straight to path route.
‫Routes, path equals path, paths, the paths filename they know, OK, so that will paths our path
‫and return us a path route that we can use.
‫So we're going to do here is if the route path is no, then there was a problem in the path.
‫Maybe format's wrong wrests equals minus invalid arguments go to out and I will have out and out labeled
‫down here.
‫So on online 111.
‫At this point we have our path split up into a path, route and path paths.
‫This is great because it means that it's nice and easy to access.
‫This is why we made the path paths earlier in the course to prepare for now and now.
‫The time's come.
‫We're making good use of it.
‫OK, so now we need to check to see if they have open just on the routes because we can't do that.
‫We need a file, OK?
‫We need a file name.
‫So what we're going to say is if if the route path doesn't have a first part, then then this is just
‫the route path.
‫And what I mean by that is it's just something like that rather than something like this.
‫OK, so I put a comment there.
‫We cannot have just a group path.
‫OK, there we go.
‫So we're going to go Sox minus invalid argument, go to out.
‫OK, so now we've checked for that.
‫So essentially this just stops somebody trying to open a directory or something like that, which is
‫not valid.
‫We might not actually need this because the lower filesystem will know it's a directory and return an
‫error anyway.
‫But by putting this here, it ensures that we don't waste any more CPU time.
‫So why not have it here?
‫Right.
‫So what we're going to do here, we're going to go struct disk disk equals diskettes, root path, drive
‫number.
‫And I we're going to say if the disk is is null, then it doesn't exist.
‫So in which case we go rest's equals minus invalid argument or you know what, EIO would make more sense.
‫Go to out.
‫So basically what if someone tries to f open this file or something like that.
‫Well, we don't have to drive one in the system.
‫So obviously that's an immediate read failure.
‫So that's what we're checking for here.
‫OK, on these lines.
‫Like, you can't read from drive on windows if drive doesn't exist.
‫This is the same thing here apart from we're talking about drive or drive one.
‫OK, so the next step is to check that there's actually a file system on this disk if if the disk doesn't
‫have a file system associated with it.
‫In other words, we didn't have a file system driver that could manage that disk, that its file system
‫whereby we know we cannot open files on a disk that has no file system.
‫So we need to check for that.
‫So we go if disk file system is No.
‫Then REST's equals minus EIO, go to outside, OK, and we can see it's complaining here, pointed to
‫incomplete class type is not allowed.
‫So we need to scroll to the top and we're going to include that.
‫So we're going to go include desk desktop.
‫And there we are.
‫That error is now gone.
‫Now there's more to come, but we're not ready for it.
‫So we need to create some more functions.
‫So we're going to go file MOAD file, get mowed by string.
‫And if you remember, guys, we created these in a few lectures.
‫Go and file Deitsch, you see.
‫So just pay attention to that.
‫So forget get by string and we're going to accept a string passed to us.
‫And we're basically going to say foulmouthed mode equals foulmouthed invalid and we're going to return
‫mode here and then we're going to say F string and compare and we haven't created that function yet.
‫We will create it.
‫Hestia one.
‫So we're not checking one byte here equals zero then mode equals file mode.
‫Right.
‫Elusive string and compare.
‫Star W. one one means we're reading one byte equals zero.
‫Mode's equals file, Mode's writes, al-Sahhaf string and compare.
‫Star, a one equals equals zero votes equals foulmouthed append.
‫So I hope you guys understand what's going on here.
‫You know, in our effort, when we pass in a mode such as RWA a well, that's what this is here.
‫We are checking for this mode and we're choosing the correct binary mode here.
‫Based on the string mode they provided, why do we convert it to a binary mode like we're doing here?
‫Because it's easier to to use otherwise we'd have to check the string every time we want to know if
‫it's a read or write mode.
‫It's very messy.
‫By doing this is much cleaner, you will notice we don't take into account the plus character and all
‫this sort of stuff.
‫We're just making a basic implementation here.
‫Simple operations, rewrite, append.
‫We're now going to need to implement Stia and compare.
‫OK, so we are going to go to String Dot, see?
‫And we're going to go in it Essere and compare const char, string one, const char, string two, and
‫we're going to accept in any number of bytes and this is the maximum number of bytes.
‫So we're going to go and sancha you one, you two while NP minus minus above zero, you one equals unsigned
‫char
‫asterisk S1 plus plus while still one I should say S.R. one guy's.
‫You two equals unsign child star two plus plus.
‫If you one does not equal you two, then return you one minus you to.
‫If you want equals equals another Terminator.
‫Then return zero, otherwise down here, downhere return zero, so that's a string and compare function
‫and that will work absolutely fine.
‫While we're in String Dot, see, let's just implement a few more functions that were surely going to
‫use just so we can save a bit of time.
‫OK, so we're going to implement a string and then Terminator.
‫So in its string and then Terminator const char Aschiana and Max Char Terminator.
‫So while this is going to do, it's going to read the string until it either finds an old Terminator
‫or it finds the Terminator that you specified in this variable.
‫And we're going to need this because we use things like spaces in fact 16 regarding the filename.
‫So it's going to be easier to make a function that can tell us the length this way.
‫So we're going to go in.
‫It equals zero for R equals zero i.e. below max ie plus plus.
‫If string AI is equal to zero or string AI is equal to Terminator then break.
‫So that looks for null Terminator there.
‫Well I said zero earlier.
‫I meant another Terminator.
‫It looks for another Terminator or it looks for the Terminator here.
‫If either of those are found that were done, OK.
‫And we're going to return I by here.
‫OK, so that is that function implemented.
‫So in Microsoft's Fat 16 file system filenames not case sensitive.
‫So because of that we need to have a string compar function that doesn't care about the case, doesn't
‫care about capital letters essentially.
‫So we're going to have to create a function that does this.
‫So we're going to do that now.
‫So we're going to say in it I string and compare Consejo as one const char as two and it's n OK.
‫And I were going to say unsign charge you one, you two and get rid of that comma there.
‫And I was going to say well N minus minus is above zero.
‫You one equals unsigned char.
‫Asterisks one plus plus two equals unsigned shock, because we're casting now here as two plus plus
‫if you one does not equal you two and two lower.
‫So we're changing the case of of the character.
‫Now, you one does not equal to lower you two.
‫And we haven't made those functions yet, guys.
‫OK, then return you one minus you to.
‫If you one is equal to another Terminator, then return zero.
‫Otherwise, at the very bottom here, we return zero.
‫So you can see this is much like our string and compare function here, apart from we change the case
‫of the characters to the lower case.
‫So any characters become lower, lower case characters.
‫And that way then we can compare a lower case with the lower case.
‫So then it becomes case insensitive.
‫OK, that's what the I stands for here.
‫Insensitive.
‫Now we need to create the two lower function now.
‫So the two law functions pretty easy, just scroll to the top here and we're going to say cha to lower
‫and that's going to accept a character and we're going to say if this one is above or equal 65 and as
‫one is below equal 90, and if you check the ASCII table, you will see that 65 and 90 AlphaBeta characters.
‫OK, so check the each table if you're confused on that.
‫So now we just go as one plus equals thirty two, OK, and now we return as one.
‫That's just check the ASCII table a minute to check.
‫That's right.
‫So we go ASCII table and again I'm teaching you guys how to debug, how to figure things out yourself.
‫Kernel development is very much problem solving.
‫So we look here, then we see 65.
‫That is a capital and we can see the 97 is a lowercase eight, so that's why we plus 32 here, to convert
‫it to a lower case, a because notice they're in line.
‫OK, so I hope that makes sense.
‫OK, so we've now implemented all the functions we care about.
‫So all I want to do now is just get the prototypes in the header files so we can use them.
‫So string and compare, copy and paste that prototype and string.
‫Uh.
‫I string and compare, copy and paste that one, remember the semicolons on the end here, guys.
‫String Anel Terminator, copy and paste that one.
‫Um, two lower copy and paste that one, and I think that is it.
‫So we've now updated our string functionality and it's far more superior than it was.
‫OK, so let's go back to Father C. And we're going to scroll up and we're going to include String,
‫String, Dot Hage and now our string and compare what will work.
‫Absolutely fine.
‫OK, so in F Open now we're going to rename Kohnstamm Mode here because I should have done Mode String
‫because we're going to create a local variable called Moat's.
‫So if you've got a father who can also change it here as well.
‫Just the mode string.
‫And now down here, we're going to go foul mode, mode equals file, get mode by string and we're just
‫going to pass in mode string and now we're going to say if the mode is invalid.
‫In other words, they didn't pass us either RW or A because by default the mode is invalid.
‫Right.
‫So so if none of these match, then it's going to return invalid.
‫Right.
‫So they didn't pass any of those then they've passed us in invalid mode.
‫So we should return an invalid arguments and go to throughout.
‫So now we're just going to go void to script private data, and this is the private data file system
‫that we're going to call is going to give us.
‫So in our case, five, 16, and then we're going to go disc filesystem open and we're going to go disk
‫route, path, first mode.
‫So what's happening here then?
‫Well, we access disk, then we access file systems.
‫Let's take a look at disk we're going to peek into here.
‫And what we see in disk, we see filesystem.
‫So this is the variable here.
‫Yeah.
‫So what we're doing is we're saying disk file system open.
‫So let's just go to filesystem a moment.
‫And we can see filesystem open is our function pointer aifs open function.
‫And I'm fed up, see?
‫Our open function, we pointed to our first 16 open function here.
‫And we return this implementation of ourself in the result function, which sets filesystem in desk.
‫So that's how all that slinking guys, so this filesystem open will point to fact 16 open because the
‫disc with the drive number of zero.
‫Is pointing to the fact 16 filesystem, so this calls fat 16 open.
‫OK, so that's how that's working.
‫So we're now going to say is is if is error and we haven't implemented is there yet.
‫But we will.
‫Descript private data
‫then rests equals error I error integer.
‫We haven't implemented that yet either.
‫Bu but we will just scripter private data go to ours.
‫So when are we going to implement is error and error I.
‫OK, so just go to Colonel at the Hajj because these are macro's by the way.
‫So we're going to go to find error.
‫This is the very first thing we're going to do and this will accept a value and it's going to return
‫a viewpoint and then value is basically just going to cast it essentially.
‫And that's what's happening here.
‫OK.
‫Simple as that.
‫I hear we're going to go hashtag define era I and this is going to take in a value.
‫And what's that going to do?
‫It's going to cost it to insure.
‫That's what that's going to do.
‫And now we're going to go hashtag to find.
‫Is error that's going to take in a value and it's just going to cast it to an inside job and then check.
‫If it's below zero, so that should look like that, OK?
‫And that's what that's going to do.
‫And also, guys, we should get rid of the semicolons at the back of these.
‫That was a mistake.
‫So I can only assume you all know what macros do in the C programming language.
‫Essentially, it'll take our value here and they rule and it will replace it with this.
‫So avoid cast, avoid point of cast our value.
‫And the same here is error will take a viewpoint like this or an integer or whatever.
‫It'll Cassard Integer and then check this below zero.
‫So that's why we're doing things like this.
‫These macros, they're very powerful.
‫So back to Dot C, we've included Canada.
‫That's good.
‫So we can now see the bypass and descriptor.
‫Private data to error will cast our vote pointer to an integer and then check if it's below zero.
‫And if it is, then it's true.
‫OK, so that's what that's doing.
‫So now we're going to go struct file descriptor.
‫DGSE for descriptor equals zero rascal's file, new descriptor.
‫We're going to pass in the address a descriptor here.
‫If the response is below zero, there was an error and we should go to out.
‫So we're now going to go to scripter filesystem equals disk file system.
‫Just script, a private equity script, a private data to script a desk equals desk, and we're now
‫going to go.
‫Equals equals descriptor index.
‫OK, and now we're now out.
‫We're going to say if REST's is below zero, then race equals zero because F open should only return
‫all if it fails.
‫OK, it should return zero fails.
‫Not not the negative value.
‫So that's why we do that check by here.
‫Half open shouldn't return negative values.
‫OK, so let's briefly go over this then, and then we'll compile it.
‫So somebody comes in and they call F open on us.
‫We go in here, we pass the path, we now have the path, for example, it might be zero test of 60.
‫It's now split up into a path route.
‫In the path part.
‫We now get the current desk and we know the current desk because they passed in the desk.
‫No, here, OK, so we get the current desk and that the desks that they want to read from, we get
‫that while the desks they want to open, I should say, the file they want to open, we get the desk
‫number.
‫Now we have the desk.
‫We check if the desk doesn't have a file system associated with it.
‫If it doesn't, then we can't do anything online.
‫One, five, three.
‫We know that it's a valid desk and it has a file system.
‫So we get them out.
‫OK, if they've passed us in a valid mode, then we simply call open on the file system that's associated
‫with the desk.
‫OK, passing the highest, most path part and the most.
‫Now this will call Fat 16 open and fat 16 open.
‫We'll return a pointer to to its private data, not the private, but some internal private data it
‫will create for this file.
‫So it's separate data from from the private data.
‫In fact, private because private stores information about filesystem.
‫This is not what we want.
‫What this is returning as private data relating to the file that the user is trying to open.
‫OK, that's what that's doing.
‫So at this point, we have the descript private data.
‫We go in here, we create a new file descriptor.
‫OK, remember, we implemented this recently.
‫So this is the code for that.
‫It'll create a new file descriptor for us.
‫Now, what we do is, is we set the filesystem of this descriptor we just created to the filesystem,
‫we just open the file from we set the private data that they return to us in the scripter here and we
‫set the death to the correct disk.
‫We open the file from now here.
‫Our result will be the descriptor index.
‫It's as simple as that, the person who called F open, they now have the index of the file descriptor
‫that we just created here on line 168.
‫And now when they call things like F read, we can get the file descriptor using file, get the script
‫and we will know exactly which file system to talk with because we mentioned it here and we will know
‫exactly which private data to pass because they gave it to us online, 160.
‫So I hope that makes sense.
‫Let's now try to compile our code.
‫So we're going to go make clean clear builder say, and we can see that compiles absolutely fine.
‫So congratulations.
‫You just implemented F Open.