Main Page Class Hierarchy Compound List File List Compound Members File Members

# copyfile.c

Go to the documentation of this file.

    00001
    00002 /*
    00003  *   THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE
    00004  *   CHECKED IT OUT USING THE COMMAND CHECKOUT.
    00005  *
    00006  *    $Id: copyfile_8c-source.html 2161 2006-05-19 16:55:03Z paulf $
    00007  *
    00008  *    Revision history:
    00009  *     $Log$
    00009  *     Revision 1.1  2006/05/19 16:55:01  paulf
    00009  *     first inclusion
    00009  *
    00010  *     Revision 1.1  2000/02/14 18:53:30  lucky
    00011  *     Initial revision
    00012  *
    00013  *
    00014  */
    00015
    00016    /***********************************************************************
    00017     *                               copyfile.c                            *
    00018     *                           Windows NT version                        *
    00019     *                                                                     *
    00020     *  Copies a file from local machine to a remote machine using rcp.    *
    00021     *                                                                     *
    00022     *  For this function to work, make sure that the following files are  *
    00023     *  set up properly on the remote machine:                             *
    00024     *    /etc/hosts         must contain address and localhostname        *
    00025     *    /etc/hosts.equiv   must contain local_hostname                   *
    00026     *    .rhosts            in <userid>'s home directory must contain a   *
    00027     *                       line: local_hostname local_username           *
    00028     *                       describing who is running this program.       *
    00029     *                                                                     *
    00030     *  Also make sure that entries for the remote host are in the         *
    00031     *  local machine's \winnt\system32\drivers\etc\hosts file.            *
    00032     *                                                                     *
    00033     *  Returns 0 if all is ok                                             *
    00034     *          1 if error creating the child process                      *
    00035     *          2 if error waiting for the child process to complete       *
    00036     *          3 if the child process ended abnormally                    *
    00037     ***********************************************************************/
    00038
    00039 #include <stdio.h>
    00040 #include <string.h>
    00041 #include <windows.h>
    00042 #include <earthworm.h>
    00043
    00044
    00045 int copyfile( char *fname,         /* Name of file to copy                 */
    00046               char *tname,         /* Temporary remote file name           */
    00047               char *host,          /* Remote machine to copy file to       */
    00048               char *dir,           /* Directory on remote machine          */
    00049               char *userid,        /* Use this user name on remote machine */
    00050               char *passwd,        /* Userid's password on remote machine  */
    00051               char *errtxt )       /* String to return error message in    */
    00052
    00053
    00054 {
    00055    char        rcpPath[175];       /* Copy the file to this path           */
    00056    char        tmpName[100];       /* Temporary file name                  */
    00057    char        finalName[100];     /* Final name for the copied file       */
    00058    char        commandLine[100];   /* Command that invokes the child proc  */
    00059    DWORD       display;
    00060    BOOL        success;
    00061    DWORD       priorityClass;
    00062    DWORD       rc;
    00063    DWORD       exitCode;
    00064
    00065    STARTUPINFO         startUpInfo;
    00066    PROCESS_INFORMATION procInfo;
    00067
    00068 /* Build temporary and final path names on the target system
    00069    *********************************************************/
    00070    if ( dir[strlen(dir)-1] == '/' )
    00071    {
    00072       sprintf( rcpPath,  "%s.%s:%s%s",  host, userid, dir, tname );
    00073       sprintf( tmpName,  "%s%s",        dir, tname );
    00074       sprintf( finalName, "%s%s",       dir, fname );
    00075    }
    00076    else
    00077    {
    00078       sprintf( rcpPath,  "%s.%s:%s/%s", host, userid, dir, tname );
    00079       sprintf( tmpName,  "%s/%s",       dir, tname );
    00080       sprintf( finalName, "%s/%s",      dir, fname );
    00081    }
    00082
    00083 /* Retrieve the STARTUPINFOR structure for the current process.
    00084    Use this structure for the child processes.
    00085    ***********************************************************/
    00086    GetStartupInfo( &startUpInfo );
    00087
    00088 /* Copy the file using rcp
    00089    ***********************/
    00090    display = 0;                                /* Do not create a new console window */
    00091
    00092    priorityClass = GetPriorityClass( GetCurrentProcess() );
    00093
    00094    sprintf( commandLine, "rcp %s %s", fname, rcpPath );
    00095
    00096    success = CreateProcess( 0,
    00097                             commandLine,             /* Command line to invoke child */
    00098                             0, 0,                          /* No security attributes */
    00099                             FALSE,                           /* No inherited handles */
    00100                             display |                 /* Child does not get a window */
    00101                             priorityClass,             /* Same as priority of parent */
    00102                             0,                     /* Not passing environmental vars */
    00103                             0,                         /* Current dir same as parent */
    00104                             &startUpInfo,            /* Attributes of process window */
    00105                             &procInfo );              /* Attributes of child process */
    00106    if ( !success )
    00107    {
    00108       sprintf( errtxt, "Error starting the rcp command: %d", GetLastError() );
    00109       return 1;
    00110    }
    00111
    00112 /* Wait 2 minutes for the remote copy to complete.
    00113    Check the process exit code for abnormal termination.
    00114    ****************************************************/
    00115    rc = WaitForSingleObject( procInfo.hProcess, 120000 );
    00116
    00117    if ( rc == WAIT_FAILED )
    00118    {
    00119       sprintf( errtxt, "rcp WaitForSingleObject() failed with error: %d", GetLastError() );
    00120       return 2;
    00121    }
    00122    if ( rc == WAIT_TIMEOUT )
    00123    {
    00124       sprintf( errtxt, "Error. Remote copy not completed within 2 minutes." );
    00125       return 2;
    00126    }
    00127    success = GetExitCodeProcess( procInfo.hProcess, &exitCode );
    00128    if ( !success )
    00129    {
    00130       sprintf( errtxt, "Error getting the rcp exit code: %d", GetLastError() );
    00131       return 3;
    00132    }
    00133    if ( exitCode != 0 )
    00134    {
    00135       strcpy( errtxt, "Remote copy failed." );
    00136       return 3;
    00137    }
    00138
    00139 /* Close the process and thread handles for rcp
    00140    ********************************************/
    00141    success = CloseHandle( procInfo.hProcess );
    00142    if ( !success )
    00143    {
    00144       sprintf( errtxt, "Error closing the rcp process handle: %d", GetLastError() );
    00145       return 3;
    00146    }
    00147    success = CloseHandle( procInfo.hThread );
    00148    if ( !success )
    00149    {
    00150       sprintf( errtxt, "Error closing the rcp thread handle: %d", GetLastError() );
    00151       return 3;
    00152    }
    00153
    00154 /* Rename the remote file using rsh
    00155    ********************************/
    00156    sprintf( commandLine, "rsh %s -l %s /usr/bin/mv %s %s",
    00157                           host, userid, tmpName, finalName );
    00158
    00159    success = CreateProcess( 0,
    00160                             commandLine,             /* Command line to invoke child */
    00161                             0, 0,                          /* No security attributes */
    00162                             FALSE,                           /* No inherited handles */
    00163                             display |                 /* Child does not get a window */
    00164                             priorityClass,             /* Same as priority of parent */
    00165                             0,                     /* Not passing environmental vars */
    00166                             0,                         /* Current dir same as parent */
    00167                             &startUpInfo,            /* Attributes of process window */
    00168                             &procInfo );              /* Attributes of child process */
    00169    if ( !success )
    00170    {
    00171       sprintf( errtxt, "Error starting the rsh command: %d", GetLastError() );
    00172       return 1;
    00173    }
    00174
    00175 /* Wait 2 minutes for the remote move command to complete.
    00176    Check the process exit code for abnormal termination.
    00177    ******************************************************/
    00178    rc = WaitForSingleObject( procInfo.hProcess, 120000 );
    00179
    00180    if ( rc == WAIT_FAILED )
    00181    {
    00182       sprintf( errtxt, "rsh WaitForSingleObject() failed with error: %d", GetLastError() );
    00183       return 2;
    00184    }
    00185    if ( rc == WAIT_TIMEOUT )
    00186    {
    00187       sprintf( errtxt, "Error. Remote mv command not completed within 2 minutes." );
    00188       return 2;
    00189    }
    00190    success = GetExitCodeProcess( procInfo.hProcess, &exitCode );
    00191    if ( !success )
    00192    {
    00193       sprintf( errtxt, "Error getting the rsh exit code: %d", GetLastError() );
    00194       return 3;
    00195    }
    00196    if ( exitCode != 0 )
    00197    {
    00198       strcpy( errtxt, "Remote mv command failed." );
    00199       return 3;
    00200    }
    00201
    00202 /* Close the process and thread handles for rsh
    00203    ********************************************/
    00204    success = CloseHandle( procInfo.hProcess );
    00205    if ( !success )
    00206    {
    00207       sprintf( errtxt, "Error closing the rsh process handle: %d", GetLastError() );
    00208       return 3;
    00209    }
    00210    success = CloseHandle( procInfo.hThread );
    00211    if ( !success )
    00212    {
    00213       sprintf( errtxt, "Error closing the rsh thread handle: %d", GetLastError() );
    00214       return 3;
    00215    }
    00216
    00217 /* Everything went smoothly
    00218    ************************/
    00219    return 0;
    00220 }