File: ext2_fs.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 (203 lines) | stat: -rw-r--r-- 3,695 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
/*
 *
 * (c) 2008-2009 Laurent Vivier <Laurent@lvivier.info>
 *
 * This file has been copied from EMILE, http://emile.sf.net
 *
 */

#include "libext2.h"
#include "ext2_utils.h"
#include "openbios/fs.h"
#include "libc/vsprintf.h"

typedef struct {
	enum { FILE, DIR } type;
	union {
		ext2_FILE *file;
		ext2_DIR *dir;
	};
} ext2_COMMON;

static void
umount( fs_ops_t *fs )
{
        ext2_VOLUME *volume = (ext2_VOLUME *)fs->fs_data;

        ext2_umount( volume );
}

static const int days_month[12] =
	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static const int days_month_leap[12] =
	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

static inline int is_leap(int year)
{
	return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

static void
print_date(time_t sec)
{
	unsigned int second, minute, hour, month, day, year;
	int current;
	const int *days;

	second = sec % 60;
	sec /= 60;

	minute = sec % 60;
	sec /= 60;

	hour = sec % 24;
	sec /= 24;

	year = sec * 100 / 36525;
	sec -= year * 36525 / 100;
	year += 1970;

	days = is_leap(year) ?  days_month_leap : days_month;

	current = 0;
	month = 0;
	while (month < 12) {
		if (sec <= current + days[month]) {
			break;
		}
		current += days[month];
		month++;
	}
	month++;

	day = sec - current + 1;

	forth_printf("%d-%02d-%02d %02d:%02d:%02d ",
		     year, month, day, hour, minute, second);
}

static void
dir_fs ( file_desc_t *fd)
{
	ext2_COMMON *common = (ext2_COMMON *)fd;
	struct ext2_dir_entry_2 *entry;
	struct ext2_inode inode;

	if (common->type != DIR)
		return;

	forth_printf("\n");
	while ( (entry = ext2_readdir(common->dir)) ) {
		ext2_get_inode(common->dir->volume, entry->inode, &inode);
		forth_printf("% 10d ", inode.i_size);
		print_date(inode.i_mtime);
		if (S_ISDIR(inode.i_mode))
			forth_printf("%s\\\n", entry->name);
		else
			forth_printf("%s\n", entry->name);
	}
}

static file_desc_t *
open_path( fs_ops_t *fs, const char *path )
{
	ext2_VOLUME *volume = (ext2_VOLUME *)fs->fs_data;
	ext2_COMMON *common;

	common = (ext2_COMMON*)malloc(sizeof(*common));
	if (common == NULL)
		return NULL;

	common->dir = ext2_opendir(volume, path);
	if (common->dir == NULL) {
		common->file = ext2_open(volume, path);
		if (common->file == NULL) {
			free(common);
			return NULL;
		}
		common->type = FILE;
		return (file_desc_t *)common;
	}
	common->type = DIR;
	return (file_desc_t *)common;
}

static char *
get_path( file_desc_t *fd, char *buf, int size )
{
	ext2_COMMON *common =(ext2_COMMON *)fd;

	if (common->type != FILE)
		return NULL;

	strncpy(buf, common->file->path, size);

	return buf;
}

static int
file_lseek( file_desc_t *fd, off_t offs, int whence )
{
	ext2_COMMON *common =(ext2_COMMON *)fd;

	if (common->type != FILE)
		return -1;

	return ext2_lseek(common->file, offs, whence);
}

static void
file_close( file_desc_t *fd )
{
	ext2_COMMON *common =(ext2_COMMON *)fd;

	if (common->type == FILE)
		ext2_close(common->file);
	else if (common->type == DIR)
		ext2_closedir(common->dir);
	free(common);
}

static int
file_read( file_desc_t *fd, void *buf, size_t count )
{
	ext2_COMMON *common =(ext2_COMMON *)fd;

	if (common->type != FILE)
		return -1;

	return ext2_read(common->file, buf, count);
}

static const char *
get_fstype( fs_ops_t *fs)
{
	return "EXT2";
}

static const fs_ops_t ext2_ops = {
	.dir		= dir_fs,
	.close_fs	= umount,

	.open_path	= open_path,
	.get_path	= get_path,
	.close		= file_close,
	.read		= file_read,
	.lseek		= file_lseek,

	.get_fstype	= get_fstype,
};

int fs_ext2_open(int fd, fs_ops_t *fs)
{
	ext2_VOLUME *volume;

	volume = ext2_mount(fd);
	if (volume == NULL)
		return -1;

	*fs = ext2_ops;
	fs->fs_data = volume;

	return 0;
}