You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: xio-creat.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* this file contains the source for opening addresses of create type */
  5. #include "xiosysincludes.h"
  6. #if WITH_CREAT
  7. #include "xioopen.h"
  8. #include "xio-named.h"
  9. #include "xio-creat.h"
  10. static int xioopen_creat(int arg, const char *argv[], struct opt *opts, int rw, xiofile_t *fd, unsigned groups, int dummy1, int dummy2, int dummy3);
  11. /*! within stream model, this is a write-only address - use 2 instead of 3 */
  12. const struct addrdesc addr_creat = { "create", 3, xioopen_creat, GROUP_FD|GROUP_NAMED|GROUP_FILE, 0, 0, 0 HELP(":<filename>") };
  13. /* retrieve the mode option and perform the creat() call.
  14. returns the file descriptor or a negative value. */
  15. static int _xioopen_creat(const char *path, int rw, struct opt *opts) {
  16. mode_t mode = 0666;
  17. int fd;
  18. retropt_modet(opts, OPT_PERM, &mode);
  19. if ((fd = Creat(path, mode)) < 0) {
  20. Error3("creat(\"%s\", 0%03o): %s",
  21. path, mode, strerror(errno));
  22. return STAT_RETRYLATER;
  23. }
  24. return fd;
  25. }
  26. static int xioopen_creat(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *fd, unsigned groups, int dummy1, int dummy2, int dummy3) {
  27. const char *filename = argv[1];
  28. int rw = (xioflags&XIO_ACCMODE);
  29. bool exists;
  30. bool opt_unlink_close = false;
  31. int result;
  32. /* remove old file, or set user/permissions on old file; parse options */
  33. if ((result = _xioopen_named_early(argc, argv, fd, groups, &exists, opts)) < 0) {
  34. return result;
  35. }
  36. retropt_bool(opts, OPT_UNLINK_CLOSE, &opt_unlink_close);
  37. if (opt_unlink_close) {
  38. if ((fd->stream.unlink_close = strdup(filename)) == NULL) {
  39. Error1("strdup(\"%s\"): out of memory", filename);
  40. }
  41. fd->stream.opt_unlink_close = true;
  42. }
  43. Notice2("creating regular file \"%s\" for %s", filename, ddirection[rw]);
  44. if ((result = _xioopen_creat(filename, rw, opts)) < 0)
  45. return result;
  46. fd->stream.fd = result;
  47. applyopts_named(filename, opts, PH_PASTOPEN);
  48. if ((result = applyopts2(fd->stream.fd, opts, PH_PASTOPEN, PH_LATE2)) < 0)
  49. return result;
  50. applyopts_cloexec(fd->stream.fd, opts);
  51. applyopts_fchown(fd->stream.fd, opts);
  52. if ((result = _xio_openlate(&fd->stream, opts)) < 0)
  53. return result;
  54. return 0;
  55. }
  56. #endif /* WITH_CREAT */