Credit Andrew - improve in-tree documentation
[goodguy/cinelerra.git] / cinelerra / renderfarmfsserver.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "renderfarm.h"
23 #include "renderfarmfsserver.h"
24 #include "units.h"
25
26
27 #include <string.h>
28 #include <sys/stat.h>
29
30 #define DEBUG 0
31
32 RenderFarmFSServer::RenderFarmFSServer(RenderFarmServerThread *server)
33 {
34         this->server = server;
35 }
36
37
38
39 RenderFarmFSServer::~RenderFarmFSServer()
40 {
41 }
42
43 void RenderFarmFSServer::initialize()
44 {
45 }
46
47 int RenderFarmFSServer::handle_request(int request_id, int request_size, unsigned char *buffer)
48 {
49         int result = 0;
50
51 if(DEBUG)
52 printf("RenderFarmFSServer::handle_request request_id=%d\n", request_id);
53         switch(request_id)
54         {
55                 case RENDERFARM_FOPEN:
56                 {
57                         char *path;
58                         char *mode;
59                         FILE *file;
60                         unsigned char datagram[8];
61                         int i = 0;
62                         int64_t file_int64;
63
64
65                         path = (char*)buffer;
66                         mode = (char*)buffer + strlen(path) + 1;
67
68
69                         file = fopen64(path, mode);
70                         file_int64 = Units::ptr_to_int64(file);
71                         STORE_INT64(file_int64);
72                         server->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT);
73 if(DEBUG)
74 printf("RenderFarmFSServer::handle_request RENDERFARM_FOPEN file=%p file_int64=%llx datagram=%02x%02x%02x%02x%02x%02x%02x%02x path=%s mode=%s\n",
75 file, file_int64, datagram[0], datagram[1], datagram[2], datagram[3], datagram[4], datagram[5], datagram[6], datagram[7], path, mode);
76                         result = 1;
77                         break;
78                 }
79
80                 case RENDERFARM_FCLOSE:
81                 {
82                         int64_t pointer = READ_INT64((unsigned char*)buffer);
83                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
84 if(DEBUG)
85 printf("RenderFarmFSServer::handle_request RENDERFARM_FCLOSE file=%p\n", file);
86                         fclose(file);
87                         result = 1;
88                         break;
89                 }
90
91                 case RENDERFARM_REMOVE:
92                 {
93                         remove((char*)buffer);
94 if(DEBUG)
95 printf("RenderFarmFSServer::handle_request path=%s\n", buffer);
96                         result = 1;
97                         break;
98                 }
99
100                 case RENDERFARM_RENAME:
101                 {
102                         char *oldpath = (char*)buffer;
103                         char *newpath = (char*)buffer + strlen(oldpath) + 1;
104                         rename(oldpath, newpath);
105 if(DEBUG)
106 printf("RenderFarmFSServer::handle_request old=%s new=%s\n", oldpath, newpath);
107                         result = 1;
108                         break;
109                 }
110
111                 case RENDERFARM_FGETC:
112                 {
113                         int64_t pointer = READ_INT64((unsigned char*)buffer);
114                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
115                         unsigned char datagram[1];
116                         datagram[0] = fgetc(file);
117                         server->write_socket((char*)datagram, 1, RENDERFARM_TIMEOUT);
118 if(DEBUG)
119 printf("RenderFarmFSServer::handle_request file=%p\n", file);
120                         result = 1;
121                         break;
122                 }
123
124                 case RENDERFARM_FPUTC:
125                 {
126                         int64_t pointer = READ_INT64((unsigned char*)buffer);
127                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
128                         fputc(buffer[8], file);
129                         result = 1;
130 if(DEBUG)
131 printf("RenderFarmFSServer::handle_request file=%p\n", file);
132                         break;
133                 }
134
135                 case RENDERFARM_FREAD:
136                 {
137                         int64_t pointer = READ_INT64((unsigned char*)buffer);
138                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
139                         int size = READ_INT32((unsigned char*)buffer + 8);
140                         int num = READ_INT32((unsigned char*)buffer + 12);
141                         unsigned char datagram[4];
142                         int i = 0;
143                         int bytes;
144 // printf("RenderFarmFSServer::handle_request RENDERFARM_FREAD %02x%02x%02x%02x%02x%02x%02x%02x %p %d %d\n",
145 // buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7],
146 // pointer,
147 // file,
148 // size,
149 // num);
150
151                         server->reallocate_buffer(size * num);
152                         bytes = fread(server->buffer, size, num, file);
153                         STORE_INT32(bytes);
154                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
155                         server->write_socket((char*)server->buffer, size * bytes, RENDERFARM_TIMEOUT);
156                         result = 1;
157 if(DEBUG)
158 printf("RenderFarmFSServer::handle_request file=%p size=%d num=%d bytes=%d\n",
159 file, size, num, bytes);
160                         break;
161                 }
162
163                 case RENDERFARM_FGETS:
164                 {
165                         int64_t pointer = READ_INT64((unsigned char*)buffer);
166                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
167                         int size = READ_INT32((unsigned char*)buffer + 8);
168                         unsigned char datagram[4];
169                         int i = 0;
170
171                         server->reallocate_buffer(size);
172                         char *return_value = fgets((char*)server->buffer, size, file);
173                         int bytes = 0;
174                         if(return_value)
175                         {
176                                 bytes = strlen(return_value) + 1;
177                         }
178                         STORE_INT32(bytes);
179                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
180                         server->write_socket((char*)server->buffer, bytes, RENDERFARM_TIMEOUT);
181                         result = 1;
182                         break;
183                 }
184
185                 case RENDERFARM_FILENO:
186                 {
187                         int64_t pointer = READ_INT64((unsigned char*)buffer);
188                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
189                         unsigned char datagram[4];
190                         int i = 0;
191
192                         int return_value = fileno(file);
193                         STORE_INT32(return_value);
194                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
195 if(DEBUG)
196 printf("RenderFarmFSServer::handle_request file=%p fileno=%d\n",
197 file, return_value);
198                         result = 1;
199                         break;
200                 }
201
202                 case RENDERFARM_FWRITE:
203                 {
204                         int64_t pointer = READ_INT64((unsigned char*)buffer);
205                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
206                         int size = READ_INT32((unsigned char*)buffer + 8);
207                         int num = READ_INT32((unsigned char*)buffer + 12);
208                         unsigned char datagram[4];
209                         int i = 0;
210                         int bytes;
211
212                         server->reallocate_buffer(size * num);
213                         server->read_socket((char*)server->buffer, size * num, RENDERFARM_TIMEOUT);
214                         bytes = fwrite(server->buffer, size, num, file);
215                         STORE_INT32(bytes);
216                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
217                         result = 1;
218 if(DEBUG)
219 printf("RenderFarmFSServer::handle_request RENDERFARM_FWRITE file=%p size=%d num=%d bytes=%d\n",
220 file, size, num, bytes);
221                         break;
222                 }
223
224                 case RENDERFARM_FSEEK:
225                 {
226                         int64_t pointer = READ_INT64((unsigned char*)buffer);
227                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
228 // printf("RENDERFARM_FSEEK 1 buffer=%02x%02x%02x%02x%02x%02x%02x%02x %p %llx\n",
229 // buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7],  file, pointer);
230                         int64_t offset = READ_INT64((unsigned char*)buffer + 8);
231                         int whence = READ_INT32((unsigned char*)buffer + 16);
232                         int return_value;
233                         unsigned char datagram[4];
234                         int i = 0;
235
236                         return_value = fseeko64(file, offset, whence);
237                         STORE_INT32(return_value);
238                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
239                         result = 1;
240 if(DEBUG)
241 printf("RenderFarmFSServer::handle_request file=%p offset=%jd whence=%d result=%d\n",
242 file, offset, whence, result);
243                         break;
244                 }
245
246                 case RENDERFARM_FTELL:
247                 {
248                         int64_t pointer = READ_INT64((unsigned char*)buffer);
249                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
250                         unsigned char datagram[8];
251                         int i = 0;
252                         int64_t return_value = ftello64(file);
253                         STORE_INT64(return_value);
254                         server->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT);
255                         result = 1;
256 if(DEBUG)
257 printf("RenderFarmFSServer::handle_request file=%p result=%jd\n",
258 file, return_value);
259                         break;
260                 }
261
262                 case RENDERFARM_STAT:
263                 {
264                         struct stat stat_buf;
265                         int return_value = stat((char*)buffer, &stat_buf);
266                         server->write_socket((char*)&stat_buf, sizeof(struct stat), RENDERFARM_TIMEOUT);
267                         result = 1;
268 if(DEBUG)
269 printf("RenderFarmFSServer::handle_request path=%s result=%d\n",
270 buffer, return_value);
271                         break;
272                 }
273
274                 case RENDERFARM_STAT64:
275                 {
276                         struct stat64 stat_buf;
277                         int return_value = stat64((char*)buffer, &stat_buf);
278                         vfs_stat_t arg;
279                         arg.dev = stat_buf.st_dev;
280 //                      arg.ino32 = stat_buf.__st_ino;
281                         arg.ino = stat_buf.st_ino;
282                         arg.nlink = stat_buf.st_nlink;
283                         arg.mode = stat_buf.st_mode;
284                         arg.uid = stat_buf.st_uid;
285                         arg.gid = stat_buf.st_gid;
286                         arg.rdev = stat_buf.st_rdev;
287                         arg.size = stat_buf.st_size;
288                         arg.blksize = stat_buf.st_blksize;
289                         arg.blocks = stat_buf.st_blocks;
290                         arg.atim = stat_buf.st_atim.tv_sec;
291                         arg.mtim = stat_buf.st_mtim.tv_sec;
292                         arg.ctim = stat_buf.st_ctim.tv_sec;
293                         server->write_socket((char*)&arg, sizeof(arg), RENDERFARM_TIMEOUT);
294                         result = 1;
295 if(DEBUG)
296 printf("RenderFarmFSServer::handle_request path=%s result=%d\n",
297 buffer, return_value);
298                         break;
299                 }
300         }
301
302         return result;
303 }