mktemp.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_private.h"
  17. #include "fspr_file_io.h" /* prototype of fspr_mkstemp() */
  18. #include "fspr_strings.h" /* prototype of fspr_mkstemp() */
  19. #include "fspr_arch_file_io.h" /* prototype of fspr_mkstemp() */
  20. #include "fspr_portable.h" /* for fspr_os_file_put() */
  21. #include <stdlib.h> /* for mkstemp() - Single Unix */
  22. APR_DECLARE(fspr_status_t) fspr_file_mktemp(fspr_file_t **fp, char *template, fspr_int32_t flags, fspr_pool_t *p)
  23. {
  24. int fd;
  25. fspr_status_t rv;
  26. flags = (!flags) ? APR_CREATE | APR_READ | APR_WRITE |
  27. APR_DELONCLOSE : flags & ~APR_EXCL;
  28. fd = mkstemp(template);
  29. if (fd == -1) {
  30. return errno;
  31. }
  32. /* We need to reopen the file to get rid of the o_excl flag.
  33. * Otherwise file locking will not allow the file to be shared.
  34. */
  35. close(fd);
  36. if ((rv = fspr_file_open(fp, template, flags|APR_FILE_NOCLEANUP,
  37. APR_UREAD | APR_UWRITE, p)) == APR_SUCCESS) {
  38. fspr_pool_cleanup_register((*fp)->pool, (void *)(*fp),
  39. fspr_unix_file_cleanup, fspr_unix_file_cleanup);
  40. }
  41. return rv;
  42. }