Hello, everybody, and welcome back to lesson to this lesson will be about using system calls for file
manipulation.
Now there are a lot of system calls related to farm manipulation, but these are the main ones that
I will be covering in this course.
The first one is create.
We will learn how exactly do we create a file using a system call?
Next, we will look at delete.
How do we actually delete a file from a file system?
Next, we learn how to read a file, write to a file, reposition within a file instead of just writing
a reading to the beginning or end of a file.
It can be right to say the middle of a file.
And lastly, we will learn how to close a file using a system call.
Now, when these translate to the actual system calls, you can see that they're very similar to the
naming, except for create and also reposition.
But the rest of them are pretty intuitive, such as delete is for the system calls.
Remove, read is read, write is right and close is close.
Now, the only two ones that you might have questions about is the create system call.
So why is the current system called called open?
We're going to go more into that in the future, but basically it's going to try to open a file name,
and if the file does not exist, it is going to create it and then along with the preposition command.
This is if you want to seek re-election, go exactly what does this look mean and how can we use it?
OK, let's get started with the first system call for manipulation that is open and like it sounds.
This system call opens a file.
However, something that is unique about this open system call is that if it does not exist, it will
create a file.
But this is dependent on some of the flags that you set, which will be covering in one of the next
slides, and also the mode which sets what permissions file permissions does that file have?
And it also takes in a character pointer that points to the path of the file.
The next system call is the closest in call, and like the open system, call it returns an integer.
And with both of these system calls, you want to check to make sure that it does not return a negative
number.
If it does return a negative number, then that means an error has occurred and you need to handle that
error properly.
But with the closed command, it does exactly what it sounds like.
It closes the file and it takes in a file descriptor, and this file descriptor is the one that is returned
here.
Before we get onto an actual coding example of this, I want to go more in-depth about what exactly
are these flags?
What are these mode underscore?
Key mode?
The mode can be represented in a variety of formats, but the format I find myself using the most is
through out the value.
And here's an example of an open system called with the associated mode.
Here we're just trying to open a file called Hello Data.
And here are the flags which will cover this in a later slide.
And here are the mode.
And in this example, the mode is zero seven five five.
What exactly does this correspond to?
Well, each of these values represents your permission to a specific user group.
The first group is a special group, which we're not going to really cover this in this course.
So most of the time, just leave the set zero.
The next octave value corresponds to a user permission that's more associated with the owner of the
file.
The next one is the group permission and the last one is the other users permission.
These values correspond to specific permissions, and this is how you can find out what they are.
A read permission is correspond with the value for right.
You use a value to execute, use a value of one.
And you can use any combination of these three.
So you can get a value of one two four.
And then you can also add up these two for up to six.
That would give a read and write permission to somebody.
First, let's actually go through what these mean right here.
These seven, when a user has a permission of seven, that means that they have read, write and execute
permission for a certain file.
That's four plus two plus one, which is seven when a group has a value of five.
That means they have a read and execute permission, which is four plus one.
And lastly, for the other group, it has the same read and execute permission because it's four plus
one or two equals five.
A little more in depth about other varieties of modes that you can set.
And like I said before, Read is associated with a value of four right, is associated with a value
of two, and execute is associated with a value of one.
We wanted to give a certain user a read and write permission for a file.
You would give it a octal value of six because four plus two is six.
Let's say you wanted to give a specific group a read, write and execute permission.
You would give that specific group a seven because four plus two plus one for read, write and execute
gives you seven.
And lastly, if you want to group to only have write permission, then you would only give that group
a of the value of two.
Now let's go head over to my VM so we can actually see real examples of setting the file modes when
opening a file.
OK, now that we are at my virtual machine, the first thing as.
Always is to have a terminal window open, and here we are, going to open the Emanual page for open
because we know that that's what we're trying to do is use the open symbol.
And as you can see, when you type in open, it is going to say the name of this one is open vti one,
which just doesn't look like the right one.
So as before?
Let's go to the second one.
Man two open.
So we're the second page for open.
And here this looks like the one that we want.
So this one is to open and positive period to file, and that is exactly what we want to do.
And as I showed on the slides, it here is the call for it.
It returns an integer, it takes it a path theme, has some flags and also mode, as we did before in
my example in lesson one.
You can scroll down here to see exactly what are these values and what are the flags.
For example, the flags are set by an access mode, and there is a huge variety of these which you can
look online for a full list of them.
But for example, if you do, oh, underscore R.T only that means that you want to open the file for
read only.
This one's for right only.
This one's for read and write and so on.
But this one, we will be using a read and write only access mode.
And now that we have the manual page open for open, let's start reading our system calls.
As we can see here, it returns an integer.
First thing we want to do is create an integer so you can force anything into file, for example.
And here we want to call it open.
And then it takes in the path name.
The name of the path that we want to open is called a new file that text.
Now, as I stated before, the open system call is going to try to open that certain file that you put
here.
But in this case, right now in our directory, new file does not exist.
How exactly can we create the file if it does not exist?
Well, this is when the flag bits come into handy.
Here you can type in o underscore our d w r, so this will open the file for read and write access.
And then also, if it does not exist, we will open it to create it.
That is how you can create the file if it doesn't exist.
And lastly is where we set our mode bits.
And as before, we figured out how to calculate them out.
But just for a quick refresher, if it goes user group other the user for and then it also goes with
these values of read, write, being two and execute being one.
And they go in this order, well, I guess I actually go special, but we don't really worry about that
one for now.
This will just be a zero.
So zero and then the user permission, group permission and other permission.
In this case, we will start with these zero.
That's for the special group.
Then for the owner, which is the user, we want to give permission of seven.
So we want to be able to read, write and execute.
And just for example, for the group we want to give them or permission of five, we want them to be
able to read and execute our file.
And lastly, for the other groups, we want to just give them zero for now because we don't want them
to have access.
And as I stated before, you want to always check the return value.
Here you can do if the file is less than zero.
So that means that the values negative, we are going to just print f error.
And we want to give it an exit value.
Exit one.
This code is fairly simple, and it is simply opening a file called new file activity with a read and
write only access.
But if it does not exist, we are going to try to create it.
And when we create it, we're going to give it these permissions.
OK, so let's go ahead and compile it.
And before I do that, I want to show you that the only file in our directory is file no, Darcy.
And that is this file that we've been manipulating over here.
The first thing that we want to do is compile our program, farmer Darcy.
And as you can see, it made an output binary for eight out.
But when we run the air out, what it is doing is opening a file called a new file that takes tea.
And since it does not exist, it is going to try to create it with these file permissions.
Oh, seven five zero.
So now let's go ahead and check our file to see if it was created.
And as expected, a new file was created called New File.
That activity, which was associated with this value right here.
And here it is right here.
Check on these mode bits that we set, which were zero seven five zero.
How you could check that is by doing an LS minus L A right.
Here is the file permissions and for new file that t, as you can see for the user, we set it for a
read, write and execute.
That's seven for the group.
We gave it a R and an X that equals the five that we put right here.
And lastly, for the other groups, we gave it a zero by.
There are no read, write or execute permissions there.
I think that was a pretty good example of how exactly you could set the node bit.
But if you want to do a couple more examples, let's go ahead and try to create with zero zero zero.
We save this, and what we actually needed to do is remove new file.
And let's clear this.
Let's go ahead and compile our new program with the bits zero zero zero zero, we are going to try to
create a file called New File that you see with the file permissions zero zero zero.
And as you can see in here, there is no other files.
Go ahead and read the pile and run that out.
And as expected, it created a new file called New File that takes time.
And I don't know if you realize this, but this time when we compiled it, it does not highlight this
as green, which means we do not have any permissions in this virtual box.
It is white, which means we really don't have any permissions for that to execute it.
But what we could do is double check that with L s minus L.
And as you can see for new file data, there are no permission bits set for either the user group or
other.
One more example of setting the a quick one doing zero seven zero zero.
That is, if you want to only have permission to the file, being the owner will give you read, write
and execute access while giving the group and other users no access.
Go ahead re compile our program and let's run it.
And as you can see, there is an error.
Something that I want to mention really quick is how to use the closed system call.
Just because it is very simple.
Whenever you open a file, you always want to be sure to close it at the end of your program.
Here we are at the end of a program, and what we are going to do is create an integer that will store
the return value.
We'll call this integer X and X will be equal to close and then the file descriptor of our open file.
And in this case, the file descriptor is called file.
And here we will type file x returns negative.
That means an error occurred.
So what we want to do is check that if X is less than zero and we can just copy this right here copy.
Paste, if X is less than zero f error and also exit the program.
This is just a failsafe to make sure that it close properly and if not, you can handle it when you
run out and you do this myself.
You will see that the owner, which is also the user, has a permission of read, write and execute
and that associates with the value seven that we put here real quick.
I just wanted to share with you this website called Permissions, Dash Calculator, dot org.
What this does is calculate the Unix permissions bits that will help you set your bits if you're confused.
As I stated before, I use this example, if you wanted to give the user a read, write and execute
permission, then you set it like that to the group.
Let's say you want to give it read and other you want to only execute, then you would, but set the
mode to zero seven four one.
I'm going to end this lesson right here for now, but in the next lesson we'll be covering, how exactly
can we delete a file?