Exemplo n.º 1
0
/**
 * gs_file_create:
 * @file: Path to non-existent file
 * @mode: Unix access permissions
 * @out_stream: (out) (transfer full) (allow-none): Newly created output, or %NULL
 * @cancellable: a #GCancellable
 * @error: a #GError
 *
 * Like g_file_create(), except this function allows specifying the
 * access mode.  This allows atomically creating private files.
 */
gboolean
gs_file_create (GFile          *file,
                int             mode,
                GOutputStream **out_stream,
                GCancellable   *cancellable,
                GError        **error)
{
    gboolean ret = FALSE;
    int fd;
    GOutputStream *ret_stream = NULL;

    fd = open_nointr (gs_file_get_path_cached (file), O_WRONLY | O_CREAT | O_EXCL, mode);
    if (fd < 0)
    {
        gs_set_prefix_error_from_errno (error, errno, "open");
        goto out;
    }

    if (fchmod (fd, mode) < 0)
    {
        close (fd);
        gs_set_prefix_error_from_errno (error, errno, "fchmod");
        goto out;
    }

    ret_stream = g_unix_output_stream_new (fd, TRUE);

    ret = TRUE;
    gs_transfer_out_value (out_stream, &ret_stream);
out:
    g_clear_object (&ret_stream);
    return ret;
}
Exemplo n.º 2
0
/*
 * Opens the random number generator devices if not already open.
 * Always returns the opened fd of the device, or error.
 */
static int
pkcs11_open_common(int *fd, const char *dev, int oflag)
{
	if (*fd < 0) {
		if (*fd < 0)
			*fd = open_nointr(dev, oflag);
	}
	return (*fd);
}