HP-UX 11i and largefiles on rsync 2.6.2

Steve Bonds knnf6cy7w001 at sneakemail.com
Wed Jul 28 23:22:21 GMT 2004


Attached is some public domain source code that tests mkstemp() to see if
it functions properly by creating a 2GB sparse file in /tmp, first via the
normal open()/lseek()/write() pathway, then using
mkstemp()/lseek()/write().

Perhaps this could be migrated into an rsync test case?  My only concern
would be possibly filling up someone's filesystem if it didn't support
sparse files.

  -- Steve
-------------- next part --------------
/* Demonstration code that tests the mkstemp() system call
to see if it can safely create files larger than 2GB.

Steve Bonds
July 28 2004

no Copyright claimed
This code is released into the public domain

build with:

gcc -g -D_FILE_OFFSET_BITS=64 -o <output file> <this file>

or

cc -g -D_FILE_OFFSET_BITS=64 -o <output file> <this file>

*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#define BUFSIZE 1024

int main(void) {
  char mkstemp_template[2048] = "/tmp/mkstemp_XXXXXX\0";
  char static_template[2048] = "/tmp/mkstemp_static\0";
  off_t file_offset;
  long long twogigs = 2147483648;
  int fd;
  int i;

  /* How many of the below buffers to write out after seeking to 2GB */
  int buffers_to_write = 10;

  /* Buffer that will contain zeros for writing out to disk */
  void *buffer;

  /* Allocate some memory for our buffer and fill it with known data */
  if ( (buffer = malloc(BUFSIZE)) == 0) {
    printf("Problem malloc()ing %d bytes of memory for a buffer: %s\n",
	   (int)BUFSIZE, strerror(errno));
  }
  (void) memset( buffer, 1, BUFSIZE);

  /* Test that a normally created file can exceed 2GB */

  printf("Testing normally opened file.\n");
       
  if ( (fd = open(static_template,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR)) < 0) {
    printf("Problem opening normal file %s: %s\n",
	   static_template,strerror(errno));
    exit(1);
  }
  printf("  open(%s,O_CREAT|O_EXCL|O_WRONLY) = %d\n",
	 static_template,fd);

  if ( (file_offset = lseek(fd,twogigs,SEEK_SET)) < 0 ) {
    printf("Problem seeking to %lld in %s: %s\n",
	   (long long)twogigs,static_template,strerror(errno));
    exit(1);
  }
  printf("  lseek(%d,%lld,SEEK_SET) = %lld\n",
	 fd,(long long)twogigs,(long long)file_offset);

  for (i=0;i<buffers_to_write;i++) {
    if (write(fd,buffer,sizeof(buffer)) < 0) {
      printf("Problem writing data to %s: %s\n",
	     static_template,strerror(errno));
      exit(1);
    }
  }

  printf("Normally opened file writes OK\n");
  close(fd);

  /* Create a temporary file using mkstemp() */
  printf("Creating a temporary file based on %s using mkstemp()\n",
	 mkstemp_template);
  if ( (fd = mkstemp(mkstemp_template)) < 0) {
    printf("Problem creating file based on template %s: %s\n",
	   mkstemp_template, strerror(errno));
    exit(1);
  }
  printf("  Temp file created, fd = %d, filename = '%s'\n",
	 fd,mkstemp_template);

  /* Seek to 2GB mark creating a sparse file in the process */
  if ( (file_offset = lseek(fd,twogigs,SEEK_SET)) < 0 ) {
    printf("Problem seeking to %lld in %s: %s\n",
	   (long long)twogigs,mkstemp_template,strerror(errno));
    exit(1);
  }
  printf("  lseek(%d,%lld,SEEK_SET) = %lld\n",
	 fd,(long long)twogigs,(long long)file_offset);

  for (i=0;i<buffers_to_write;i++) {
    if (write(fd,buffer,sizeof(buffer)) < 0) {
      printf("Problem writing data to %s: %s\n",
	     mkstemp_template,strerror(errno));
      exit(1);
    }
  }

  printf("All writes OK, you may wish to remove the temp file '%s'\n",
	 mkstemp_template);
  close(fd);

  printf("Remember to remove the two sparse files that were created:\n");
  printf("  %s\n",static_template);
  printf("  %s\n",mkstemp_template);

  exit(0);
}


More information about the rsync mailing list