write() not writing to device - c

code:
#include <fcntl.h>
#include <linux/fs.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
void write_zero(char * file, unsigned long bytes)
{
printf("Zeroing %s\n", file);
unsigned int wrote = 0, total = 0;
int fd, i, buf;
char obj = 0x00;
fd = open(file, O_RDWR, DEFFILEMODE);
lseek(fd, 0, SEEK_SET);
write(fd, &obj, bytes);
}
int main(int argc, char * * argv)
{
int fd;
unsigned long blocks = 0;
char check = 0x0;
fd = open(argv[1], O_RDONLY);
ioctl(fd, BLKGETSIZE, &blocks);
close(fd);
printf("Blocks: %lu\tBytes: %lu\tGB: %.2f\n",
blocks, blocks * 512, (double)blocks * 512.0 / (1024 * 1024 * 1024));
do
{
printf("Write 0x0 to %s? [y/N] ", argv[1]);
fflush(stdout);
}
while (scanf("%c", &check) < 1);
if (check == 'y')
{
write_zero(argv[1], blocks * 512);
}
}
I get nothing actually written to the device.. I copied my open line from the 'dd' source code, thinking maybe it was not opened right. dd can zero the device, but this program does not. Any ideas?

It seems like this has been beaten to death but
char obj = 0x00;
fd = open(file, O_RDWR, DEFFILEMODE);
lseek(fd, 0, SEEK_SET);
write(fd, &obj, bytes);
Is not going to write zeros. It's going to write garbage from the stack.

Related

problem in a variant of copy command using mmap()

I have a program which is another variant of copy program in linux(Actually I'm on Mac OSX).
In order to support copying large files, I wrote something like this:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#define BUFFSIZE 65535
#define PAGESIZE 4096
int main(int argc, char **argv){
char *source, *destination;
int src_fd, dst_fd;
unsigned long long bytes_read;
int bytes = BUFFSIZE;
struct timeval start, end;
int overall_time = 0;
unsigned long long offset = 0;
struct stat statbuf;
if(argc < 3){
printf("copy <source> <destination>\n");
exit(EXIT_FAILURE);
}
source = argv[1];
destination = argv[2];
src_fd = open(source, O_RDONLY, 0777);
if(src_fd < 0){
perror("src_fd");
exit(EXIT_FAILURE);
}
//bytes_read = lseek(src_fd, 0, SEEK_END);
fstat(src_fd, &statbuf);
bytes_read = statbuf.st_size;
dst_fd = open(destination, O_RDWR | O_CREAT, 0777);
if(dst_fd < 0){
perror("dst_fd");
exit(EXIT_FAILURE);
}
lseek(dst_fd, bytes_read -1, SEEK_SET);
write(dst_fd, "", 1);
gettimeofday(&start, NULL);
while(bytes_read > 0){
if(bytes_read < BUFFSIZE){
bytes = bytes_read;
bytes_read = 0;
}
else{
bytes_read -= bytes;
}
void *src_map = mmap(NULL, bytes, PROT_READ, MAP_SHARED, src_fd, (off_t)offset);
if(src_map == (void*) MAP_FAILED){
perror("src_map");
exit(EXIT_FAILURE);
}
void *dst_map = mmap(NULL, bytes, PROT_WRITE, MAP_SHARED, dst_fd, (off_t)offset);
if(dst_map == (void*) MAP_FAILED){
perror("dst_map");
exit(EXIT_FAILURE);
}
memcpy(dst_map, src_map, bytes);
int src_unmp = munmap(src_map, bytes);
if(src_unmp == -1){
perror("src_unmap");
exit(EXIT_FAILURE);
}
int dst_unmp = munmap(dst_map, bytes);
if(dst_unmp == -1){
perror("dst_unmap");
exit(EXIT_FAILURE);
}
offset += 4096;
bytes_read -= bytes;
}
gettimeofday(&end, NULL);
printf("overall = %d\n", (end.tv_usec - start.tv_usec));
close(src_fd);
close(dst_fd);
return 0;
}
The goal is to measure the amount of time elapsed to copy a large file with the use of mmap().
The above code is not working for transferring 1GB file.
Any hint for that?
Thank you
Yes. The problem is in offset value. The offset value should be a multiple of page size.

mmap is wiping my file instead of copying it

So I'm using mmap to then write to another file. But the weird thing is, when my code hits mmap, what it does is clears the file. So I have a file that's populated with random characters (AB, HAA, JAK, etc...). What it's supposed to do is use mmap as read basically and then write that file to the new file. So that first if (argc == 3) is the normal read and write, the second if (argc ==4) is supposed to use mmap. Does anyone have any idea why on Earth this is happening?
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
int main(int argc, char const *argv[])
{
int nbyte = 512;
char buffer[nbyte];
unsigned char *f;
int bytesRead = 0;
int size;
int totalBuffer;
struct stat s;
const char * file_name = argv[1];
int fd = open (argv[1], O_RDONLY);
int i = 0;
char c;
int fileInput = open(argv[1], O_RDONLY);
int fileOutPut = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
fstat(fileInput, &s);
size = s.st_size;
printf("%d\n", size);
if (argc == 3)
{
printf("size: %d\n", size);
printf("nbyte: %d\n", nbyte);
while (size - bytesRead >= nbyte)
{
read(fileInput, buffer, nbyte);
bytesRead += nbyte;
write(fileOutPut, buffer, nbyte);
}
read(fileInput, buffer, size - bytesRead);
write(fileOutPut, buffer, size - bytesRead);
}
else if (argc == 4)
{
int i = 0;
printf("4 arg\n");
f = (char *) mmap (0, size, PROT_READ, MAP_PRIVATE, fileInput, 0);
/* This is where it is being wipped */
}
close(fileInput);
close(fileOutPut);
int who = RUSAGE_SELF;
struct rusage usage;
int ret;
/* Get the status of the file and print some. Easy to do what "ls" does with fstat system call... */
int status = fstat (fd, & s);
printf("File Size: %d bytes\n",s.st_size);
printf("Number of Links: %d\n",s.st_nlink);
return 0;
}
EDIT: I wanted to mention that the first read and write works perfectly, it is only when you try to do it through the mmap.
If you mean it's clearing your destination file, then yes, that's exactly what your code will do.
It opens the destination with truncation and then, in your argc==4 section, you map the input file but do absolutely nothing to transfer the data to the output file.
You'll need a while loop of some description, similar to the one in the argc==3 case, but which writes the bytes in mapped memory to the fileOutput descriptor.

read() error in C

I wrote a toy program to learn file I/O in C, the following is part of my code, and the output from this program is just wrong - I've no idea what is wrong in my code.
int fd = open(path_to_file, O_RDWR | O_APPEND | O_CREAT, 0777);
int size = 100;
int offset = 0;
write(fd, &size, sizeof(int));
write(fd, &offset, sizeof(int));
lseek(fd, 0, SEEK_SET); /* start reading from the beginning */
int *size = malloc(sizeof(int));
int *offset = malloc(sizeof(int));
read(fd, size, sizeof(int));
read(fd, offset, sizeof(int));
printf("size is %d, offset is %d \n", *size, *offset);
free(size);
free(offset);
The output I got is:
size is 1953719668, offset is 1684497779
These numbers are so huge, but they should be 100, and 0, respectively. I don't understand how this happens. Could someone offer help with understanding this?
Ingo has it right, if the file already exists you append to it...
Mallocing for 1 single int is not very efficient, but that should work.
I guess you copied and pasted your code, the same names on variables prevent it from compiling.
Working example even if the file exists:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int fd = open("test.dat", O_RDWR | O_CREAT, 0777);
int size = 100;
int offset = 0;
write(fd, &size, sizeof(int));
write(fd, &offset, sizeof(int));
lseek(fd, 0, SEEK_SET); /* start reading from the beginning */
int *result_size = malloc(sizeof(int));
int *result_offset = malloc(sizeof(int));
read(fd, result_size, sizeof(int));
read(fd, result_offset, sizeof(int));
printf("size is %d, offset is %d \n", *result_size, *result_offset);
free(result_size);
free(result_offset);
}

How does read(2) interact with carriage returns?

I am writing a simple program to flip all the bits in a file, but right now it only does the first 1000 bytes until I get that much working. Why does my call to read() ignore \r characters? When I run this code on a file that only contains \r\n\r\n, the read call returns 2 and the buffer contains \n\n. The \r characters are completely ignored. I'm running this on Windows (this wouldn't even be an issue on Linux machines)
Why does read(2) skip over the \r character when it finds it? Or is that what is happening?
EDIT: Conclusion is that windows defaults to opening files in "text" mode as opposed to "binary" mode. For this reason, when calling open, we must specify O_BINARY as the mode.
Thanks, code below.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
void invertBytes(size_t amount, char* buffer);
int main(int argv, char** argc)
{
int fileCount = 1;
char* fileName;
int fd = 0;
size_t bufSize = 1000;
size_t amountRead = 0;
char* text;
int offset = 0;
if(argv <= 1)
{
printf("Usages: encode [filenames...]\n");
return 0;
}
text = (char *)malloc(sizeof(char) * bufSize);
for(fileCount = 1; fileCount < argv; fileCount++)
{
fileName = argc[fileCount];
fd = open(fileName, O_RDWR);
printf("fd: %d\n", fd);
amountRead = read(fd, (void *)text, bufSize);
printf("Amount read: %d\n", amountRead);
invertBytes(amountRead, text);
offset = (int)lseek(fd, 0, SEEK_SET);
printf("Lseek to %d\n", offset);
offset = write(fd, text, amountRead);
printf("write returned %d\n", offset);
close(fd);
}
return 0;
}
void invertBytes(size_t amount, char* buffer)
{
int byteCount = 0;
printf("amount: %d\n", amount);
for(byteCount = 0; byteCount < amount; byteCount++)
{
printf("%x, ", buffer[byteCount]);
buffer[byteCount] = ~buffer[byteCount];
printf("%x\r\n", buffer[byteCount]);
}
printf("byteCount: %d\n", byteCount);
}
fd = open(fileName, O_RDWR);
should be
fd = open(fileName, O_RDWR | O_BINARY);
See read() only reads a few bytes from file for details.
Try opening with O_BINARY to use binary mode, text mode may be default and may ignore \r.
open(fileName, O_RDWR|O_BINARY);

Read and write struct with unistd.h read/write

I'm studying UNIX programming and was experimenting with read/write system calls.
I have a file with a pair of integer:
4 5
and I wrote this code to read the numbers:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef struct prova {
int first;
int second;
} prova_t;
int main(void) {
int fd;
prova_t origin;
prova_t result;
ssize_t bytes_read;
size_t nbytes;
fd = open("file.bin", O_WRONLY | O_CREAT);
origin.first = 24;
origin.second = 3;
write(fd, &origin, sizeof(prova_t));
close(fd);
fd = open("file.bin", O_RDONLY);
nbytes = sizeof(prova_t);
/* 1.BAD */
bytes_read = read(fd, &result, nbytes);
write(STDOUT_FILENO, &(result.first), sizeof(int));
write(STDOUT_FILENO, &(result.second), sizeof(int));
close(fd);
/* 2.GOOD */
nbytes = sizeof(int);
bytes_read = read(fd, &(result.first), nbytes);
write(STDOUT_FILENO, &(result.first), bytes_read);
bytes_read = read(fd, &(result.second), nbytes);
write(STDOUT_FILENO, &(result.second), bytes_read);
return 0;
}
In my first attempt I tried to read the whole struct from file and write its members to stdout. In this way, along with the numbers, I get some weird characters
4 5
E�^�
In my second attempt I read the numbers one by one and there were no problems in the output.
Is there any way to read and write the struct using the first method?
Edit: I updated the code to reflect suggestion from other users but still getting strange characters instead of numbers
First, let's do a hex dump to see what is really stored in the file.
hexdump -C b.txt or od -t x2 -t c b.txt are two examples (od is for octal dump, more common, less pretty output in my opinion)
00000000 34 20 35 0a |4 5.|
00000004
That's is what the file looks like if it was a created as an ASCII text file (such as using a text editor like vi). You can use man ascii to double check the hexadecimal values.
Now if you had a binary file that only contains two 8-bit bytes, in the system's native byte ordering (e.g. little-endian for x86, big endian for MIPS, PA-RISC, 680x0) then the hexdump would look like:
00000000 04 05 |..|
00000004
Here is the code to both create (open & write) a binary file, and read it back.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h> /* uint32_t */
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
/* User has read & write perms, group and others have read permission */
const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
typedef struct prova {
uint32_t first;
uint32_t second;
} prova_t;
#define FILENAME "file.b"
/* 'Safe' write */
int safewrite( int fd, const void *p, size_t want) {
int ret;
errno = 0;
while (want) {
ret = write(fd, (uint8_t *)p, want);
if (ret <= 0) {
if (errno != EINTR && errno != EAGAIN) {
return -1;
}
errno = 0;
continue;
}
want -= ret;
p = (uint8_t*) p + ret;
}
return 0;
}
int saferead(int fd, const void *p, size_t want) {
int ret;
errno = 0;
while (want) {
ret = read(fd, (uint8_t*)p, want);
if( ret == 0 )
return -1; /* EOF */
if (ret <= 0) {
if( errno != EINTR && errno != EAGAIN ) {
return -1;
}
errno = 0;
continue;
}
want -= ret;
p = (uint8_t*) p + ret;
}
return 0;
}
int main(int argc, char **argv) {
int fd;
prova_t result;
size_t nbytes;
/* Create file */
fd = creat(FILENAME, mode);
if (fd < 0) {
fprintf(stderr, "Unable to open " FILENAME ": %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
nbytes = sizeof(prova_t);
result.first = 4;
result.second = 5;
if (0 != safewrite(fd, &result, nbytes)) {
fprintf(stderr, "Unable to write to " FILENAME ": %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
close(fd);
fd = -1;
/* Reopen and read from binary file */
fd = open(FILENAME, O_RDONLY);
nbytes = sizeof(prova_t);
if (0 != saferead(fd, &result, nbytes)) {
fprintf(stderr, "Unable to read file \"" FILENAME "\": %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
close(fd);
printf( "Read: %d %d (%#.02x%.02x)\n",
result.first, result.second,
result.first, result.second);
return EXIT_SUCCESS;
}
Now the data file contents look like:
00000000 04 00 00 00 05 00 00 00 |........|
00000008
Because the integers were specified as 32-bit integers (32-bits / 8 bits per byte = 4 bytes). I'm using a 64-bit system (little endian, x86), so I wanted to be explicit so the your results should match, assuming little-endian.
You tried to read to a struct containing two ints, by passing a pointer to some data and telling read that you had one int's worth of storage. The first should be
bytes_read = read(fd, &result, sizeof(prova_t));
From the name of your file, I assume that you are trying to read a text file. read from unistd.h reads from binary files. If you are indeed trying to read from a text file, you should use fscanf or in ifstream
To read a struct in binary:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef struct prova {
int first;
int second;
} prova_t;
int main(void) {
int fd;
prova_t result;
ssize_t bytes_read;
size_t nbytes;
prova_t initial;
// create a binary file
fd = open("file.bin", O_WRONLY | O_CREAT);
initial.first = 4;
initial.second = 5;
write(fd, &initial, sizeof(prova_t));
close(fp);
// read it back
fd = open("file.bin", O_RDONLY);
nbytes = sizeof(prova_t);
bytes_read = read(fd, &result, nbytes);
write(STDOUT_FILENO, &(result.first), sizeof(int));
write(STDOUT_FILENO, &(result.second), sizeof(int));
close(fp);
return 0;
}
Include flatbuffers/util.h, there are save and load functions sepeartely
SaveFile(const char *name, const char *buf, size_t len,
bool binary);
LoadFile(const char *name, bool binary, std::string *buf);

Resources