4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "renderfarm.h"
23 #include "renderfarmfsserver.h"
32 RenderFarmFSServer::RenderFarmFSServer(RenderFarmServerThread *server)
34 this->server = server;
39 RenderFarmFSServer::~RenderFarmFSServer()
43 void RenderFarmFSServer::initialize()
47 int RenderFarmFSServer::handle_request(int request_id, int request_size, unsigned char *buffer)
52 printf("RenderFarmFSServer::handle_request request_id=%d\n", request_id);
55 case RENDERFARM_FOPEN:
60 unsigned char datagram[8];
66 mode = (char*)buffer + strlen(path) + 1;
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);
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);
80 case RENDERFARM_FCLOSE:
82 int64_t pointer = READ_INT64((unsigned char*)buffer);
83 FILE *file = (FILE*)Units::int64_to_ptr(pointer);
85 printf("RenderFarmFSServer::handle_request RENDERFARM_FCLOSE file=%p\n", file);
91 case RENDERFARM_REMOVE:
93 remove((char*)buffer);
95 printf("RenderFarmFSServer::handle_request path=%s\n", buffer);
100 case RENDERFARM_RENAME:
102 char *oldpath = (char*)buffer;
103 char *newpath = (char*)buffer + strlen(oldpath) + 1;
104 rename(oldpath, newpath);
106 printf("RenderFarmFSServer::handle_request old=%s new=%s\n", oldpath, newpath);
111 case RENDERFARM_FGETC:
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);
119 printf("RenderFarmFSServer::handle_request file=%p\n", file);
124 case RENDERFARM_FPUTC:
126 int64_t pointer = READ_INT64((unsigned char*)buffer);
127 FILE *file = (FILE*)Units::int64_to_ptr(pointer);
128 fputc(buffer[8], file);
131 printf("RenderFarmFSServer::handle_request file=%p\n", file);
135 case RENDERFARM_FREAD:
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];
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],
151 server->reallocate_buffer(size * num);
152 bytes = fread(server->buffer, size, num, file);
154 server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
155 server->write_socket((char*)server->buffer, size * bytes, RENDERFARM_TIMEOUT);
158 printf("RenderFarmFSServer::handle_request file=%p size=%d num=%d bytes=%d\n",
159 file, size, num, bytes);
163 case RENDERFARM_FGETS:
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];
171 server->reallocate_buffer(size);
172 char *return_value = fgets((char*)server->buffer, size, file);
176 bytes = strlen(return_value) + 1;
179 server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
180 server->write_socket((char*)server->buffer, bytes, RENDERFARM_TIMEOUT);
185 case RENDERFARM_FILENO:
187 int64_t pointer = READ_INT64((unsigned char*)buffer);
188 FILE *file = (FILE*)Units::int64_to_ptr(pointer);
189 unsigned char datagram[4];
192 int return_value = fileno(file);
193 STORE_INT32(return_value);
194 server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
196 printf("RenderFarmFSServer::handle_request file=%p fileno=%d\n",
202 case RENDERFARM_FWRITE:
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];
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);
216 server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
219 printf("RenderFarmFSServer::handle_request RENDERFARM_FWRITE file=%p size=%d num=%d bytes=%d\n",
220 file, size, num, bytes);
224 case RENDERFARM_FSEEK:
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);
233 unsigned char datagram[4];
236 return_value = fseeko64(file, offset, whence);
237 STORE_INT32(return_value);
238 server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
241 printf("RenderFarmFSServer::handle_request file=%p offset=%jd whence=%d result=%d\n",
242 file, offset, whence, result);
246 case RENDERFARM_FTELL:
248 int64_t pointer = READ_INT64((unsigned char*)buffer);
249 FILE *file = (FILE*)Units::int64_to_ptr(pointer);
250 unsigned char datagram[8];
252 int64_t return_value = ftello64(file);
253 STORE_INT64(return_value);
254 server->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT);
257 printf("RenderFarmFSServer::handle_request file=%p result=%jd\n",
262 case RENDERFARM_STAT:
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);
269 printf("RenderFarmFSServer::handle_request path=%s result=%d\n",
270 buffer, return_value);
274 case RENDERFARM_STAT64:
276 struct stat64 stat_buf;
277 int return_value = stat64((char*)buffer, &stat_buf);
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);
296 printf("RenderFarmFSServer::handle_request path=%s result=%d\n",
297 buffer, return_value);