Ejemplo n.º 1
0
/*
 * Open a file specified by a parsed file name (which may be only a
 * device).
 */
int
zopen_file(i_ctx_t *i_ctx_p, const gs_parsed_file_name_t *pfn,
	   const char *file_access, stream **ps, gs_memory_t *mem)
{
    gx_io_device *const iodev = pfn->iodev;

    if (pfn->fname == NULL)	/* just a device */
	return iodev->procs.open_device(iodev, file_access, ps, mem);
    else {			/* file */
	iodev_proc_open_file((*open_file)) = iodev->procs.open_file;

	if (open_file == 0)
	    open_file = iodev_os_open_file;
	/* Check OS files to make sure we allow the type of access */
	if (open_file == iodev_os_open_file) {
	    int code = check_file_permissions(i_ctx_p, pfn->fname, pfn->len,
		file_access[0] == 'r' ? "PermitFileReading" : "PermitFileWriting");

	    if (code < 0 && !file_is_tempfile(i_ctx_p,
                                          (const uchar *)pfn->fname, pfn->len))
		return code;
	}
	return open_file(iodev, pfn->fname, pfn->len, file_access, ps, mem);
    }
}
Ejemplo n.º 2
0
/*
 * Open a stream using a filename that can include a PS style IODevice prefix
 * If iodev_default is the '%os' device, then the file will be on the host
 * file system transparently to the caller. The "%os%" prefix can be used
 * to explicilty access the host file system.
 */
stream *
sfopen(const char *path, const char *mode, gs_memory_t *memory)
{
    gs_parsed_file_name_t pfn;
    stream *s;
    iodev_proc_open_file((*open_file));
    gs_memory_t *mem = (memory == NULL) ? gs_lib_ctx_get_non_gc_memory_t() : memory;

    int code = gs_parse_file_name(&pfn, path, strlen(path));
    if (code < 0) {
#	define EMSG	"sfopen: gs_parse_file_name failed.\n"
	errwrite(EMSG, strlen(EMSG));
#	undef EMSG
	return NULL;
    }
    if (pfn.fname == NULL) {	/* just a device */
#	define EMSG	"sfopen: not allowed with %device only.\n"
	errwrite(EMSG, strlen(EMSG));
#	undef EMSG
	return NULL;
    }
    if (pfn.iodev == NULL)
	pfn.iodev = iodev_default;
    open_file = pfn.iodev->procs.open_file;
    if (open_file == 0)
	code = file_open_stream(pfn.fname, pfn.len, mode, 2048, &s,
				pfn.iodev, pfn.iodev->procs.fopen, mem);
    else
	code = open_file(pfn.iodev, pfn.fname, pfn.len, mode, &s, mem);
    if (code < 0)
	return NULL;
    s->position = 0;
    code = ssetfilename(s, (const byte *)path, strlen(path));
    if (code < 0) {
	/* Only error is e_VMerror */
	sclose(s);
	gs_free_object(s->memory, s, "sfopen: allocation error");
#	define EMSG	"sfopen: allocation error setting path name into stream.\n"
	errwrite(EMSG, strlen(EMSG));
#	undef EMSG
	return NULL;
    }
    return s;
}