2 * $Id: target_manager.c 44 2011-02-15 12:32:29Z kaori $
4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, 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.
37 #include <sys/types.h>
43 #include "target_manager.h"
46 #include <curl/curl.h>
47 #include "fcgi_stdio.h"
48 #define logstream FCGI_stdout
50 #define FCGI_stdout stdout
51 #define FCGI_stderr stderr
52 #define logstream stderr
55 targetlist_param_t * gene_targetlist()
57 targetlist_param_t *targetlist;
59 targetlist = (targetlist_param_t *)malloc( sizeof(targetlist_param_t));
61 targetlist->first = NULL;
62 targetlist->last = NULL;
69 * open jp2 format image file
71 * @param[in] filepath file name (.jp2)
72 * @param[out] tmpfname new file name if filepath is a URL
73 * @return file descriptor
75 int open_jp2file( char filepath[], char tmpfname[]);
77 target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath)
79 target_param_t *target;
81 index_param_t *jp2idx;
82 char tmpfname[MAX_LENOFTID];
83 static int last_csn = 0;
85 if( targetpath[0]=='\0'){
86 fprintf( FCGI_stderr, "Error: exception, no targetpath in gene_target()\n");
90 if((fd = open_jp2file( targetpath, tmpfname)) == -1){
91 fprintf( FCGI_stdout, "Status: 404\r\n");
95 if( !(jp2idx = parse_jp2file( fd))){
96 fprintf( FCGI_stdout, "Status: 501\r\n");
100 target = (target_param_t *)malloc( sizeof(target_param_t));
101 snprintf( target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL), (unsigned int)rand());
102 target->targetname = strdup( targetpath);
106 target->tmpfname = strdup( tmpfname);
108 target->tmpfname = NULL;
110 target->csn = last_csn++;
111 target->codeidx = jp2idx;
112 target->num_of_use = 0;
113 target->jppstream = true;
114 target->jptstream = isJPTfeasible( *jp2idx);
117 if( targetlist->first) /* there are one or more entries*/
118 targetlist->last->next = target;
119 else /* first entry*/
120 targetlist->first = target;
121 targetlist->last = target;
124 fprintf( logstream, "local log: target %s generated\n", targetpath);
130 void refer_target( target_param_t *reftarget, target_param_t **ptr)
133 reftarget->num_of_use++;
136 void unrefer_target( target_param_t *target)
138 target->num_of_use--;
141 void delete_target( target_param_t **target)
143 close( (*target)->fd);
146 if( (*target)->tmpfname){
147 fprintf( FCGI_stderr, "Temporal file %s is deleted\n", (*target)->tmpfname);
148 remove( (*target)->tmpfname);
152 if( (*target)->codeidx)
153 delete_index ( &(*target)->codeidx);
156 fprintf( logstream, "local log: target: %s deleted\n", (*target)->targetname);
159 free( (*target)->targetname);
164 void delete_target_in_list( target_param_t **target, targetlist_param_t *targetlist)
168 if( *target == targetlist->first)
169 targetlist->first = (*target)->next;
171 ptr = targetlist->first;
172 while( ptr->next != *target){
176 ptr->next = (*target)->next;
178 if( *target == targetlist->last)
179 targetlist->last = ptr;
181 delete_target( target);
184 void delete_targetlist(targetlist_param_t **targetlist)
186 target_param_t *targetPtr, *targetNext;
188 targetPtr = (*targetlist)->first;
189 while( targetPtr != NULL){
190 targetNext=targetPtr->next;
191 delete_target( &targetPtr);
192 targetPtr=targetNext;
197 void print_target( target_param_t *target)
199 fprintf( logstream, "target:\n");
200 fprintf( logstream, "\t tid=%s\n", target->tid);
201 fprintf( logstream, "\t csn=%d\n", target->csn);
202 fprintf( logstream, "\t target=%s\n\n", target->targetname);
205 void print_alltarget( targetlist_param_t *targetlist)
209 ptr = targetlist->first;
216 target_param_t * search_target( char targetname[], targetlist_param_t *targetlist)
218 target_param_t *foundtarget;
220 foundtarget = targetlist->first;
222 while( foundtarget != NULL){
224 if( strcmp( targetname, foundtarget->targetname) == 0)
227 foundtarget = foundtarget->next;
232 target_param_t * search_targetBytid( char tid[], targetlist_param_t *targetlist)
234 target_param_t *foundtarget;
236 foundtarget = targetlist->first;
238 while( foundtarget != NULL){
240 if( strcmp( tid, foundtarget->tid) == 0)
243 foundtarget = foundtarget->next;
249 int open_remotefile( char filepath[], char tmpfname[]);
251 int open_jp2file( char filepath[], char tmpfname[])
256 /* download remote target file to local storage*/
257 if( strncmp( filepath, "http://", 7) == 0){
258 if( (fd = open_remotefile( filepath, tmpfname)) == -1)
263 if( (fd = open( filepath, O_RDONLY)) == -1){
264 fprintf( FCGI_stdout, "Reason: Target %s not found\r\n", filepath);
268 /* Check resource is a JP family file.*/
269 if( lseek( fd, 0, SEEK_SET)==-1){
271 fprintf( FCGI_stdout, "Reason: Target %s broken (lseek error)\r\n", filepath);
275 data = (char *)malloc( 12); /* size of header*/
277 if( read( fd, data, 12) != 12){
280 fprintf( FCGI_stdout, "Reason: Target %s broken (read error)\r\n", filepath);
284 if( *data || *(data + 1) || *(data + 2) ||
285 *(data + 3) != 12 || strncmp (data + 4, "jP \r\n\x87\n", 8)){
288 fprintf( FCGI_stdout, "Reason: No JPEG 2000 Signature box in target %s\r\n", filepath);
298 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
301 int open_remotefile( char filepath[], char tmpfname[])
306 fprintf( FCGI_stderr, "Remote file can not be opened in local mode\n");
314 curl_handle = curl_easy_init();
315 curl_easy_setopt(curl_handle, CURLOPT_URL, filepath);
316 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
317 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
319 snprintf( tmpfname, MAX_LENOFTID, "%x-%x.jp2", (unsigned int)time(NULL), (unsigned int)rand());
320 fprintf( FCGI_stderr, "%s is downloaded to a temporal new file %s\n", filepath, tmpfname);
321 if( (fd = open( tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRWXU)) == -1){
322 fprintf( FCGI_stdout, "Reason: File open error %s\r\n", tmpfname);
323 curl_easy_cleanup(curl_handle);
326 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &fd);
327 curl_easy_perform(curl_handle);
328 curl_easy_cleanup(curl_handle);
335 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
337 int *fd = (int *)stream;
338 int written = write( *fd, ptr, size*nmemb);