forked from intel/zephyr.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzjs_file_utils.c
214 lines (180 loc) · 4.85 KB
/
zjs_file_utils.c
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
// Copyright (c) 2016-2018, Intel Corporation.
/**
* @file
* @brief Simulates the disk access to create a writtable memory section
* to help on the transactions between the UART and the Javascript fs_file_t
* this is a basic stub, do not expect a full implementation.
*/
// C includes
#if defined(ZJS_ASHELL) || defined(ZJS_DYNAMIC_LOAD)
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
// Zephyr includes
#include <arch/cpu.h>
#include <fs.h>
#include <device.h>
#include <init.h>
#include <misc/printk.h>
// ZJS includes
#include "zjs_file_utils.h"
#include "zjs_util.h"
#define FILE_ERR ERR_PRINT("Read file failed\n")
int fs_exist(const char *path)
{
int res;
struct fs_dirent entry;
res = fs_stat(path, &entry);
return !res;
}
fs_file_t *fs_open_alloc(const char *filename, const char *mode)
{
int res;
/* Delete file if exists */
if (mode[0] == 'w') {
if (fs_exist(filename)) {
/* Delete the file and verify checking its status */
res = fs_unlink(filename);
if (res) {
printk("Error deleting file [%d]\n", res);
return NULL;
}
}
} else {
/* Return NULL if trying to read from a nonexistent file */
if (!fs_exist(filename)) {
return NULL;
}
}
fs_file_t *file = (fs_file_t *)zjs_malloc(sizeof(fs_file_t));
res = fs_open(file, filename);
if (res) {
printk("Failed opening file [%d]\n", res);
return NULL;
}
return file;
}
ssize_t fs_size(fs_file_t *file)
{
if (fs_seek(file, 0, SEEK_END) != 0)
return -1;
off_t file_len = fs_tell(file);
fs_seek(file, 0, SEEK_SET);
return file_len;
}
int fs_close_alloc(fs_file_t *fp)
{
int res = fs_close(fp);
zjs_free(fp);
return res;
}
bool fs_valid_filename(char *filename)
{
if (filename == NULL) {
printf("No filename given\n");
return false;
}
char *ptr1 = filename;
char *ptr2 = NULL;
ssize_t namelen;
ssize_t extlen;
ssize_t size = strlen(filename);
ptr1 = strchr(ptr1, '.');
if (ptr1 != NULL) {
// Check there aren't multiple periods
ptr2 = strchr(ptr1 + 1, '.');
if (ptr2 != NULL) {
printf("Invalid file extension format\n");
return false;
}
namelen = ptr1 - filename;
extlen = size - namelen - 1;
} else {
// Filename has no extension
namelen = size;
extlen = 0;
}
if (namelen == 0) {
printf("Filename length is zero\n");
return false;
} else if (namelen > 8 || extlen > 3) {
printf("Filename must be 8.3 format\n");
return false;
}
return true;
}
int fs_get_boot_cfg_filename(const char *timestamp, char *filename)
{
fs_file_t *file;
size_t count;
file = fs_open_alloc("boot.cfg", "r");
if (!file) {
DBG_PRINT("failed to open boot.cfg\n");
return -1;
}
size_t tssize;
if (timestamp) {
tssize = strlen(timestamp);
} else {
tssize = strlen(__DATE__ " " __TIME__ "\n");
}
ssize_t size = fs_size(file);
// Check that there is something after the timestamp
if (size > tssize) {
char ts[tssize];
count = fs_read(file, ts, tssize);
if (count == tssize) {
// if there's timestamp, check against timestamp
if (!timestamp ||
(timestamp && strncmp(ts, timestamp, tssize) == 0)) {
size_t filenamesize = size - tssize;
count = fs_read(file, filename, filenamesize);
filename[filenamesize] = '\0';
if (count > 0) {
fs_close_alloc(file);
return 0;
}
}
}
}
// boot.cfg is invalid, remove it
if (fs_unlink("boot.cfg") != 0) {
DBG_PRINT("cannot remove boot.cfg\n");
}
fs_close_alloc(file);
return -1;
}
char *read_file_alloc(const char *file_name, ssize_t *size)
{
char *file_buf = NULL;
fs_file_t *fp = fs_open_alloc(file_name, "r");
if (fp == NULL)
return NULL;
*size = fs_size(fp);
if (*size == 0) {
DBG_PRINT("Empty file (%s)\n", file_name);
FILE_ERR;
fs_close_alloc(fp);
return NULL;
}
file_buf = (char *)zjs_malloc(*size);
if (file_buf == NULL) {
DBG_PRINT("Not enough memory for (%s)\n", file_name);
FILE_ERR;
fs_close_alloc(fp);
return NULL;
}
ssize_t brw = fs_read(fp, file_buf, *size);
if (brw != *size) {
DBG_PRINT("Read failed for (%s)\n", file_name);
FILE_ERR;
fs_close_alloc(fp);
zjs_free(file_buf);
return NULL;
}
fs_close_alloc(fp);
return file_buf;
}
#endif // defined(ZJS_ASHELL) || defined(ZJS_DYNAMIC_LOAD)