static int write_one_file(Item *i, const char *path) {
        _cleanup_close_ int fd = -1;
        int flags, r = 0;
        struct stat st;

        assert(i);
        assert(path);

        flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND|O_NOFOLLOW :
                i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC|O_NOFOLLOW : 0;

        RUN_WITH_UMASK(0000) {
                mac_selinux_create_file_prepare(path, S_IFREG);
                fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
                mac_selinux_create_file_clear();
        }

        if (fd < 0) {
                if (i->type == WRITE_FILE && errno == ENOENT) {
                        log_debug_errno(errno, "Not writing \"%s\": %m", path);
                        return 0;
                }

                r = -errno;
                if (!i->argument && errno == EROFS && stat(path, &st) == 0 &&
                    (i->type == CREATE_FILE || st.st_size == 0))
                        goto check_mode;

                return log_error_errno(r, "Failed to create file %s: %m", path);
        }

        if (i->argument) {
                log_debug("%s to \"%s\".", i->type == CREATE_FILE ? "Appending" : "Writing", path);

                r = loop_write(fd, i->argument, strlen(i->argument), false);
                if (r < 0)
                        return log_error_errno(r, "Failed to write file \"%s\": %m", path);
        } else
                log_debug("\"%s\" has been created.", path);

        fd = safe_close(fd);

        if (stat(path, &st) < 0)
                return log_error_errno(errno, "stat(%s) failed: %m", path);

 check_mode:
        if (!S_ISREG(st.st_mode)) {
                log_error("%s is not a file.", path);
                return -EEXIST;
        }

        r = path_set_perms(i, path);
        if (r < 0)
                return r;

        return 0;
}
static int write_one_file(Item *i, const char *path) {
        _cleanup_close_ int fd = -1;
        int flags, r = 0;
        struct stat st;

        assert(i);
        assert(path);

        flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND|O_NOFOLLOW :
                i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC|O_NOFOLLOW : 0;

        RUN_WITH_UMASK(0000) {
                mac_selinux_create_file_prepare(path, S_IFREG);
                fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
                mac_selinux_create_file_clear();
        }

        if (fd < 0) {
                if (i->type == WRITE_FILE && errno == ENOENT) {
                        log_debug_errno(errno, "Not writing \"%s\": %m", path);
                        return 0;
                }

                r = -errno;
                if (!i->argument && errno == EROFS && stat(path, &st) == 0 &&
                    (i->type == CREATE_FILE || st.st_size == 0))
                        goto check_mode;

                return log_error_errno(r, "Failed to create file %s: %m", path);
        }

        if (i->argument) {
                log_debug("%s to \"%s\".", i->type == CREATE_FILE ? "Appending" : "Writing", path);

                r = loop_write(fd, i->argument, strlen(i->argument), false);
                if (r < 0)
                        return log_error_errno(r, "Failed to write file \"%s\": %m", path);
        } else
                log_debug("\"%s\" has been created.", path);

        fd = safe_close(fd);

        if (stat(path, &st) < 0)
                return log_error_errno(errno, "stat(%s) failed: %m", path);

 check_mode:
        if (!S_ISREG(st.st_mode)) {
                log_error("%s is not a file.", path);
                return -EEXIST;
        }

        r = path_set_perms(i, path);
        if (r < 0)
                return r;

        return 0;
}