]>
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> | |
7100d074 | 11 | #include <com_err.h> |
12 | #include <gdb.h> | |
13 | #include <dcm.h> | |
14 | #include <sms.h> | |
de56407f | 15 | #include <sys/file.h> |
7100d074 | 16 | #include <sys/stat.h> |
17 | #include <update.h> | |
18 | ||
de56407f | 19 | |
20 | extern CONNECTION conn; | |
21 | extern int errno; | |
22 | char buf[BUFSIZ]; | |
de56407f | 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 | |
7100d074 | 42 | send_file(pathname, target_path) |
43 | char *pathname; | |
44 | char *target_path; | |
de56407f | 45 | { |
7100d074 | 46 | int n, fd, code, n_to_send; |
de56407f | 47 | STRING data; |
7100d074 | 48 | struct stat statb; |
49 | ||
50 | string_alloc(&data, UPDATE_BUFSIZ); | |
de56407f | 51 | |
7100d074 | 52 | /* send file over */ |
de56407f | 53 | fd = open(pathname, O_RDONLY, 0); |
7100d074 | 54 | if (fd < 0) { |
55 | com_err(whoami, errno, "unable to open %s for read", pathname); | |
56 | return(SMS_OCONFIG); | |
57 | } | |
58 | if (fstat(fd, &statb)) { | |
59 | com_err(whoami, errno, "unable to stat %s", pathname); | |
60 | close(fd); | |
61 | return(SMS_OCONFIG); | |
62 | } | |
63 | n_to_send = statb.st_size; | |
64 | ||
65 | sprintf(STRING_DATA(data), "XFER_002 %d %d %s", | |
66 | n_to_send, checksum_file(pathname), target_path); | |
67 | code = send_object(conn, (char *)&data, STRING_T); | |
68 | if (code) { | |
69 | com_err(whoami, code, " sending XFER_002 request"); | |
70 | close(fd); | |
71 | return(code); | |
de56407f | 72 | } |
7100d074 | 73 | code = receive_object(conn, (char *)&n, INTEGER_T); |
74 | if (code) { | |
75 | com_err(whoami, code, " getting reply from XFER_002 request"); | |
76 | close(fd); | |
77 | return(code); | |
78 | } | |
79 | if (n) { | |
80 | com_err(whoami, n, " transfer request (XFER_002) rejected"); | |
81 | close(fd); | |
82 | return(n); | |
83 | } | |
84 | ||
de56407f | 85 | code = receive_object(conn, (char *)&n, INTEGER_T); |
86 | if (code) { | |
87 | com_err(whoami, connection_errno(conn), ": lost connection"); | |
de56407f | 88 | close(fd); |
7100d074 | 89 | return(code); |
de56407f | 90 | } |
91 | if (n) { | |
7100d074 | 92 | com_err(whoami, n, " from remote server: can't update %s", |
de56407f | 93 | pathname); |
de56407f | 94 | close(fd); |
7100d074 | 95 | return(n); |
de56407f | 96 | } |
de56407f | 97 | while (n_to_send > 0) { |
98 | #ifdef DEBUG | |
99 | printf("n_to_send = %d\n", n_to_send); | |
100 | #endif /* DEBUG */ | |
101 | n = read(fd, STRING_DATA(data), UPDATE_BUFSIZ); | |
102 | if (n < 0) { | |
7100d074 | 103 | com_err(whoami, errno, " reading %s for transmission", pathname); |
de56407f | 104 | close(fd); |
7100d074 | 105 | return(SMS_ABORTED); |
de56407f | 106 | } |
107 | MAX_STRING_SIZE(data) = n; | |
108 | code = send_object(conn, (char *)&data, STRING_T); | |
109 | if (code) { | |
7100d074 | 110 | com_err(whoami, connection_errno(conn), " transmitting file %s", |
111 | pathname); | |
de56407f | 112 | close(fd); |
7100d074 | 113 | return(code); |
de56407f | 114 | } |
115 | n_to_send -= n; | |
116 | code = receive_object(conn, (char *)&n, INTEGER_T); | |
117 | if (code) { | |
7100d074 | 118 | com_err(whoami, connection_errno(conn), |
119 | " awaiting ACK remote server during transmission of %s", | |
120 | pathname); | |
de56407f | 121 | close(fd); |
7100d074 | 122 | return(code); |
de56407f | 123 | } |
124 | if (n) { | |
7100d074 | 125 | com_err(whoami, n, " from remote server during transmission of %s", |
126 | pathname); | |
de56407f | 127 | close(fd); |
7100d074 | 128 | return(n); |
de56407f | 129 | } |
130 | } | |
131 | close(fd); | |
7100d074 | 132 | return(SMS_SUCCESS); |
de56407f | 133 | } |