summaryrefslogtreecommitdiff
path: root/content/fetchers/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'content/fetchers/file.c')
-rw-r--r--content/fetchers/file.c186
1 files changed, 106 insertions, 80 deletions
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 */