File: diskio.c

package info (click to toggle)
openbios-sparc 1.0%2Bsvn640-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,412 kB
  • ctags: 12,091
  • sloc: ansic: 57,249; asm: 2,680; xml: 1,335; cpp: 414; makefile: 224; sh: 190
file content (215 lines) | stat: -rw-r--r-- 3,768 bytes parent folder | download | duplicates (2)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 *   Creation Date: <2003/12/07 19:36:00 samuel>
 *   Time-stamp: <2004/01/07 19:28:43 samuel>
 *
 *	<diskio.c>
 *
 *	I/O wrappers
 *
 *   Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   version 2
 *
 */

#include "openbios/config.h"
#include "openbios/bindings.h"
#include "libc/diskio.h"

typedef struct {
	ihandle_t ih;
	int	do_close;
	xt_t	read_xt;
	xt_t	seek_xt;

	xt_t	reopen_xt;
	xt_t	tell_xt;
	xt_t	get_path_xt;
	xt_t	get_fstype_xt;
	xt_t	open_nwrom_xt;
	xt_t	volume_name_xt;
} priv_fd_t;

#define MAX_FD 32
static priv_fd_t *file_descriptors[MAX_FD];

static int
lookup_xt( ihandle_t ih, const char *method, xt_t *xt )
{
	if( *xt )
		return 0;
	*xt = find_ih_method( method, ih );
	return (*xt) ? 0:1;
}

int
open_ih( ihandle_t ih )
{
	xt_t read_xt=0, seek_xt=0;
	priv_fd_t *fdp;
	int fd;

	if( !ih || lookup_xt(ih, "read", &read_xt) )
		return -1;
	if( lookup_xt(ih, "seek", &seek_xt) )
		return -1;

	for (fd=0; fd<MAX_FD; fd++)
		if(file_descriptors[fd]==NULL)
			break;
	if(fd==MAX_FD)
		return -1;

	fdp = malloc( sizeof(*fdp) );
	/* Better clear the fd, as it
	 * contains valuable information
	 */
	memset(fdp, 0, sizeof(*fdp));
	fdp->ih = ih;
	fdp->read_xt = read_xt;
	fdp->seek_xt = seek_xt;
	fdp->do_close = 0;

	file_descriptors[fd]=fdp;
	return fd;
}

int
open_io( const char *spec )
{
	int fd;
	ihandle_t ih = open_dev( spec );
	priv_fd_t *fdp;

	if( !ih )
		return -1;

	if( (fd=open_ih(ih)) == -1 ) {
		close_dev( ih );
		return -1;
	}

	fdp = file_descriptors[fd];
	fdp->do_close = 1;

	return fd;
}

int
reopen( int fd, const char *filename )
{
	priv_fd_t *fdp = file_descriptors[fd];
	int ret;

	if( lookup_xt(fdp->ih, "reopen", &fdp->reopen_xt) )
		return -1;

	push_str( filename );
	call_package( fdp->reopen_xt, fdp->ih );
        ret = (POP() == (ucell)-1)? 0 : -1;

	return ret;
}

int
reopen_nwrom( int fd )
{
	priv_fd_t *fdp = file_descriptors[fd];

	if( lookup_xt(fdp->ih, "open-nwrom", &fdp->open_nwrom_xt) )
		return -1;
	call_package( fdp->open_nwrom_xt, fdp->ih );
        return (POP() == (ucell)-1)? 0 : -1;
}

ihandle_t
get_ih_from_fd( int fd )
{
	priv_fd_t *fdp = file_descriptors[fd];
	return fdp->ih;
}

const char *
get_file_path( int fd )
{
	priv_fd_t *fdp = file_descriptors[fd];
	if( lookup_xt(fdp->ih, "get-path", &fdp->get_path_xt) )
		return NULL;
	call_package( fdp->get_path_xt, fdp->ih );
	return (char*)POP();
}

const char *
get_volume_name( int fd )
{
	priv_fd_t *fdp = file_descriptors[fd];
	if( lookup_xt(fdp->ih, "volume-name", &fdp->volume_name_xt) )
		return NULL;
	call_package( fdp->volume_name_xt, fdp->ih );
	return (char*)POP();
}

const char *
get_fstype( int fd )
{
	priv_fd_t *fdp = file_descriptors[fd];
	if( lookup_xt(fdp->ih, "get-fstype", &fdp->get_fstype_xt) )
		return NULL;
	call_package( fdp->get_fstype_xt, fdp->ih );
	return (char*)POP();
}

int
read_io( int fd, void *buf, size_t cnt )
{
	priv_fd_t *fdp = file_descriptors[fd];
	ucell ret;

	PUSH( (ucell)buf );
	PUSH( cnt );
	call_package( fdp->read_xt, fdp->ih );
	ret = POP();

	if( !ret && cnt )
		ret = -1;
	return ret;
}

int
seek_io( int fd, llong offs )
{
	priv_fd_t *fdp = file_descriptors[fd];

	DPUSH( offs );
	call_package( fdp->seek_xt, fdp->ih );
	return ((((cell)POP()) >= 0)? 0 : -1);
}

llong
tell( int fd )
{
	priv_fd_t *fdp = file_descriptors[fd];
	llong offs;

	if( lookup_xt(fdp->ih, "tell", &fdp->tell_xt) )
		return -1;
	call_package( fdp->tell_xt, fdp->ih );
	offs = DPOP();
	return offs;
}

int
close_io( int fd )
{
	priv_fd_t *fdp = file_descriptors[fd];

	if( fdp->do_close )
		close_dev( fdp->ih );
	free( fdp );

	file_descriptors[fd]=NULL;

	return 0;
}