Estimated time: 30 minutes
Given a file containing arbitrary bytes, you must write a function that copies the file. Obviously, your function cannot modify the content of the original file. Beware that the copied file should have the same permissions as the original file.
close(2). You can call
memcpy(3) only once.
Hint : msync(2) is a function that ensures that your modifications done in memory (so in the address returned by mmap) will also be saved in the file. Check the documentation to learn how to use it (pay attention to the flags). Call it before munmap() (or your modifications to the memory may be lost) !
Hint : The function ftruncate(3) is a function that you won't need to use frequently. However, this function is mandatory to pass this exercice ! In your code, you have to use it to extend the size of the file you opened for copying the original one. When you'll call open (with the appropriate flags to meet the requirements above), the size of the new file freshly created will be set to zero. So, in order for the mmap function to work, you'll need to extend its size to the one you want by calling ftruncate. Read the documentation linked and use this trick BEFORE calling mmap !
Hint : Permissions are sometimes hard to deal with. Be aware that, when using mmap with MAP_SHARED and PROT_WRITE, your file descriptor argument must have been opened with the O_RDWR flag. You need to read the file to copy it in memory and you ask to write in this piece of memory (with the PROT_WRITE) so you also need the write permission on the file !