GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



next up previous contents
Next: Wow Your Friends with Up: Some Useful Programs Previous: Some Useful Programs   Contents

A Fancy-Pants cp

The program cp is one of the most useful, yet extremely simple, programs that the fingers of a Unix user has access to. In it most simple form it is used to copy a file to a new location. For example, to copy the file source to the location destination the program cp could be used as follows

% cp source destination

Here we will create a ``grid enabled'' version of this bread and butter of the Unix world. The full program is as follows

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

int main( int argc, char *argv[] )
{
  GATResult result;
  GATFile sourceFile;
  GATContext context;
  GATLocation sourceLocation;
  GATLocation destinationLocation;
  
  /* 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 GATFile sourceFile */
        sourceFile = GATFile_Create( context, sourceLocation, NULL );
        
        /* Check GATFile creation */
        if( NULL != sourceFile )
        {
          /* Copy sourceFile to destinationLocation */
          result = GATFile_Copy( sourceFile, destinationLocation, GATFileMode_Overwrite );
          
          /* Destroy GATFile sourceFile */
          GATFile_Destroy( &sourceFile );
        }
        
        /* 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;
}

This entire program contains no novel code, all of its functions have been previously examined; so, we will not examine it line by line.



Andre Merzky 2004-05-13