]> arthur.barton.de Git - netatalk.git/blob - test/afpd/afpfunc_helpers.c
Merge branch-allea
[netatalk.git] / test / afpd / afpfunc_helpers.c
1 /*
2   $Id: afpfunc_helpers.c,v 1.1.2.1 2010-02-01 10:56:08 franklahm Exp $
3   Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif /* HAVE_CONFIG_H */
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24
25 #include <atalk/util.h>
26 #include <atalk/cnid.h>
27 #include <atalk/logger.h>
28 #include <atalk/volume.h>
29 #include <atalk/directory.h>
30 #include <atalk/queue.h>
31 #include <atalk/bstrlib.h>
32 #include <atalk/globals.h>
33
34 #include "file.h"
35 #include "filedir.h"
36 #include "directory.h"
37 #include "dircache.h"
38 #include "hash.h"
39 #include "afp_config.h"
40 #include "volume.h"
41
42 #include "test.h"
43 #include "subtests.h"
44
45
46 #define rbufsize 128000
47 static char rbuf[rbufsize];
48 static size_t rbuflen;
49
50 #define ADD(a, b, c) (a) += (c); \
51                          (b) += (c)
52
53 #define PUSHBUF(p, val, size, len) \
54     memcpy((p), (val), (size));    \
55     (p) += (size);                 \
56     (len) += (size)
57
58 #define PUSHVAL(p, type, val, len)         \
59     { \
60         type type = val;                          \
61         memcpy(p, &type, sizeof(type));           \
62         (p) += sizeof(type);                      \
63         (len) += sizeof(type);                    \
64     }
65
66 static int push_path(char **bufp, const char *name)
67 {
68     int len = 0;
69     int slen = strlen(name);
70     char *p = *bufp;
71
72     PUSHVAL(p, uint8_t, 3, len); /* path type */
73     PUSHVAL(p, uint32_t, kTextEncodingUTF8, len); /* text encoding hint */
74     PUSHVAL(p, uint16_t, htons(slen), len);
75     if (slen) {
76         for (int i = 0; i < slen; i++) {
77             if (name[i] == '/')
78                 p[i] = 0;
79             else
80                 p[i] = name[i];
81         }
82         len += slen;
83     }
84
85     *bufp += len;
86     return len;
87 }
88
89 /***********************************************************************************
90  * Interface
91  ***********************************************************************************/
92
93 char **cnamewrap(const char *name)
94 {
95     static char buf[256];
96     static char *p = buf;
97     int len = 0;
98
99     PUSHVAL(p, uint8_t, 3, len); /* path type */
100     PUSHVAL(p, uint32_t, kTextEncodingUTF8, len); /* text encoding hint */
101     PUSHVAL(p, uint16_t, ntohs(strlen(name)), len);
102     strcpy(p, name);
103
104     p = buf;
105     return &p;
106 }
107
108 int getfiledirparms(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
109 {
110     const int bufsize = 256;
111     char buf[bufsize];
112     char *p = buf;
113     int len = 0;
114
115     ADD(p, len , 2);
116
117     PUSHVAL(p, uint16_t, vid, len);
118     PUSHVAL(p, cnid_t, did, len);
119     PUSHVAL(p, uint16_t, htons(FILPBIT_FNUM | FILPBIT_PDINFO), len);
120     PUSHVAL(p, uint16_t, htons(DIRPBIT_DID | DIRPBIT_PDINFO), len);
121
122     len += push_path(&p, name);
123
124     return afp_getfildirparams(obj, buf, len, rbuf, &rbuflen);
125 }
126
127 int createdir(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
128 {
129     const int bufsize = 256;
130     char buf[bufsize];
131     char *p = buf;
132     int len = 0;
133
134     ADD(p, len , 2);
135
136     PUSHVAL(p, uint16_t, vid, len);
137     PUSHVAL(p, cnid_t, did, len);
138     len += push_path(&p, name);
139
140     return afp_createdir(obj, buf, len, rbuf, &rbuflen);
141 }
142
143 int createfile(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
144 {
145     const int bufsize = 256;
146     char buf[bufsize];
147     char *p = buf;
148     int len = 0;
149
150     PUSHVAL(p, uint16_t, htons(128), len); /* hard create */
151     PUSHVAL(p, uint16_t, vid, len);
152     PUSHVAL(p, cnid_t, did, len);
153     len += push_path(&p, name);
154
155     return afp_createfile(obj, buf, len, rbuf, &rbuflen);
156 }
157
158 int delete(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
159 {
160     const int bufsize = 256;
161     char buf[bufsize];
162     char *p = buf;
163     int len = 0;
164
165     PUSHVAL(p, uint16_t, htons(128), len); /* hard create */
166     PUSHVAL(p, uint16_t, vid, len);
167     PUSHVAL(p, cnid_t, did, len);
168     len += push_path(&p, name);
169
170     return afp_delete(obj, buf, len, rbuf, &rbuflen);
171 }
172
173 int enumerate(AFPObj *obj, uint16_t vid, cnid_t did)
174 {
175     const int bufsize = 256;
176     char buf[bufsize];
177     char *p = buf;
178     int len = 0;
179     int ret;
180
181     ADD(p, len , 2);
182
183     PUSHVAL(p, uint16_t, vid, len);
184     PUSHVAL(p, cnid_t, did, len);
185     PUSHVAL(p, uint16_t, htons(FILPBIT_PDID | FILPBIT_FNUM | FILPBIT_PDINFO), len);
186     PUSHVAL(p, uint16_t, htons(DIRPBIT_PDID | DIRPBIT_DID | DIRPBIT_PDINFO), len);
187     PUSHVAL(p, uint16_t, htons(20), len);       /* reqcount */
188     PUSHVAL(p, uint32_t, htonl(1), len);        /* startindex */
189     PUSHVAL(p, uint32_t, htonl(rbufsize), len); /* max replysize */
190
191     len += push_path(&p, "");
192
193     ret = afp_enumerate_ext2(obj, buf, len, rbuf, &rbuflen);
194
195     if (ret != AFPERR_NOOBJ && ret != AFP_OK)
196         return -1;
197     return 0;
198 }
199
200 uint16_t openvol(AFPObj *obj, const char *name)
201 {
202     int ret;
203     uint16_t bitmap;
204     uint16_t vid;
205     const int bufsize = 32;
206     char buf[bufsize];
207     char *p = buf;
208     char len = strlen(name);
209
210     memset(p, 0, bufsize);
211     p += 2;
212
213     /* bitmap */
214     bitmap = htons(1<<VOLPBIT_VID);
215     memcpy(p, &bitmap, 2);
216     p += 2;
217
218     /* name */
219     *p = len;
220     p++;
221     memcpy(p, name, len);
222     p += len;
223
224     len += 2 + 2 + 1; /* (command+pad) + bitmap + len */
225     if (len & 1)
226         len++;
227
228     rbuflen = 0;
229     if ((ret = afp_openvol(obj, buf, len, rbuf, &rbuflen)) != AFP_OK)
230         return 0;
231
232     p = rbuf;
233     memcpy(&bitmap, p, 2);
234     p += 2;
235     bitmap = ntohs(bitmap);
236     if ( ! (bitmap & 1<<VOLPBIT_VID))
237         return 0;
238
239     memcpy(&vid, p, 2);
240     return vid;
241 }
242