NetSurf
file.c
Go to the documentation of this file.
1/*
2 * Copyright 2014, 2015 Vincent Sanders <vince@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * \file
21 * Windows file operation table implementation.
22 */
23
24#include <stdbool.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28#include <windows.h>
29
30#include "utils/errors.h"
31#include "utils/nsurl.h"
32#include "utils/log.h"
33#include "utils/utils.h"
34#include "utils/corestrings.h"
35#include "utils/url.h"
36#include "utils/file.h"
37#include "utils/string.h"
39
40#include "windows/file.h"
41
42/**
43 * Generate a windows path from one or more component elemnts.
44 *
45 * If a string is allocated it must be freed by the caller.
46 *
47 * @param[in,out] str pointer to string pointer if this is NULL enough
48 * storage will be allocated for the complete path.
49 * @param[in,out] size The size of the space available if \a str not
50 * NULL on input and if not NULL set to the total
51 * output length on output.
52 * @param[in] nelm The number of elements.
53 * @param[in] ap The elements of the path as string pointers.
54 * @return NSERROR_OK and the complete path is written to str
55 * or error code on faliure.
56 */
57static nserror windows_mkpath(char **str, size_t *size, size_t nelm, va_list ap)
58{
59 return vsnstrjoin(str, size, '\\', nelm, ap);
60}
61
62
63/**
64 * Get the basename of a file using windows path handling.
65 *
66 * This gets the last element of a path and returns it.
67 *
68 * @param[in] path The path to extract the name from.
69 * @param[in,out] str Pointer to string pointer if this is NULL enough
70 * storage will be allocated for the path element.
71 * @param[in,out] size The size of the space available if \a
72 * str not NULL on input and set to the total
73 * output length on output.
74 * @return NSERROR_OK and the complete path is written to str
75 * or error code on faliure.
76 */
77static nserror windows_basename(const char *path, char **str, size_t *size)
78{
79 const char *leafname;
80 char *fname;
81
82 if (path == NULL) {
84 }
85
86 leafname = strrchr(path, '\\');
87 if (!leafname) {
88 leafname = path;
89 } else {
90 leafname += 1;
91 }
92
93 fname = strdup(leafname);
94 if (fname == NULL) {
95 return NSERROR_NOMEM;
96 }
97
98 *str = fname;
99 if (size != NULL) {
100 *size = strlen(fname);
101 }
102 return NSERROR_OK;
103}
104
105
106/**
107 * Create a path from a nsurl using windows file handling.
108 *
109 * @param[in] url The url to encode.
110 * @param[out] path_out A string containing the result path which should
111 * be freed by the caller.
112 * @return NSERROR_OK and the path is written to \a path or error code
113 * on faliure.
114 */
115static nserror windows_nsurl_to_path(struct nsurl *url, char **path_out)
116{
117 lwc_string *urlpath;
118 char *path;
119 bool match;
120 lwc_string *scheme;
121 nserror res;
122
123 if ((url == NULL) || (path_out == NULL)) {
125 }
126
127 scheme = nsurl_get_component(url, NSURL_SCHEME);
128
129 if (lwc_string_caseless_isequal(scheme, corestring_lwc_file,
130 &match) != lwc_error_ok)
131 {
133 }
134 lwc_string_unref(scheme);
135 if (match == false) {
137 }
138
139 urlpath = nsurl_get_component(url, NSURL_PATH);
140 if (urlpath == NULL) {
142 }
143
144 res = url_unescape(lwc_string_data(urlpath),
145 lwc_string_length(urlpath),
146 NULL,
147 &path);
148 lwc_string_unref(urlpath);
149 if (res != NSERROR_OK) {
150 return res;
151 }
152
153 /* if there is a drive: prefix treat path as DOS filename */
154 if ((path[2] == ':') || (path[2] == '|')) {
155 char *sidx; /* slash index */
156
157 /* move the string down to remove leading / note the
158 * strlen is *not* copying too much data as we are
159 * moving the null too!
160 */
161 memmove(path, path + 1, strlen(path));
162
163 /* swap / for \ */
164 sidx = strrchr(path, '/');
165 while (sidx != NULL) {
166 *sidx = '\\';
167 sidx = strrchr(path, '/');
168 }
169 }
170 /* if the path does not have a drive letter we return the
171 * complete path.
172 */
173 /** @todo Need to check returning the unaltered path in this
174 * case is correct
175 */
176
177 *path_out = path;
178
179 return NSERROR_OK;
180}
181
182
183/**
184 * Create a nsurl from a path using windows file handling.
185 *
186 * Perform the necessary operations on a path to generate a nsurl.
187 *
188 * @param[in] path The path to convert.
189 * @param[out] url_out pointer to recive the nsurl, The returned url
190 * should be unreferenced by the caller.
191 * @return NSERROR_OK and the url is placed in \a url or error code on
192 * faliure.
193 */
194static nserror windows_path_to_nsurl(const char *path, struct nsurl **url_out)
195{
196 nserror ret;
197 int urllen;
198 char *urlstr;
199 char *sidx; /* slash index */
200
201 if ((path == NULL) || (url_out == NULL) || (*path == 0)) {
203 }
204
205 /* build url as a string for nsurl constructor */
206 urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 5;
207 urlstr = malloc(urllen);
208 if (urlstr == NULL) {
209 return NSERROR_NOMEM;
210 }
211
212 /** @todo check if this should be url escaping the path. */
213 if (*path == '/') {
214 /* unix style path start, so try wine Z: */
215 snprintf(urlstr, urllen, "%sZ%%3A%s", FILE_SCHEME_PREFIX, path);
216 } else {
217 snprintf(urlstr, urllen, "%s%s", FILE_SCHEME_PREFIX, path);
218 }
219
220 sidx = strrchr(urlstr, '\\');
221 while (sidx != NULL) {
222 *sidx = '/';
223 sidx = strrchr(urlstr, '\\');
224 }
225
226 ret = nsurl_create(urlstr, url_out);
227 free(urlstr);
228
229 return ret;
230}
231
232
233/**
234 * Ensure that all directory elements needed to store a filename exist.
235 *
236 * @param fname The filename to ensure the path to exists.
237 * @return NSERROR_OK on success or error code on failure.
238 */
239static nserror windows_mkdir_all(const char *fname)
240{
241 char *dname;
242 char *sep;
243 struct stat sb;
244
245 dname = strdup(fname);
246
247 sep = strrchr(dname, '\\');
248 if (sep == NULL) {
249 /* no directory separator path is just filename so its ok */
250 free(dname);
251 return NSERROR_OK;
252 }
253
254 *sep = 0; /* null terminate directory path */
255
256 if (stat(dname, &sb) == 0) {
257 free(dname);
258 if (S_ISDIR(sb.st_mode)) {
259 /* path to file exists and is a directory */
260 return NSERROR_OK;
261 }
263 }
264 *sep = '\\'; /* restore separator */
265
266 sep = dname;
267 while (*sep == '\\') {
268 sep++;
269 }
270 while ((sep = strchr(sep, '\\')) != NULL) {
271 *sep = 0;
272 if (stat(dname, &sb) != 0) {
273 if (nsmkdir(dname, S_IRWXU) != 0) {
274 /* could not create path element */
275 free(dname);
276 return NSERROR_NOT_FOUND;
277 }
278 } else {
279 if (! S_ISDIR(sb.st_mode)) {
280 /* path element not a directory */
281 free(dname);
283 }
284 }
285 *sep = '\\'; /* restore separator */
286 /* skip directory separators */
287 while (*sep == '\\') {
288 sep++;
289 }
290 }
291
292 free(dname);
293 return NSERROR_OK;
294}
295
296/* windows file handling */
297static struct gui_file_table file_table = {
299 .basename = windows_basename,
300 .nsurl_to_path = windows_nsurl_to_path,
301 .path_to_nsurl = windows_path_to_nsurl,
302 .mkdir_all = windows_mkdir_all,
303};
304
Browser window creation and manipulation interface.
Useful interned string pointers (interface).
#define FILE_SCHEME_PREFIX_LEN
File url prefix length.
Definition: corestrings.h:33
#define FILE_SCHEME_PREFIX
File url prefix.
Definition: corestrings.h:30
Error codes.
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOT_FOUND
Requested item not found.
Definition: errors.h:34
@ NSERROR_BAD_PARAMETER
Bad Parameter.
Definition: errors.h:48
@ NSERROR_NOT_DIRECTORY
Missing directory.
Definition: errors.h:35
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
static nserror windows_nsurl_to_path(struct nsurl *url, char **path_out)
Create a path from a nsurl using windows file handling.
Definition: file.c:115
struct gui_file_table * win32_file_table
Definition: file.c:305
static nserror windows_basename(const char *path, char **str, size_t *size)
Get the basename of a file using windows path handling.
Definition: file.c:77
static nserror windows_mkpath(char **str, size_t *size, size_t nelm, va_list ap)
Generate a windows path from one or more component elemnts.
Definition: file.c:57
static nserror windows_path_to_nsurl(const char *path, struct nsurl **url_out)
Create a nsurl from a path using windows file handling.
Definition: file.c:194
static struct gui_file_table file_table
Definition: file.c:297
static nserror windows_mkdir_all(const char *fname)
Ensure that all directory elements needed to store a filename exist.
Definition: file.c:239
Windows file operation table interface.
NetSurf URL handling (interface).
nserror nsurl_create(const char *const url_s, nsurl **url)
Create a NetSurf URL object from a URL string.
lwc_string * nsurl_get_component(const nsurl *url, nsurl_component part)
Get part of a URL as a lwc_string, from a NetSurf URL object.
@ NSURL_SCHEME
Definition: nsurl.h:45
@ NSURL_PATH
Definition: nsurl.h:52
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
Interface to utility string handling.
nserror vsnstrjoin(char **str, size_t *size, char sep, size_t nelm, va_list ap)
Generate a string from one or more component elements separated with a single value.
Definition: utils.c:107
/brief function table for file and filename operations.
Definition: file.h:50
nserror(* mkpath)(char **str, size_t *size, size_t nemb, va_list ap)
Generate a path from one or more component elemnts.
Definition: file.h:68
nserror url_unescape(const char *str, size_t length, size_t *length_out, char **result_out)
Convert an escaped string to plain.
Definition: url.c:67
Interface to URL parsing and joining operations.
Default operations table for files.
Interface to a number of general purpose functionality.
#define nsmkdir(dir, mode)
POSIX mkdir function.
Definition: utils.h:64
static nserror path(const struct redraw_context *ctx, const plot_style_t *pstyle, const float *p, unsigned int n, const float transform[6])
Plots a path.
Definition: plot.c:821