fopen.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * Curl_fopen() opens a file for writing with a temp name, to be renamed
  39. * to the final name when completed. If there is an existing file using this
  40. * name at the time of the open, this function will clone the mode from that
  41. * file. if 'tempname' is non-NULL, it needs a rename after the file is
  42. * written.
  43. */
  44. CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
  45. FILE **fh, char **tempname)
  46. {
  47. CURLcode result = CURLE_WRITE_ERROR;
  48. unsigned char randsuffix[9];
  49. char *tempstore = NULL;
  50. struct_stat sb;
  51. int fd = -1;
  52. *tempname = NULL;
  53. if(stat(filename, &sb) == -1 || !S_ISREG(sb.st_mode)) {
  54. /* a non-regular file, fallback to direct fopen() */
  55. *fh = fopen(filename, FOPEN_WRITETEXT);
  56. if(*fh)
  57. return CURLE_OK;
  58. goto fail;
  59. }
  60. result = Curl_rand_hex(data, randsuffix, sizeof(randsuffix));
  61. if(result)
  62. goto fail;
  63. tempstore = aprintf("%s.%s.tmp", filename, randsuffix);
  64. if(!tempstore) {
  65. result = CURLE_OUT_OF_MEMORY;
  66. goto fail;
  67. }
  68. result = CURLE_WRITE_ERROR;
  69. fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, 0600);
  70. if(fd == -1)
  71. goto fail;
  72. #ifdef HAVE_FCHMOD
  73. {
  74. struct_stat nsb;
  75. if((fstat(fd, &nsb) != -1) &&
  76. (nsb.st_uid == sb.st_uid) && (nsb.st_gid == sb.st_gid)) {
  77. /* if the user and group are the same, clone the original mode */
  78. if(fchmod(fd, sb.st_mode) == -1)
  79. goto fail;
  80. }
  81. }
  82. #endif
  83. *fh = fdopen(fd, FOPEN_WRITETEXT);
  84. if(!*fh)
  85. goto fail;
  86. *tempname = tempstore;
  87. return CURLE_OK;
  88. fail:
  89. if(fd != -1) {
  90. close(fd);
  91. unlink(tempstore);
  92. }
  93. free(tempstore);
  94. return result;
  95. }
  96. #endif /* ! disabled */