CVS: rdesktop disk.c,1.35,1.36

Peter Åstrand <[email protected]>
Newsgroups gmane.network.rdesktop.cvs
Message-ID <[email protected]>
Update of /cvsroot/rdesktop/rdesktop
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18894

Modified Files:
	disk.c 
Log Message:
Implemented fix for the problem with ftruncate on FAT fs, which
doesn't support growing files. Should solve problems with MS Office
and FAT disks.


Index: disk.c
===================================================================
RCS file: /cvsroot/rdesktop/rdesktop/disk.c,v
retrieving revision 1.35
retrieving revision 1.36
diff -C2 -d -r1.35 -r1.36
*** disk.c	15 Oct 2004 18:02:45 -0000	1.35
--- disk.c	21 Oct 2004 08:10:04 -0000	1.36
***************
*** 133,136 ****
--- 133,195 ----
  }
  
+ /* A wrapper for ftruncate which supports growing files, even if the
+    native ftruncate doesn't. This is needed on Linux FAT filesystems,
+    for example. */
+ static int
+ ftruncate_growable(int fd, off_t length)
+ {
+ 	int ret;
+ 	off_t pos;
+ 	static const char zero;
+ 
+ 	/* Try the simple method first */
+ 	if ((ret = ftruncate(fd, length)) != -1)
+ 	{
+ 		return ret;
+ 	}
+ 
+ 	/* 
+ 	 * Some kind of error. Perhaps we were trying to grow. Retry
+ 	 * in a safe way.
+ 	 */
+ 
+ 	/* Get current position */
+ 	if ((pos = lseek(fd, 0, SEEK_CUR)) == -1)
+ 	{
+ 		perror("lseek");
+ 		return -1;
+ 	}
+ 
+ 	/* Seek to new size */
+ 	if (lseek(fd, length, SEEK_SET) == -1)
+ 	{
+ 		perror("lseek");
+ 		return -1;
+ 	}
+ 
+ 	/* Write a zero */
+ 	if (write(fd, &zero, 1) == -1)
+ 	{
+ 		perror("write");
+ 		return -1;
+ 	}
+ 
+ 	/* Truncate. This shouldn't fail. */
+ 	if (ftruncate(fd, length) == -1)
+ 	{
+ 		perror("ftruncate");
+ 		return -1;
+ 	}
+ 
+ 	/* Restore position */
+ 	if (lseek(fd, pos, SEEK_SET) == -1)
+ 	{
+ 		perror("lseek");
+ 		return -1;
+ 	}
+ 
+ 	return 0;
+ }
+ 
  
  /* Enumeration of devices from rdesktop.c        */
***************
*** 659,667 ****
  					return STATUS_DISK_FULL;
  
! 			/* FIXME: Growing file with ftruncate doesn't
! 			   work with Linux FAT fs */
! 			if (ftruncate(handle, length) != 0)
  			{
- 				perror("ftruncate");
  				return STATUS_DISK_FULL;
  			}
--- 718,723 ----
  					return STATUS_DISK_FULL;
  
! 			if (ftruncate_growable(handle, length) != 0)
  			{
  				return STATUS_DISK_FULL;
  			}



-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.