summaryrefslogtreecommitdiffhomepage
path: root/src/cmds/ntux_cmd_stat.c
blob: 8634c4aba8c7a5ba181996c69fa9dc09f07ec21b (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/***********************************************************/
/*  ntux: native translation und extension                 */
/*  Copyright (C) 2016--2018  Z. Gilboa                    */
/*  Released under GPLv2 and GPLv3; see COPYING.NTUX.      */
/***********************************************************/

#include <ntapi/ntapi.h>
#include <psxabi/sys_sysapi.h>
#include <psxabi/sys_stat.h>
#include <psxabi/sys_errno.h>

#include <psxxfi/xfi_ofd.h>
#include <psxxfi/xfi_unicode.h>

#include <ntux/ntux.h>
#include "ntux_driver_impl.h"
#include "ntux_nolibc_impl.h"
#include "ntux_errinfo_impl.h"

static int ntux_cmd_stat_ret(int fd, struct __ofd * ofd, void * buf, void * sbuf, int ret)
{
	if (fd >= 0)
		__sys_close(fd);

	if (ofd)
		__xfi_ofd_ref_dec(ofd);

	if (buf)
		ntux_free(buf);

	if (sbuf)
		ntux_free(sbuf);

	return ret;
}

int ntux_cmd_stat(const struct ntux_driver_ctx * dctx, const char * dunit)
{
	intptr_t		ret;
	int32_t			status;
	int			fdout;
	int			fdcwd;
	const unsigned char *	unit;
	nt_stat *		nstat;
	struct __stat		st;
	char *			str;
	int			fd      = -1;
	struct __ofd *		ofd     = 0;
	void *			buf     = 0;
	void *			sbuf    = 0;
	const size_t		bufsize = 0x10000;

	/* init */
	ntux_driver_set_ectx(
		dctx,0,dunit);

	unit = (const unsigned char *)dunit;

	/* fdctx */
	fdout = ntux_driver_fdout(dctx);
	fdcwd = ntux_driver_fdcwd(dctx);

	/* open */
	if ((ret = __sys_openat(fdcwd,unit,0,0)) < 0)
		if (ntux_errno_set(dctx,ret))
			return ntux_cmd_stat_ret(
				fd,ofd,buf,sbuf,
				NTUX_SYSTEM_ERROR(dctx));

	fd = ret;

	/* ofd */
	if (!(ofd = __xfi_ofd_ref_inc(fd)))
		return ntux_cmd_stat_ret(
			fd,ofd,buf,sbuf,
			NTUX_CUSTOM_ERROR(
				dctx,
				NTUX_ERR_FLOW_ERROR));

	/* fstat */
	if ((ret = __sys_fstat(fd,&st)))
		if (ntux_errno_set(dctx,ret))
			return ntux_cmd_stat_ret(
				fd,ofd,buf,sbuf,
				NTUX_SYSTEM_ERROR(dctx));

	/* buffers */
	if (!(buf = ntux_calloc(1,bufsize)))
		if (ntux_errno_set(dctx,ENOMEM))
			return ntux_cmd_stat_ret(
				fd,ofd,buf,sbuf,
				NTUX_SYSTEM_ERROR(dctx));

	if (!(sbuf = ntux_calloc(1,bufsize)))
		if (ntux_errno_set(dctx,ENOMEM))
			return ntux_cmd_stat_ret(
				fd,ofd,buf,sbuf,
				NTUX_SYSTEM_ERROR(dctx));

	nstat = (nt_stat *)sbuf;

	/* stat */
	if ((status = ntapi->tt_stat(
			ofd->info.hfile,
			nstat,bufsize,
			buf,bufsize,
			NT_STAT_DEV_NAME_COPY)))
		return ntux_cmd_stat_ret(
			fd,ofd,buf,sbuf,
			NTUX_NATIVE_ERROR(dctx,status));

	/* native name */
	str = (char *)buf;

	if ((status = __xfi_strconv_utf16_to_utf8(
			nstat->dev_name,
			str,bufsize,0)))
		return ntux_cmd_stat_ret(
			fd,ofd,buf,sbuf,
			NTUX_NATIVE_ERROR(dctx,status));

	ntux_dprintf(fdout,"fname: %s\n",str);

	/* inode */
	ntux_dprintf(fdout,"inode: 0x%p (%lld)\n",st.st_ino,st.st_ino);

	/* all done */
	return ntux_cmd_stat_ret(fd,ofd,buf,sbuf,0);
}