diff options
Diffstat (limited to 'firmware/target/hosted/ypr0/fs-ypr0.c')
-rw-r--r-- | firmware/target/hosted/ypr0/fs-ypr0.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/firmware/target/hosted/ypr0/fs-ypr0.c b/firmware/target/hosted/ypr0/fs-ypr0.c new file mode 100644 index 0000000000..7f49a5f91a --- /dev/null +++ b/firmware/target/hosted/ypr0/fs-ypr0.c | |||
@@ -0,0 +1,141 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 by Thomas Martitz | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include <stdlib.h> | ||
23 | #include <sys/stat.h> /* stat() */ | ||
24 | #include <stdio.h> /* snprintf */ | ||
25 | #include <string.h> /* size_t */ | ||
26 | #include <dirent.h> | ||
27 | #include <time.h> /* localtime() */ | ||
28 | #include "system-target.h" | ||
29 | #include "dir-target.h" | ||
30 | #include "file.h" | ||
31 | #include "dir.h" | ||
32 | #include "rbpaths.h" | ||
33 | |||
34 | |||
35 | long filesize(int fd) | ||
36 | { | ||
37 | struct stat buf; | ||
38 | |||
39 | if (!fstat(fd, &buf)) | ||
40 | return buf.st_size; | ||
41 | else | ||
42 | return -1; | ||
43 | } | ||
44 | |||
45 | /* do we really need this in the app? */ | ||
46 | void fat_size(unsigned long* size, unsigned long* free) | ||
47 | { | ||
48 | *size = *free = 0; | ||
49 | } | ||
50 | |||
51 | #undef opendir | ||
52 | #undef closedir | ||
53 | #undef mkdir | ||
54 | #undef readdir | ||
55 | |||
56 | /* need to wrap around DIR* because we need to save the parent's | ||
57 | * directory path in order to determine dirinfo */ | ||
58 | struct __dir { | ||
59 | DIR *dir; | ||
60 | char *path; | ||
61 | }; | ||
62 | |||
63 | DIR* _opendir(const char *name) | ||
64 | { | ||
65 | char *buf = malloc(sizeof(struct __dir) + strlen(name)+1); | ||
66 | if (!buf) | ||
67 | return NULL; | ||
68 | |||
69 | struct __dir *this = (struct __dir*)buf; | ||
70 | |||
71 | this->path = buf+sizeof(struct __dir); | ||
72 | /* definitely fits due to strlen() */ | ||
73 | strcpy(this->path, name); | ||
74 | |||
75 | this->dir = opendir(name); | ||
76 | |||
77 | if (!this->dir) | ||
78 | { | ||
79 | free(buf); | ||
80 | return NULL; | ||
81 | } | ||
82 | return (DIR*)this; | ||
83 | } | ||
84 | |||
85 | int _mkdir(const char *name) | ||
86 | { | ||
87 | return mkdir(name, 0777); | ||
88 | } | ||
89 | |||
90 | int _closedir(DIR *dir) | ||
91 | { | ||
92 | struct __dir *this = (struct __dir*)dir; | ||
93 | int ret = closedir(this->dir); | ||
94 | free(this); | ||
95 | return ret; | ||
96 | } | ||
97 | |||
98 | struct dirent* _readdir(DIR* dir) | ||
99 | { | ||
100 | struct __dir *d = (struct __dir*)dir; | ||
101 | return readdir(d->dir); | ||
102 | } | ||
103 | |||
104 | struct dirinfo dir_get_info(DIR* _parent, struct dirent *dir) | ||
105 | { | ||
106 | struct __dir *parent = (struct __dir*)_parent; | ||
107 | struct stat s; | ||
108 | struct tm *tm = NULL; | ||
109 | struct dirinfo ret; | ||
110 | char path[MAX_PATH]; | ||
111 | |||
112 | snprintf(path, sizeof(path), "%s/%s", parent->path, dir->d_name); | ||
113 | memset(&ret, 0, sizeof(ret)); | ||
114 | |||
115 | if (!stat(path, &s)) | ||
116 | { | ||
117 | if (S_ISDIR(s.st_mode)) | ||
118 | { | ||
119 | ret.attribute = ATTR_DIRECTORY; | ||
120 | } | ||
121 | ret.size = s.st_size; | ||
122 | tm = localtime(&(s.st_mtime)); | ||
123 | } | ||
124 | |||
125 | if (!lstat(path, &s) && S_ISLNK(s.st_mode)) | ||
126 | { | ||
127 | ret.attribute |= ATTR_LINK; | ||
128 | } | ||
129 | |||
130 | if (tm) | ||
131 | { | ||
132 | ret.wrtdate = ((tm->tm_year - 80) << 9) | | ||
133 | ((tm->tm_mon + 1) << 5) | | ||
134 | tm->tm_mday; | ||
135 | ret.wrttime = (tm->tm_hour << 11) | | ||
136 | (tm->tm_min << 5) | | ||
137 | (tm->tm_sec >> 1); | ||
138 | } | ||
139 | |||
140 | return ret; | ||
141 | } | ||