Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / libafscp / afscp_file.c
bloba3985e3b950165c38498cb1797c6cb6fb2ed7e3d
1 /* AUTORIGHTS
2 Copyright (C) 2003 - 2010 Chaskiel Grundman
3 All rights reserved
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <afsconfig.h>
28 #include <afs/param.h>
30 #include <afs/vlserver.h>
31 #include <afs/vldbint.h>
32 #include "afscp.h"
33 #include "afscp_internal.h"
35 /* this is not yet 64-bit clean */
36 ssize_t
37 afscp_PRead(const struct afscp_venusfid * fid, void *buffer,
38 size_t count, off_t offset)
40 struct AFSFetchStatus fst;
41 struct AFSVolSync vs;
42 struct AFSCallBack cb;
43 struct AFSFid tf = fid->fid;
44 struct afscp_volume *vol;
45 struct afscp_server *server;
46 struct rx_call *c = NULL;
47 int code, code2 = 0;
48 int i, j, bytes, totalbytes = 0;
49 int bytesremaining;
50 char *p;
51 time_t now;
53 vol = afscp_VolumeById(fid->cell, fid->fid.Volume);
54 if (vol == NULL) {
55 afscp_errno = ENOENT;
56 return -1;
58 code = ENOENT;
59 for (i = 0; i < vol->nservers; i++) {
60 server = afscp_ServerByIndex(vol->servers[i]);
61 if (server && server->naddrs > 0) {
62 for (j = 0; j < server->naddrs; j++) {
63 c = rx_NewCall(server->conns[j]);
64 if (c != 0) {
65 p = buffer;
66 code = StartRXAFS_FetchData(c, &tf, offset, count);
67 if (code != 0) {
68 code = rx_EndCall(c, code);
69 continue;
71 bytes =
72 rx_Read(c, (char *)&bytesremaining,
73 sizeof(afs_int32));
74 if (bytes != sizeof(afs_int32)) {
75 code = rx_EndCall(c, bytes);
76 continue;
78 bytesremaining = ntohl(bytesremaining);
79 totalbytes = 0;
80 while (bytesremaining > 0) {
81 bytes = rx_Read(c, p, bytesremaining);
82 if (bytes <= 0)
83 break;
84 p += bytes;
85 totalbytes += bytes;
86 bytesremaining -= bytes;
88 if (bytesremaining == 0) {
89 time(&now);
90 code2 = EndRXAFS_FetchData(c, &fst, &cb, &vs);
91 if (code2 == 0)
92 afscp_AddCallBack(server, &fid->fid, &fst, &cb,
93 now);
95 code = rx_EndCall(c, code2);
97 if (code == 0) {
98 return totalbytes;
103 afscp_errno = code;
104 return -1;
107 /* this is not yet 64-bit clean */
108 ssize_t
109 afscp_PWrite(const struct afscp_venusfid * fid, const void *buffer,
110 size_t count, off_t offset)
112 struct AFSFetchStatus fst;
113 struct AFSStoreStatus sst;
114 struct AFSVolSync vs;
115 struct AFSCallBack cb;
116 struct AFSFid tf = fid->fid;
117 struct afscp_volume *vol;
118 struct afscp_server *server;
119 struct rx_call *c = NULL;
120 int code, code2 = 0;
121 int i, j, bytes, totalbytes = 0;
122 int bytesremaining;
123 const char *p;
124 off_t filesize;
125 time_t now;
127 memset(&sst, 0, sizeof(sst));
128 vol = afscp_VolumeById(fid->cell, fid->fid.Volume);
129 if (vol == NULL) {
130 afscp_errno = ENOENT;
131 return -1;
133 if (vol->voltype != RWVOL) {
134 afscp_errno = EROFS;
135 return -1;
138 code = ENOENT;
139 for (i = 0; i < vol->nservers; i++) {
140 server = afscp_ServerByIndex(vol->servers[i]);
141 if (server && server->naddrs > 0) {
142 for (j = 0; j < server->naddrs; j++) {
143 code =
144 RXAFS_FetchStatus(server->conns[j], &tf, &fst, &cb, &vs);
145 if (code != 0)
146 continue;
147 sst.Mask = AFS_SETMODTIME;
148 time(&now);
149 sst.ClientModTime = now;
150 filesize = fst.Length;
151 if (offset + count > filesize)
152 filesize = offset + count;
153 c = rx_NewCall(server->conns[j]);
154 if (c != 0) {
155 p = buffer;
156 code =
157 StartRXAFS_StoreData(c, &tf, &sst, offset, count,
158 filesize);
159 if (code != 0) {
160 code = rx_EndCall(c, code);
161 continue;
164 * seems to write file length to beginning of file -- why?
167 * bytesremaining = htonl(count);
168 * bytes = rx_Write(c, (char *)&bytesremaining,
169 * sizeof(afs_int32));
170 * if (bytes != sizeof(afs_int32)) {
171 * code = rx_EndCall(c, bytes);
172 * continue;
175 bytesremaining = count;
176 totalbytes = 0;
177 while (bytesremaining > 0) {
178 bytes = rx_Write(c, (char *)p, bytesremaining);
179 if (bytes <= 0)
180 break;
181 p += bytes;
182 totalbytes += bytes;
183 bytesremaining -= bytes;
185 if (bytesremaining == 0) {
186 code2 = EndRXAFS_StoreData(c, &fst, &vs);
188 code = rx_EndCall(c, code2);
190 if (code == 0) {
191 return totalbytes;
196 afscp_errno = code;
197 return -1;