LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2008, 11:27 AM   #1
rohanak
Member
 
Registered: Aug 2007
Posts: 38

Rep: Reputation: 15
using mmap for copying large files


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);
		//while((bytes = read(fps, buffer, BSIZE)) > 0)
		//	write(fpd, buffer, bytes);
		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.Can anyone suggest changes?
 
Old 05-12-2008, 11:42 AM   #2
rohanak
Member
 
Registered: Aug 2007
Posts: 38

Original Poster
Rep: Reputation: 15
Hello again,

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??
 
Old 05-13-2008, 08:55 AM   #3
rohanak
Member
 
Registered: Aug 2007
Posts: 38

Original Poster
Rep: Reputation: 15
I changed the code to this :

Code:
#define BSIZE 1048576
.......................

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");
		
		fstat(fps,&statbuf);
		filesize=statbuf.st_size;
		lseek(fpd,filesize-1,SEEK_SET);
		
		write(fpd,"",1);
		bytes=BSIZE;
		offset=0;
		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,offset))==MAP_FAILED)
			{	
				printf("mmap error : fps\n");
				exit(1);
			}
			if((dst=mmap((caddr_t)0,bytes,PROT_READ | PROT_WRITE,MAP_SHARED,fpd,offset))==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);
			}
			offset=offset+bytes;
		}
This works fine for all kinds of files...However it is much slower than read/write.
First I used mmap to copy all files in a directory totalling to 3GB.It took 5m48s while a read/write took only 4m48s.
 
Old 05-15-2008, 07:15 PM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
what's the loop for?

you sure it's not looping over and over?
the mmap doesn't need a loop.
 
Old 05-18-2008, 06:28 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
mmap does not save you anything; it costs more memory. If you are copying one file to one destination.
If you are copying 1 file to multiple destinations then it makes sense. Otherwise just fopen(), loop{ fread() fwrite() }, then fclose().
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying large files to FAT32 sketch7 Linux - General 11 10-22-2007 10:13 AM
NFS Trouble on copying large files (400Mb+) Yalla-One Slackware 2 03-17-2007 08:29 AM
Copying a large quantity of files (1.5Million) to a new disc matonb Linux - Enterprise 2 08-21-2006 02:08 AM
Samba hangs up at copying many large files ogcu Linux - Networking 1 05-19-2004 01:29 AM
Error copying large files from cdrom patrickyeo Linux - Hardware 1 11-03-2002 06:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration