4 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2014, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
36 #define snprintf _snprintf /* Visual Studio */
39 #include <sys/types.h>
45 #include "target_manager.h"
48 #include <curl/curl.h>
49 #include "fcgi_stdio.h"
50 #define logstream FCGI_stdout
52 #define FCGI_stdout stdout
53 #define FCGI_stderr stderr
54 #define logstream stderr
57 targetlist_param_t * gene_targetlist(void)
59 targetlist_param_t *targetlist;
61 targetlist = (targetlist_param_t *)opj_malloc(sizeof(targetlist_param_t));
63 targetlist->first = NULL;
64 targetlist->last = NULL;
71 * open jp2 format image file
73 * @param[in] filepath file name (.jp2)
74 * @param[out] tmpfname new file name if filepath is a URL
75 * @return file descriptor
77 int open_jp2file(const char filepath[], char tmpfname[]);
79 target_param_t * gene_target(targetlist_param_t *targetlist, char *targetpath)
81 target_param_t *target;
83 index_param_t *jp2idx;
84 char tmpfname[MAX_LENOFTID];
85 static int last_csn = 0;
87 if (targetpath[0] == '\0') {
88 fprintf(FCGI_stderr, "Error: exception, no targetpath in gene_target()\n");
92 if ((fd = open_jp2file(targetpath, tmpfname)) == -1) {
93 fprintf(FCGI_stdout, "Status: 404\r\n");
97 if (!(jp2idx = parse_jp2file(fd))) {
98 fprintf(FCGI_stdout, "Status: 501\r\n");
102 target = (target_param_t *)opj_malloc(sizeof(target_param_t));
103 snprintf(target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL),
104 (unsigned int)rand());
105 target->targetname = strdup(targetpath);
109 target->tmpfname = strdup(tmpfname);
111 target->tmpfname = NULL;
114 target->csn = last_csn++;
115 target->codeidx = jp2idx;
116 target->num_of_use = 0;
117 target->jppstream = OPJ_TRUE;
118 target->jptstream = isJPTfeasible(*jp2idx);
121 if (targetlist->first) { /* there are one or more entries*/
122 targetlist->last->next = target;
123 } else { /* first entry*/
124 targetlist->first = target;
126 targetlist->last = target;
129 fprintf(logstream, "local log: target %s generated\n", targetpath);
135 void refer_target(target_param_t *reftarget, target_param_t **ptr)
138 reftarget->num_of_use++;
141 void unrefer_target(target_param_t *target)
143 target->num_of_use--;
146 void delete_target(target_param_t **target)
148 close((*target)->fd);
151 if ((*target)->tmpfname) {
152 fprintf(FCGI_stderr, "Temporal file %s is deleted\n", (*target)->tmpfname);
153 remove((*target)->tmpfname);
157 if ((*target)->codeidx) {
158 delete_index(&(*target)->codeidx);
162 fprintf(logstream, "local log: target: %s deleted\n", (*target)->targetname);
165 opj_free((*target)->targetname);
170 void delete_target_in_list(target_param_t **target,
171 targetlist_param_t *targetlist)
175 if (*target == targetlist->first) {
176 targetlist->first = (*target)->next;
178 ptr = targetlist->first;
179 while (ptr->next != *target) {
183 ptr->next = (*target)->next;
185 if (*target == targetlist->last) {
186 targetlist->last = ptr;
189 delete_target(target);
192 void delete_targetlist(targetlist_param_t **targetlist)
194 target_param_t *targetPtr, *targetNext;
196 targetPtr = (*targetlist)->first;
197 while (targetPtr != NULL) {
198 targetNext = targetPtr->next;
199 delete_target(&targetPtr);
200 targetPtr = targetNext;
202 opj_free(*targetlist);
205 void print_target(target_param_t *target)
207 fprintf(logstream, "target:\n");
208 fprintf(logstream, "\t tid=%s\n", target->tid);
209 fprintf(logstream, "\t csn=%d\n", target->csn);
210 fprintf(logstream, "\t target=%s\n\n", target->targetname);
213 void print_alltarget(targetlist_param_t *targetlist)
217 ptr = targetlist->first;
218 while (ptr != NULL) {
224 target_param_t * search_target(const char targetname[],
225 targetlist_param_t *targetlist)
227 target_param_t *foundtarget;
229 foundtarget = targetlist->first;
231 while (foundtarget != NULL) {
233 if (strcmp(targetname, foundtarget->targetname) == 0) {
237 foundtarget = foundtarget->next;
242 target_param_t * search_targetBytid(const char tid[],
243 targetlist_param_t *targetlist)
245 target_param_t *foundtarget;
247 foundtarget = targetlist->first;
249 while (foundtarget != NULL) {
251 if (strcmp(tid, foundtarget->tid) == 0) {
255 foundtarget = foundtarget->next;
261 int open_remotefile(const char filepath[], char tmpfname[]);
263 int open_jp2file(const char filepath[], char tmpfname[])
268 /* download remote target file to local storage*/
269 if (strncmp(filepath, "http://", 7) == 0) {
270 if ((fd = open_remotefile(filepath, tmpfname)) == -1) {
275 if ((fd = open(filepath, O_RDONLY)) == -1) {
276 fprintf(FCGI_stdout, "Reason: Target %s not found\r\n", filepath);
280 /* Check resource is a JP family file.*/
281 if (lseek(fd, 0, SEEK_SET) == -1) {
283 fprintf(FCGI_stdout, "Reason: Target %s broken (lseek error)\r\n", filepath);
287 data = (char *)opj_malloc(12); /* size of header*/
289 if (read(fd, data, 12) != 12) {
292 fprintf(FCGI_stdout, "Reason: Target %s broken (read error)\r\n", filepath);
296 if (*data || *(data + 1) || *(data + 2) ||
297 *(data + 3) != 12 || strncmp(data + 4, "jP \r\n\x87\n", 8)) {
300 fprintf(FCGI_stdout, "Reason: No JPEG 2000 Signature box in target %s\r\n",
311 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
314 int open_remotefile(const char filepath[], char tmpfname[])
319 fprintf(FCGI_stderr, "Remote file can not be opened in local mode\n");
327 curl_handle = curl_easy_init();
328 curl_easy_setopt(curl_handle, CURLOPT_URL, filepath);
329 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
330 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
332 snprintf(tmpfname, MAX_LENOFTID, "%x-%x.jp2", (unsigned int)time(NULL),
333 (unsigned int)rand());
334 fprintf(FCGI_stderr, "%s is downloaded to a temporal new file %s\n", filepath,
336 if ((fd = open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRWXU)) == -1) {
337 fprintf(FCGI_stdout, "Reason: File open error %s\r\n", tmpfname);
338 curl_easy_cleanup(curl_handle);
341 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &fd);
342 curl_easy_perform(curl_handle);
343 curl_easy_cleanup(curl_handle);
350 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
352 int *fd = (int *)stream;
353 ssize_t written = write(*fd, ptr, size * nmemb);
354 assert(written >= 0);
356 return (size_t)written;