Just try to delete the Pos, if we realy found the entry.
[citadel.git] / citadel / file_ops.c
1 /* 
2  * Server functions which handle file transfers and room directories.
3  */
4
5 #include "sysdep.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <sys/stat.h>
11 #include <errno.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <sys/mman.h>
16
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
19 # include <time.h>
20 #else
21 # if HAVE_SYS_TIME_H
22 #  include <sys/time.h>
23 # else
24 #  include <time.h>
25 # endif
26 #endif
27
28 #include <limits.h>
29 #include <libcitadel.h>
30 #include "citadel.h"
31 #include "server.h"
32 #include "config.h"
33 #include "file_ops.h"
34 #include "sysdep_decls.h"
35 #include "support.h"
36 #include "room_ops.h"
37 #include "msgbase.h"
38 #include "citserver.h"
39 #include "threads.h"
40
41 #ifndef HAVE_SNPRINTF
42 #include "snprintf.h"
43 #endif
44
45 #include "ctdl_module.h"
46 #include "user_ops.h"
47
48 /*
49  * network_talking_to()  --  concurrency checker
50  */
51 static HashList *nttlist = NULL;
52 int network_talking_to(const char *nodename, long len, int operation) {
53
54         int retval = 0;
55         HashPos *Pos = NULL;
56         void *vdata;
57
58         begin_critical_section(S_NTTLIST);
59
60         switch(operation) {
61
62                 case NTT_ADD:
63                         if (nttlist == NULL) 
64                                 nttlist = NewHash(1, NULL);
65                         Put(nttlist, nodename, len, NewStrBufPlain(nodename, len), HFreeStrBuf);
66                         syslog(LOG_DEBUG, "nttlist: added <%s>\n", nodename);
67                         break;
68                 case NTT_REMOVE:
69                         if ((nttlist == NULL) ||
70                             (GetCount(nttlist) == 0))
71                                 break;
72                         Pos = GetNewHashPos(nttlist, 1);
73                         if (GetHashPosFromKey (nttlist, nodename, len, Pos))
74                                 DeleteEntryFromHash(nttlist, Pos);
75                         DeleteHashPos(&Pos);
76                         syslog(LOG_DEBUG, "nttlist: removed <%s>\n", nodename);
77
78                         break;
79
80                 case NTT_CHECK:
81                         if ((nttlist == NULL) ||
82                             (GetCount(nttlist) == 0))
83                                 break;
84                         if (!GetHash(nttlist, nodename, len, &vdata))
85                                 retval ++;
86                         syslog(LOG_DEBUG, "nttlist: have [%d] <%s>\n", retval, nodename);
87                         break;
88         }
89
90         end_critical_section(S_NTTLIST);
91         return(retval);
92 }
93
94 void cleanup_nttlist(void)
95 {
96         begin_critical_section(S_NTTLIST);
97         DeleteHash(&nttlist);
98         end_critical_section(S_NTTLIST);
99 }
100
101
102
103 /*
104  * Server command to delete a file from a room's directory
105  */
106 void cmd_delf(char *filename)
107 {
108         char pathname[64];
109         int a;
110
111         if (CtdlAccessCheck(ac_room_aide))
112                 return;
113
114         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
115                 cprintf("%d No directory in this room.\n",
116                         ERROR + NOT_HERE);
117                 return;
118         }
119
120         if (IsEmptyStr(filename)) {
121                 cprintf("%d You must specify a file name.\n",
122                         ERROR + FILE_NOT_FOUND);
123                 return;
124         }
125         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
126                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
127                         filename[a] = '_';
128                 }
129         }
130         snprintf(pathname, sizeof pathname,
131                          "%s/%s/%s",
132                          ctdl_file_dir,
133                          CC->room.QRdirname, filename);
134         a = unlink(pathname);
135         if (a == 0) {
136                 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
137         }
138         else {
139                 cprintf("%d File '%s' not found.\n",
140                         ERROR + FILE_NOT_FOUND, pathname);
141         }
142 }
143
144
145
146
147 /*
148  * move a file from one room directory to another
149  */
150 void cmd_movf(char *cmdbuf)
151 {
152         char filename[PATH_MAX];
153         char pathname[PATH_MAX];
154         char newpath[PATH_MAX];
155         char newroom[ROOMNAMELEN];
156         char buf[PATH_MAX];
157         int a;
158         struct ctdlroom qrbuf;
159         int rv = 0;
160
161         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
162         extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
163
164         if (CtdlAccessCheck(ac_room_aide)) return;
165
166         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
167                 cprintf("%d No directory in this room.\n",
168                         ERROR + NOT_HERE);
169                 return;
170         }
171
172         if (IsEmptyStr(filename)) {
173                 cprintf("%d You must specify a file name.\n",
174                         ERROR + FILE_NOT_FOUND);
175                 return;
176         }
177
178         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
179                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
180                         filename[a] = '_';
181                 }
182         }
183         snprintf(pathname, sizeof pathname, "./files/%s/%s",
184                  CC->room.QRdirname, filename);
185         if (access(pathname, 0) != 0) {
186                 cprintf("%d File '%s' not found.\n",
187                         ERROR + FILE_NOT_FOUND, pathname);
188                 return;
189         }
190
191         if (CtdlGetRoom(&qrbuf, newroom) != 0) {
192                 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
193                 return;
194         }
195         if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
196                 cprintf("%d '%s' is not a directory room.\n",
197                         ERROR + NOT_HERE, qrbuf.QRname);
198                 return;
199         }
200         snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
201                  filename);
202         if (link(pathname, newpath) != 0) {
203                 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
204                         strerror(errno));
205                 return;
206         }
207         unlink(pathname);
208
209         /* this is a crude method of copying the file description */
210         snprintf(buf, sizeof buf,
211                  "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
212                  CC->room.QRdirname, filename, qrbuf.QRdirname);
213         rv = system(buf);
214         cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
215 }
216
217
218 /*
219  * This code is common to all commands which open a file for downloading,
220  * regardless of whether it's a file from the directory, an image, a network
221  * spool file, a MIME attachment, etc.
222  * It examines the file and displays the OK result code and some information
223  * about the file.  NOTE: this stuff is Unix dependent.
224  */
225 void OpenCmdResult(char *filename, const char *mime_type)
226 {
227         struct stat statbuf;
228         time_t modtime;
229         long filesize;
230
231         fstat(fileno(CC->download_fp), &statbuf);
232         CC->download_fp_total = statbuf.st_size;
233         filesize = (long) statbuf.st_size;
234         modtime = (time_t) statbuf.st_mtime;
235
236         cprintf("%d %ld|%ld|%s|%s\n",
237                 CIT_OK, filesize, (long)modtime, filename, mime_type);
238 }
239
240
241 /*
242  * open a file for downloading
243  */
244 void cmd_open(char *cmdbuf)
245 {
246         char filename[256];
247         char pathname[PATH_MAX];
248         int a;
249
250         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
251
252         if (CtdlAccessCheck(ac_logged_in)) return;
253
254         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
255                 cprintf("%d No directory in this room.\n",
256                         ERROR + NOT_HERE);
257                 return;
258         }
259
260         if (IsEmptyStr(filename)) {
261                 cprintf("%d You must specify a file name.\n",
262                         ERROR + FILE_NOT_FOUND);
263                 return;
264         }
265
266         if (CC->download_fp != NULL) {
267                 cprintf("%d You already have a download file open.\n",
268                         ERROR + RESOURCE_BUSY);
269                 return;
270         }
271
272         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
273                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
274                         filename[a] = '_';
275                 }
276         }
277
278         snprintf(pathname, sizeof pathname,
279                          "%s/%s/%s",
280                          ctdl_file_dir,
281                          CC->room.QRdirname, filename);
282         CC->download_fp = fopen(pathname, "r");
283
284         if (CC->download_fp == NULL) {
285                 cprintf("%d cannot open %s: %s\n",
286                         ERROR + INTERNAL_ERROR, pathname, strerror(errno));
287                 return;
288         }
289
290         OpenCmdResult(filename, "application/octet-stream");
291 }
292
293 /*
294  * open an image file
295  */
296 void cmd_oimg(char *cmdbuf)
297 {
298         char filename[256];
299         char pathname[PATH_MAX];
300         char MimeTestBuf[32];
301         struct ctdluser usbuf;
302         char which_user[USERNAME_SIZE];
303         int which_floor;
304         int a;
305         int rv;
306
307         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
308
309         if (IsEmptyStr(filename)) {
310                 cprintf("%d You must specify a file name.\n",
311                         ERROR + FILE_NOT_FOUND);
312                 return;
313         }
314
315         if (CC->download_fp != NULL) {
316                 cprintf("%d You already have a download file open.\n",
317                         ERROR + RESOURCE_BUSY);
318                 return;
319         }
320
321         if (!strcasecmp(filename, "_userpic_")) {
322                 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
323                 if (CtdlGetUser(&usbuf, which_user) != 0) {
324                         cprintf("%d No such user.\n",
325                                 ERROR + NO_SUCH_USER);
326                         return;
327                 }
328                 snprintf(pathname, sizeof pathname, 
329                                  "%s/%ld",
330                                  ctdl_usrpic_dir,
331                                  usbuf.usernum);
332         } else if (!strcasecmp(filename, "_floorpic_")) {
333                 which_floor = extract_int(cmdbuf, 1);
334                 snprintf(pathname, sizeof pathname,
335                                  "%s/floor.%d",
336                                  ctdl_image_dir, which_floor);
337         } else if (!strcasecmp(filename, "_roompic_")) {
338                 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
339         } else {
340                 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
341                         filename[a] = tolower(filename[a]);
342                         if ( (filename[a] == '/') || (filename[a] == '\\') ) {
343                                 filename[a] = '_';
344                         }
345                 }
346                 snprintf(pathname, sizeof pathname,
347                                  "%s/%s",
348                                  ctdl_image_dir,
349                                  filename);
350         }
351
352         CC->download_fp = fopen(pathname, "rb");
353         if (CC->download_fp == NULL) {
354                 strcat(pathname, ".gif");
355                 CC->download_fp = fopen(pathname, "rb");
356         }
357         if (CC->download_fp == NULL) {
358                 cprintf("%d Cannot open %s: %s\n",
359                         ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
360                 return;
361         }
362         rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
363         if (rv == -1) {
364                 cprintf("%d Cannot access %s: %s\n",
365                         ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
366                 return;
367         }
368
369         rewind (CC->download_fp);
370         OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
371 }
372
373 /*
374  * open a file for uploading
375  */
376 void cmd_uopn(char *cmdbuf)
377 {
378         int a;
379
380         extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
381         extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
382         extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
383
384         if (CtdlAccessCheck(ac_logged_in)) return;
385
386         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
387                 cprintf("%d No directory in this room.\n",
388                         ERROR + NOT_HERE);
389                 return;
390         }
391
392         if (IsEmptyStr(CC->upl_file)) {
393                 cprintf("%d You must specify a file name.\n",
394                         ERROR + FILE_NOT_FOUND);
395                 return;
396         }
397
398         if (CC->upload_fp != NULL) {
399                 cprintf("%d You already have a upload file open.\n",
400                         ERROR + RESOURCE_BUSY);
401                 return;
402         }
403
404         for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
405                 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
406                         CC->upl_file[a] = '_';
407                 }
408         }
409         snprintf(CC->upl_path, sizeof CC->upl_path, 
410                          "%s/%s/%s",
411                          ctdl_file_dir,
412                          CC->room.QRdirname, CC->upl_file);
413         snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
414                          "%s/%s/filedir", 
415                          ctdl_file_dir,
416                          CC->room.QRdirname);
417
418         CC->upload_fp = fopen(CC->upl_path, "r");
419         if (CC->upload_fp != NULL) {
420                 fclose(CC->upload_fp);
421                 CC->upload_fp = NULL;
422                 cprintf("%d '%s' already exists\n",
423                         ERROR + ALREADY_EXISTS, CC->upl_path);
424                 return;
425         }
426
427         CC->upload_fp = fopen(CC->upl_path, "wb");
428         if (CC->upload_fp == NULL) {
429                 cprintf("%d Cannot open %s: %s\n",
430                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
431                 return;
432         }
433         cprintf("%d Ok\n", CIT_OK);
434 }
435
436
437
438 /*
439  * open an image file for uploading
440  */
441 void cmd_uimg(char *cmdbuf)
442 {
443         int is_this_for_real;
444         char basenm[256];
445         int which_floor;
446         int a;
447
448         if (num_parms(cmdbuf) < 2) {
449                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
450                 return;
451         }
452
453         is_this_for_real = extract_int(cmdbuf, 0);
454         extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
455         extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
456         if (CC->upload_fp != NULL) {
457                 cprintf("%d You already have an upload file open.\n",
458                         ERROR + RESOURCE_BUSY);
459                 return;
460         }
461
462         strcpy(CC->upl_path, "");
463
464         for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
465                 basenm[a] = tolower(basenm[a]);
466                 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
467                         basenm[a] = '_';
468                 }
469         }
470
471         if (CC->user.axlevel >= AxAideU) {
472                 snprintf(CC->upl_path, sizeof CC->upl_path, 
473                                  "%s/%s",
474                                  ctdl_image_dir,
475                                  basenm);
476         }
477
478         if (!strcasecmp(basenm, "_userpic_")) {
479                 snprintf(CC->upl_path, sizeof CC->upl_path,
480                                  "%s/%ld.gif",
481                                  ctdl_usrpic_dir,
482                                  CC->user.usernum);
483         }
484
485         if ((!strcasecmp(basenm, "_floorpic_"))
486             && (CC->user.axlevel >= AxAideU)) {
487                 which_floor = extract_int(cmdbuf, 2);
488                 snprintf(CC->upl_path, sizeof CC->upl_path,
489                                  "%s/floor.%d.gif",
490                                  ctdl_image_dir,
491                                  which_floor);
492         }
493
494         if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
495                 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
496         }
497
498         if (IsEmptyStr(CC->upl_path)) {
499                 cprintf("%d Higher access required.\n",
500                         ERROR + HIGHER_ACCESS_REQUIRED);
501                 return;
502         }
503
504         if (is_this_for_real == 0) {
505                 cprintf("%d Ok to send image\n", CIT_OK);
506                 return;
507         }
508
509         CC->upload_fp = fopen(CC->upl_path, "wb");
510         if (CC->upload_fp == NULL) {
511                 cprintf("%d Cannot open %s: %s\n",
512                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
513                 return;
514         }
515         cprintf("%d Ok\n", CIT_OK);
516         CC->upload_type = UPL_IMAGE;
517 }
518
519
520 /*
521  * close the download file
522  */
523 void cmd_clos(char *cmdbuf)
524 {
525         char buf[256];
526
527         if (CC->download_fp == NULL) {
528                 cprintf("%d You don't have a download file open.\n",
529                         ERROR + RESOURCE_NOT_OPEN);
530                 return;
531         }
532
533         fclose(CC->download_fp);
534         CC->download_fp = NULL;
535
536         if (CC->dl_is_net == 1) {
537                 CC->dl_is_net = 0;
538                 snprintf(buf, sizeof buf, 
539                                  "%s/%s",
540                                  ctdl_netout_dir,
541                                  CC->net_node);
542                 unlink(buf);
543         }
544
545         cprintf("%d Ok\n", CIT_OK);
546 }
547
548
549 /*
550  * abort an upload
551  */
552 void abort_upl(CitContext *who)
553 {
554         if (who->upload_fp != NULL) {
555                 fclose(who->upload_fp);
556                 who->upload_fp = NULL;
557                 unlink(CC->upl_path);
558         }
559 }
560
561
562
563 /*
564  * close the upload file
565  */
566 void cmd_ucls(char *cmd)
567 {
568         FILE *fp;
569         char upload_notice[512];
570         static int seq = 0;
571
572         if (CC->upload_fp == NULL) {
573                 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
574                 return;
575         }
576
577         fclose(CC->upload_fp);
578         CC->upload_fp = NULL;
579
580         if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
581                 cprintf("%d Upload completed.\n", CIT_OK);
582
583                 if (CC->upload_type == UPL_NET) {
584                         char final_filename[PATH_MAX];
585                         snprintf(final_filename, sizeof final_filename,
586                                 "%s/%s.%04lx.%04x",
587                                 ctdl_netin_dir,
588                                 CC->net_node,
589                                 (long)getpid(),
590                                 ++seq
591                         );
592
593                         if (link(CC->upl_path, final_filename) == 0) {
594                                 unlink(CC->upl_path);
595                         }
596                         else {
597                                 syslog(LOG_ALERT, "Cannot link %s to %s: %s\n",
598                                         CC->upl_path, final_filename, strerror(errno)
599                                 );
600                         }
601
602                         /* FIXME ... here we need to trigger a network run */
603                 }
604
605                 CC->upload_type = UPL_FILE;
606                 return;
607         }
608
609         if (!strcasecmp(cmd, "1")) {
610                 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
611                 fp = fopen(CC->upl_filedir, "a");
612                 if (fp == NULL) {
613                         fp = fopen(CC->upl_filedir, "w");
614                 }
615                 if (fp != NULL) {
616                         fprintf(fp, "%s %s %s\n", CC->upl_file,
617                                 CC->upl_mimetype,
618                                 CC->upl_comment);
619                         fclose(fp);
620                 }
621
622                 /* put together an upload notice */
623                 snprintf(upload_notice, sizeof upload_notice,
624                         "NEW UPLOAD: '%s'\n %s\n%s\n",
625                          CC->upl_file, 
626                          CC->upl_comment, 
627                          CC->upl_mimetype);
628                 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
629                                 upload_notice, 0, NULL);
630         } else {
631                 abort_upl(CC);
632                 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
633         }
634 }
635
636
637 /*
638  * read from the download file
639  */
640 void cmd_read(char *cmdbuf)
641 {
642         long start_pos;
643         size_t bytes;
644         char buf[SIZ];
645
646         /* The client will transmit its requested offset and byte count */
647         start_pos = extract_long(cmdbuf, 0);
648         bytes = extract_int(cmdbuf, 1);
649
650         if (CC->download_fp == NULL) {
651                 cprintf("%d You don't have a download file open.\n",
652                         ERROR + RESOURCE_NOT_OPEN);
653                 return;
654         }
655
656         /* If necessary, reduce the byte count to the size of our buffer */
657         if (bytes > sizeof(buf)) {
658                 bytes = sizeof(buf);
659         }
660
661         fseek(CC->download_fp, start_pos, 0);
662         bytes = fread(buf, 1, bytes, CC->download_fp);
663         if (bytes > 0) {
664                 /* Tell the client the actual byte count and transmit it */
665                 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
666                 client_write(buf, bytes);
667         }
668         else {
669                 cprintf("%d %s\n", ERROR, strerror(errno));
670         }
671 }
672
673
674 /*
675  * write to the upload file
676  */
677 void cmd_writ(char *cmdbuf)
678 {
679         int bytes;
680         char *buf;
681         int rv;
682
683         unbuffer_output();
684
685         bytes = extract_int(cmdbuf, 0);
686
687         if (CC->upload_fp == NULL) {
688                 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
689                 return;
690         }
691
692         if (bytes > 100000) {
693                 cprintf("%d You may not write more than 100000 bytes.\n",
694                         ERROR + TOO_BIG);
695                 return;
696         }
697
698         cprintf("%d %d\n", SEND_BINARY, bytes);
699         buf = malloc(bytes + 1);
700         client_read(buf, bytes);
701         rv = fwrite(buf, bytes, 1, CC->upload_fp);
702         if (rv == -1) {
703                 syslog(LOG_EMERG, "Couldn't write: %s\n",
704                        strerror(errno));
705         }
706         free(buf);
707 }
708
709
710
711
712 /*
713  * cmd_ndop() - open a network spool file for downloading
714  */
715 void cmd_ndop(char *cmdbuf)
716 {
717         char pathname[256];
718         struct stat statbuf;
719
720         if (IsEmptyStr(CC->net_node)) {
721                 cprintf("%d Not authenticated as a network node.\n",
722                         ERROR + NOT_LOGGED_IN);
723                 return;
724         }
725
726         if (CC->download_fp != NULL) {
727                 cprintf("%d You already have a download file open.\n",
728                         ERROR + RESOURCE_BUSY);
729                 return;
730         }
731
732         snprintf(pathname, sizeof pathname, 
733                          "%s/%s",
734                          ctdl_netout_dir,
735                          CC->net_node);
736
737         /* first open the file in append mode in order to create a
738          * zero-length file if it doesn't already exist 
739          */
740         CC->download_fp = fopen(pathname, "a");
741         if (CC->download_fp != NULL)
742                 fclose(CC->download_fp);
743
744         /* now open it */
745         CC->download_fp = fopen(pathname, "r");
746         if (CC->download_fp == NULL) {
747                 cprintf("%d cannot open %s: %s\n",
748                         ERROR + INTERNAL_ERROR, pathname, strerror(errno));
749                 return;
750         }
751
752
753         /* set this flag so other routines know that the download file
754          * currently open is a network spool file 
755          */
756         CC->dl_is_net = 1;
757
758         stat(pathname, &statbuf);
759         CC->download_fp_total = statbuf.st_size;
760         cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
761 }
762
763 /*
764  * cmd_nuop() - open a network spool file for uploading
765  */
766 void cmd_nuop(char *cmdbuf)
767 {
768         static int seq = 1;
769
770         if (IsEmptyStr(CC->net_node)) {
771                 cprintf("%d Not authenticated as a network node.\n",
772                         ERROR + NOT_LOGGED_IN);
773                 return;
774         }
775
776         if (CC->upload_fp != NULL) {
777                 cprintf("%d You already have an upload file open.\n",
778                         ERROR + RESOURCE_BUSY);
779                 return;
780         }
781
782         snprintf(CC->upl_path, sizeof CC->upl_path,
783                          "%s/%s.%04lx.%04x",
784                          ctdl_nettmp_dir,
785                          CC->net_node, 
786                          (long)getpid(), 
787                          ++seq);
788
789         CC->upload_fp = fopen(CC->upl_path, "r");
790         if (CC->upload_fp != NULL) {
791                 fclose(CC->upload_fp);
792                 CC->upload_fp = NULL;
793                 cprintf("%d '%s' already exists\n",
794                         ERROR + ALREADY_EXISTS, CC->upl_path);
795                 return;
796         }
797
798         CC->upload_fp = fopen(CC->upl_path, "w");
799         if (CC->upload_fp == NULL) {
800                 cprintf("%d Cannot open %s: %s\n",
801                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
802                 return;
803         }
804
805         CC->upload_type = UPL_NET;
806         cprintf("%d Ok\n", CIT_OK);
807 }
808
809
810 /*****************************************************************************/
811 /*                      MODULE INITIALIZATION STUFF                          */
812 /*****************************************************************************/
813
814 CTDL_MODULE_INIT(file_ops)
815 {
816         if (!threading) {
817                 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
818                 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
819                 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
820                 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
821                 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
822                 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
823                 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
824                 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
825                 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
826                 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
827                 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
828                 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
829                 CtdlRegisterCleanupHook(cleanup_nttlist);
830         }
831         /* return our Subversion id for the Log */
832         return "file_ops";
833 }