summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openbios/fs/ext2/ext2_open.c
blob: 03a89bbd0c8b5504ce588438f19ef5fbc3b33a89 (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
/*
 *
 * (c) 2008-2009 Laurent Vivier <Laurent@lvivier.info>
 *
 * This file has been copied from EMILE, http://emile.sf.net
 *
 */

#include "libext2.h"
#include "ext2.h"
#include "ext2_utils.h"

ext2_FILE* ext2_open(ext2_VOLUME *volume, const char* pathname)
{
	ext2_FILE *file;
	struct ext2_inode *inode;
	int ino;
	int ret;

	ino = ext2_seek_name(volume, pathname);
	if (ino == 0)
		return NULL;

	inode = (struct ext2_inode*)malloc(sizeof(struct ext2_inode));
	if (inode == NULL)
		return NULL;

	ret = ext2_get_inode(volume, ino, inode);
	if (ret == -1) {
		free(inode);
		return NULL;
	}
	if (S_ISLNK(inode->i_mode)) {
		static char buffer[1024];
		int i, last = 0;
		strcpy(buffer, pathname);
		for (i = 0; buffer[i]; i++)
			if (buffer[i] == '\\')
				last = i;
		buffer[last] = '\\';
		strcpy(buffer + last + 1, (char*)inode->i_block);
		ino = ext2_seek_name((ext2_VOLUME*)volume, buffer);
		if (ino == 0) {
			free(inode);
			return NULL;
		}
		ret = ext2_get_inode((ext2_VOLUME*)volume, ino, inode);
		if (ret == -1) {
			free(inode);
			return NULL;
		}
	}

	file = (ext2_FILE*)malloc(sizeof(ext2_FILE));
	if (file == NULL) {
		free(inode);
		return NULL;
	}
	file->volume = volume;
	file->inode = inode;
	file->offset = 0;
	file->path = strdup(pathname);

	return file;
}