[trunk] Update JPIP (FolderReorgProposal task)
[openjpeg.git] / src / lib / openjpip / target_manager.c
1 /*
2  * $Id$
3  *
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
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #ifdef _WIN32
36 #define snprintf _snprintf /* Visual Studio */
37 #include <io.h>
38 #else
39 #include <sys/types.h>
40 #include <unistd.h>
41 #endif
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <time.h>
45 #include "target_manager.h"
46
47 #ifdef SERVER
48 #include <curl/curl.h>
49 #include "fcgi_stdio.h"
50 #define logstream FCGI_stdout
51 #else
52 #define FCGI_stdout stdout
53 #define FCGI_stderr stderr
54 #define logstream stderr
55 #endif /*SERVER*/
56
57 targetlist_param_t * gene_targetlist(void)
58 {
59   targetlist_param_t *targetlist;
60
61   targetlist = (targetlist_param_t *)opj_malloc( sizeof(targetlist_param_t));
62   
63   targetlist->first = NULL;
64   targetlist->last  = NULL;
65
66   return targetlist;
67 }
68
69
70 /**
71  * open jp2 format image file
72  *
73  * @param[in]  filepath file name (.jp2)
74  * @param[out] tmpfname new file name if filepath is a URL
75  * @return              file descriptor
76  */
77 int open_jp2file( const char filepath[], char tmpfname[]);
78
79 target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath)
80 {
81   target_param_t *target;
82   int fd;
83   index_param_t *jp2idx;
84   char tmpfname[MAX_LENOFTID];
85   static int last_csn = 0;
86   
87   if( targetpath[0]=='\0'){
88     fprintf( FCGI_stderr, "Error: exception, no targetpath in gene_target()\n");
89     return NULL;
90   }
91
92   if((fd = open_jp2file( targetpath, tmpfname)) == -1){
93     fprintf( FCGI_stdout, "Status: 404\r\n"); 
94     return NULL;
95   }
96   
97   if( !(jp2idx = parse_jp2file( fd))){
98     fprintf( FCGI_stdout, "Status: 501\r\n");
99     return NULL;
100   }
101
102   target = (target_param_t *)opj_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); 
105   target->fd = fd;
106 #ifdef SERVER
107   if( tmpfname[0])
108     target->tmpfname = strdup( tmpfname);
109   else
110     target->tmpfname = NULL;
111 #endif
112   target->csn = last_csn++;
113   target->codeidx = jp2idx;
114   target->num_of_use = 0; 
115   target->jppstream = true;
116   target->jptstream = isJPTfeasible( *jp2idx);
117   target->next=NULL;
118
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;
124
125 #ifndef SERVER
126   fprintf( logstream, "local log: target %s generated\n", targetpath);
127 #endif
128   
129   return target;
130 }
131
132 void refer_target( target_param_t *reftarget, target_param_t **ptr)
133 {
134   *ptr = reftarget;
135   reftarget->num_of_use++;
136 }
137
138 void unrefer_target( target_param_t *target)
139 {
140   target->num_of_use--;
141 }
142
143 void delete_target( target_param_t **target)
144 {
145   close( (*target)->fd);
146
147 #ifdef SERVER
148   if( (*target)->tmpfname){
149     fprintf( FCGI_stderr, "Temporal file %s is deleted\n", (*target)->tmpfname);
150     remove( (*target)->tmpfname);
151   }
152 #endif
153
154   if( (*target)->codeidx)
155     delete_index ( &(*target)->codeidx);
156   
157 #ifndef SERVER
158   fprintf( logstream, "local log: target: %s deleted\n", (*target)->targetname);
159 #endif
160
161   opj_free( (*target)->targetname);
162
163   opj_free(*target);
164 }
165
166 void delete_target_in_list( target_param_t **target, targetlist_param_t *targetlist)
167 {
168   target_param_t *ptr;
169
170   if( *target == targetlist->first)
171     targetlist->first = (*target)->next;
172   else{
173     ptr = targetlist->first;
174     while( ptr->next != *target){
175       ptr=ptr->next;
176     }
177     
178     ptr->next = (*target)->next;
179     
180     if( *target == targetlist->last)
181       targetlist->last = ptr;
182   }
183   delete_target( target);
184 }
185
186 void delete_targetlist(targetlist_param_t **targetlist)
187 {
188   target_param_t *targetPtr, *targetNext;
189   
190   targetPtr = (*targetlist)->first;
191   while( targetPtr != NULL){
192     targetNext=targetPtr->next;
193     delete_target( &targetPtr);
194     targetPtr=targetNext;
195   }
196   opj_free( *targetlist);
197 }
198
199 void print_target( target_param_t *target)
200 {
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);
205 }
206
207 void print_alltarget( targetlist_param_t *targetlist)
208 {
209   target_param_t *ptr;
210
211   ptr = targetlist->first;
212   while( ptr != NULL){
213     print_target( ptr);
214     ptr=ptr->next;
215   }
216 }
217
218 target_param_t * search_target( const char targetname[], targetlist_param_t *targetlist)
219 {
220   target_param_t *foundtarget;
221
222   foundtarget = targetlist->first;
223   
224   while( foundtarget != NULL){
225     
226     if( strcmp( targetname, foundtarget->targetname) == 0)
227       return foundtarget;
228       
229     foundtarget = foundtarget->next;
230   }
231   return NULL;
232 }
233
234 target_param_t * search_targetBytid( const char tid[], targetlist_param_t *targetlist)
235 {
236   target_param_t *foundtarget;
237   
238   foundtarget = targetlist->first;
239   
240   while( foundtarget != NULL){
241     
242     if( strcmp( tid, foundtarget->tid) == 0)
243       return foundtarget;
244       
245     foundtarget = foundtarget->next;
246   }
247
248   return NULL;
249 }
250
251 int open_remotefile( const char filepath[], char tmpfname[]);
252
253 int open_jp2file( const char filepath[], char tmpfname[])
254 {
255   int fd;
256   char *data;
257   
258   /* download remote target file to local storage*/
259   if( strncmp( filepath, "http://", 7) == 0){
260     if( (fd = open_remotefile( filepath, tmpfname)) == -1)
261       return -1;
262   }
263   else{
264     tmpfname[0] = 0;
265     if( (fd = open( filepath, O_RDONLY)) == -1){
266       fprintf( FCGI_stdout, "Reason: Target %s not found\r\n", filepath);
267       return -1;
268     }
269   }
270   /* Check resource is a JP family file.*/
271   if( lseek( fd, 0, SEEK_SET)==-1){
272     close(fd);
273     fprintf( FCGI_stdout, "Reason: Target %s broken (lseek error)\r\n", filepath);
274     return -1;
275   }
276   
277   data = (char *)opj_malloc( 12); /* size of header*/
278
279   if( read( fd, data, 12) != 12){
280     opj_free( data);
281     close(fd);
282     fprintf( FCGI_stdout, "Reason: Target %s broken (read error)\r\n", filepath);
283     return -1;
284   }
285     
286   if( *data || *(data + 1) || *(data + 2) ||
287       *(data + 3) != 12 || strncmp (data + 4, "jP  \r\n\x87\n", 8)){
288     opj_free( data);
289     close(fd);
290     fprintf( FCGI_stdout, "Reason: No JPEG 2000 Signature box in target %s\r\n", filepath);
291     return -1;
292   } 
293
294   opj_free( data);
295
296   return fd;
297 }
298
299 #ifdef SERVER
300 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
301 #endif
302
303 int open_remotefile( const char filepath[], char tmpfname[])
304 {
305 #ifndef SERVER
306   (void)filepath;
307   (void)tmpfname;
308   fprintf( FCGI_stderr, "Remote file can not be opened in local mode\n");
309   return -1;
310
311 #else
312
313   CURL *curl_handle;
314   int fd;
315     
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);
320   
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);
326     return -1;
327   }
328   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &fd);
329   curl_easy_perform(curl_handle);
330   curl_easy_cleanup(curl_handle);
331
332   return fd;
333 #endif /*SERVER*/
334 }
335
336 #ifdef SERVER
337 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
338 {
339   int *fd = (int *)stream;
340   ssize_t written = write( *fd, ptr, size*nmemb);
341   assert( written >= 0 );
342
343   return (size_t)written;
344 }
345 #endif /*SERVER*/