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.
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 *)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 *)malloc( sizeof(target_param_t));
103 snprintf( target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL), (unsigned int)rand());
104 target->targetname = strdup( targetpath);
108 target->tmpfname = strdup( tmpfname);
110 target->tmpfname = NULL;
112 target->csn = last_csn++;
113 target->codeidx = jp2idx;
114 target->num_of_use = 0;
115 target->jppstream = true;
116 target->jptstream = isJPTfeasible( *jp2idx);
119 if( targetlist->first) /* there are one or more entries*/
120 targetlist->last->next = target;
121 else /* first entry*/
122 targetlist->first = target;
123 targetlist->last = target;
126 fprintf( logstream, "local log: target %s generated\n", targetpath);
132 void refer_target( target_param_t *reftarget, target_param_t **ptr)
135 reftarget->num_of_use++;
138 void unrefer_target( target_param_t *target)
140 target->num_of_use--;
143 void delete_target( target_param_t **target)
145 close( (*target)->fd);
148 if( (*target)->tmpfname){
149 fprintf( FCGI_stderr, "Temporal file %s is deleted\n", (*target)->tmpfname);
150 remove( (*target)->tmpfname);
154 if( (*target)->codeidx)
155 delete_index ( &(*target)->codeidx);
158 fprintf( logstream, "local log: target: %s deleted\n", (*target)->targetname);
161 free( (*target)->targetname);
166 void delete_target_in_list( target_param_t **target, targetlist_param_t *targetlist)
170 if( *target == targetlist->first)
171 targetlist->first = (*target)->next;
173 ptr = targetlist->first;
174 while( ptr->next != *target){
178 ptr->next = (*target)->next;
180 if( *target == targetlist->last)
181 targetlist->last = ptr;
183 delete_target( target);
186 void delete_targetlist(targetlist_param_t **targetlist)
188 target_param_t *targetPtr, *targetNext;
190 targetPtr = (*targetlist)->first;
191 while( targetPtr != NULL){
192 targetNext=targetPtr->next;
193 delete_target( &targetPtr);
194 targetPtr=targetNext;
199 void print_target( target_param_t *target)
201 fprintf( logstream, "target:\n");
202 fprintf( logstream, "\t tid=%s\n", target->tid);
203 fprintf( logstream, "\t csn=%d\n", target->csn);
204 fprintf( logstream, "\t target=%s\n\n", target->targetname);
207 void print_alltarget( targetlist_param_t *targetlist)
211 ptr = targetlist->first;
218 target_param_t * search_target( const char targetname[], targetlist_param_t *targetlist)
220 target_param_t *foundtarget;
222 foundtarget = targetlist->first;
224 while( foundtarget != NULL){
226 if( strcmp( targetname, foundtarget->targetname) == 0)
229 foundtarget = foundtarget->next;
234 target_param_t * search_targetBytid( const char tid[], targetlist_param_t *targetlist)
236 target_param_t *foundtarget;
238 foundtarget = targetlist->first;
240 while( foundtarget != NULL){
242 if( strcmp( tid, foundtarget->tid) == 0)
245 foundtarget = foundtarget->next;
251 int open_remotefile( const char filepath[], char tmpfname[]);
253 int open_jp2file( const char filepath[], char tmpfname[])
258 /* download remote target file to local storage*/
259 if( strncmp( filepath, "http://", 7) == 0){
260 if( (fd = open_remotefile( filepath, tmpfname)) == -1)
265 if( (fd = open( filepath, O_RDONLY)) == -1){
266 fprintf( FCGI_stdout, "Reason: Target %s not found\r\n", filepath);
270 /* Check resource is a JP family file.*/
271 if( lseek( fd, 0, SEEK_SET)==-1){
273 fprintf( FCGI_stdout, "Reason: Target %s broken (lseek error)\r\n", filepath);
277 data = (char *)malloc( 12); /* size of header*/
279 if( read( fd, data, 12) != 12){
282 fprintf( FCGI_stdout, "Reason: Target %s broken (read error)\r\n", filepath);
286 if( *data || *(data + 1) || *(data + 2) ||
287 *(data + 3) != 12 || strncmp (data + 4, "jP \r\n\x87\n", 8)){
290 fprintf( FCGI_stdout, "Reason: No JPEG 2000 Signature box in target %s\r\n", filepath);
300 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
303 int open_remotefile( const char filepath[], char tmpfname[])
308 fprintf( FCGI_stderr, "Remote file can not be opened in local mode\n");
316 curl_handle = curl_easy_init();
317 curl_easy_setopt(curl_handle, CURLOPT_URL, filepath);
318 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
319 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
321 snprintf( tmpfname, MAX_LENOFTID, "%x-%x.jp2", (unsigned int)time(NULL), (unsigned int)rand());
322 fprintf( FCGI_stderr, "%s is downloaded to a temporal new file %s\n", filepath, tmpfname);
323 if( (fd = open( tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRWXU)) == -1){
324 fprintf( FCGI_stdout, "Reason: File open error %s\r\n", tmpfname);
325 curl_easy_cleanup(curl_handle);
328 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &fd);
329 curl_easy_perform(curl_handle);
330 curl_easy_cleanup(curl_handle);
337 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
339 int *fd = (int *)stream;
340 ssize_t written = write( *fd, ptr, size*nmemb);
341 assert( written >= 0 );
343 return (size_t)written;