Novell Home

How to copy a file without decompressing it

From Developer Community

This shows how to copy a file that the NetWare filesystem has compressed, from one location to another w/o making the FileSystem Engine decompress the file. uses: NWSetCompressedFileLengths, FEsopen, NWGetCompressedFileLengths, close

Sample Code

#include <errno.h>
#include <fcntl.h>
#include <nwerrno.h>
#include <nwfattr.h>
#include <nwfileng.h>
#include <nwfinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int main( int argc, char* argv[]) {
  int  rcode;
  int  handle_in = (-1);
  int  handle_out = (-1);
  LONG uncompressedLength;
  LONG compressedLength;
  long bytes_read;
  char buffer[ 1024];
  if ( argc != 3) {
    printf( "Usage: %s <in_file> <out_file>\n", argv[ 0]);
    rcode = EXIT_FAILURE;
	goto ERR;
  }

  handle_in = FEsopen( argv[ 1], O_RDONLY, SH_DENYWR, 0, ENABLE_IO_ON_COMPRESSED_DATA_BIT|LEAVE_FILE_COMPRESSED_DATA_BIT, PrimaryDataStream);
  if ( handle_in == -1) {
    printf( "FEsopen failed, errno = %d (%s), NetWareErrno = %d\n", errno, strerror( errno), NetWareErrno);
    rcode = EXIT_FAILURE;
	goto ERR;
  }

  if ( NWGetCompressedFileLengths( handle_in, &uncompressedLength, &compressedLength) != 0) {
    printf( "NWGetCompressedFileLengths failed, errno = %d (%s), NetWareErrno = %d\n", errno, strerror( errno), NetWareErrno);
    rcode = EXIT_FAILURE;
	goto ERR;
  }

  printf( "uncompressedLength = %u, compressedLength = %u\n", uncompressedLength, compressedLength);
  handle_out = FEsopen( argv[ 2], O_CREAT|O_TRUNC, SH_DENYRW, 0, DELETE_FILE_ON_CREATE_BIT|ENABLE_IO_ON_COMPRESSED_DATA_BIT|LEAVE_FILE_COMPRESSED_DATA_BIT, PrimaryDataStream);
  if ( handle_out == -1) {
    printf( "FEsopen failed, errno = %d (%s), NetWareErrno = %d\n", errno, strerror( errno), NetWareErrno);
    rcode = EXIT_FAILURE;
	goto ERR;
  }

  while (( bytes_read = read( handle_in, buffer, sizeof( buffer))) > 0) {
    if ( write( handle_out, buffer, ( LONG) bytes_read) != bytes_read) {
      printf( "write failed, errno = %d (%s), NetWareErrno = %d\n", errno, strerror( errno), NetWareErrno);
	  rcode = EXIT_FAILURE;
	  goto ERR;
    }
  }

  if ( NWSetCompressedFileLengths( handle_out, uncompressedLength, compressedLength) != 0) {
    printf( "NWSetCompressedFileLengths failed, errno = %d (%s), NetWareErrno = %d\n", errno, strerror( errno), NetWareErrno);
    rcode = EXIT_FAILURE;
	goto ERR;
  }

ERR:
  if((-1) != handle_out)
  	close( handle_out);
  if((-1) != handle_in)
  	close( handle_in);
  pressanykey();
  return rcode;
}

--Benjamin Fjeldsted

Novell® Making IT Work As One

© 2009 Novell, Inc. All Rights Reserved.