Make dev.cpu.X.cx_usage sysctl also report current average of sleep time.
[dragonfly.git] / lib / libncp / ncpl_file.c
blob067f33441214f18299395a7159dfe9dc1c0b022a
1 /*
2 * Copyright (c) 1999, Boris Popov
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:
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.
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.
32 * $FreeBSD: src/lib/libncp/ncpl_file.c,v 1.2 1999/10/31 03:39:03 bp Exp $
33 * $DragonFly: src/lib/libncp/ncpl_file.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <string.h>
43 #include <netncp/ncp_lib.h>
44 #include <netncp/ncp_file.h>
45 #include <nwfs/nwfs.h>
47 int
48 ncp_read(NWCONN_HANDLE connid, ncp_fh *fh, off_t offset, size_t count, char *target) {
49 int result;
50 struct ncp_rw rwrq;
51 DECLARE_RQ;
53 ncp_init_request(conn);
54 ncp_add_byte(conn, NCP_CONN_READ);
55 rwrq.nrw_fh = *fh;
56 rwrq.nrw_base = target;
57 rwrq.nrw_cnt = count;
58 rwrq.nrw_offset = offset;
59 ncp_add_mem(conn, &rwrq, sizeof(rwrq));
60 if ((result = ncp_conn_request(connid, conn)) < 0)
61 return -1;
62 return result;
65 int
66 ncp_write(NWCONN_HANDLE connid, ncp_fh *fh, off_t offset, size_t count, char *source)
68 int result;
69 struct ncp_rw rwrq;
70 DECLARE_RQ;
72 ncp_init_request(conn);
73 ncp_add_byte(conn, NCP_CONN_WRITE);
74 rwrq.nrw_fh = *fh;
75 rwrq.nrw_base = source;
76 rwrq.nrw_cnt = count;
77 rwrq.nrw_offset = offset;
78 ncp_add_mem(conn, &rwrq, sizeof(rwrq));
80 if ((result = ncp_conn_request(connid, conn)) < 0)
81 return -1;
82 return result;
85 int
86 ncp_geteinfo(char *path, struct nw_entry_info *fi) {
87 int d, error;
89 if ((d = open(path, O_RDONLY)) < 0) return errno;
90 if ((error = ioctl(d, NWFSIOC_GETEINFO, fi)) != 0) return errno;
91 close(d);
92 return 0;
96 int
97 ncp_AllocTempDirHandle(char *path, NWDIR_HANDLE *pdh) {
98 int d;
100 if ((d = open(path, O_RDONLY)) < 0) return errno;
101 *pdh = d;
102 return 0;
106 ncp_DeallocateDirHandle(NWDIR_HANDLE dh) {
107 close(dh);
108 return 0;
112 ncp_GetNSEntryInfo(NWDIR_HANDLE dh, struct nw_entry_info *fi, int *ns) {
113 int error;
115 if ((error = ioctl(dh, NWFSIOC_GETEINFO, fi)) != 0) return errno;
116 if ((error = ioctl(dh, NWFSIOC_GETNS, ns)) != 0) return errno;
117 return 0;
120 NWCCODE
121 ncp_ScanForDeletedFiles(NWCONN_HANDLE cH, pnuint32 iterHandle,
122 pnuint32 volNum, pnuint32 dirBase, nuint8 ns,
123 NWDELETED_INFO *entryInfo)
125 int error;
126 struct nw_entry_info *pfi;
127 DECLARE_RQ;
128 #define UNITEDT(d,t) (((d) << 16) | (t))
130 bzero(entryInfo, sizeof(NWDELETED_INFO));
131 ncp_init_request(conn);
132 ncp_add_byte(conn, 16);
133 ncp_add_byte(conn, ns);
134 ncp_add_byte(conn, 0); /* data stream */
135 ncp_add_dword_lh(conn, IM_ALL & ~(IM_SPACE_ALLOCATED | IM_TOTAL_SIZE | IM_EA | IM_DIRECTORY));
136 ncp_add_dword_lh(conn, *iterHandle);
138 ncp_add_byte(conn, *volNum);
139 ncp_add_dword_lh(conn, *dirBase);
140 ncp_add_byte(conn, NCP_HF_DIRBASE); /* dirBase */
141 ncp_add_byte(conn, 0); /* no component */
142 if ((error = ncp_request(cH, 87, conn)) != 0) {
143 return error;
145 if (conn->rpsize < 0x61) {
146 return EBADRPC; /* EACCES ? */
148 *iterHandle = entryInfo->sequence = ncp_reply_dword_lh(conn, 0x00);
149 entryInfo->deletedTime = ncp_reply_word_lh(conn, 0x04);
150 entryInfo->deletedDateAndTime = UNITEDT(ncp_reply_word_lh(conn, 0x06), entryInfo->deletedTime);
151 entryInfo->deletorID = ncp_reply_dword_hl(conn, 0x08);
152 *volNum = ncp_reply_dword_lh(conn, 0x0C);
153 *dirBase = ncp_reply_dword_lh(conn, 0x10);
154 entryInfo->parent = ncp_reply_dword_lh(conn, 0x10);
155 pfi = (struct nw_entry_info*) ncp_reply_data(conn, 0x14);
156 entryInfo->nameLength = pfi->nameLen;
157 memcpy(entryInfo->name, pfi->entryName, pfi->nameLen);
158 return error;
161 NWCCODE
162 ncp_PurgeDeletedFile(NWCONN_HANDLE cH, nuint32 iterHandle,
163 nuint32 volNum, nuint32 dirBase, nuint8 ns)
165 DECLARE_RQ;
167 ncp_init_request(conn);
168 ncp_add_byte(conn, 18);
169 ncp_add_byte(conn, ns);
170 ncp_add_byte(conn, 0); /* reserved */
171 ncp_add_dword_lh(conn, iterHandle);
172 ncp_add_dword_lh(conn, volNum);
173 ncp_add_dword_lh(conn, dirBase);
174 return ncp_request(cH, 87, conn);
178 static void
179 ncp_extract_entryInfo(char *data, NW_ENTRY_INFO *entry) {
180 u_char l;
181 const int info_struct_size = sizeof(NW_ENTRY_INFO) - 257;
183 memcpy(entry, data, info_struct_size);
184 data += info_struct_size;
185 l = *data++;
186 entry->nameLen = l;
187 memcpy(entry->entryName, data, l);
188 entry->entryName[l] = '\0';
189 return;
192 NWCCODE
193 ncp_ScanNSEntryInfo(NWCONN_HANDLE cH,
194 nuint8 namSpc, nuint16 attrs, SEARCH_SEQUENCE *seq,
195 pnstr8 searchPattern, nuint32 retInfoMask, NW_ENTRY_INFO *entryInfo)
197 int error, l;
198 DECLARE_RQ;
200 if (seq->searchDirNumber == -1) {
201 seq->searchDirNumber = 0;
202 ncp_init_request(conn);
203 ncp_add_byte(conn, 2);
204 ncp_add_byte(conn, namSpc);
205 ncp_add_byte(conn, 0);
206 ncp_add_handle_path(conn, seq->volNumber, seq->dirNumber,
207 NCP_HF_DIRBASE, NULL);
208 error = ncp_request(cH, 87, conn);
209 if (error) return error;
210 memcpy(seq, ncp_reply_data(conn, 0), 9);
212 ncp_init_request(conn);
213 ncp_add_byte(conn, 3);
214 ncp_add_byte(conn, namSpc);
215 ncp_add_byte(conn, 0); /* dataStream */
216 ncp_add_word_lh(conn, attrs); /* SearchAttributes */
217 ncp_add_dword_lh(conn, retInfoMask);
218 ncp_add_mem(conn, seq, sizeof(*seq));
219 l = strlen(searchPattern);
220 ncp_add_byte(conn, l);
221 ncp_add_mem(conn, searchPattern, l);
222 error = ncp_request(cH, 87, conn);
223 if (error) return error;
224 memcpy(seq, ncp_reply_data(conn, 0), sizeof(*seq));
225 ncp_extract_entryInfo(ncp_reply_data(conn, 10), entryInfo);
226 return 0;
230 ncp_NSEntryInfo(NWCONN_HANDLE cH, nuint8 ns, nuint8 vol, nuint32 dirent,
231 NW_ENTRY_INFO *entryInfo)
233 DECLARE_RQ;
234 int error;
236 ncp_init_request(conn);
237 ncp_add_byte(conn, 6);
238 ncp_add_byte(conn, ns);
239 ncp_add_byte(conn, ns); /* DestNameSpace */
240 ncp_add_word_lh(conn, htons(0xff00)); /* get all */
241 ncp_add_dword_lh(conn, IM_ALL);
242 ncp_add_handle_path(conn, vol, dirent, NCP_HF_DIRBASE, NULL);
243 error = ncp_request(cH, 87, conn);
244 if (error) return error;
245 ncp_extract_entryInfo(ncp_reply_data(conn, 0), entryInfo);
246 return 0;
249 NWCCODE
250 NWGetVolumeName(NWCONN_HANDLE cH, u_char volume, char *name) {
251 int error, len;
252 DECLARE_RQ;
254 ncp_init_request_s(conn, 44);
255 ncp_add_byte(conn, volume);
256 error = ncp_request(cH, 22, conn);
257 if (error) return error;
258 len = ncp_reply_byte(conn, 29);
259 if (len == 0)
260 return ENOENT;
261 bcopy(ncp_reply_data(conn, 30), name, len);
262 name[len] = 0;
263 return 0;