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.
34 #include <sys/types.h>
39 #include <curl/curl.h>
40 #include "target_manager.h"
43 #include "fcgi_stdio.h"
44 #define logstream FCGI_stdout
46 #define FCGI_stdout stdout
47 #define FCGI_stderr stderr
48 #define logstream stderr
52 targetlist_param_t * gene_targetlist()
54 targetlist_param_t *targetlist;
56 targetlist = (targetlist_param_t *)malloc( sizeof(targetlist_param_t));
58 targetlist->first = NULL;
59 targetlist->last = NULL;
66 * open jp2 format image file
68 * @param[in] filepath file name (.jp2)
69 * @param[out] tmpfname new file name if filepath is a URL
70 * @return file descriptor
72 int open_jp2file( char filepath[], char tmpfname[]);
74 target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath)
76 target_param_t *target;
78 index_param_t *jp2idx;
79 char tmpfname[MAX_LENOFTID];
80 static int last_csn = 0;
82 if( targetpath[0]=='\0'){
83 fprintf( FCGI_stderr, "Error: exception, no targetpath in gene_target()\n");
87 if((fd = open_jp2file( targetpath, tmpfname)) == -1){
88 fprintf( FCGI_stdout, "Status: 404\r\n");
92 if( !(jp2idx = parse_jp2file( fd))){
93 fprintf( FCGI_stdout, "Status: 501\r\n");
97 target = (target_param_t *)malloc( sizeof(target_param_t));
98 snprintf( target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL), (unsigned int)rand());
99 target->targetname = strdup( targetpath);
102 target->tmpfname = strdup( tmpfname);
104 target->tmpfname = NULL;
105 target->csn = last_csn++;
106 target->codeidx = jp2idx;
107 target->num_of_use = 0;
108 target->jppstream = true;
109 target->jptstream = isJPTfeasible( *jp2idx);
112 if( targetlist->first) // there are one or more entries
113 targetlist->last->next = target;
115 targetlist->first = target;
116 targetlist->last = target;
119 fprintf( logstream, "local log: target %s generated\n", targetpath);
125 void refer_target( target_param_t *reftarget, target_param_t **ptr)
128 reftarget->num_of_use++;
131 void unrefer_target( target_param_t *target)
133 target->num_of_use--;
136 void delete_target( target_param_t **target)
138 close( (*target)->fd);
140 if( (*target)->tmpfname){
141 fprintf( FCGI_stderr, "Temporal file %s is deleted\n", (*target)->tmpfname);
142 remove( (*target)->tmpfname);
145 if( (*target)->codeidx)
146 delete_index ( &(*target)->codeidx);
149 fprintf( logstream, "local log: target: %s deleted\n", (*target)->targetname);
152 free( (*target)->targetname);
157 void delete_target_in_list( target_param_t **target, targetlist_param_t *targetlist)
161 if( *target == targetlist->first)
162 targetlist->first = (*target)->next;
164 ptr = targetlist->first;
165 while( ptr->next != *target){
169 ptr->next = (*target)->next;
171 if( *target == targetlist->last)
172 targetlist->last = ptr;
174 delete_target( target);
177 void delete_targetlist(targetlist_param_t **targetlist)
179 target_param_t *targetPtr, *targetNext;
181 targetPtr = (*targetlist)->first;
182 while( targetPtr != NULL){
183 targetNext=targetPtr->next;
184 delete_target( &targetPtr);
185 targetPtr=targetNext;
190 void print_target( target_param_t *target)
192 fprintf( logstream, "target:\n");
193 fprintf( logstream, "\t tid=%s\n", target->tid);
194 fprintf( logstream, "\t csn=%d\n", target->csn);
195 fprintf( logstream, "\t target=%s\n\n", target->targetname);
198 void print_alltarget( targetlist_param_t *targetlist)
202 ptr = targetlist->first;
209 target_param_t * search_target( char targetname[], targetlist_param_t *targetlist)
211 target_param_t *foundtarget;
213 foundtarget = targetlist->first;
215 while( foundtarget != NULL){
217 if( strcmp( targetname, foundtarget->targetname) == 0)
220 foundtarget = foundtarget->next;
225 target_param_t * search_targetBytid( char tid[], targetlist_param_t *targetlist)
227 target_param_t *foundtarget;
229 foundtarget = targetlist->first;
231 while( foundtarget != NULL){
233 if( strcmp( tid, foundtarget->tid) == 0)
236 foundtarget = foundtarget->next;
242 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
244 int open_jp2file( char filepath[], char tmpfname[])
250 // download remote target file to local storage
251 if( strncmp( filepath, "http://", 7) == 0){
252 curl_handle = curl_easy_init();
253 curl_easy_setopt(curl_handle, CURLOPT_URL, filepath);
254 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
255 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
257 snprintf( tmpfname, MAX_LENOFTID, "%x-%x.jp2", (unsigned int)time(NULL), (unsigned int)rand());
258 fprintf( FCGI_stderr, "%s is downloaded to a temporal new file %s\n", filepath, tmpfname);
259 if( (fd = open( tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRWXU)) == -1){
260 fprintf( FCGI_stdout, "Reason: File open error %s\r\n", tmpfname);
261 curl_easy_cleanup(curl_handle);
264 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &fd);
265 curl_easy_perform(curl_handle);
266 curl_easy_cleanup(curl_handle);
270 if( (fd = open( filepath, O_RDONLY)) == -1){
271 fprintf( FCGI_stdout, "Reason: Target %s not found\r\n", filepath);
275 // Check resource is a JP family file.
276 if( lseek( fd, 0, SEEK_SET)==-1){
278 fprintf( FCGI_stdout, "Reason: Target %s broken (lseek error)\r\n", filepath);
282 data = (char *)malloc( 12); // size of header
284 if( read( fd, data, 12) != 12){
287 fprintf( FCGI_stdout, "Reason: Target %s broken (read error)\r\n", filepath);
291 if( *data || *(data + 1) || *(data + 2) ||
292 *(data + 3) != 12 || strncmp (data + 4, "jP \r\n\x87\n", 8)){
295 fprintf( FCGI_stdout, "Reason: No JPEG 2000 Signature box in target %s\r\n", filepath);
304 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
306 int *fd = (int *)stream;
307 int written = write( *fd, ptr, size*nmemb);