summaryrefslogtreecommitdiff
path: root/src/helper/mdso_create_output.c
blob: b0dc0fc62272f5d84bd8c3d65e7115a3fc03c7a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/****************************************************************/
/*  mdso: midipix dso scavenger                                 */
/*  Copyright (C) 2015--2017  Z. Gilboa                         */
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
/****************************************************************/

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <fcntl.h>

#include <mdso/mdso.h>
#include "mdso_driver_impl.h"
#include "mdso_errinfo_impl.h"

static FILE * mdso_create_output(
	const struct mdso_driver_ctx *	dctx,
	const char *			name,
	int				fdat)
{
	struct mdso_driver_ctx_impl *	ictx;
	uintptr_t			addr;
	int				fdout;
	FILE *				fout;

	addr = (uintptr_t)dctx - offsetof(struct mdso_driver_ctx_impl,ctx);
	ictx = (struct mdso_driver_ctx_impl *)addr;
	fdat = (fdat == AT_FDCWD) ? AT_FDCWD : ictx->fddst;

	if ((fdout = openat(fdat,name,
                        O_CREAT|O_TRUNC|O_WRONLY|O_NOCTTY|O_NOFOLLOW,
                        S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
		MDSO_SYSTEM_ERROR(dctx);
		return 0;
	}

	if (!(fout = fdopen(fdout,"w"))) {
		close(fdout);
		MDSO_SYSTEM_ERROR(dctx);
		return 0;
	}

	return fout;
}

FILE * mdso_create_archive(
	const struct mdso_driver_ctx *	dctx,
	const char *			arname)
{
	return mdso_create_output(dctx,arname,AT_FDCWD);
}

FILE * mdso_create_asmsrc(
	const struct mdso_driver_ctx *	dctx,
	const char *			asmname)
{
	if (!dctx->cctx->dstdir)
		return stdout;

	return mdso_create_output(dctx,asmname,-1);
}

FILE * mdso_create_object(
	const struct mdso_driver_ctx *	dctx,
	const char *			objname)
{
	if (!dctx->cctx->dstdir) {
		MDSO_CUSTOM_ERROR(dctx,MDSO_ERR_INVALID_DSTDIR);
		return 0;
	}

	return mdso_create_output(dctx,objname,-1);
}