tool_cb_see.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "tool_setup.h"
00023 
00024 #define ENABLE_CURLX_PRINTF
00025 /* use our own printf() functions */
00026 #include "curlx.h"
00027 
00028 #include "tool_cfgable.h"
00029 #include "tool_cb_see.h"
00030 
00031 #include "memdebug.h" /* keep this as LAST include */
00032 
00033 /* OUR_MAX_SEEK_L has 'long' data type, OUR_MAX_SEEK_O has 'curl_off_t,
00034    both represent the same value. Maximum offset used here when we lseek
00035    using a 'long' data type offset */
00036 
00037 #define OUR_MAX_SEEK_L  2147483647L - 1L
00038 #define OUR_MAX_SEEK_O  CURL_OFF_T_C(0x7FFFFFFF) - CURL_OFF_T_C(0x1)
00039 
00040 /*
00041 ** callback for CURLOPT_SEEKFUNCTION
00042 **
00043 ** Notice that this is not supposed to return the resulting offset. This
00044 ** shall only return CURL_SEEKFUNC_* return codes.
00045 */
00046 
00047 int tool_seek_cb(void *userdata, curl_off_t offset, int whence)
00048 {
00049   struct InStruct *in = userdata;
00050 
00051 #if(CURL_SIZEOF_CURL_OFF_T > SIZEOF_OFF_T) && !defined(USE_WIN32_LARGE_FILES)
00052 
00053   /* The offset check following here is only interesting if curl_off_t is
00054      larger than off_t and we are not using the WIN32 large file support
00055      macros that provide the support to do 64bit seeks correctly */
00056 
00057   if(offset > OUR_MAX_SEEK_O) {
00058     /* Some precaution code to work around problems with different data sizes
00059        to allow seeking >32bit even if off_t is 32bit. Should be very rare and
00060        is really valid on weirdo-systems. */
00061     curl_off_t left = offset;
00062 
00063     if(whence != SEEK_SET)
00064       /* this code path doesn't support other types */
00065       return CURL_SEEKFUNC_FAIL;
00066 
00067     if(LSEEK_ERROR == lseek(in->fd, 0, SEEK_SET))
00068       /* couldn't rewind to beginning */
00069       return CURL_SEEKFUNC_FAIL;
00070 
00071     while(left) {
00072       long step = (left > OUR_MAX_SEEK_O) ? OUR_MAX_SEEK_L : (long)left;
00073       if(LSEEK_ERROR == lseek(in->fd, step, SEEK_CUR))
00074         /* couldn't seek forwards the desired amount */
00075         return CURL_SEEKFUNC_FAIL;
00076       left -= step;
00077     }
00078     return CURL_SEEKFUNC_OK;
00079   }
00080 #endif
00081 
00082   if(LSEEK_ERROR == lseek(in->fd, offset, whence))
00083     /* couldn't rewind, the reason is in errno but errno is just not portable
00084        enough and we don't actually care that much why we failed. We'll let
00085        libcurl know that it may try other means if it wants to. */
00086     return CURL_SEEKFUNC_CANTSEEK;
00087 
00088   return CURL_SEEKFUNC_OK;
00089 }
00090 
00091 #if defined(WIN32) && !defined(__MINGW64__)
00092 
00093 #ifdef __BORLANDC__
00094 /* 64-bit lseek-like function unavailable */
00095 #  define _lseeki64(hnd,ofs,whence) lseek(hnd,ofs,whence)
00096 #endif
00097 
00098 #ifdef __POCC__
00099 #  if(__POCC__ < 450)
00100 /* 64-bit lseek-like function unavailable */
00101 #    define _lseeki64(hnd,ofs,whence) _lseek(hnd,ofs,whence)
00102 #  else
00103 #    define _lseeki64(hnd,ofs,whence) _lseek64(hnd,ofs,whence)
00104 #  endif
00105 #endif
00106 
00107 #ifdef _WIN32_WCE
00108 /* 64-bit lseek-like function unavailable */
00109 #  undef _lseeki64
00110 #  define _lseeki64(hnd,ofs,whence) lseek(hnd,ofs,whence)
00111 #  undef _get_osfhandle
00112 #  define _get_osfhandle(fd) (fd)
00113 #endif
00114 
00115 /*
00116  * Truncate a file handle at a 64-bit position 'where'.
00117  */
00118 
00119 int tool_ftruncate64(int fd, curl_off_t where)
00120 {
00121   if(_lseeki64(fd, where, SEEK_SET) < 0)
00122     return -1;
00123 
00124   if(!SetEndOfFile((HANDLE)_get_osfhandle(fd)))
00125     return -1;
00126 
00127   return 0;
00128 }
00129 
00130 #endif /* WIN32  && ! __MINGW64__ */
00131 


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06