]>
Commit | Line | Data |
---|---|---|
de56407f | 1 | /* |
2 | * $Source$ | |
3 | * $Header$ | |
4 | */ | |
5 | ||
6 | #ifndef lint | |
7 | static char *rcsid_send_file_c = "$Header$"; | |
8 | #endif lint | |
9 | ||
10 | #include <stdio.h> | |
11 | #include "com_err.h" | |
12 | #include "gdb.h" | |
13 | #include "smsu_int.h" | |
14 | #include <sys/file.h> | |
15 | #include "update.h" | |
16 | #include "sms_update_int.h" | |
17 | #include "kludge.h" | |
18 | ||
19 | extern CONNECTION conn; | |
20 | extern int errno; | |
21 | char buf[BUFSIZ]; | |
22 | extern struct update_desc *info; | |
23 | ||
24 | /* | |
25 | * syntax: | |
26 | * (already sent) pathname file_size checksum | |
27 | * <<< (int)code can we send it? | |
28 | * >>> data | |
29 | * <<< 0 | |
30 | * >>> data | |
31 | * <<< 0 | |
32 | * .... | |
33 | * >>> data (last block) | |
34 | * <<< 0 (on final write, close, sync, checksum) | |
35 | * | |
36 | * returns: | |
37 | * 0 on success | |
38 | * 1 on error (file not found, etc) | |
39 | */ | |
40 | ||
41 | int | |
42 | send_file(pathname, n_to_send) | |
43 | char *pathname; | |
44 | int n_to_send; | |
45 | { | |
46 | int n, fd, code; | |
47 | STRING data; | |
48 | ||
49 | fd = open(pathname, O_RDONLY, 0); | |
50 | if (fd == -1) { | |
51 | sprintf(buf, "%s: can't open %s for transmission", | |
52 | error_message(errno), pathname); | |
53 | sms_log_error(buf); | |
54 | return(1); | |
55 | } | |
56 | code = receive_object(conn, (char *)&n, INTEGER_T); | |
57 | if (code) { | |
58 | com_err(whoami, connection_errno(conn), ": lost connection"); | |
59 | info->override = INTERVAL_connection_lost; | |
60 | close(fd); | |
61 | return(1); | |
62 | } | |
63 | if (n) { | |
64 | log_priority = log_ERROR; | |
65 | com_err(whoami, n, ": from remote server: can't update %s", | |
66 | pathname); | |
67 | info->override = INTERVAL_update_failed; | |
68 | close(fd); | |
69 | return(1); | |
70 | } | |
71 | string_alloc(&data, UPDATE_BUFSIZ); | |
72 | while (n_to_send > 0) { | |
73 | #ifdef DEBUG | |
74 | printf("n_to_send = %d\n", n_to_send); | |
75 | #endif /* DEBUG */ | |
76 | n = read(fd, STRING_DATA(data), UPDATE_BUFSIZ); | |
77 | if (n < 0) { | |
78 | sprintf(buf, "%s: reading %s for transmission", | |
79 | error_message(errno), pathname); | |
80 | sms_log_error(buf); | |
81 | close(fd); | |
82 | return(1); | |
83 | } | |
84 | MAX_STRING_SIZE(data) = n; | |
85 | code = send_object(conn, (char *)&data, STRING_T); | |
86 | if (code) { | |
87 | sprintf(buf, "%s: transmitting file %s", | |
88 | error_message(connection_errno(conn)), pathname); | |
89 | sms_log_error(buf); | |
90 | close(fd); | |
91 | return(1); | |
92 | } | |
93 | n_to_send -= n; | |
94 | code = receive_object(conn, (char *)&n, INTEGER_T); | |
95 | if (code) { | |
96 | sprintf(buf, | |
97 | "%s: awaiting ACK remote server during transmission of %s", | |
98 | error_message(connection_errno(conn)), pathname); | |
99 | sms_log_error(buf); | |
100 | close(fd); | |
101 | return(1); | |
102 | } | |
103 | if (n) { | |
104 | sprintf(buf, "%s: from remote server during transmission of %s", | |
105 | error_message(n), pathname); | |
106 | sms_log_error(buf); | |
107 | close(fd); | |
108 | return(1); | |
109 | } | |
110 | } | |
111 | close(fd); | |
112 | return(0); | |
113 | } |