write() performs differently in two situations - c

So I was required to solve this exercise:
This exercise is designed to demonstrate why the atomicity guaranteed by opening a file with the O_APPEND flag is necessary. Write a program that takes up to three command-line arguments:
$ atomic_append filename num-bytes [x]
This file should open the specified filename (creating it if necessary) and append num-bytes bytes to the file by using write() to write a byte at a time. By default, the program should open the file with the O_APPEND flag, but if a third command-line argument (x) is supplied, then the O_APPEND flag should be omitted, and instead the program should perform an lseek(fd, 0, SEEK_END) call before each write(). Run two instances of this program at the same time without the x argument to write 1 million bytes to the same file:
$ atomic_append f1 1000000 & atomic_append f1 1000000
Repeat the same steps, writing to a different file, but this time specifying the x argument:
$ atomic_append f2 1000000 x & atomic_append f2 1000000 x
List the sizes of the files f1 and f2 using ls –l and explain the difference.
So this is what I wrote:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[]) {
int fd, flags, num_bytes;
if (argc < 3 || strcmp(argv[1], "--help") == 0) {
printf("Usage: %s filename num-bytes [x]\n", argv[0]);
return 1;
}
num_bytes = atoi(argv[2]);
if (argc == 4 && strcmp(argv[3], "x") == 0) {
fd = open(argv[1], O_CREAT | O_WRONLY, 0666);
if (fd == -1)
perror("open");
while (num_bytes-- > 0) {
lseek(fd, 0, SEEK_END);
write(fd, "a", 1);
}
if (close(fd) == -1)
perror("close");
}
else {
fd = open(argv[1], O_CREAT | O_APPEND | O_WRONLY, 0666);
if (fd == -1)
perror("open");
while(num_bytes-- > 0)
write(fd, "a", 1);
if (close(fd) == -1)
perror("close");
}
return 0;
}
Now after I ran it as required:
abhinav#cr33p:~/System/5$ ./a.out f1 1000000 & ./a.out f1 1000000
[1] 4335
[1]+ Done ./a.out f1 1000000
abhinav#cr33p:~/System/5$ ./a.out f2 1000000 x & ./a.out f2 1000000 x
[1] 4352
[1]+ Done ./a.out f2 1000000 x
abhinav#cr33p:~/System/5$ ls f1 f2
f1 f2
abhinav#cr33p:~/System/5$ ls -l f*
-rw-rw-r-- 1 abhinav abhinav 2000000 Dec 10 16:23 f1
-rw-rw-r-- 1 abhinav abhinav 1000593 Dec 10 16:24 f2
Definitely, there is a difference in file sizes I am somewhat unable to clearly understand why? I searched and found somewhere this explanation:
The sizes were definitely different:
-rw------- 1 posborne posborne 1272426 2012-01-15 21:31 test2.txt
-rw------- 1 posborne posborne 2000000 2012-01-15 21:29 test.txt
Where test2.txt was run without O_APPEND. test2.txt is short by the
number of times (or bytes as a result of times) that seeking to the
end of the file did not happen at the same time as the write (quite
frequently).
But it does not seem to make any sense. So why the difference in sizes?

This code, run on a file not opened with O_APPEND:
while (num_bytes-- > 0) {
lseek(fd, 0, SEEK_END);
write(fd, "a", 1);
writes to the location of the end of the file as it was when the call to lseek() was made. The end of the file can change in the time between the lseek() and write() call.
This code, on a file that was opened with O_APPEND:
while(num_bytes-- > 0)
write(fd, "a", 1);
is guaranteed by the standard behavior of write()'ing to a file opened with O_APPEND to write to the end of the file no matter where that end is.
That's the entire point of the O_APPEND flag - lseek() then write() doesn't work.

Related

C program hangs after creating file

I'm doing an exercise for University, which asks:
Create a C program called 'split', which accepts a file, and a number. The program will divide the input file in two files, one called 'part1.txt' which will contain the first n bytes, and one called 'part2.txt' which will contain the other bytes.
If the input file contains less than n bytes, the file 'part2.txt' will not be created.
This is my program. What happens when I execute it, is that it creates the part1.txt file (without anything written in it) and the program hangs.
I've been looking at this for a day, but can't spot the problem. Any help?
I've compiled using:
gcc -o split split.c
When i execute it, i write:
./split text.txt 10
Where 'text.txt' is a text file containing words i accurately typed by pressing random buttons on my keyboard.
int splitter;
int fd, fd1, fd2;
char buffer[5000];
int main(int argc, char** argv){
if(argc<2){
printf("Insert 2 arguments.");
exit(1);
}
splitter = atoi(argv[2]);
if (fd=open(argv[1], O_RDONLY) < 0){
perror("Error\n");
exit(1);
} else {
if (fd1=open("part1.txt", O_RDWR | O_CREAT, S_IRWXU) <0){
perror("Error");
exit(1);
}
if(read(fd,buffer,splitter) == splitter){
write(fd1,buffer,splitter);
if (fd2=open("part2.txt", O_RDWR | O_CREAT, S_IRWXU)<0){
perror("Errore");
exit(1);
};
while (read(fd,buffer,1) ==1){
write(fd2,buffer,1);
}
close(fd1);
close(fd2);
} else {
while (read(fd,buffer,1) ==1){
write(fd1,buffer,1);
}
close(fd1);
}
close(fd);
}
The relational operators reside at the 6th level of C's precedence table, much higher than the assignment operators.
This statement:
if (fd=open(argv[1], O_RDONLY) < 0)
is equivalent to:
if (fd = (open(argv[1], O_RDONLY) < 0))
The < operator returns either 1 or 0, or in other words, true or false, which gets assigned to fd.
Change it to:
if ((fd = open(argv[1], O_RDONLY)) < 0)
You have the same issue in the subsequent if statement.
From Expert C Programming:
Some authorities recommend that there are only two precedence levels
to remember in C: multiplication and division come before addition and
subtraction.
Everything else should be in parentheses. We think that's
excellent advice.

Implementing the cp command using read/write system calls [duplicate]

This question already has an answer here:
Using open(), read() and write() system calls to copy a file
(1 answer)
Closed last year.
I am trying to implement the cp command only using read/write system calls.
Here is my code:
/**
* cp file1 file 2
*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int errsv;
char contents[1024];
int fd_read, fd_write;
fd_read = open(argv[1], O_RDONLY);
if (fd_read == -1)
{
errsv = errno;
printf("Error occured: %d\n", errsv);
}
read(fd_read, contents, sizeof(contents));
fd_write = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0744);
if (fd_write == -1)
{
errsv = errno;
printf("Error occured: %d\n", errsv);
}
write(fd_write, contents, sizeof(contents));
close(fd_read);
close(fd_write);
return 0;
}
I tested the code using the commands:
cc test.c
./a.out file1 file2
Here is my file1:
dummy text
dummy text
After running the code, although file2 contains the text from file1, it also has some gibberish characters. [not keeping this here.]
Why is this so?
You need to call read() and write() in a loop to copy the entire file. read() returns 0 when you reach EOF, or a negative result if there's an error, then you can end the loop.
read() returns the number of bytes that were read, which may be less than the size of the buffer. You need to use that number when calling write(), otherwise you'll write extra characters to the output file. These will be unitialized characters on the first iteration, and on other iterations they'll be left over characters from previous iterations.
int main(int argc, char *argv[])
{
char contents[1024];
int fd_read, fd_write;
fd_read = open(argv[1], O_RDONLY);
if (fd_read == -1)
{
perror("open input file");
exit(1);
}
fd_write = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0744);
if (fd_write == -1)
{
perror("open output file");
exit(1)
}
int n_read;
while ((n_read = read(fd_read, contents, sizeof(contents))) > 0) {
write(fd_write, contents, n_read);
}
close(fd_read);
close(fd_write);
return 0;
}
write(fd_write, contents, strlen(contents));
Strlen returns the filled entries number but sizeof returns the buffer size which is 1024

create new file with system calls

Im trying to create a new file / overwrite an existing file using systemcalls , but for some reason I have two problems:
1. When I'm first running the program it exits with value 0, so it seems like it created the file successfully, but I can't see anything in my project directory.
then when I secondly running the program the file is created, but an error message is printed on the screen.
2. Also after the first iteration of the program, I can't see the prinf message at the end of the main function.
Thanks for helping.
int readFileDesc = 0, writeFiledesc = 0;
int sourceFile = 1, destFile = 2, bufferSize = 3, isOverwrite;
if (argc != 4 && argc != 5) {
printf("Invalid number of arguments\n");
printf("Usage:\n");
printf(" ex1 [-f] SOURCE DEST BUFFER_SIZE");
exit(EXIT_FAILURE);
}
//Checking if -f [OP] is activated.
isOverwrite = (strcmp(argv[1], "-f") == 0);
if (isOverwrite) {
sourceFile++;
destFile++;
bufferSize++;
}
//Opening the source file
readFileDesc = open(argv[sourceFile], O_RDONLY);
if (readFileDesc < 0) {
perror("Unable to open source file for reading: ");
exit(EXIT_FAILURE);
}
//opening the destination file
if (!isOverwrite) {
//Case we dont have the -f [op] so we create the file.
writeFiledesc = open(argv[destFile],
O_CREAT | O_EXCL | O_WRONLY ,
S_IRUSR | S_IWUSR);
if (writeFiledesc < 0) {
perror("Unable to open destination file for reading: ");
exit(EXIT_FAILURE);
}
} else {
//Case we have the -f [op] so we override existing file.
writeFiledesc = open(argv[destFile], O_RDONLY | O_WRONLY | O_TRUNC);
if (writeFiledesc < 0) {
perror("Unable to open destination file for writing: ");
exit(EXIT_FAILURE);
}
}
//Assume the buffersize is legal.
bufferSize = atoi(argv[bufferSize]);
char data[bufferSize];
int nread, nwrite;
while ((nread = read(readFileDesc, data, bufferSize)) > 0) {
if ((nwrite = write(writeFiledesc, data, nread)) != nread) {
printf("write problem: ");
}
}
// cant see this!
printf("File %s was copied to %s" , argv[sourceFile] , argv[destFile]);
//handling errors
close(sourceFile);
close(destFile);
return EXIT_SUCCESS;
}
This is wrong:
writeFiledesc = open(argv[destFile], O_RDONLY | O_WRONLY | O_TRUNC);
Using both O_RDONLY and O_WRONLY is wrong. You need to use O_RDWR.
Per the POSIX standard for open():
SYNOPSIS
#include <sys/stat.h> #include <fcntl.h>
int open(const char *path, int oflag, ...);
...
Values for oflag are constructed by a bitwise-inclusive OR of flags
from the following list, defined in . Applications shall
specify exactly one of the first five values (file access modes)
below in the value of oflag:
O_EXEC
Open for execute only (non-directory files). The result is unspecified if this flag is applied to a directory.
O_RDONLY
Open for reading only.
O_RDWR
Open for reading and writing. The result is undefined if this flag is applied to a FIFO.
O_SEARCH
Open directory for search only. The result is unspecified if this flag is applied to a non-directory file.
O_WRONLY
Open for writing only.
Any combination of the following may be used:
...
Also, read() and write() return ssize_t, not int.

Creating a file with a hole

I was trying to create a file with a hole using a textbook code and modifying it slightly. However, something must've gone wrong, since I don't see any difference between both files in terms of size and disk blocks.
Code to create a file with a hole (Advanced Programming in the Unix Environment)
#include "apue.h"
#include <fcntl.h>
char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";
int
main(void)
{
int fd;
if ((fd = creat("file.hole", FILE_MODE)) < 0)
printf("creat error");
if (write(fd, buf1, 10) != 10)
printf("buf1 write error");
/* offset now = 10 */
if (lseek(fd, 16384, SEEK_SET) == -1)
printf("lseek error");
/* offset now = 16384 */
if (write(fd, buf2, 10) != 10)
printf("buf2 write error");
/* offset now = 16394 */
exit(0);
}
My code, creating a file basically full of abcdefghij's.
#include "apue.h"
#include <fcntl.h>
#include<unistd.h>
char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";
int
main(void)
{
int fd;
if ((fd = creat("file.nohole", FILE_MODE)) < 0)
printf("creat error");
while(lseek(fd,0,SEEK_CUR)<16394)
{
if (write(fd, buf1, 10) != 10)
printf("buf1 write error");
}
exit(0);
}
Printing both files I get the expected output. However, their size is identical.
{linux1:~/dir} ls -ls *hole
17 -rw-------+ 1 user se 16394 Sep 14 11:42 file.hole
17 -rw-------+ 1 user se 16400 Sep 14 11:33 file.nohole
You misunderstand what is meant by a "hole".
What it means is that you write some number of bytes, skip over some other number of bytes, then write more bytes. The bytes you don't explicitly write in between are set to 0.
The file itself doesn't have a hole in it, i.e. two separate sections. It just has bytes with 0 in them.
If you were to look at the contents of the first file, you'll see it has "abcdefghij" followed by 16373 (16384 - 10 - 1) bytes containing the value 0 (not the character "0") followed by ABCDEFGHIJ.

Copying file fails, EBADF on closing output file descriptor

So I was following a little outdated book (2010) and I'm trying to copy a file with Linux system calls. This is what i have:
NOTE: Ignore the tlpi_hdr.h and error_functions.h, they define errExit() and fatal() and some otheres, they just print the error and exit.
#include <stdio.h>
#include <fcntl.h>
#include "lib/tlpi_hdr.h"
#include "lib/error_functions.h"
#ifndef BUF_SIZE
#define BUF_SIZE 1024
#endif
int main(int argc, char *argv[])
{
int inputFd, outputFd, openFlags;
mode_t filePerms;
ssize_t numRead;
char buf[BUF_SIZE];
if (argc != 3 || strcmp(argv[1], "--help") == 0) {
usageErr("%s old-file new-file\n", argv[0]);
}
inputFd = open(argv[1], O_RDONLY);
if (inputFd == -1) {
errExit("Opening file %s", argv[1]);
}
openFlags = O_CREAT | O_WRONLY | O_TRUNC;
filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
outputFd = open(argv[2], openFlags, filePerms);
if (outputFd == -1) {
errExit("Opening file for writing %s", argv[1]);
}
while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0) {
if (write(outputFd, buf, numRead) != numRead))
fatal("I/O Error");
if (numRead == -1)
fatal("Reading error");
}
if (close(outputFd == -1))
errExit("close input");
if (close(inputFd == -1))
errExit("close output");
return EXIT_SUCCESS;
}
I'm failing on closing of the output file descriptor with EBADF Bad file descriptor:
thinkpad :: ~/.tlpi % ./cp.o a b
ERROR [EBADF Bad file descriptor] close output
The file copies fine tho:
thinkpad :: ~/.tlpi % sha1sum a
40a925a93e149ac53d2630cde8adeb63b8134b29 a
thinkpad :: ~/.tlpi % sha1sum b
40a925a93e149ac53d2630cde8adeb63b8134b29 b
thinkpad :: ~/.tlpi %
Why?
Let's take a closer look at your close call:
close(outputFd == -1)
Here you are comparing outputFd to the value -1. The result of that is a boolean value, which in C will be either 0 or 1. This happens to be either standard input or standard output, depending on the result. Not a file you descriptor you should close.
My guess is that you meant
if (close(outputFd) == -1)

Resources