summaryrefslogtreecommitdiff
path: root/content/fetchers
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2014-05-07 16:14:18 +0100
committerVincent Sanders <vince@kyllikki.org>2014-05-07 16:24:51 +0100
commitc56642819eed87431dff3446fe111f7f3eefaa7d (patch)
tree397126ca4baac425f9c1c44f3d5c56c681e2c4fe /content/fetchers
parentc1e2da80dfa62793ea107cf12408c814e268a54b (diff)
downloadnetsurf-c56642819eed87431dff3446fe111f7f3eefaa7d.tar.gz
netsurf-c56642819eed87431dff3446fe111f7f3eefaa7d.tar.bz2
add file operations table and make all frontends use it.
This rationalises the path construction and basename file operations. The default implementation is POSIX which works for all frontends except windows, riscos and amiga which have differeing path separators and rules. These implementations are significantly more robust than the previous nine implementations and also do not use unsafe strncpy or buffers with arbitrary length limits. These implementations also carry full documentation comments.
Diffstat (limited to 'content/fetchers')
-rw-r--r--content/fetchers/curl.c11
-rw-r--r--content/fetchers/file.c186
2 files changed, 112 insertions, 85 deletions
diff --git a/content/fetchers/curl.c b/content/fetchers/curl.c
index 612b77d0b..80ac5ec89 100644
--- a/content/fetchers/curl.c
+++ b/content/fetchers/curl.c
@@ -50,6 +50,7 @@
#include "utils/utils.h"
#include "utils/ring.h"
#include "utils/useragent.h"
+#include "utils/file.h"
#include "content/fetch.h"
#include "content/fetchers/curl.h"
@@ -1280,15 +1281,15 @@ fetch_curl_post_convert(const struct fetch_multipart_data *control)
{
struct curl_httppost *post = 0, *last = 0;
CURLFORMcode code;
+ nserror ret;
for (; control; control = control->next) {
if (control->file) {
- char *leafname = 0;
-
- leafname = guit->fetch->filename_from_path(control->value);
-
- if (leafname == NULL)
+ char *leafname = NULL;
+ ret = guit->file->basename(control->value, &leafname, NULL);
+ if (ret != NSERROR_OK) {
continue;
+ }
/* We have to special case filenames of "", so curl
* a) actually attempts the fetch and
diff --git a/content/fetchers/file.c b/content/fetchers/file.c
index 00d5cc5f1..93e3bf84c 100644
--- a/content/fetchers/file.c
+++ b/content/fetchers/file.c
@@ -18,6 +18,8 @@
/* file: URL handling. Based on the data fetcher by Rob Kendrick */
+#include "utils/config.h"
+
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@@ -34,18 +36,12 @@
#include <limits.h>
#include <stdarg.h>
-#include "utils/config.h"
-
#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif
#include <libwapcaplet/libwapcaplet.h>
-#include "content/dirlist.h"
-#include "content/fetch.h"
-#include "content/fetchers/file.h"
-#include "content/urldb.h"
#include "desktop/netsurf.h"
#include "desktop/gui_factory.h"
#include "utils/corestrings.h"
@@ -56,6 +52,12 @@
#include "utils/url.h"
#include "utils/utils.h"
#include "utils/ring.h"
+#include "utils/file.h"
+
+#include "content/dirlist.h"
+#include "content/fetch.h"
+#include "content/urldb.h"
+#include "content/fetchers/file.h"
/* Maximum size of read buffer */
#define FETCH_FILE_MAX_BUF_SIZE (1024 * 1024)
@@ -489,6 +491,97 @@ static char *gen_nice_title(char *path)
return title;
}
+/**
+ * generate an output row of the directory listing.
+ *
+ * @param ent current directory entry.
+ */
+static nserror
+process_dir_ent(struct fetch_file_context *ctx,
+ struct dirent *ent,
+ bool even,
+ char *buffer,
+ size_t buffer_len)
+{
+ nserror ret;
+ char *path; /* url for list entries */
+ char *urlpath = NULL; /* buffer for leaf entry path */
+ struct stat ent_stat; /* stat result of leaf entry */
+ char datebuf[64]; /* buffer for date text */
+ char timebuf[64]; /* buffer for time text */
+
+ /* skip hidden files */
+ if (ent->d_name[0] == '.') {
+ return NSERROR_BAD_PARAMETER;
+ }
+
+ ret = netsurf_mkpath(&urlpath, NULL, 2, ctx->path, ent->d_name);
+ if (ret != NSERROR_OK) {
+ return ret;
+ }
+
+ if (stat(urlpath, &ent_stat) != 0) {
+ ent_stat.st_mode = 0;
+ datebuf[0] = 0;
+ timebuf[0] = 0;
+ } else {
+ /* Get date in output format */
+ if (strftime((char *)&datebuf, sizeof datebuf, "%a %d %b %Y",
+ localtime(&ent_stat.st_mtime)) == 0) {
+ datebuf[0] = '-';
+ datebuf[1] = 0;
+ }
+
+ /* Get time in output format */
+ if (strftime((char *)&timebuf, sizeof timebuf, "%H:%M",
+ localtime(&ent_stat.st_mtime)) == 0) {
+ timebuf[0] = '-';
+ timebuf[1] = 0;
+ }
+ }
+
+ if ((path = guit->fetch->path_to_url(urlpath)) == NULL) {
+ free(urlpath);
+ return NSERROR_NOMEM;
+ }
+
+ if (S_ISREG(ent_stat.st_mode)) {
+ /* regular file */
+ dirlist_generate_row(even,
+ false,
+ path,
+ ent->d_name,
+ guit->fetch->filetype(urlpath),
+ ent_stat.st_size,
+ datebuf, timebuf,
+ buffer, buffer_len);
+ } else if (S_ISDIR(ent_stat.st_mode)) {
+ /* directory */
+ dirlist_generate_row(even,
+ true,
+ path,
+ ent->d_name,
+ messages_get("FileDirectory"),
+ -1,
+ datebuf, timebuf,
+ buffer, buffer_len);
+ } else {
+ /* something else */
+ dirlist_generate_row(even,
+ false,
+ path,
+ ent->d_name,
+ "",
+ -1,
+ datebuf, timebuf,
+ buffer, buffer_len);
+ }
+
+ free(path);
+ free(urlpath);
+
+ return NSERROR_OK;
+}
static void fetch_file_process_dir(struct fetch_file_context *ctx,
struct stat *fdstat)
@@ -499,13 +592,7 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx,
char *title; /* pretty printed title */
nserror err; /* result from url routines */
nsurl *up; /* url of parent */
- char *path; /* url for list entries */
- struct stat ent_stat; /* stat result of leaf entry */
- char datebuf[64]; /* buffer for date text */
- char timebuf[64]; /* buffer for time text */
- char urlpath[PATH_MAX]; /* buffer for leaf entry path */
- struct dirent *ent; /* current directory entry */
struct dirent **listing = NULL; /* directory entry listing */
int i; /* directory entry index */
int n; /* number of directory entries */
@@ -570,78 +657,17 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx,
goto fetch_file_process_dir_aborted;
for (i = 0; i < n; i++) {
- ent = listing[i];
-
- if (ent->d_name[0] == '.')
- continue;
- strncpy(urlpath, ctx->path, sizeof urlpath);
- if (guit->fetch->path_add_part(urlpath, sizeof urlpath,
- ent->d_name) == false)
- continue;
-
- if (stat(urlpath, &ent_stat) != 0) {
- ent_stat.st_mode = 0;
- datebuf[0] = 0;
- timebuf[0] = 0;
- } else {
- /* Get date in output format */
- if (strftime((char *)&datebuf, sizeof datebuf,
- "%a %d %b %Y",
- localtime(&ent_stat.st_mtime)) == 0) {
- strncpy(datebuf, "-", sizeof datebuf);
- }
+ err = process_dir_ent(ctx, listing[i], even, buffer,
+ sizeof(buffer));
- /* Get time in output format */
- if (strftime((char *)&timebuf, sizeof timebuf,
- "%H:%M",
- localtime(&ent_stat.st_mtime)) == 0) {
- strncpy(timebuf, "-", sizeof timebuf);
- }
- }
-
- if((path = guit->fetch->path_to_url(urlpath)) == NULL)
- continue;
+ if (err == NSERROR_OK) {
+ msg.data.header_or_data.len = strlen(buffer);
+ if (fetch_file_send_callback(&msg, ctx))
+ goto fetch_file_process_dir_aborted;
- if (S_ISREG(ent_stat.st_mode)) {
- /* regular file */
- dirlist_generate_row(even,
- false,
- path,
- ent->d_name,
- guit->fetch->filetype(urlpath),
- ent_stat.st_size,
- datebuf, timebuf,
- buffer, sizeof(buffer));
- } else if (S_ISDIR(ent_stat.st_mode)) {
- /* directory */
- dirlist_generate_row(even,
- true,
- path,
- ent->d_name,
- messages_get("FileDirectory"),
- -1,
- datebuf, timebuf,
- buffer, sizeof(buffer));
- } else {
- /* something else */
- dirlist_generate_row(even,
- false,
- path,
- ent->d_name,
- "",
- -1,
- datebuf, timebuf,
- buffer, sizeof(buffer));
+ even = !even;
}
-
- free(path);
-
- msg.data.header_or_data.len = strlen(buffer);
- if (fetch_file_send_callback(&msg, ctx))
- goto fetch_file_process_dir_aborted;
-
- even = !even;
}
/* directory listing bottom */