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 */
5 /* this file contains the source for opening addresses of create type */
7 #include "xiosysincludes.h"
12 #include "xio-named.h"
13 #include "xio-creat.h"
16 static int xioopen_creat(int arg
, const char *argv
[], struct opt
*opts
, int rw
, xiofile_t
*fd
, const struct addrdesc
*addrdesc
);
19 /*! within stream model, this is a write-only address - use 2 instead of 3 */
20 const struct addrdesc xioaddr_creat
= { "CREATE", 1+XIO_WRONLY
, xioopen_creat
, GROUP_FD
|GROUP_NAMED
|GROUP_FILE
, 0, 0, 0 HELP(":<filename>") };
23 /* retrieve the mode option and perform the creat() call.
24 returns the file descriptor or a negative value. */
25 static int _xioopen_creat(const char *path
, int rw
, struct opt
*opts
) {
29 retropt_modet(opts
, OPT_PERM
, &mode
);
31 if ((fd
= Creat(path
, mode
)) < 0) {
32 Error3("creat(\"%s\", 0%03o): %s",
33 path
, mode
, strerror(errno
));
34 return STAT_RETRYLATER
;
40 static int xioopen_creat(
46 const struct addrdesc
*addrdesc
)
48 struct single
*sfd
= &xxfd
->stream
;
49 const char *filename
= argv
[1];
50 int rw
= (xioflags
&XIO_ACCMODE
);
52 bool opt_unlink_close
= false;
55 /* remove old file, or set user/permissions on old file; parse options */
57 _xioopen_named_early(argc
, argv
, xxfd
, addrdesc
->groups
, &exists
, opts
,
63 retropt_bool(opts
, OPT_UNLINK_CLOSE
, &opt_unlink_close
);
64 if (opt_unlink_close
) {
65 if ((sfd
->unlink_close
= strdup(filename
)) == NULL
) {
66 Error1("strdup(\"%s\"): out of memory", filename
);
68 sfd
->opt_unlink_close
= true;
71 Notice2("creating regular file \"%s\" for %s", filename
, ddirection
[rw
]);
72 if ((result
= _xioopen_creat(filename
, rw
, opts
)) < 0)
76 applyopts_named(filename
, opts
, PH_PASTOPEN
);
77 if ((result
= applyopts2(sfd
, -1, opts
, PH_PASTOPEN
, PH_LATE2
)) < 0)
80 applyopts_cloexec(sfd
->fd
, opts
);
82 applyopts_fchown(sfd
->fd
, opts
);
84 if ((result
= _xio_openlate(sfd
, opts
)) < 0)
90 #endif /* WITH_CREAT */