Novell Home

NSS09

From Developer Community

This code verifies the correctness of files created with sample NSS07 (NSS APIs, NetWare). Useful for testing backup applications.

Sample Code

#include <stdio.h>
#include <stdlib.h>
#include <zPublics.h>

static void print64u( unsigned __int64 x) {
  unsigned __int64 quot = x / 1000;
  if ( quot != 0) {
    print64u( quot);
    printf( ".%03u", ( unsigned int)( x % 1000));
  }
  else {
    printf( "%u", ( unsigned int)( x % 1000));
  }
}
int main( int argc, char* argv[]) {
  Key_t   root_key;
  NINT    task_id;
  Key_t   file_key;
  zInfo_s info;
  STATUS  ccode;
  size_t  i;
  if ( argc != 2) {
    printf( "Usage: %s <filename>\n", argv[ 0]);
    return EXIT_FAILURE;
  }
  if (( ccode = zRootKey( 0, &root_key)) != zOK) {
    printf( "zRootKey failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  if (( ccode = zBeginTask( root_key, 0, &task_id)) != zOK) {
    zClose( root_key);
    printf( "zBeginTask failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  if (( ccode = zOpen( root_key, task_id, zNSPACE_LONG|zMODE_UTF8, argv[ 1], zRR_READ_ACCESS|zRR_DENY_WRITE, &file_key)) != zOK) {
    zEndTask( root_key, task_id);
    zClose( root_key);
    printf( "zOpen failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  /* verify the file kength */
  if (( ccode = zGetInfo( file_key, zGET_STD_INFO, sizeof( info), zINFO_VERSION_A, &info)) != zOK) {
    zClose( file_key);
    zEndTask( root_key, task_id);
    zClose( root_key);
    printf( "zGetInfo failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  printf( "file length = ");
  print64u( info.std.logicalEOF);
  puts( "");
  if ( info.std.logicalEOF != 5 * 1024 * 1024 * ( unsigned __int64) 1024) {
    zClose( file_key);
    zEndTask( root_key, task_id);
    zClose( root_key);
    puts( "file size != 5G");
    return EXIT_FAILURE;
  }
  /* verify the data */
  for ( i = 0; i < 5 * 1024 * 1024; ++i) {
    char   buffer[ 1024];
    NINT   bytes_read;
    size_t j;
    if (( ccode = zRead( file_key, zNILXID, i * ( unsigned __int64) sizeof( buffer), sizeof( buffer), buffer, &bytes_read)) != zOK) {
      zClose( file_key);
      zEndTask( root_key, task_id);
      zClose( root_key);
      printf( "zRead failed, ccode = %d\n", ccode);
      return EXIT_FAILURE;
    }
    if (( i + 1) % 1024 == 0) {
      printf( "Read %uM\n", ( i + 1)/ 1024);
    }
    if ( bytes_read != sizeof( buffer)) {
      zClose( file_key);
      zEndTask( root_key, task_id);
      zClose( root_key);
      printf( "bytes_read != sizeof( buffer), %u %u\n", bytes_read, sizeof( buffer));
      return EXIT_FAILURE;
    }
    if ( buffer[ 0] != ( char) ( i / ( 1024 * 1024))) {
      zClose( file_key);
      zEndTask( root_key, task_id);
      zClose( root_key);
      puts( "Unexpected data (1)");
      return EXIT_FAILURE;
    }
    for ( j = 1; j < sizeof( buffer); ++j) {
      if ( buffer[ j] != ( char) j) break;
    }
    if ( j != sizeof( buffer)) {
      zClose( file_key);
      zEndTask( root_key, task_id);
      zClose( root_key);
      puts( "Unexpected data (2)");
      return EXIT_FAILURE;
    }
  }
  if (( ccode = zClose( file_key)) != zOK) {
    zEndTask( root_key, task_id);
    zClose( root_key);
    printf( "zClose failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  if (( ccode = zEndTask( root_key, task_id)) != zOK) {
    zClose( root_key);
    printf( "zEndTask failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  if (( ccode = zClose( root_key)) != zOK) {
    printf( "zClose failed, ccode = %d\n", ccode);
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.