]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libncp/ncpl_file.c
No longer needed.
[FreeBSD/FreeBSD.git] / lib / libncp / ncpl_file.c
1 /*
2  * Copyright (c) 1999, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <arpa/inet.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <strings.h>
44
45 #include <netncp/ncp_lib.h>
46 #include <netncp/ncp_file.h>
47 #include <fs/nwfs/nwfs.h>
48
49 int
50 ncp_read(NWCONN_HANDLE connid, ncp_fh *fh, off_t offset, size_t count, char *target) {
51         int result;
52         struct ncp_rw rwrq;
53         DECLARE_RQ;
54
55         ncp_init_request(conn);
56         ncp_add_byte(conn, NCP_CONN_READ);
57         rwrq.nrw_fh = *fh;
58         rwrq.nrw_base = target;
59         rwrq.nrw_cnt = count;
60         rwrq.nrw_offset = offset;
61         ncp_add_mem(conn, &rwrq, sizeof(rwrq));
62         if ((result = ncp_conn_request(connid, conn)) < 0) 
63                 return -1;
64         return result;
65 }
66
67 int
68 ncp_write(NWCONN_HANDLE connid, ncp_fh *fh, off_t offset, size_t count, char *source)
69 {
70         int result;
71         struct ncp_rw rwrq;
72         DECLARE_RQ;
73
74         ncp_init_request(conn);
75         ncp_add_byte(conn, NCP_CONN_WRITE);
76         rwrq.nrw_fh = *fh;
77         rwrq.nrw_base = source;
78         rwrq.nrw_cnt = count;
79         rwrq.nrw_offset = offset;
80         ncp_add_mem(conn, &rwrq, sizeof(rwrq));
81
82         if ((result = ncp_conn_request(connid, conn)) < 0)
83                 return -1;
84         return result;
85 }
86
87 int
88 ncp_geteinfo(char *path, struct nw_entry_info *fi) {
89         int d, error;
90
91         if ((d = open(path, O_RDONLY)) < 0) return errno;
92         if ((error = ioctl(d, NWFSIOC_GETEINFO, fi)) != 0) return errno;
93         close(d);
94         return 0;
95 }
96
97
98 int
99 ncp_AllocTempDirHandle(char *path, NWDIR_HANDLE *pdh) {
100         int d;
101
102         if ((d = open(path, O_RDONLY)) < 0) return errno;
103         *pdh = d;
104         return 0;
105 }
106
107 int
108 ncp_DeallocateDirHandle(NWDIR_HANDLE dh) {
109         close(dh);
110         return 0;
111 }
112
113 int
114 ncp_GetNSEntryInfo(NWDIR_HANDLE dh, struct nw_entry_info *fi, int *ns) {
115         int error;
116
117         if ((error = ioctl(dh, NWFSIOC_GETEINFO, fi)) != 0) return errno;
118         if ((error = ioctl(dh, NWFSIOC_GETNS, ns)) != 0) return errno;
119         return 0;
120 }
121
122 NWCCODE
123 ncp_ScanForDeletedFiles(NWCONN_HANDLE cH, pnuint32 iterHandle, 
124         pnuint32 volNum, pnuint32 dirBase, nuint8 ns,
125         NWDELETED_INFO *entryInfo)
126 {
127         int error;
128         struct nw_entry_info *pfi;
129         DECLARE_RQ;
130 #define UNITEDT(d,t)    (((d) << 16) | (t))
131
132         bzero(entryInfo, sizeof(NWDELETED_INFO));
133         ncp_init_request(conn);
134         ncp_add_byte(conn, 16);
135         ncp_add_byte(conn, ns);
136         ncp_add_byte(conn, 0);          /* data stream */
137         ncp_add_dword_lh(conn, IM_ALL & ~(IM_SPACE_ALLOCATED | IM_TOTAL_SIZE | IM_EA | IM_DIRECTORY));
138         ncp_add_dword_lh(conn, *iterHandle);
139
140         ncp_add_byte(conn, *volNum);
141         ncp_add_dword_lh(conn, *dirBase);
142         ncp_add_byte(conn, NCP_HF_DIRBASE);     /* dirBase */
143         ncp_add_byte(conn, 0);                  /* no component */
144         if ((error = ncp_request(cH, 87, conn)) != 0) {
145                 return error;
146         }
147         if (conn->rpsize < 0x61) {
148                 return EBADRPC; /* EACCES ? */
149         }
150         *iterHandle = entryInfo->sequence = ncp_reply_dword_lh(conn, 0x00);
151         entryInfo->deletedTime = ncp_reply_word_lh(conn, 0x04);
152         entryInfo->deletedDateAndTime = UNITEDT(ncp_reply_word_lh(conn, 0x06), entryInfo->deletedTime);
153         entryInfo->deletorID = ncp_reply_dword_hl(conn, 0x08);
154         *volNum = ncp_reply_dword_lh(conn, 0x0C);
155         *dirBase = ncp_reply_dword_lh(conn, 0x10);
156         entryInfo->parent = ncp_reply_dword_lh(conn, 0x10);
157         pfi = (struct nw_entry_info*) ncp_reply_data(conn, 0x14);
158         entryInfo->nameLength = pfi->nameLen;
159         memcpy(entryInfo->name, pfi->entryName, pfi->nameLen);
160         return error;
161 }
162
163 NWCCODE
164 ncp_PurgeDeletedFile(NWCONN_HANDLE cH, nuint32 iterHandle, 
165         nuint32 volNum, nuint32 dirBase, nuint8 ns)
166 {
167         DECLARE_RQ;
168
169         ncp_init_request(conn);
170         ncp_add_byte(conn, 18);
171         ncp_add_byte(conn, ns);
172         ncp_add_byte(conn, 0);          /* reserved */
173         ncp_add_dword_lh(conn, iterHandle);
174         ncp_add_dword_lh(conn, volNum);
175         ncp_add_dword_lh(conn, dirBase);
176         return ncp_request(cH, 87, conn);
177 }
178
179
180 static void 
181 ncp_extract_entryInfo(char *data, NW_ENTRY_INFO *entry) {
182         u_char l;
183         const int info_struct_size = sizeof(NW_ENTRY_INFO) - 257;
184
185         memcpy(entry, data, info_struct_size);
186         data += info_struct_size;
187         l = *data++;
188         entry->nameLen = l;
189         memcpy(entry->entryName, data, l);
190         entry->entryName[l] = '\0';
191         return;
192 }
193
194 NWCCODE
195 ncp_ScanNSEntryInfo(NWCONN_HANDLE cH,
196         nuint8 namSpc, nuint16 attrs, SEARCH_SEQUENCE *seq,
197         pnstr8 searchPattern, nuint32 retInfoMask, NW_ENTRY_INFO *entryInfo) 
198 {
199         int error, l;
200         DECLARE_RQ;
201
202         if (seq->searchDirNumber == -1) {
203                 seq->searchDirNumber = 0;
204                 ncp_init_request(conn);
205                 ncp_add_byte(conn, 2);
206                 ncp_add_byte(conn, namSpc);
207                 ncp_add_byte(conn, 0);
208                 ncp_add_handle_path(conn, seq->volNumber, seq->dirNumber, 
209                     NCP_HF_DIRBASE, NULL);
210                 error = ncp_request(cH, 87, conn);
211                 if (error) return error;
212                 memcpy(seq, ncp_reply_data(conn, 0), 9);
213         }
214         ncp_init_request(conn);
215         ncp_add_byte(conn, 3);
216         ncp_add_byte(conn, namSpc);
217         ncp_add_byte(conn, 0);          /* dataStream */
218         ncp_add_word_lh(conn, attrs);   /* SearchAttributes */
219         ncp_add_dword_lh(conn, retInfoMask);
220         ncp_add_mem(conn, seq, sizeof(*seq));
221         l = strlen(searchPattern);
222         ncp_add_byte(conn, l);
223         ncp_add_mem(conn, searchPattern, l);
224         error = ncp_request(cH, 87, conn);
225         if (error) return error;
226         memcpy(seq, ncp_reply_data(conn, 0), sizeof(*seq));
227         ncp_extract_entryInfo(ncp_reply_data(conn, 10), entryInfo);
228         return 0;
229 }
230
231 int
232 ncp_NSEntryInfo(NWCONN_HANDLE cH, nuint8 ns, nuint8 vol, nuint32 dirent,
233     NW_ENTRY_INFO *entryInfo)
234 {
235         DECLARE_RQ;
236         int error;
237
238         ncp_init_request(conn);
239         ncp_add_byte(conn, 6);
240         ncp_add_byte(conn, ns);
241         ncp_add_byte(conn, ns); /* DestNameSpace */
242         ncp_add_word_lh(conn, htons(0xff00));   /* get all */
243         ncp_add_dword_lh(conn, IM_ALL);
244         ncp_add_handle_path(conn, vol, dirent, NCP_HF_DIRBASE, NULL);
245         error = ncp_request(cH, 87, conn);
246         if (error) return error;
247         ncp_extract_entryInfo(ncp_reply_data(conn, 0), entryInfo);
248         return 0;
249 }
250
251 NWCCODE
252 NWGetVolumeName(NWCONN_HANDLE cH, u_char volume, char *name) {
253         int error, len;
254         DECLARE_RQ;
255
256         ncp_init_request_s(conn, 44);
257         ncp_add_byte(conn, volume);
258         error = ncp_request(cH, 22, conn);
259         if (error) return error;
260         len = ncp_reply_byte(conn, 29);
261         if (len == 0)
262                 return ENOENT;
263         bcopy(ncp_reply_data(conn, 30), name, len);
264         name[len] = 0;
265         return 0;
266 }