更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
fullpath.c 文件参考

浏览源代码.

函数

static unsigned int vfs_strnlen (const char *str, size_t maxlen)
 
static char * str_path (char *path)
 
static void str_remove_path_end_slash (char *dest, const char *fullpath)
 
static char * str_normalize_path (char *fullpath)
 
static int vfs_normalize_path_parame_check (const char *filename, char **pathname)
 
static char * vfs_not_absolute_path (const char *directory, const char *filename, char **pathname, int namelen)
 
static char * vfs_normalize_fullpath (const char *directory, const char *filename, char **pathname, int namelen)
 
int vfs_normalize_path (const char *directory, const char *filename, char **pathname)
 
int vfs_normalize_pathat (int dirfd, const char *filename, char **pathname)
 

函数说明

◆ str_normalize_path()

static char * str_normalize_path ( char *  fullpath)
static

在文件 fullpath.c93 行定义.

94{
95 char *dest = fullpath;
96 char *src = fullpath;
97
98 /* 2: The position of the path character: / and the end character /0 */
99
100 while (*src != '\0') {
101 if (*src == '.') {
102 if (*(src + 1) == '/') {
103 src += 2;
104 continue;
105 } else if (*(src + 1) == '.') {
106 if ((*(src + 2) == '/') || (*(src + 2) == '\0')) {
107 src += 2;
108 } else {
109 while ((*src != '\0') && (*src != '/')) {
110 *dest++ = *src++;
111 }
112 continue;
113 }
114 } else {
115 *dest++ = *src++;
116 continue;
117 }
118 } else {
119 *dest++ = *src++;
120 continue;
121 }
122
123 if ((dest - 1) != fullpath) {
124 dest--;
125 }
126
127 while ((dest > fullpath) && (*(dest - 1) != '/')) {
128 dest--;
129 }
130
131 if (*src == '/') {
132 src++;
133 }
134 }
135
136 *dest = '\0';
137
138 /* remove '/' in the end of path if exist */
139
140 dest--;
141
142 str_remove_path_end_slash(dest, fullpath);
143 return dest;
144}
static void str_remove_path_end_slash(char *dest, const char *fullpath)
Definition: fullpath.c:82
函数调用图:
这是这个函数的调用关系图:

◆ str_path()

static char * str_path ( char *  path)
static

在文件 fullpath.c63 行定义.

64{
65 char *dest = path;
66 char *src = path;
67
68 while (*src != '\0') {
69 if (*src == '/') {
70 *dest++ = *src++;
71 while (*src == '/') {
72 src++;
73 }
74 continue;
75 }
76 *dest++ = *src++;
77 }
78 *dest = '\0';
79 return path;
80}
这是这个函数的调用关系图:

◆ str_remove_path_end_slash()

static void str_remove_path_end_slash ( char *  dest,
const char *  fullpath 
)
static

在文件 fullpath.c82 行定义.

83{
84 if ((*dest == '.') && (*(dest - 1) == '/')) {
85 *dest = '\0';
86 dest--;
87 }
88 if ((dest != fullpath) && (*dest == '/')) {
89 *dest = '\0';
90 }
91}
这是这个函数的调用关系图:

◆ vfs_normalize_fullpath()

static char * vfs_normalize_fullpath ( const char *  directory,
const char *  filename,
char **  pathname,
int  namelen 
)
static

在文件 fullpath.c213 行定义.

214{
215 char *fullpath = NULL;
216
217 if (filename[0] != '/') {
218 /* not a absolute path */
219
220 fullpath = vfs_not_absolute_path(directory, filename, pathname, namelen);
221 if (fullpath == NULL) {
222 return (char *)NULL;
223 }
224 } else {
225 /* it's a absolute path, use it directly */
226
227 fullpath = strdup(filename); /* copy string */
228
229 if (fullpath == NULL) {
230 *pathname = NULL;
231 set_errno(ENOMEM);
232 return (char *)NULL;
233 }
234 if (filename[1] == '/') {
235 *pathname = NULL;
236 free(fullpath);
237 set_errno(EINVAL);
238 return (char *)NULL;
239 }
240 }
241
242 return fullpath;
243}
static char * vfs_not_absolute_path(const char *directory, const char *filename, char **pathname, int namelen)
Definition: fullpath.c:181
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
函数调用图:
这是这个函数的调用关系图:

◆ vfs_normalize_path()

int vfs_normalize_path ( const char *  directory,
const char *  filename,
char **  pathname 
)

在文件 fullpath.c245 行定义.

246{
247 char *fullpath = NULL;
248 int namelen;
249#ifdef VFS_USING_WORKDIR
250 UINTPTR lock_flags;
252 BOOL dir_flags = (directory == NULL) ? TRUE : FALSE;
253#endif
254
255 namelen = vfs_normalize_path_parame_check(filename, pathname);
256 if (namelen < 0) {
257 return namelen;
258 }
259
260#ifdef VFS_USING_WORKDIR
261 if (directory == NULL)
262 {
263 spin_lock_irqsave(&curr->files->workdir_lock, lock_flags);
264 directory = curr->files->workdir;
265 }
266#else
267 if ((directory == NULL) && (filename[0] != '/')) {
268 PRINT_ERR("NO_WORKING_DIR\n");
269 *pathname = NULL;
270 return -EINVAL;
271 }
272#endif
273
274 /* 2: The position of the path character: / and the end character /0 */
275
276 if ((filename[0] != '/') && (strlen(directory) + namelen + 2 > TEMP_PATH_MAX)) {
277#ifdef VFS_USING_WORKDIR
278 if (dir_flags == TRUE)
279 {
280 spin_unlock_irqrestore(&curr->files->workdir_lock, lock_flags);
281 }
282#endif
283 return -ENAMETOOLONG;
284 }
285
286 fullpath = vfs_normalize_fullpath(directory, filename, pathname, namelen);
287#ifdef VFS_USING_WORKDIR
288 if (dir_flags == TRUE)
289 {
290 spin_unlock_irqrestore(&curr->files->workdir_lock, lock_flags);
291 }
292#endif
293 if (fullpath == NULL) {
294 return -get_errno();
295 }
296
297 (void)str_path(fullpath);
298 (void)str_normalize_path(fullpath);
299 if (strlen(fullpath) >= PATH_MAX) {
300 *pathname = NULL;
301 free(fullpath);
302 return -ENAMETOOLONG;
303 }
304
305 *pathname = fullpath;
306 return ENOERR;
307}
static char * vfs_normalize_fullpath(const char *directory, const char *filename, char **pathname, int namelen)
Definition: fullpath.c:213
static char * str_path(char *path)
Definition: fullpath.c:63
static char * str_normalize_path(char *fullpath)
Definition: fullpath.c:93
static int vfs_normalize_path_parame_check(const char *filename, char **pathname)
Definition: fullpath.c:146
STATIC INLINE LosProcessCB * OsCurrProcessGet(VOID)
unsigned long UINTPTR
Definition: los_typedef.h:68
size_t BOOL
Definition: los_typedef.h:88
struct files_struct * files
spinlock_t workdir_lock
工作区目录自旋锁
Definition: fd_table.h:99
char workdir[PATH_MAX]
工作区路径,最大 256个字符
Definition: fd_table.h:100
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
函数调用图:
这是这个函数的调用关系图:

◆ vfs_normalize_path_parame_check()

static int vfs_normalize_path_parame_check ( const char *  filename,
char **  pathname 
)
static

在文件 fullpath.c146 行定义.

147{
148 int namelen;
149 char *name = NULL;
150
151 if (pathname == NULL) {
152 return -EINVAL;
153 }
154
155 /* check parameters */
156
157 if (filename == NULL) {
158 *pathname = NULL;
159 return -EINVAL;
160 }
161
162 namelen = vfs_strnlen(filename, PATH_MAX);
163 if (!namelen) {
164 *pathname = NULL;
165 return -EINVAL;
166 } else if (namelen >= PATH_MAX) {
167 *pathname = NULL;
168 return -ENAMETOOLONG;
169 }
170
171 for (name = (char *)filename + namelen; ((name != filename) && (*name != '/')); name--) {
172 if (strlen(name) > NAME_MAX) {
173 *pathname = NULL;
174 return -ENAMETOOLONG;
175 }
176 }
177
178 return namelen;
179}
static unsigned int vfs_strnlen(const char *str, size_t maxlen)
Definition: fullpath.c:52
函数调用图:
这是这个函数的调用关系图:

◆ vfs_normalize_pathat()

int vfs_normalize_pathat ( int  dirfd,
const char *  filename,
char **  pathname 
)

在文件 fullpath.c309 行定义.

310{
311 /* Get path by dirfd*/
312 char *relativeoldpath = NULL;
313 char *fullpath = NULL;
314 int ret = 0;
315
316 ret = get_path_from_fd(dirfd, &relativeoldpath);
317 if (ret < 0) {
318 return ret;
319 }
320
321 ret = vfs_normalize_path((const char *)relativeoldpath, filename, &fullpath);
322 if (relativeoldpath) {
323 free(relativeoldpath);
324 }
325
326 if (ret < 0) {
327 return ret;
328 }
329
330 *pathname = fullpath;
331 return ret;
332}
int vfs_normalize_path(const char *directory, const char *filename, char **pathname)
Definition: fullpath.c:245
函数调用图:
这是这个函数的调用关系图:

◆ vfs_not_absolute_path()

static char * vfs_not_absolute_path ( const char *  directory,
const char *  filename,
char **  pathname,
int  namelen 
)
static

在文件 fullpath.c181 行定义.

182{
183 int ret;
184 char *fullpath = NULL;
185
186 /* 2: The position of the path character: / and the end character /0 */
187
188 if ((namelen > 1) && (filename[0] == '.') && (filename[1] == '/')) {
189 filename += 2;
190 }
191
192 fullpath = (char *)malloc(strlen(directory) + namelen + 2);
193 if (fullpath == NULL) {
194 *pathname = NULL;
195 set_errno(ENOMEM);
196 return (char *)NULL;
197 }
198
199 /* join path and file name */
200
201 ret = snprintf_s(fullpath, strlen(directory) + namelen + 2, strlen(directory) + namelen + 1,
202 "%s/%s", directory, filename);
203 if (ret < 0) {
204 *pathname = NULL;
205 free(fullpath);
206 set_errno(ENAMETOOLONG);
207 return (char *)NULL;
208 }
209
210 return fullpath;
211}
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
函数调用图:
这是这个函数的调用关系图:

◆ vfs_strnlen()

static unsigned int vfs_strnlen ( const char *  str,
size_t  maxlen 
)
static

在文件 fullpath.c52 行定义.

53{
54 const char *p = NULL;
55
56 for (p = str; ((maxlen-- != 0) && (*p != '\0')); ++p) {}
57
58 return p - str;
59}
这是这个函数的调用关系图: