aboutsummaryrefslogtreecommitdiff
path: root/fs/fat/file.c
blob: 89706117b9410c1be7363f268881e75640b36b2a (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
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
/*
 * file.c
 *
 * Mini "VFS" by Marcus Sundberg
 *
 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <config.h>
#include <malloc.h>
#include <fat.h>
#include <linux/stat.h>
#include <linux/time.h>

/* Supported filesystems */
static const struct filesystem filesystems[] = {
	{ file_fat_detectfs,  file_fat_ls,  file_fat_read,  "FAT" },
};
#define NUM_FILESYS	(sizeof(filesystems)/sizeof(struct filesystem))

/* The filesystem which was last detected */
static int current_filesystem = FSTYPE_NONE;

/* The current working directory */
#define CWD_LEN		511
char file_cwd[CWD_LEN+1] = "/";

const char *
file_getfsname(int idx)
{
	if (idx < 0 || idx >= NUM_FILESYS)
		return NULL;

	return filesystems[idx].name;
}

static void
pathcpy(char *dest, const char *src)
{
	char *origdest = dest;

	do {
		if (dest-file_cwd >= CWD_LEN) {
			*dest = '\0';
			return;
		}
		*(dest) = *(src);
		if (*src == '\0') {
			if (dest-- != origdest && ISDIRDELIM(*dest)) {
				*dest = '\0';
			}
			return;
		}
		++dest;

		if (ISDIRDELIM(*src))
			while (ISDIRDELIM(*src)) src++;
		else
			src++;
	} while (1);
}

int
file_cd(const char *path)
{
	if (ISDIRDELIM(*path)) {
		while (ISDIRDELIM(*path)) path++;
		strncpy(file_cwd+1, path, CWD_LEN-1);
	} else {
		const char *origpath = path;
		char *tmpstr = file_cwd;
		int back = 0;

		while (*tmpstr != '\0') tmpstr++;
		do {
			tmpstr--;
		} while (ISDIRDELIM(*tmpstr));

		while (*path == '.') {
			path++;
			while (*path == '.') {
				path++;
				back++;
			}
			if (*path != '\0' && !ISDIRDELIM(*path)) {
				path = origpath;
				back = 0;
				break;
			}
			while (ISDIRDELIM(*path)) path++;
			origpath = path;
		}

		while (back--) {
			/* Strip off path component */
			while (!ISDIRDELIM(*tmpstr)) {
				tmpstr--;
			}
			if (tmpstr == file_cwd) {
				/* Incremented again right after the loop. */
				tmpstr--;
				break;
			}
			/* Skip delimiters */
			while (ISDIRDELIM(*tmpstr)) tmpstr--;
		}
		tmpstr++;
		if (*path == '\0') {
			if (tmpstr == file_cwd) {
				*tmpstr = '/';
				tmpstr++;
			}
			*tmpstr = '\0';
			return 0;
		}
		*tmpstr = '/';
		pathcpy(tmpstr+1, path);
	}

	return 0;
}

int
file_detectfs(void)
{
	int i;

	current_filesystem = FSTYPE_NONE;

	for (i = 0; i < NUM_FILESYS; i++) {
		if (filesystems[i].detect() == 0) {
			strcpy(file_cwd, "/");
			current_filesystem = i;
			break;
		}
	}

	return current_filesystem;
}

int
file_ls(const char *dir)
{
	char fullpath[1024];
	const char *arg;

	if (current_filesystem == FSTYPE_NONE) {
		printf("Can't list files without a filesystem!\n");
		return -1;
	}

	if (ISDIRDELIM(*dir)) {
		arg = dir;
	} else {
		sprintf(fullpath, "%s/%s", file_cwd, dir);
		arg = fullpath;
	}
	return filesystems[current_filesystem].ls(arg);
}

int file_read(const char *filename, void *buffer, int maxsize)
{
	char fullpath[1024];
	const char *arg;

	if (current_filesystem == FSTYPE_NONE) {
		printf("Can't load file without a filesystem!\n");
		return -1;
	}

	if (ISDIRDELIM(*filename)) {
		arg = filename;
	} else {
		sprintf(fullpath, "%s/%s", file_cwd, filename);
		arg = fullpath;
	}

	return filesystems[current_filesystem].read(arg, buffer, maxsize);
}