Develop and Download Open Source Software

Browse Subversion Repository

Diff of /trunk/ftp_get_buffer.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 30 by hirohitohigashi, Sat Feb 28 13:03:02 2009 UTC revision 38 by hirohitohigashi, Sun Mar 1 15:51:51 2009 UTC
# Line 41  Line 41 
41   *@param        fname   サーバ上のファイル名   *@param        fname   サーバ上のファイル名
42   *@param        buf     バッファへのポインタ   *@param        buf     バッファへのポインタ
43   *@param        bufsiz  バッファサイズ   *@param        bufsiz  バッファサイズ
44   *@retval       int     エラーコード   *@retval       int     取得したバイト数 マイナス値ならエラーコード
45   *@note   *@note
46   */   */
47  int ftp_get_buffer( LIBOFTP *ftp, const char *fname, char *buf, int bufsiz )  int ftp_get_buffer( LIBOFTP *ftp, const char *fname, char *buf, int bufsiz )
48  {  {
49      int data_socket;      int data_socket;
50      char *p = buf;      char *p = buf;
51      int n, res;      int res;
52    
53      /*      /*
54       * 受信準備       * 受信準備
# Line 66  int ftp_get_buffer( LIBOFTP *ftp, const Line 66  int ftp_get_buffer( LIBOFTP *ftp, const
66       * タイムアウトが意図通りに働くように、分割してrecvする。       * タイムアウトが意図通りに働くように、分割してrecvする。
67       */       */
68      while( 1 ) {      while( 1 ) {
69            int n;
70          int len = bufsiz;          int len = bufsiz;
71          if( len > TRANSFER_SEGMENT_SIZE ) {          if( len > TRANSFER_SEGMENT_SIZE ) {
72              len = TRANSFER_SEGMENT_SIZE;              len = TRANSFER_SEGMENT_SIZE;
# Line 73  int ftp_get_buffer( LIBOFTP *ftp, const Line 74  int ftp_get_buffer( LIBOFTP *ftp, const
74          n = recv( data_socket, p, len, 0 );          n = recv( data_socket, p, len, 0 );
75          DEBUGPRINT1( "RECV: n=%d\n", n );          DEBUGPRINT1( "RECV: n=%d\n", n );
76          if( n < 0 ) {          if( n < 0 ) {
77                int ret;
78              DEBUGPRINT1( "recv error. %s\n", strerror(errno) );              DEBUGPRINT1( "recv error. %s\n", strerror(errno) );
79              copy_strerror();              if( errno == EAGAIN ) {
80                    ret = LIBOFTP_ERROR_TIMEOUT;
81                } else {
82                    ret = LIBOFTP_ERROR_OS;
83                    copy_strerror();
84                }
85              close( data_socket );              close( data_socket );
86              return LIBOFTP_ERROR_OS;              return ret;
87          }          }
88          if( n == 0 ) {          if( n == 0 ) {
89              break;              break;
# Line 84  int ftp_get_buffer( LIBOFTP *ftp, const Line 91  int ftp_get_buffer( LIBOFTP *ftp, const
91          p += n;          p += n;
92          bufsiz -= n;          bufsiz -= n;
93          if( bufsiz <= 0 ) {          if( bufsiz <= 0 ) {
94              break;              break;              /* buffer too small */
95          }          }
96      }      }
     close( data_socket );  
97    
98      /*      if( bufsiz > 0 ) {          /* バッファ不足だったか? */
99       * receive response.          /*
100       */           * 不足していない
101      res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );           */
102      if( res != 226 ) {                                          /* 226: Closing data connection. */          close( data_socket );
         DEBUGPRINT1( "got illegal response %d\n", res );  
         return res<0? res: LIBOFTP_ERROR_PROTOCOL;  
     }  
103    
104      if( bufsiz <= 0 ) {          /* receive response. */
105            res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
106            if( res != 226 ) {                                              /* 226: Closing data connection. */
107                DEBUGPRINT1( "got illegal response %d\n", res );
108                return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
109            }
110    
111            return p - buf;         /* return with transfered bytes */
112            
113        } else {
114            /*
115             * バッファ不足時
116             */
117          DEBUGPRINT1( "buffer too small. %s\n", "" );          DEBUGPRINT1( "buffer too small. %s\n", "" );
118          strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) - 1 );          strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) - 1 );
119    
120            if( ftp_send_command( ftp, "ABOR\r\n" ) < 0 ) {
121                DEBUGPRINT1( "command sending error. %s\n", "ABOR" );
122                close( data_socket );
123                return LIBOFTP_ERROR_BUFFER;
124            }
125    
126            res = ftp_receive_response( ftp, 0, 0 );
127            if( res == 426 ) {                                              /* 426: Connection closed; transfer aborted. */
128                res = ftp_receive_response( ftp, 0, 0 );
129            }
130            close( data_socket );
131            if( res != 226 ) {                                              /* 226: Closing data connection. */
132                DEBUGPRINT1( "got illegal response %d\n", res );
133                return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
134            }
135    
136          return LIBOFTP_ERROR_BUFFER;          return LIBOFTP_ERROR_BUFFER;
137      }      }
   
     return LIBOFTP_NOERROR;  
138  }  }

Legend:
Removed from v.30  
changed lines
  Added in v.38

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26