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>
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.
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.
18 #endif /* HAVE_CONFIG_H */
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>
35 #include "directory.h"
39 #include "afp_config.h"
46 #define rbufsize 128000
47 static char rbuf[rbufsize];
48 static size_t rbuflen;
50 #define ADD(a, b, c) (a) += (c); \
53 #define PUSHBUF(p, val, size, len) \
54 memcpy((p), (val), (size)); \
58 #define PUSHVAL(p, type, val, len) \
61 memcpy(p, &type, sizeof(type)); \
62 (p) += sizeof(type); \
63 (len) += sizeof(type); \
66 static int push_path(char **bufp, const char *name)
69 int slen = strlen(name);
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);
76 for (int i = 0; i < slen; i++) {
89 /***********************************************************************************
91 ***********************************************************************************/
93 char **cnamewrap(const char *name)
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);
108 int getfiledirparms(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
110 const int bufsize = 256;
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);
122 len += push_path(&p, name);
124 return afp_getfildirparams(obj, buf, len, rbuf, &rbuflen);
127 int createdir(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
129 const int bufsize = 256;
136 PUSHVAL(p, uint16_t, vid, len);
137 PUSHVAL(p, cnid_t, did, len);
138 len += push_path(&p, name);
140 return afp_createdir(obj, buf, len, rbuf, &rbuflen);
143 int createfile(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
145 const int bufsize = 256;
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);
155 return afp_createfile(obj, buf, len, rbuf, &rbuflen);
158 int delete(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
160 const int bufsize = 256;
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);
170 return afp_delete(obj, buf, len, rbuf, &rbuflen);
173 int enumerate(AFPObj *obj, uint16_t vid, cnid_t did)
175 const int bufsize = 256;
182 PUSHVAL(p, uint16_t, vid, len);
183 PUSHVAL(p, cnid_t, did, len);
184 PUSHVAL(p, uint16_t, htons(FILPBIT_PDID | FILPBIT_FNUM | FILPBIT_PDINFO), len);
185 PUSHVAL(p, uint16_t, htons(DIRPBIT_PDID | DIRPBIT_DID | DIRPBIT_PDINFO), len);
186 PUSHVAL(p, uint16_t, htons(20), len); /* reqcount */
187 PUSHVAL(p, uint32_t, htonl(1), len); /* startindex */
188 PUSHVAL(p, uint32_t, htonl(rbufsize), len); /* max replysize */
190 len += push_path(&p, "");
192 return afp_enumerate_ext2(obj, buf, len, rbuf, &rbuflen);
195 uint16_t openvol(AFPObj *obj, const char *name)
200 const int bufsize = 32;
203 char len = strlen(name);
205 memset(p, 0, bufsize);
209 bitmap = htons(1<<VOLPBIT_VID);
210 memcpy(p, &bitmap, 2);
216 memcpy(p, name, len);
219 len += 2 + 2 + 1; /* (command+pad) + bitmap + len */
224 if ((ret = afp_openvol(obj, buf, len, rbuf, &rbuflen)) != AFP_OK)
228 memcpy(&bitmap, p, 2);
230 bitmap = ntohs(bitmap);
231 if ( ! (bitmap & 1<<VOLPBIT_VID))