Hello everyone,
I tried using mmap to copy very large files in the following way :
Code:
if((fps = open(source, O_RDONLY)) == -1)
printf("error : can't open source file for reading\n");
if((fpd = open(dest, O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1)
printf("error : cant open destination file for writing\n");
printf("\nsource = %s\n",source);
fstat(fps,&statbuf);
filesize=statbuf.st_size;
lseek(fpd,filesize-1,SEEK_SET);
write(fpd,"",1);
lseek(fpd,0,SEEK_SET);
bytes=2097152;
while(filesize > 0)
{
if(filesize < bytes)
{
bytes=filesize;
filesize=0;
}
else
filesize-=bytes;
if((src=mmap((caddr_t)0,bytes,PROT_READ,MAP_SHARED,fps,0))==MAP_FAILED)
{
printf("mmap error : fps\n");
exit(1);
}
if((dst=mmap((caddr_t)0,bytes,PROT_READ | PROT_WRITE,MAP_SHARED,fpd,0))==MAP_FAILED)
{
printf("mmap error : fpd\n");
exit(1);
}
memcpy(dst,src,bytes);
if((munmap(src,bytes))==-1)
{
printf("munmap error : src\n");
exit(1);
}
if((munmap(dst,bytes)==-1))
{
printf("munmap error : dst\n");
exit(1);
}
lseek(fps,bytes,SEEK_SET);
lseek(fpd,bytes,SEEK_SET);
}
I used the variables bytes instead of the filesize for mmap as I guess mapping wont work for very large files.This is working for text files but not for binary files.
The problem with copying binary files especially video files using mmap is that the destination file is broken (as reported by VLC media player).How can I solve this??