[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using FTPAPI or QSH



Sender: Scott Klement <sk@xxxxxxxxxxxxxxxx>


Personally, I'd use FTP_getraw() and save the data to the stream file manually. That way, you have complete control over where & how the data gets saved.


Here's an example that does that. It logs into FTP.freebsd.org and downloads all *.txt files in their /pub/FreeBSD/doc/newsletter directory.

It's kind-of a dumb example, since there are only two .txt files in that directory, and all they contain is the table of contents of some really old newsletters... but it does exactly what you're trying to do. It downloads both files and combines them into one.

Here's the code:

      *  To Compile:
      *         ADDLIBLE LIBFTP
      *         CRTBNDRPG PGM(BIGFILE) SRCFILE(mylib/QRPGLESRC)
      *
     H DFTACTGRP(*NO) BNDDIR('FTPAPI':'QC2LE')

      /copy FTPAPI_H
      /copy IFSIO_H

     D IfsError        PR
     D FtpError        PR

     D OutputFile      s           1000A
     D fd              s             10I 0 inz(-1)
     D ftp             s             10I 0 inz(-1)
     D errnum          s             10I 0
     D rc              s             10I 0
     D count           s             10I 0
     D x               s             10I 0
     D FileList        s            256A   dim(1000)

/free

             // *********************************************
             // * Get a file descriptor (fd) to an open IFS
             // * file.  We'll save everything we download
             // * into that file.
             // *********************************************

OutputFile = '/tmp/myfile.txt';

             fd = open( %trimr(OutputFile)
                      : O_WRONLY + O_TRUNC + O_CREAT
                      : S_IRWXU + S_IRWXG + S_IRWXO );
             if (fd < 0);
                IfsError();
                return;

endif;


// ********************************************* // Open a new FTP session tp ftp.freebsd.org // & log this session to the job log. // *********************************************

             ftp = ftp_open( 'ftp.freebsd.org' );
             if (ftp < 0);
                FtpError();
                return;
             endif;

ftp_logging(ftp: *ON);


// ********************************************* // Log in to FTP server, and switch to // the FreeBSD newsletter directory // *********************************************

             if  ( FTP_login( ftp
                            : 'anonymous'
                            : 'ftp@xxxxxxxxxxx' ) = -1 );
                FtpError();
                return;
             endif;

             if ( FTP_chdir(ftp: '/pub/FreeBSD/doc/newsletter')  = -1 );
                FtpError();
                return;
             endif;


// ********************************************* // Get a listing of .txt files in directory // *********************************************

             rc = FTP_list( ftp              // FTPAPI session handle
                          : '*.txt'          // Argument to NLIST
                          : %elem(FileList)  // Number of array elements
                          : %addr(FileList)  // Array
                          : count );         // Files Received

             if (rc < 0);
                  ftp_errorMsg(ftp: ErrNum);
                  if (ErrNum = FTP_NOFILE);
                     count = 0;
                  else;
                     FtpError();
                     return;
                  endif;
              endif;


// ********************************************* // Download all of the .TXT files // *********************************************

             for x = 1 to Count;
                FTP_getraw(ftp: FileList(x): fd: %paddr(write));
             endfor;


// ********************************************* // Done! BING! // ********************************************* callp close(fd); callp FTP_Quit(ftp);

             *inlr = *on;
      /end-free


*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * This sends an *ESCAPE message when unable to open * an IFS file. *++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P IfsError B D IfsError PI

     D get_errno       PR              *   ExtProc('__errno')
     D ptrToErrno      s               *
     D errno           s             10I 0 based(ptrToErrno)

     D QMHSNDPM        PR                  ExtPgm('QMHSNDPM')
     D   MessageID                    7A   Const
     D   QualMsgF                    20A   Const
     D   MsgData                      1A   Const
     D   MsgDtaLen                   10I 0 Const
     D   MsgType                     10A   Const
     D   CallStkEnt                  10A   Const
     D   CallStkCnt                  10I 0 Const
     D   MessageKey                   4A
     D   ErrorCode                 8192A   options(*varsize)

     D ErrorCode       DS                  qualified
     D  BytesProv              1      4I 0 inz(0)
     D  BytesAvail             5      8I 0 inz(0)

     D MsgKey          S              4A
     D MsgID           s              7A

/free

         if (fd >= 0);
            callp close(fd);
         endif;
         if (ftp >= 0);
            callp FTP_quit(ftp);
         endif;

         ptrToErrno = get_errno();
         MsgID = 'CPE' + %char(errno);

         QMHSNDPM( MsgID
                 : 'QCPFMSG   *LIBL'
                 : ' '
                 : 0
                 : '*ESCAPE'
                 : '*PGMBDY'
                 : 1
                 : MsgKey
                 : ErrorCode         );

      /end-free
     P                 E


*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * This sends an *ESCAPE message when unable to perform * an FTP operation *++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P FtpError B D FtpError PI

     D QMHSNDPM        PR                  ExtPgm('QMHSNDPM')
     D   MessageID                    7A   Const
     D   QualMsgF                    20A   Const
     D   MsgData                     80A   Const
     D   MsgDtaLen                   10I 0 Const
     D   MsgType                     10A   Const
     D   CallStkEnt                  10A   Const
     D   CallStkCnt                  10I 0 Const
     D   MessageKey                   4A
     D   ErrorCode                 8192A   options(*varsize)

     D ErrorCode       DS                  qualified
     D  BytesProv              1      4I 0 inz(0)
     D  BytesAvail             5      8I 0 inz(0)

     D MsgDta          s             80A
     D MsgKey          S              4A
      /free

MsgDta = FTP_errorMsg(ftp);

         if (fd >= 0);
            callp close(fd);
         endif;
         if (ftp >= 0);
            callp FTP_quit(ftp);
         endif;

         QMHSNDPM( 'CPF9897'
                 : 'QCPFMSG   *LIBL'
                 : MsgDta
                 : %len(%trimr(MsgDta))
                 : '*ESCAPE'
                 : '*PGMBDY'
                 : 1
                 : MsgKey
                 : ErrorCode         );
      /end-free
     P                 E

---
Scott Klement  http://www.scottklement.com


On Tue, 14 Mar 2006, Stephen Mooney wrote:


Greetings,

I currently have an FTP script which will mget *.txt files from a Linux
server and place them on my IFS drive.  I would like to copy those *.txt
files into one IFS file that can then be read by an already existing ILE
program to update physical files on our iSeries.

I have downloaded the FTPAPI code and have created the API on our iSeries.
Does anyone have an example of code using FTPAPI that I could use as a
reference for what I need to do, or, does anyone have an example of shell
script I can run to do the same?  Thanks.

If there is anything that is not consistent with your understanding,
please let me know immediately, thank you.
Regards,

-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubsribe from the list send mail
to majordomo@xxxxxxxxxxxxx with the body: unsubscribe ftpapi mymailaddr
-----------------------------------------------------------------------