GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



next up previous contents
Next: gridhexdump, theme and variation Up: Some Useful Programs Previous: From hexdump to gridhexdump   Contents

A cp from the ground up

Next we will revisit, with a slight variation, the theme of cp. Previously we had written a ``grid enabled'' cp which used the class GATFile to copy a file from point A to point B. In this next example we will rewrite this program to using GATFileStream instead of the class GATFile to get the job done. This process if involve copying, byte by byte, the entire source file to the destination. Here's the code

#include <stdio.h>
#include "GAT.h"

int main( int argc, char *argv[] )
{
  char buffer[32];
  GATResult result;
  GATuint32 readBytes;
  GATContext context;
  GATuint32 writtenBytes;
  GATLocation sourceLocation;
  GATFileStream sourceFileStream;
  GATLocation destinationLocation;
  GATFileStream destinationFileStream;
  GATObject sourceFileStreamObject;
  GATObject destinationFileStreamObject;

  
  /* Check command line syntax */
  if( 3 != argc )
  {
     printf("usage: %s source destination\n", argv[0]);
     
     return 1;
  }
  
  /* Set result to a memory failure */
  result = GAT_MEMORYFAILURE;
  
  /* Create GATLocation sourceLocation */
  sourceLocation = GATLocation_Create( argv[1] );
  
  /* Check previous GATLocation creation */
  if( NULL != sourceLocation )
  {
    /* Create GATLocation destinationLocation */
    destinationLocation = GATLocation_Create( argv[2] );
    
    /* Check previous GATLocation creation */
    if( NULL != destinationLocation )
    {
      /* Create GATContext context */
      context = GATContext_Create();
      
      /* Check previous GATContext creation */
      if( NULL != context )
      {
        /* Create GATFileStream sourceFileStream */
        sourceFileStream = GATFileStream_Create( context, 
                                                 NULL, 
                                                 sourceLocation, 
                                                 GATFileStreamMode_Read );
        
        /* Check GATFileStream creation */
        if( NULL != sourceFileStream )
        {
          /* Create GATFileStream destinationFileStream */
          destinationFileStream = GATFileStream_Create( context, 
                                                        NULL, 
                                                        destinationLocation, 
                                                        GATFileStreamMode_Write );
          
          /* Check GATFileStream creation */
          if( NULL != destinationFileStream )
          {
            /* Convert  sourceFileStream to a GATObject */
            sourceFileStreamObject = 
              GATFileStream_ToGATObject( sourceFileStream );
            
            /* Convert  destinationFileStream to a GATObject */
            destinationFileStreamObject = 
              GATFileStream_ToGATObject( destinationFileStream );
            
            /* Read from sourceFileStreamObject */
            result = 
              GATStreamable_Read( sourceFileStreamObject, 
                                  (void *) buffer, 
                                  32, 
                                  &readBytes );
            
            /* Loop until done */
            while( GAT_SUCCEEDED( result ) )
            {
              /* Write to destinationFileStreamObject */
              result = 
                GATStreamable_Write( destinationFileStreamObject, 
                                     (void *) buffer, 
                                     readBytes, 
                                     &writtenBytes );
              
              /* Read from sourceFileStreamObject */
              if( GAT_SUCCEEDED( result ) )
              {
                result = 
                  GATStreamable_Read( sourceFileStreamObject, 
                                      (void *) buffer, 
                                      32, 
                                      &readBytes );
              }
            }
            
            /* Destroy GATFileStream destinationFileStream */
            GATFileStream_Destroy( &destinationFileStream );
          }
          
          /* Destroy GATFileStream sourceFile */
          GATFileStream_Destroy( &sourceFileStream );
        }
        
        /* Destroy GATContext context */
        GATContext_Destroy( &context );
      }
      
      /* Destroy GATLocation destinationLocation */
      GATLocation_Destroy( &destinationLocation );
    }
    
    /* Destroy GATLocation sourceLocation */
    GATLocation_Destroy( &sourceLocation );
  }
  
  /* Check result for success and print error */
  if( GAT_FAILED( result) )
  {
    printf( "An error has occurred during the copy operation\n");
    
    return 1;
  }
  
  return 0;
}

We will not belabor the the code above by reviewing it line by line, take it as a homework assignment.



Andre Merzky 2004-05-13