added timestamp support

This commit is contained in:
Fufu Fang 2018-07-24 06:44:41 +01:00
parent c43c0cd345
commit c1fcf109b0
3 changed files with 35 additions and 26 deletions

5
main.c
View File

@ -74,6 +74,9 @@ static int fs_getattr(const char *path, struct stat *stbuf)
if (!link) { if (!link) {
return -ENOENT; return -ENOENT;
} }
struct timespec spec;
spec.tv_sec = link->time;
stbuf->st_mtim = spec;
switch (link->type) { switch (link->type) {
case LINK_DIR: case LINK_DIR:
stbuf->st_mode = S_IFDIR | 0755; stbuf->st_mode = S_IFDIR | 0755;
@ -88,8 +91,6 @@ static int fs_getattr(const char *path, struct stat *stbuf)
return -ENOENT; return -ENOENT;
} }
} }
return res; return res;
} }

View File

@ -18,7 +18,7 @@ typedef struct {
} MemoryStruct; } MemoryStruct;
typedef enum { typedef enum {
FILESIZE = 's', FILESTAT = 's',
DATA = 'd' DATA = 'd'
} TransferType; } TransferType;
@ -43,11 +43,12 @@ static CURLM *curl_multi;
/* Link related */ /* Link related */
static Link *Link_new(const char *p_url, LinkType type); static Link *Link_new(const char *p_url, LinkType type);
static void Link_get_size(Link *this_link); static void Link_get_stat(Link *this_link);
static Link *path_to_Link_recursive(char *path, LinkTable *linktbl); static Link *path_to_Link_recursive(char *path, LinkTable *linktbl);
static CURL *Link_to_curl(Link *link); static CURL *Link_to_curl(Link *link);
static LinkType p_url_type(const char *p_url); static LinkType p_url_type(const char *p_url);
static char *url_append(const char *url, const char *sublink); static char *url_append(const char *url, const char *sublink);
static void link_set_stat(Link* this_link, CURL *curl);
/* LinkTable related */ /* LinkTable related */
static void LinkTable_add(LinkTable *linktbl, Link *link); static void LinkTable_add(LinkTable *linktbl, Link *link);
@ -170,29 +171,13 @@ static int curl_multi_perform_once()
url); url);
} else { } else {
/* Transfer successful, query the file size */ /* Transfer successful, query the file size */
if (transfer->type == FILESIZE) { if (transfer->type == FILESTAT) {
Link *this_link = transfer->link; link_set_stat(transfer->link, curl);
long http_resp;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (http_resp == HTTP_OK) {
double cl = 0;
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl);
if (cl == -1) {
/* Turns out not to be a file after all */
this_link->content_length = 0;
this_link->type = LINK_DIR;
} else {
this_link->content_length = cl;
this_link->type = LINK_FILE;
}
} else {
this_link->type = LINK_INVALID;
}
} }
} }
curl_multi_remove_handle(curl_multi, curl); curl_multi_remove_handle(curl_multi, curl);
/* clean up the handle, if we are querying the file size */ /* clean up the handle, if we are querying the file size */
if (transfer->type == FILESIZE) { if (transfer->type == FILESTAT) {
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
free(transfer); free(transfer);
} }
@ -467,7 +452,28 @@ static void LinkTable_add(LinkTable *linktbl, Link *link)
linktbl->links[linktbl->num - 1] = link; linktbl->links[linktbl->num - 1] = link;
} }
void Link_get_size(Link *this_link) static void link_set_stat(Link* this_link, CURL *curl)
{
long http_resp;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (http_resp == HTTP_OK) {
double cl = 0;
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl);
curl_easy_getinfo(curl, CURLINFO_FILETIME, &(this_link->time));
if (cl == -1) {
/* Turns out not to be a file after all */
this_link->content_length = 0;
this_link->type = LINK_DIR;
} else {
this_link->content_length = cl;
this_link->type = LINK_FILE;
}
} else {
this_link->type = LINK_INVALID;
}
}
void Link_get_stat(Link *this_link)
{ {
#ifdef HTTPDIRFS_INFO #ifdef HTTPDIRFS_INFO
fprintf(stderr, "Link_get_size(%s);\n", this_link->f_url); fprintf(stderr, "Link_get_size(%s);\n", this_link->f_url);
@ -476,13 +482,14 @@ void Link_get_size(Link *this_link)
if (this_link->type == LINK_FILE) { if (this_link->type == LINK_FILE) {
CURL *curl = Link_to_curl(this_link); CURL *curl = Link_to_curl(this_link);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1); curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
TransferStruct *transfer = malloc(sizeof(TransferStruct)); TransferStruct *transfer = malloc(sizeof(TransferStruct));
if (!transfer) { if (!transfer) {
fprintf(stderr, "Link_get_size(): malloc failed!\n"); fprintf(stderr, "Link_get_size(): malloc failed!\n");
} }
transfer->link = this_link; transfer->link = this_link;
transfer->type = FILESIZE; transfer->type = FILESTAT;
curl_easy_setopt(curl, CURLOPT_PRIVATE, transfer); curl_easy_setopt(curl, CURLOPT_PRIVATE, transfer);
nonblocking_transfer(curl); nonblocking_transfer(curl);
@ -500,7 +507,7 @@ void LinkTable_fill(LinkTable *linktbl)
strncpy(this_link->f_url, url, URL_LEN_MAX); strncpy(this_link->f_url, url, URL_LEN_MAX);
free(url); free(url);
if (this_link->type == LINK_FILE && !(this_link->content_length)) { if (this_link->type == LINK_FILE && !(this_link->content_length)) {
Link_get_size(this_link); Link_get_stat(this_link);
} }
} }
} }

View File

@ -35,6 +35,7 @@ struct Link {
LinkType type; LinkType type;
size_t content_length; size_t content_length;
LinkTable *next_table; LinkTable *next_table;
long time;
}; };
struct LinkTable { struct LinkTable {