0
|
1 /*
|
|
2 * $Id: do_filecom.c,v 1.13 2003/06/07 02:23:58 hiroo Exp $
|
|
3 */
|
|
4
|
|
5 /*
|
|
6 * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
|
|
7 * This file is part of FreeWnn.
|
|
8 *
|
|
9 * Copyright Kyoto University Research Institute for Mathematical Sciences
|
|
10 * 1987, 1988, 1989, 1990, 1991, 1992
|
|
11 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
|
|
12 * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
|
|
13 * Copyright FreeWnn Project 1999, 2000, 2002, 2003
|
|
14 *
|
|
15 * Maintainer: FreeWnn Project <freewnn@tomo.gr.jp>
|
|
16 *
|
|
17 * This program is free software; you can redistribute it and/or modify
|
|
18 * it under the terms of the GNU General Public License as published by
|
|
19 * the Free Software Foundation; either version 2 of the License, or
|
|
20 * (at your option) any later version.
|
|
21 *
|
|
22 * This program is distributed in the hope that it will be useful,
|
|
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
25 * GNU General Public License for more details.
|
|
26 *
|
|
27 * You should have received a copy of the GNU General Public License
|
|
28 * along with this program; if not, write to the Free Software
|
|
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
30 */
|
|
31
|
|
32 /*
|
|
33 * Do Global File command
|
|
34 */
|
|
35
|
|
36 #if defined(HAVE_CONFIG_H)
|
|
37 #include <config.h>
|
|
38 #endif
|
|
39
|
|
40 #include <stdio.h>
|
|
41 #if STDC_HEADERS
|
|
42 # include <stdlib.h>
|
|
43 # include <string.h>
|
|
44 #else
|
|
45 # if HAVE_MALLOC_H
|
|
46 # include <malloc.h>
|
|
47 # endif
|
|
48 # if HAVE_STRINGS_H
|
|
49 # include <strings.h>
|
|
50 # endif
|
|
51 #endif /* STDC_HEADERS */
|
|
52 #include <sys/types.h>
|
|
53 #include <sys/stat.h>
|
|
54 #if STDC_HEADERS
|
|
55 # include <string.h>
|
|
56 #else
|
|
57 # if HAVE_STRINGS_H
|
|
58 # include <strings.h>
|
|
59 # endif
|
|
60 #endif /* STDC_HEADERS */
|
|
61 #if HAVE_UNISTD_H
|
|
62 # include <unistd.h>
|
|
63 #endif
|
|
64
|
|
65 #include "demcom.h"
|
|
66 #include "commonhd.h"
|
|
67 #include "de_header.h"
|
|
68 #include "jdata.h"
|
|
69
|
|
70 static int add_fid_to_env (int, int);
|
|
71 static int del_fid_from_env (int, int);
|
|
72 static void file_already_read (int, int);
|
|
73 static int file_discard (int, int);
|
|
74 static int file_loaded_local (void);
|
|
75 static int file_remove (char*, char*);
|
|
76 static int find_file_by_uniq (struct wnn_file_uniq *);
|
|
77 static int new_fid_in_env (int, int);
|
|
78 static int x_file_already_read (int, int);
|
|
79
|
|
80 int
|
|
81 file_init (void)
|
|
82 {
|
|
83 int i;
|
|
84 files = (struct wnn_file *)calloc(MAX_FILES, sizeof(struct wnn_file));
|
|
85 if ( files == NULL )
|
|
86 {
|
|
87 log_err ("file_init: malloc faild.");
|
1
|
88 return (FAILURE);
|
0
|
89 }
|
|
90 for (i = 0; i < MAX_FILES; i++)
|
|
91 {
|
|
92 files[i].ref_count = -1;
|
|
93 }
|
|
94 return (SUCCESS);
|
|
95 }
|
|
96
|
|
97 /*
|
|
98 Global File ope.
|
|
99 */
|
|
100
|
|
101 /* mkdir */
|
|
102
|
|
103 /* #define MODE (0000000 | 0000777) */
|
|
104 /* #define MODE (0040000 | 0000731) */
|
|
105 /* #define MODE (0000000 | 0000733) */
|
|
106 #define MODE (0000000 | 0000755)
|
|
107
|
|
108 void
|
|
109 js_mkdir (void)
|
|
110 {
|
|
111 char path[FILENAME];
|
|
112 int x;
|
|
113 int err = 0;
|
|
114 err = envhandle ();
|
|
115 get_file_name (path, FILENAME);
|
|
116
|
|
117 if (err == -1)
|
|
118 {
|
|
119 error_ret ();
|
|
120 return;
|
|
121 }
|
|
122 x = mkdir (path, MODE);
|
|
123 error1 ("mkdir=%d\n", x);
|
|
124 if (x != 0)
|
|
125 {
|
|
126 wnn_errorno = WNN_MKDIR_FAIL;
|
|
127 error_ret ();
|
|
128 return;
|
|
129 }
|
|
130 chmod (path, MODE);
|
|
131 put4_cur (0);
|
|
132 putc_purge ();
|
|
133 }
|
|
134
|
|
135 /* access */
|
|
136 void
|
|
137 js_access (void)
|
|
138 {
|
|
139 char path[FILENAME];
|
|
140 int amode;
|
|
141 envhandle ();
|
|
142 amode = get4_cur ();
|
|
143 get_file_name (path, FILENAME);
|
|
144
|
|
145 #ifdef WRITE_CHECK
|
|
146 check_backup (path);
|
|
147 #endif
|
|
148 put4_cur (access (path, amode));
|
|
149 putc_purge ();
|
|
150 }
|
|
151
|
|
152
|
|
153 /* js_file_list_all */
|
|
154 void
|
|
155 js_file_list_all (void)
|
|
156 {
|
|
157 int i, c;
|
|
158
|
|
159 for (c = 0, i = 1; i < MAX_FILES; i++)
|
|
160 {
|
|
161 if (files[i].ref_count == -1)
|
|
162 continue;
|
|
163 c++;
|
|
164 }
|
|
165 put4_cur (c);
|
|
166 for (i = 1; i < MAX_FILES; i++)
|
|
167 {
|
|
168 if (files[i].ref_count == -1)
|
|
169 continue;
|
|
170 put4_cur (i); /* fid */
|
|
171 put4_cur (files[i].localf);
|
|
172 put4_cur (files[i].ref_count);
|
|
173 put4_cur (files[i].file_type);
|
|
174 puts_cur (files[i].name);
|
|
175 }
|
|
176 putc_purge ();
|
|
177 }
|
|
178
|
|
179 /* js_file_list */
|
|
180 void
|
|
181 js_file_list (void)
|
|
182 {
|
|
183 int i, c, env_id, fid;
|
|
184
|
|
185 env_id = envhandle ();
|
|
186
|
|
187 for (c = 0, i = 0; i < WNN_MAX_FILE_OF_AN_ENV; i++)
|
|
188 {
|
|
189 if ((env[env_id]->file)[i] == -1)
|
|
190 continue;
|
|
191 c++;
|
|
192 }
|
|
193 put4_cur (c);
|
|
194 for (i = 0; i < WNN_MAX_FILE_OF_AN_ENV; i++)
|
|
195 {
|
|
196 if ((fid = (env[env_id]->file)[i]) == -1)
|
|
197 continue;
|
|
198 put4_cur (fid); /* fid */
|
|
199 put4_cur (files[fid].localf);
|
|
200 put4_cur (files[fid].ref_count);
|
|
201 put4_cur (files[fid].file_type);
|
|
202 puts_cur (files[fid].name);
|
|
203 }
|
|
204 putc_purge ();
|
|
205 }
|
|
206
|
|
207 /* file info */
|
|
208 void
|
|
209 js_file_info (void)
|
|
210 {
|
|
211 int env_id, fid;
|
|
212 env_id = envhandle ();
|
|
213 fid = get4_cur ();
|
|
214 if (find_fid_in_env (env_id, fid) == -1)
|
|
215 { /* valid */
|
|
216 wnn_errorno = WNN_FID_ERROR;
|
|
217 error_ret ();
|
|
218 putc_purge ();
|
|
219 return;
|
|
220 }
|
|
221 put4_cur (0);
|
|
222 puts_cur (files[fid].name);
|
|
223 put4_cur (files[fid].localf);
|
|
224 put4_cur (files[fid].ref_count);
|
|
225 put4_cur (files[fid].file_type);
|
|
226 putc_purge ();
|
|
227 }
|
|
228
|
|
229 /* file stat */
|
|
230 void
|
|
231 js_file_stat (void)
|
|
232 {
|
|
233 char n[FILENAME];
|
|
234
|
|
235 get4_cur (); /* env_id */
|
|
236 get_file_name (n, FILENAME);
|
|
237
|
|
238 put4_cur (file_stat (n));
|
|
239 putc_purge ();
|
|
240 }
|
|
241
|
|
242 int
|
|
243 file_stat (char *file_name)
|
|
244 {
|
|
245 FILE *fp;
|
|
246 struct wnn_file_head fh;
|
|
247 struct JT jt;
|
|
248 struct HJT hjt;
|
|
249 int ret;
|
|
250
|
|
251 #ifdef WRITE_CHECK
|
|
252 check_backup (file_name);
|
|
253 #endif
|
|
254 if ((fp = fopen (file_name, "r")) == NULL)
|
|
255 {
|
|
256 return WNN_FILE_NOT_EXIST;
|
|
257 }
|
|
258 if (input_file_header (fp, &fh) == -1)
|
|
259 {
|
|
260 ret = WNN_NOT_A_JISHO;
|
|
261 goto end;
|
|
262 }
|
|
263 switch (fh.file_type)
|
|
264 {
|
|
265 case WNN_FT_DICT_FILE:
|
|
266 if (input_header_jt (fp, &jt) == -1)
|
|
267 {
|
|
268 ret = WNN_NOT_A_JISHO;
|
|
269 goto end;
|
|
270 }
|
|
271 switch (jt.syurui)
|
|
272 {
|
|
273 case WNN_UD_DICT:
|
|
274 case WNN_REV_DICT:
|
|
275 case WNN_REG_DICT:
|
|
276 case WNN_STATIC_DICT:
|
|
277 #ifdef CONVERT_by_STROKE
|
|
278 case BWNN_REV_DICT:
|
|
279 #endif /* CONVERT_by_STROKE */
|
|
280 #ifdef CONVERT_with_SiSheng
|
|
281 case CWNN_REV_DICT:
|
|
282 #endif /* CONVERT_with_SiSheng */
|
|
283 ret = jt.syurui;
|
|
284 goto end;
|
|
285 default:
|
|
286 ret = WNN_NOT_A_JISHO;
|
|
287 goto end;
|
|
288 }
|
|
289 case WNN_FT_HINDO_FILE:
|
|
290 if (input_header_hjt (fp, &hjt) == -1)
|
|
291 {
|
|
292 ret = WNN_NOT_A_JISHO;
|
|
293 goto end;
|
|
294 }
|
|
295 ret = WNN_HINDO_FILE;
|
|
296 goto end;
|
|
297 case WNN_FT_FUZOKUGO_FILE:
|
|
298 ret = WNN_FUZOKUGO_FILE;
|
|
299 goto end;
|
|
300 default:
|
|
301 ret = WNN_NOT_A_JISHO;
|
|
302 goto end;
|
|
303 }
|
|
304 end:
|
|
305 fclose (fp);
|
|
306 return (ret);
|
|
307 }
|
|
308
|
|
309 /* js_file_discard */
|
|
310 void
|
|
311 js_file_discard (void)
|
|
312 {
|
|
313 int x;
|
|
314 int env_id, fid;
|
|
315 env_id = envhandle ();
|
|
316 fid = get4_cur ();
|
|
317 if (env_id == -1)
|
|
318 {
|
|
319 error_ret ();
|
|
320 return;
|
|
321 }
|
|
322 x = file_discard (env_id, fid);
|
|
323 if (x == -1)
|
|
324 {
|
|
325 error_ret ();
|
|
326 putc_purge ();
|
|
327 return;
|
|
328 }
|
|
329 put4_cur (x);
|
|
330 putc_purge ();
|
|
331 return;
|
|
332 }
|
|
333
|
|
334 static int
|
|
335 file_discard (int env_id, int fid)
|
|
336 {
|
|
337 if (del_fid_from_env (env_id, fid) == -1)
|
|
338 { /* invalid */
|
|
339 wnn_errorno = WNN_FID_ERROR;
|
|
340 return -1;
|
|
341 }
|
|
342 used_dic_fuzoku_delete (env_id, fid); /* Delete used dicts and fuzoku. H.T. */
|
|
343 if (files[fid].ref_count != 0)
|
|
344 {
|
|
345 return files[fid].ref_count;
|
|
346 }
|
|
347 discardfile (&files[fid]);
|
|
348 files[fid].ref_count = -1;
|
|
349 return 0;
|
|
350 }
|
|
351
|
|
352 void
|
|
353 del_all_file_in_env (int env_id)
|
|
354 {
|
|
355 int i, fid;
|
|
356
|
|
357 for (i = 0; i < WNN_MAX_FILE_OF_AN_ENV; i++)
|
|
358 {
|
|
359 if ((fid = (env[env_id]->file)[i]) == -1)
|
|
360 continue;
|
|
361 files[fid].ref_count--;
|
|
362 if (files[fid].ref_count == 0)
|
|
363 {
|
|
364 discardfile (&files[fid]);
|
|
365 files[fid].ref_count = -1;
|
|
366 }
|
|
367 }
|
|
368 }
|
|
369
|
|
370
|
|
371 /* js_hindo_file_create */
|
|
372 void
|
|
373 js_hindo_file_create (void)
|
|
374 {
|
|
375 int env_id, fid;
|
|
376 char fn[FILE_NAME_L];
|
|
377 w_char com[1024];
|
|
378 char hpasswd[WNN_PASSWD_LEN];
|
|
379
|
|
380 env_id = get4_cur (); /* env_id */
|
|
381 fid = get4_cur ();
|
|
382 get_file_name (fn, FILE_NAME_L);
|
|
383 getws_cur (com, 1024);
|
|
384 gets_cur (hpasswd, WNN_PASSWD_LEN);
|
|
385 if (find_fid_in_env (env_id, fid) == -1)
|
|
386 { /* valid */
|
|
387 wnn_errorno = WNN_FID_ERROR;
|
|
388 error_ret ();
|
|
389 putc_purge ();
|
|
390 return;
|
|
391 }
|
|
392
|
|
393 if (files[fid].ref_count == -1)
|
|
394 {
|
|
395 wnn_errorno = WNN_FID_ERROR;
|
|
396 error_ret ();
|
|
397 putc_purge ();
|
|
398 return;
|
|
399 }
|
|
400 if (create_hindo_file1 (&files[fid], fn, com, hpasswd) == -1)
|
|
401 {
|
|
402 wnn_errorno = WNN_FILE_CREATE_ERROR;
|
|
403 error_ret ();
|
|
404 putc_purge ();
|
|
405 return;
|
|
406 }
|
|
407 put4_cur (0);
|
|
408 putc_purge ();
|
|
409 }
|
|
410
|
|
411 /* js_hindo_file_create_client */
|
|
412 void
|
|
413 js_hindo_file_create_client (void)
|
|
414 {
|
|
415 int env_id, fid, i;
|
|
416 struct wnn_file_uniq *funiq;
|
|
417
|
|
418 env_id = get4_cur (); /* env_id */
|
|
419 fid = get4_cur ();
|
|
420
|
|
421 if (find_fid_in_env (env_id, fid) == -1)
|
|
422 { /* valid */
|
|
423 wnn_errorno = WNN_FID_ERROR;
|
|
424 error_ret ();
|
|
425 putc_purge ();
|
|
426 return;
|
|
427 }
|
|
428 if (files[fid].ref_count == -1)
|
|
429 {
|
|
430 wnn_errorno = WNN_FID_ERROR;
|
|
431 error_ret ();
|
|
432 putc_purge ();
|
|
433 return;
|
|
434 }
|
|
435 put4_cur (0);
|
|
436 put4_cur (((struct JT *) (files[fid].area))->maxserial);
|
|
437 funiq = &files[fid].f_uniq_org;
|
|
438 put4_cur (funiq->time);
|
|
439 put4_cur (funiq->dev);
|
|
440 put4_cur (funiq->inode);
|
|
441 for (i = 0; i < WNN_HOSTLEN; i++)
|
|
442 {
|
|
443 putc_cur (funiq->createhost[i]);
|
|
444 }
|
|
445 putc_purge ();
|
|
446 }
|
|
447
|
|
448
|
|
449 /* js_dic_file_create */
|
|
450 void
|
|
451 js_dic_file_create (void)
|
|
452 {
|
|
453 int type, x;
|
|
454 char fn[FILE_NAME_L];
|
|
455 w_char com[1024];
|
|
456 char passwd[WNN_PASSWD_LEN], hpasswd[WNN_PASSWD_LEN];
|
|
457 get4_cur (); /* env_id */
|
|
458 get_file_name (fn, FILE_NAME_L);
|
|
459 getws_cur (com, 1024);
|
|
460 gets_cur (passwd, WNN_PASSWD_LEN);
|
|
461 gets_cur (hpasswd, WNN_PASSWD_LEN);
|
|
462 type = get4_cur ();
|
|
463
|
|
464 if (type != WNN_REV_DICT &&
|
|
465 #ifdef CONVERT_by_STROKE
|
|
466 type != BWNN_REV_DICT &&
|
|
467 #endif /* CONVERT_by_STROKE */
|
|
468 #ifdef CONVERT_with_SiSheng
|
|
469 type != CWNN_REV_DICT &&
|
|
470 #endif /* CONVERT_with_SiSheng */
|
|
471 type != WNN_UD_DICT)
|
|
472 {
|
|
473 wnn_errorno = WNN_NOT_A_UD;
|
|
474 error_ret ();
|
|
475 putc_purge ();
|
|
476 return;
|
|
477 }
|
|
478 x = create_null_dic (fn, com, passwd, hpasswd, type);
|
|
479 if (x == -1)
|
|
480 {
|
|
481 wnn_errorno = WNN_FILE_CREATE_ERROR;
|
|
482 error_ret ();
|
|
483 putc_purge ();
|
|
484 return;
|
|
485 }
|
|
486 put4_cur (0);
|
|
487 putc_purge ();
|
|
488 }
|
|
489
|
|
490 void
|
|
491 js_file_comment_set (void)
|
|
492 {
|
|
493 int fid, envi;
|
|
494 w_char comment[WNN_COMMENT_LEN];
|
|
495 int dic_no;
|
|
496
|
|
497 envi = get4_cur (); /* env_id */
|
|
498 fid = get4_cur ();
|
|
499 getws_cur (comment, WNN_COMMENT_LEN);
|
|
500
|
|
501 if (find_fid_in_env (envi, fid) == -1)
|
|
502 { /* valid */
|
|
503 wnn_errorno = WNN_FID_ERROR;
|
|
504 goto ERR_RET;
|
|
505 }
|
|
506 if ((dic_no = chk_dic (envi, fid)) == -1 && (dic_no = chk_hindo (envi, fid)) == -1)
|
|
507 {
|
|
508 wnn_errorno = WNN_FID_ERROR;
|
|
509 goto ERR_RET;
|
|
510 }
|
|
511 if (dic_table[dic_no].body == fid && dic_table[dic_no].rw == WNN_DIC_RDONLY)
|
|
512 {
|
|
513 wnn_errorno = WNN_RDONLY;
|
|
514 goto ERR_RET;
|
|
515 }
|
|
516 if (dic_table[dic_no].hindo == fid && dic_table[dic_no].hindo_rw == WNN_DIC_RDONLY)
|
|
517 {
|
|
518 wnn_errorno = WNN_RDONLY;
|
|
519 goto ERR_RET;
|
|
520 }
|
1
|
521 if (!file_comment_set (&files[fid], comment))
|
0
|
522 {
|
|
523 goto ERR_RET;
|
|
524 }
|
|
525 put4_cur (0);
|
|
526 putc_purge ();
|
|
527 return;
|
|
528 ERR_RET:
|
|
529 error_ret ();
|
|
530 putc_purge ();
|
|
531 }
|
|
532
|
|
533
|
|
534 static FILE *
|
|
535 xFopen (char *n, char *mode)
|
|
536 {
|
|
537 FILE *f;
|
|
538 if (n == NULL)
|
|
539 return NULL;
|
|
540 #ifdef WRITE_CHECK
|
|
541 check_backup (n);
|
|
542 #endif
|
|
543 if ((f = fopen (n, mode)) == NULL)
|
|
544 {
|
|
545 wnn_errorno = WNN_NO_EXIST;
|
|
546 return (FILE *) - 1;
|
|
547 }
|
|
548 return f;
|
|
549 }
|
|
550
|
|
551 static void
|
|
552 xFclose (FILE *f)
|
|
553 {
|
|
554 if (f == NULL)
|
|
555 return;
|
|
556 fclose (f);
|
|
557 }
|
|
558
|
|
559 /* js_file_loaded_local (in client machine) */
|
|
560 void
|
|
561 js_file_loaded_local (void)
|
|
562 {
|
|
563
|
|
564 /* get4_cur(); env_id */
|
|
565
|
|
566 file_loaded_local ();
|
|
567 }
|
|
568
|
|
569 static int
|
|
570 file_loaded_local (void)
|
|
571 {
|
|
572 int x, i;
|
|
573 struct wnn_file_uniq fq;
|
|
574
|
|
575 fq.time = get4_cur ();
|
|
576 fq.dev = get4_cur ();
|
|
577 fq.inode = get4_cur ();
|
|
578 for (i = 0; i < WNN_HOSTLEN; i++)
|
|
579 {
|
|
580 fq.createhost[i] = getc_cur ();
|
|
581 }
|
|
582
|
|
583 /*
|
|
584 fprintf(stderr,"js_file_loaded:ti=%x,dev=%x,inode=%x,%s\n",
|
|
585 fq.time,fq.dev,fq.inode,fq.createhost);
|
|
586 */
|
|
587 x = find_file_by_uniq (&fq);
|
|
588 if (x == -1)
|
|
589 put4_cur (-1);
|
|
590 else
|
|
591 put4_cur (x);
|
|
592 putc_purge ();
|
|
593 return x;
|
|
594 }
|
|
595
|
|
596 /* js_file_loaded (in jserver machine) */
|
|
597 void
|
|
598 js_file_loaded (void)
|
|
599 {
|
|
600 char n[FILE_NAME_L];
|
|
601 int x;
|
|
602
|
|
603 /* get4_cur(); env_id */
|
|
604 get_file_name (n, FILE_NAME_L);
|
|
605
|
|
606 if ((x = file_loaded (n)) < 0)
|
|
607 put4_cur (-1);
|
|
608 else
|
|
609 put4_cur (x);
|
|
610 putc_purge ();
|
|
611 }
|
|
612
|
|
613 int
|
|
614 file_loaded (char *n)
|
|
615 {
|
|
616 int x;
|
|
617 FILE *f;
|
|
618 struct wnn_file_head fh;
|
|
619
|
|
620 f = xFopen (n, "r");
|
|
621 if (f == (FILE *) - 1 || f == NULL)
|
|
622 {
|
|
623 wnn_errorno = WNN_NO_EXIST;
|
|
624 return -1;
|
|
625 }
|
|
626 x = input_file_header (f, &fh);
|
|
627 if (x == -1)
|
|
628 { /* Must not do check_inode to non-wnn file */
|
|
629 xFclose (f);
|
|
630 wnn_errorno = WNN_NOT_A_FILE;
|
|
631 return -1;
|
|
632 }
|
|
633 /* Check I-node H.T. */
|
|
634 if (check_inode (f, &fh) == -1)
|
|
635 {
|
|
636 #ifdef WRITE_CHECK
|
|
637 if (change_file_uniq (&fh, n) == -1)
|
|
638 {
|
|
639 xFclose (f);
|
|
640 wnn_errorno = WNN_INODE_CHECK_ERROR;
|
|
641 return (-1);
|
|
642 }
|
|
643 else
|
|
644 {
|
|
645 xFclose (f);
|
|
646 f = xFopen (n, "r");
|
|
647 if (f == (FILE *) - 1 || f == NULL)
|
|
648 {
|
|
649 wnn_errorno = WNN_NO_EXIST;
|
|
650 return -1;
|
|
651 }
|
|
652 if (check_inode (f, &fh) == -1)
|
|
653 {
|
|
654 xFclose (f);
|
|
655 wnn_errorno = WNN_INODE_CHECK_ERROR;
|
|
656 return (-1);
|
|
657 }
|
|
658 }
|
|
659 #else /* WRITE_CHECK */
|
|
660 if ((change_file_uniq (&fh, n) == -1) || (check_inode (f, &fh) == -1))
|
|
661 {
|
|
662 xFclose (f);
|
|
663 wnn_errorno = WNN_INODE_CHECK_ERROR;
|
|
664 return (-1);
|
|
665 }
|
|
666 #endif /* WRITE_CHECK */
|
|
667 }
|
|
668
|
|
669 xFclose (f);
|
|
670
|
|
671 x = find_file_by_uniq (&fh.file_uniq);
|
|
672 if (x == -1)
|
|
673 x = FILE_NOT_READ;
|
|
674 return x;
|
|
675 }
|
|
676
|
|
677 static int
|
|
678 find_file_by_uniq (struct wnn_file_uniq *uniq)
|
|
679 {
|
|
680 int i;
|
|
681 for (i = 1; i < MAX_FILES; i++)
|
|
682 {
|
|
683 if (files[i].ref_count == -1)
|
|
684 continue;
|
|
685 if (!f_uniq_cmp (&files[i].f_uniq, uniq))
|
|
686 return i;
|
|
687 }
|
|
688 return -1;
|
|
689 }
|
|
690
|
|
691
|
|
692 /* js_file_send (from Client to Jserver) */
|
|
693 void
|
|
694 js_file_send (void)
|
|
695 {
|
|
696 char n[FILE_NAME_L];
|
|
697 int env_id, fid;
|
|
698
|
|
699 env_id = get4_cur (); /* env_id */
|
|
700
|
|
701 /* file loaded ? */
|
|
702 fid = file_loaded_local ();
|
|
703
|
|
704 if (fid != -1)
|
|
705 {
|
|
706 if (x_file_already_read (env_id, fid) == -1)
|
|
707 { /* H.T. */
|
|
708 error_ret ();
|
|
709 putc_purge ();
|
|
710 return;
|
|
711 }
|
|
712 else
|
|
713 {
|
|
714 put4_cur (0);
|
|
715 putc_purge ();
|
|
716 return;
|
|
717 }
|
|
718 }
|
|
719
|
|
720 if ((fid = get_new_fid ()) == -1)
|
|
721 { /* no more file */
|
|
722 wnn_errorno = WNN_NO_MORE_FILE;
|
|
723 error_ret ();
|
|
724 putc_purge ();
|
|
725 return;
|
|
726 }
|
|
727
|
|
728 put4_cur (1);
|
|
729 putc_purge ();
|
|
730
|
|
731 gets_cur (n, FILE_NAME_L);
|
|
732
|
|
733 /* read file */
|
|
734 files[fid].localf = REMOTE;
|
|
735 files[fid].ref_count = 0;
|
|
736 strcpy (files[fid].name, n);
|
|
737 if (read_file (&files[fid]) == -1)
|
|
738 {
|
|
739 /* wnn_errorno=WNN_FILE_READ_ERROR; */
|
|
740 error_ret ();
|
|
741 putc_purge ();
|
|
742 files[fid].ref_count = -1; /* fail */
|
|
743 return;
|
|
744 }
|
|
745 add_fid_to_env (env_id, fid);
|
|
746 put4_cur (fid);
|
|
747 putc_purge ();
|
|
748 }
|
|
749
|
|
750 /* js_file_read */
|
|
751 void
|
|
752 js_file_read (void)
|
|
753 {
|
|
754 char n[FILE_NAME_L];
|
|
755 int env_id, fid;
|
|
756
|
|
757 env_id = get4_cur (); /* env_id */
|
|
758 get_file_name (n, FILE_NAME_L);
|
|
759
|
|
760 fid = file_loaded (n);
|
|
761 if (fid == -1)
|
|
762 {
|
|
763 error_ret ();
|
|
764 putc_purge ();
|
|
765 return;
|
|
766 }
|
|
767 if (FILE_NOT_READ != fid)
|
|
768 { /* already read */
|
|
769 file_already_read (env_id, fid);
|
|
770 putc_purge ();
|
|
771 return;
|
|
772 }
|
|
773 if ((fid = get_new_fid ()) == -1)
|
|
774 { /* no more file */
|
|
775 wnn_errorno = WNN_NO_MORE_FILE;
|
|
776 error_ret ();
|
|
777 putc_purge ();
|
|
778 return;
|
|
779 }
|
|
780 /* read file */
|
|
781 files[fid].localf = LOCAL;
|
|
782 files[fid].ref_count = 0;
|
|
783 strcpy (files[fid].name, n);
|
|
784 if (read_file (&files[fid]) == -1)
|
|
785 {
|
|
786 /* wnn_errorno=WNN_FILE_READ_ERROR;*/
|
|
787 error_ret ();
|
|
788 putc_purge ();
|
|
789 files[fid].ref_count = -1; /* fail */
|
|
790 return;
|
|
791 }
|
|
792 add_fid_to_env (env_id, fid);
|
|
793 put4_cur (fid);
|
|
794 putc_purge ();
|
|
795 }
|
|
796
|
|
797 static void
|
|
798 file_already_read (int env_id, int fid)
|
|
799 {
|
|
800 if (-1 != find_fid_in_env (env_id, fid))
|
|
801 { /* already exists in env */
|
|
802 put4_cur (fid);
|
|
803 return;
|
|
804 }
|
|
805 if (-1 == add_fid_to_env (env_id, fid))
|
|
806 {
|
|
807 wnn_errorno = WNN_NO_MORE_FILE;
|
|
808 error_ret ();
|
|
809 return;
|
|
810 }
|
|
811 put4_cur (fid);
|
|
812 return;
|
|
813 }
|
|
814
|
|
815 static int
|
|
816 x_file_already_read (int env_id, int fid)
|
|
817 {
|
|
818 if (-1 != find_fid_in_env (env_id, fid))
|
|
819 { /* already exists in env */
|
|
820 return fid;
|
|
821 }
|
|
822 if (-1 == add_fid_to_env (env_id, fid))
|
|
823 {
|
|
824 wnn_errorno = WNN_NO_MORE_FILE;
|
|
825 return -1;
|
|
826 }
|
|
827 return fid;
|
|
828 }
|
|
829
|
|
830 /* js_file_write */
|
|
831 void
|
|
832 js_file_write (void)
|
|
833 {
|
|
834 int env_id, fid;
|
|
835 char n[FILE_NAME_L];
|
|
836 env_id = get4_cur (); /* env_id */
|
|
837 fid = get4_cur ();
|
|
838 get_file_name (n, FILE_NAME_L);
|
|
839
|
|
840 if (find_fid_in_env (env_id, fid) == -1)
|
|
841 { /* valid */
|
|
842 wnn_errorno = WNN_FID_ERROR;
|
|
843 error_ret ();
|
|
844 putc_purge ();
|
|
845 return;
|
|
846 }
|
|
847 if (files[fid].ref_count == -1)
|
|
848 {
|
|
849 wnn_errorno = WNN_FID_ERROR;
|
|
850 error_ret ();
|
|
851 putc_purge ();
|
|
852 return;
|
|
853 }
|
|
854 if (strcmp (n, "") == 0)
|
|
855 strcpy (n, files[fid].name);
|
|
856 if (write_file (&files[fid], n) == -1)
|
|
857 {
|
|
858 /* wnn_errorno=WNN_FILE_WRITE_ERROR; */ error_ret ();
|
|
859 putc_purge ();
|
|
860 return;
|
|
861 }
|
|
862 put4_cur (fid);
|
|
863 putc_purge ();
|
|
864 }
|
|
865
|
|
866
|
|
867 /* js_file_receive (from Jserver to Client) */
|
|
868 void
|
|
869 js_file_receive (void)
|
|
870 {
|
|
871 int env_id, fid;
|
|
872 struct wnn_file_uniq fq;
|
|
873 int mode = 1;
|
|
874 int i;
|
|
875 int error = 0;
|
|
876
|
|
877 env_id = get4_cur (); /* env_id */
|
|
878 fid = get4_cur ();
|
|
879 /**/ if (find_fid_in_env (env_id, fid) == -1)
|
|
880 { /* valid */
|
|
881 puts_cur ("!");
|
|
882 putc_purge (); /* dummy */
|
|
883 error = 1;
|
|
884 }
|
|
885 else
|
|
886 {
|
|
887 puts_cur (files[fid].name);
|
|
888 putc_purge ();
|
|
889 }
|
|
890 if (get4_cur () == -1)
|
|
891 return; /* Ack */
|
|
892
|
|
893 fq.time = get4_cur ();
|
|
894 fq.dev = get4_cur ();
|
|
895 fq.inode = get4_cur ();
|
|
896 for (i = 0; i < WNN_HOSTLEN; i++)
|
|
897 {
|
|
898 fq.createhost[i] = getc_cur ();
|
|
899 }
|
|
900 if (error || files[fid].ref_count == -1)
|
|
901 {
|
|
902 wnn_errorno = WNN_FID_ERROR;
|
|
903 error_ret ();
|
|
904 putc_purge ();
|
|
905 return;
|
|
906 }
|
|
907 if (fq.time != 0)
|
|
908 { /* Old File Exist */
|
|
909 mode = must_write_file (&files[fid], &fq);
|
|
910 }
|
|
911
|
|
912 if (mode == -1)
|
|
913 {
|
|
914 wnn_errorno = WNN_FID_ERROR;
|
|
915 error_ret ();
|
|
916 putc_purge ();
|
|
917 return;
|
|
918 }
|
|
919 else if (mode == 0)
|
|
920 { /* need not saving */
|
|
921 put4_cur (0);
|
|
922 putc_purge ();
|
|
923 return;
|
|
924 }
|
|
925 put4_cur (mode);
|
|
926 putc_purge ();
|
|
927
|
|
928 if (get4_cur () == -1)
|
|
929 return;
|
|
930
|
|
931 /**/ if (rcv_file (&files[fid], mode) == -1)
|
|
932 {
|
|
933 /* wnn_errorno=WNN_FILE_WRITE_ERROR; */ error_ret ();
|
|
934 putc_purge ();
|
|
935 return;
|
|
936 }
|
|
937 if (mode == 1 || mode == 2)
|
|
938 {
|
|
939 clear_dirty_bit (&files[fid]);
|
|
940 }
|
|
941
|
|
942 put4_cur (0);
|
|
943 /*
|
|
944 put4_cur(fid);
|
|
945 */
|
|
946 putc_purge ();
|
|
947 }
|
|
948
|
|
949
|
|
950 /* */
|
|
951 int
|
|
952 get_new_fid (void)
|
|
953 {
|
|
954 int fid;
|
|
955 for (fid = 1; fid < MAX_FILES; fid++)
|
|
956 {
|
|
957 if (files[fid].ref_count == -1)
|
|
958 return fid; /* find new area */
|
|
959 }
|
|
960 return -1; /* no more file */
|
|
961 }
|
|
962
|
|
963 /*
|
|
964 routines for FID in the ENV
|
|
965 */
|
|
966
|
|
967 static int
|
|
968 add_fid_to_env (int env_id, int fid)
|
|
969 {
|
|
970 int i;
|
|
971 if ((i = new_fid_in_env (env_id, -1)) == -1)
|
|
972 {
|
|
973 return -1;
|
|
974 }
|
|
975 (env[env_id]->file)[i] = fid;
|
|
976 files[fid].ref_count++;
|
|
977 return i;
|
|
978 }
|
|
979
|
|
980 static int
|
|
981 del_fid_from_env (int env_id, int fid)
|
|
982 {
|
|
983 int i;
|
|
984 if ((i = find_fid_in_env (env_id, fid)) == -1)
|
|
985 {
|
|
986 return -1;
|
|
987 }
|
|
988 (env[env_id]->file)[i] = -1;
|
|
989 files[fid].ref_count--;
|
|
990 return i;
|
|
991 }
|
|
992
|
|
993
|
|
994 int
|
|
995 find_fid_in_env (int env_id, int fid)
|
|
996 {
|
|
997 register int i;
|
|
998 register int *file = env[env_id]->file;
|
|
999 for (i = 0; i < WNN_MAX_FILE_OF_AN_ENV; i++)
|
|
1000 {
|
|
1001 if ((file)[i] == -1)
|
|
1002 continue;
|
|
1003 if ((file)[i] == fid)
|
|
1004 {
|
|
1005 return i;
|
|
1006 }
|
|
1007 }
|
|
1008 return -1;
|
|
1009 }
|
|
1010
|
|
1011 static int
|
|
1012 new_fid_in_env (int env_id, int fid)
|
|
1013 {
|
|
1014 int i;
|
|
1015 for (i = 0; i < WNN_MAX_FILE_OF_AN_ENV; i++)
|
|
1016 {
|
|
1017 if ((env[env_id]->file)[i] == -1)
|
|
1018 {
|
|
1019 return i;
|
|
1020 }
|
|
1021 }
|
|
1022 return -1;
|
|
1023 }
|
|
1024
|
|
1025 /* js_file_remove (remove file from UNIX file system) H.T. */
|
|
1026 void
|
|
1027 js_file_remove (void)
|
|
1028 {
|
|
1029 char n[FILE_NAME_L];
|
|
1030 char passwd[WNN_PASSWD_LEN];
|
|
1031 int x;
|
|
1032
|
|
1033 get_file_name (n, FILE_NAME_L);
|
|
1034 gets_cur (passwd, WNN_PASSWD_LEN);
|
|
1035
|
|
1036 x = file_remove (n, passwd);
|
|
1037 if (x == -1)
|
|
1038 {
|
|
1039 error_ret ();
|
|
1040 }
|
|
1041 else
|
|
1042 {
|
|
1043 put4_cur (0);
|
|
1044 }
|
|
1045 putc_purge ();
|
|
1046 }
|
|
1047
|
|
1048 static int
|
|
1049 file_remove (char *n, char *passwd)
|
|
1050 {
|
|
1051 int fid;
|
|
1052 FILE *fp;
|
|
1053 struct wnn_file_head fh;
|
|
1054
|
|
1055 fid = file_loaded (n);
|
|
1056 if (fid == -1)
|
|
1057 {
|
|
1058 return (-1);
|
|
1059 }
|
|
1060 if (FILE_NOT_READ != fid)
|
|
1061 { /* already read */
|
|
1062 wnn_errorno = WNN_FILE_IN_USE;
|
|
1063 return (-1);
|
|
1064 }
|
|
1065 #ifdef WRITE_CHECK
|
|
1066 check_backup (n);
|
|
1067 #endif
|
|
1068 if ((fp = fopen (n, "r")) == NULL)
|
|
1069 {
|
|
1070 wnn_errorno = WNN_FILE_READ_ERROR;
|
|
1071 return (-1);
|
|
1072 }
|
|
1073 if (input_file_header (fp, &fh) == -1)
|
|
1074 {
|
|
1075 fclose (fp);
|
|
1076 return (-1);
|
|
1077 }
|
|
1078 fclose (fp);
|
|
1079 if (!check_pwd (passwd, fh.file_passwd))
|
|
1080 {
|
|
1081 wnn_errorno = WNN_INCORRECT_PASSWD;
|
|
1082 return (-1);
|
|
1083 }
|
|
1084 if (unlink (n) == -1)
|
|
1085 {
|
|
1086 wnn_errorno = WNN_UNLINK;
|
|
1087 return (-1);
|
|
1088 }
|
|
1089 return (0);
|
|
1090 }
|
|
1091
|
|
1092
|
|
1093 void
|
|
1094 js_file_password_set (void)
|
|
1095 {
|
|
1096 int fid, envi, which;
|
|
1097 char old[WNN_PASSWD_LEN];
|
|
1098 char new[WNN_PASSWD_LEN];
|
|
1099 int ret;
|
|
1100
|
|
1101 envi = get4_cur (); /* env_id */
|
|
1102 fid = get4_cur ();
|
|
1103 which = get4_cur ();
|
|
1104 gets_cur (old, WNN_PASSWD_LEN);
|
|
1105 gets_cur (new, WNN_PASSWD_LEN);
|
|
1106
|
|
1107 if (find_fid_in_env (envi, fid) == -1)
|
|
1108 {
|
|
1109 wnn_errorno = WNN_FID_ERROR;
|
|
1110 error_ret ();
|
|
1111 putc_purge ();
|
|
1112 return;
|
|
1113 }
|
|
1114
|
|
1115 ret = file_password_set (&files[fid], which, old, new);
|
|
1116 if (ret == -1)
|
|
1117 {
|
|
1118 goto ERR_RET;
|
|
1119 }
|
|
1120 put4_cur (0);
|
|
1121 putc_purge ();
|
|
1122 return;
|
|
1123 ERR_RET:
|
|
1124 error_ret ();
|
|
1125 putc_purge ();
|
|
1126 }
|