taskrambler  v0.1.8
Web server and task management solution.
asset/asset.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <stdarg.h>
24 
25 // for mmap
26 #include <sys/mman.h>
27 
28 // for open and fstat
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 
33 // for access
34 #include <unistd.h>
35 
36 // for localtime
37 #include <time.h>
38 
39 
40 #include "class.h"
41 #include "asset.h"
42 #include "hash.h"
43 
44 #include "utils/mime_type.h"
45 #include "utils/hash.h"
46 #include "utils/http.h"
47 
48 
49 static
50 int
51 assetCtor(void * _this, va_list * params)
52 {
53  Asset this = _this;
54 
55  struct tm * tmp;
56  struct stat st;
57  char * fname = va_arg(*params, char*);
58  char * ext;
59 
60  this->nfname = va_arg(*params, size_t);
61 
62  strncpy(this->fname, fname, 2048);
63  this->fname[2048] = '\0';
64 
65  this->hash = sdbm(
66  (unsigned char *)this->fname,
67  this->nfname);
68 
69  if (-1 == access(this->fname, R_OK)) {
70  return -1;
71  } else {
72  this->handle = open(this->fname, O_RDONLY);
73  fstat(this->handle, &st);
74  }
75 
76  tmp = localtime(&(st.st_mtime));
77  this->netag = strftime(this->etag, sizeof(this->etag), "%s", tmp);
78  this->nmtime = rfc1123Gmt(
79  this->mtime,
80  sizeof(this->mtime),
81  &(st.st_mtime));
82 
83  this->size = st.st_size;
84 
85  ext = strrchr(this->fname, '.');
86  if (NULL != ext) {
87  ext++;
88  this->mime_type = getMimeType(ext, strlen(ext));
89  } else {
90  this->mime_type = "application/octet-stream";
91  }
92 
93  if (NULL != this->mime_type) {
94  this->nmime_type = strlen(this->mime_type);
95  } else {
96  this->nmime_type = 0;
97  }
98 
99  if (0 < this->size) {
100  this->data = mmap(
101  NULL, this->size, PROT_READ, MAP_PRIVATE, this->handle, 0);
102 
103  if (MAP_FAILED == this->data) {
104  close(this->handle);
105  return -1;
106  }
107  }
108 
109  this->ref_count = 1;
110 
111  return 0;
112 }
113 
114 static void assetDtor(void * _this) {
115  Asset this = _this;
116 
117  if (MAP_FAILED != this->data && NULL != this->data) {
118  munmap(this->data, this->size);
119  }
120 
121  if (0 < this->handle) {
122  close(this->handle);
123  }
124 }
125 
126 static
127 unsigned long
128 assetGetHash(void * _this)
129 {
130  Asset this = _this;
131 
132  return this->hash;
133 }
134 
135 static
136 void
137 assetHandleDouble(void * _this, void * _doub)
138 {
139 }
140 
141 INIT_IFACE(Class, assetCtor, assetDtor, NULL);
143 CREATE_CLASS(Asset, NULL, IFACE(Class), IFACE(Hashable));
144 
145 // vim: set ts=4 sw=4:
INIT_IFACE(Class, assetCtor, assetDtor, NULL)
static int assetCtor(void *_this, va_list *params)
Definition: asset/asset.c:51
unsigned long sdbm(const unsigned char *, size_t)
Definition: utils/hash.c:45
static unsigned long assetGetHash(void *_this)
Definition: asset/asset.c:128
char * getMimeType(const char *, size_t)
Definition: mime_type.c:84
#define IFACE(name)
Definition: interface.h:34
static size_t size
size_t rfc1123Gmt(char *, size_t, const time_t *)
Definition: utils/http.c:53
static void assetHandleDouble(void *_this, void *_doub)
Definition: asset/asset.c:137
static void assetDtor(void *_this)
Definition: asset/asset.c:114
CREATE_CLASS(Asset, NULL, IFACE(Class), IFACE(Hashable))