Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubles with using memcpy with mmap

Tags:

c

linux

Trying to copy a file using these functions, everything goes fine until program hits the memcpy function which gives a bus error and terminates the process.

void copy_mmap(char* in, char* out){

int input_fd, output_fd;

input_fd = open (in, O_RDONLY);
if (input_fd == -1) {
        printf("Error opening input file.\n");
        exit(2);
}

output_fd = open(out, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
if(output_fd == -1){
    printf("Error opening output file.\n");
    exit(3);
}

struct stat st;
fstat(input_fd, &st);

char* target;
target=mmap(0, st.st_size+1, PROT_READ, MAP_SHARED, input_fd, 0);
if (target==(void*) -1){
    printf("Error mapping target with errno: %d.\n", errno);
    exit(6);
}


char* destination;
destination=mmap(0, st.st_size+1, PROT_READ | PROT_WRITE, MAP_SHARED, output_fd, 0);
if (destination==(void*) -1){
    printf("Error mapping destination with errno: %d.\n", errno);
    exit(5);
}

memcpy(destination, target, st.st_size);
munmap(destination, st.st_size);



}

Failed to figure out what is wrong, as "Bus Error" isn't a descriptive error message and there isn't any much material on the internet regarding this problem.

like image 794
cngkaygusuz Avatar asked Jan 09 '13 16:01

cngkaygusuz


2 Answers

When you create the destination file as a new file, its size is 0 bytes. memcpy crashes because it tries to write data beyond the end of the file.

You can make this work by pre-sizing the destination file to the size of the source file (using ftruncate()) before you mmap() it.

Also, you should pass st.st_size as the second argument to mmap, not st.st_size+1. st.st_size+1 tries to map a range that is larger than the size of the file, which is invalid.

like image 71
Celada Avatar answered Oct 01 '22 20:10

Celada


you could also lseek to off_t fileposition= lseek(output_fd, st.st_size-1, SEEK_SET) and write an empty character to output_fd.

like image 36
Rakesh Avatar answered Oct 01 '22 21:10

Rakesh