tool_cb_see.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_setup.h"
  25. #define ENABLE_CURLX_PRINTF
  26. /* use our own printf() functions */
  27. #include "curlx.h"
  28. #include "tool_cfgable.h"
  29. #include "tool_operate.h"
  30. #include "tool_cb_see.h"
  31. #include "memdebug.h" /* keep this as LAST include */
  32. /* OUR_MAX_SEEK_L has 'long' data type, OUR_MAX_SEEK_O has 'curl_off_t,
  33. both represent the same value. Maximum offset used here when we lseek
  34. using a 'long' data type offset */
  35. #define OUR_MAX_SEEK_L 2147483647L - 1L
  36. #define OUR_MAX_SEEK_O CURL_OFF_T_C(0x7FFFFFFF) - CURL_OFF_T_C(0x1)
  37. /*
  38. ** callback for CURLOPT_SEEKFUNCTION
  39. **
  40. ** Notice that this is not supposed to return the resulting offset. This
  41. ** shall only return CURL_SEEKFUNC_* return codes.
  42. */
  43. int tool_seek_cb(void *userdata, curl_off_t offset, int whence)
  44. {
  45. struct per_transfer *per = userdata;
  46. #if(SIZEOF_CURL_OFF_T > SIZEOF_OFF_T) && !defined(USE_WIN32_LARGE_FILES)
  47. /* The offset check following here is only interesting if curl_off_t is
  48. larger than off_t and we are not using the WIN32 large file support
  49. macros that provide the support to do 64bit seeks correctly */
  50. if(offset > OUR_MAX_SEEK_O) {
  51. /* Some precaution code to work around problems with different data sizes
  52. to allow seeking >32bit even if off_t is 32bit. Should be very rare and
  53. is really valid on weirdo-systems. */
  54. curl_off_t left = offset;
  55. if(whence != SEEK_SET)
  56. /* this code path doesn't support other types */
  57. return CURL_SEEKFUNC_FAIL;
  58. if(LSEEK_ERROR == lseek(per->infd, 0, SEEK_SET))
  59. /* couldn't rewind to beginning */
  60. return CURL_SEEKFUNC_FAIL;
  61. while(left) {
  62. long step = (left > OUR_MAX_SEEK_O) ? OUR_MAX_SEEK_L : (long)left;
  63. if(LSEEK_ERROR == lseek(per->infd, step, SEEK_CUR))
  64. /* couldn't seek forwards the desired amount */
  65. return CURL_SEEKFUNC_FAIL;
  66. left -= step;
  67. }
  68. return CURL_SEEKFUNC_OK;
  69. }
  70. #endif
  71. if(LSEEK_ERROR == lseek(per->infd, offset, whence))
  72. /* couldn't rewind, the reason is in errno but errno is just not portable
  73. enough and we don't actually care that much why we failed. We'll let
  74. libcurl know that it may try other means if it wants to. */
  75. return CURL_SEEKFUNC_CANTSEEK;
  76. return CURL_SEEKFUNC_OK;
  77. }