2
0

fopen.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "curl_setup.h"
  25. #if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \
  26. !defined(CURL_DISABLE_HSTS)
  27. #ifdef HAVE_FCNTL_H
  28. #include <fcntl.h>
  29. #endif
  30. #include "urldata.h"
  31. #include "rand.h"
  32. #include "fopen.h"
  33. /* The last 3 #include files should be in this order */
  34. #include "curl_printf.h"
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. /*
  38. The dirslash() function breaks a null-terminated pathname string into
  39. directory and filename components then returns the directory component up
  40. to, *AND INCLUDING*, a final '/'. If there is no directory in the path,
  41. this instead returns a "" string.
  42. This function returns a pointer to malloc'ed memory.
  43. The input path to this function is expected to have a file name part.
  44. */
  45. #ifdef _WIN32
  46. #define PATHSEP "\\"
  47. #define IS_SEP(x) (((x) == '/') || ((x) == '\\'))
  48. #elif defined(MSDOS) || defined(__EMX__) || defined(OS2)
  49. #define PATHSEP "\\"
  50. #define IS_SEP(x) ((x) == '\\')
  51. #else
  52. #define PATHSEP "/"
  53. #define IS_SEP(x) ((x) == '/')
  54. #endif
  55. static char *dirslash(const char *path)
  56. {
  57. size_t n;
  58. struct dynbuf out;
  59. DEBUGASSERT(path);
  60. Curl_dyn_init(&out, CURL_MAX_INPUT_LENGTH);
  61. n = strlen(path);
  62. if(n) {
  63. /* find the rightmost path separator, if any */
  64. while(n && !IS_SEP(path[n-1]))
  65. --n;
  66. /* skip over all the path separators, if any */
  67. while(n && IS_SEP(path[n-1]))
  68. --n;
  69. }
  70. if(Curl_dyn_addn(&out, path, n))
  71. return NULL;
  72. /* if there was a directory, append a single trailing slash */
  73. if(n && Curl_dyn_addn(&out, PATHSEP, 1))
  74. return NULL;
  75. return Curl_dyn_ptr(&out);
  76. }
  77. /*
  78. * Curl_fopen() opens a file for writing with a temp name, to be renamed
  79. * to the final name when completed. If there is an existing file using this
  80. * name at the time of the open, this function will clone the mode from that
  81. * file. if 'tempname' is non-NULL, it needs a rename after the file is
  82. * written.
  83. */
  84. CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
  85. FILE **fh, char **tempname)
  86. {
  87. CURLcode result = CURLE_WRITE_ERROR;
  88. unsigned char randbuf[41];
  89. char *tempstore = NULL;
  90. struct_stat sb;
  91. int fd = -1;
  92. char *dir = NULL;
  93. *tempname = NULL;
  94. *fh = fopen(filename, FOPEN_WRITETEXT);
  95. if(!*fh)
  96. goto fail;
  97. if(fstat(fileno(*fh), &sb) == -1 || !S_ISREG(sb.st_mode)) {
  98. return CURLE_OK;
  99. }
  100. fclose(*fh);
  101. *fh = NULL;
  102. result = Curl_rand_alnum(data, randbuf, sizeof(randbuf));
  103. if(result)
  104. goto fail;
  105. dir = dirslash(filename);
  106. if(dir) {
  107. /* The temp file name should not end up too long for the target file
  108. system */
  109. tempstore = aprintf("%s%s.tmp", dir, randbuf);
  110. free(dir);
  111. }
  112. if(!tempstore) {
  113. result = CURLE_OUT_OF_MEMORY;
  114. goto fail;
  115. }
  116. result = CURLE_WRITE_ERROR;
  117. #if (defined(ANDROID) || defined(__ANDROID__)) && \
  118. (defined(__i386__) || defined(__arm__))
  119. fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, (mode_t)(0600|sb.st_mode));
  120. #else
  121. fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, 0600|sb.st_mode);
  122. #endif
  123. if(fd == -1)
  124. goto fail;
  125. *fh = fdopen(fd, FOPEN_WRITETEXT);
  126. if(!*fh)
  127. goto fail;
  128. *tempname = tempstore;
  129. return CURLE_OK;
  130. fail:
  131. if(fd != -1) {
  132. close(fd);
  133. unlink(tempstore);
  134. }
  135. free(tempstore);
  136. return result;
  137. }
  138. #endif /* ! disabled */