2 Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
24 #include <atalk/util.h>
25 #include <atalk/cnid.h>
26 #include <atalk/logger.h>
27 #include <atalk/volume.h>
28 #include <atalk/directory.h>
29 #include <atalk/queue.h>
30 #include <atalk/bstrlib.h>
31 #include <atalk/globals.h>
35 #include "directory.h"
38 #include "afp_config.h"
45 #define rbufsize 128000
46 static char rbuf[rbufsize];
47 static size_t rbuflen;
49 #define ADD(a, b, c) (a) += (c); \
52 #define PUSHBUF(p, val, size, len) \
53 memcpy((p), (val), (size)); \
57 #define PUSHVAL(p, type, val, len) \
60 memcpy(p, &type, sizeof(type)); \
61 (p) += sizeof(type); \
62 (len) += sizeof(type); \
65 static int push_path(char **bufp, const char *name)
68 int slen = strlen(name);
71 PUSHVAL(p, uint8_t, 3, len); /* path type */
72 PUSHVAL(p, uint32_t, kTextEncodingUTF8, len); /* text encoding hint */
73 PUSHVAL(p, uint16_t, htons(slen), len);
75 for (int i = 0; i < slen; i++) {
88 /***********************************************************************************
90 ***********************************************************************************/
92 char **cnamewrap(const char *name)
98 PUSHVAL(p, uint8_t, 3, len); /* path type */
99 PUSHVAL(p, uint32_t, kTextEncodingUTF8, len); /* text encoding hint */
100 PUSHVAL(p, uint16_t, ntohs(strlen(name)), len);
107 int getfiledirparms(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
109 const int bufsize = 256;
116 PUSHVAL(p, uint16_t, vid, len);
117 PUSHVAL(p, cnid_t, did, len);
118 PUSHVAL(p, uint16_t, htons(FILPBIT_FNUM | FILPBIT_PDINFO), len);
119 PUSHVAL(p, uint16_t, htons(DIRPBIT_DID | DIRPBIT_PDINFO), len);
121 len += push_path(&p, name);
123 return afp_getfildirparams(obj, buf, len, rbuf, &rbuflen);
126 int createdir(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
128 const int bufsize = 256;
135 PUSHVAL(p, uint16_t, vid, len);
136 PUSHVAL(p, cnid_t, did, len);
137 len += push_path(&p, name);
139 return afp_createdir(obj, buf, len, rbuf, &rbuflen);
142 int createfile(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
144 const int bufsize = 256;
149 PUSHVAL(p, uint16_t, htons(128), len); /* hard create */
150 PUSHVAL(p, uint16_t, vid, len);
151 PUSHVAL(p, cnid_t, did, len);
152 len += push_path(&p, name);
154 return afp_createfile(obj, buf, len, rbuf, &rbuflen);
157 int delete(AFPObj *obj, uint16_t vid, cnid_t did, const char *name)
159 const int bufsize = 256;
164 PUSHVAL(p, uint16_t, htons(128), len); /* hard create */
165 PUSHVAL(p, uint16_t, vid, len);
166 PUSHVAL(p, cnid_t, did, len);
167 len += push_path(&p, name);
169 return afp_delete(obj, buf, len, rbuf, &rbuflen);
172 int enumerate(AFPObj *obj, uint16_t vid, cnid_t did)
174 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 ret = afp_enumerate_ext2(obj, buf, len, rbuf, &rbuflen);
194 if (ret != AFPERR_NOOBJ && ret != AFP_OK)
199 uint16_t openvol(AFPObj *obj, const char *name)
204 const int bufsize = 32;
207 char len = strlen(name);
209 memset(p, 0, bufsize);
213 bitmap = htons(1<<VOLPBIT_VID);
214 memcpy(p, &bitmap, 2);
220 memcpy(p, name, len);
223 len += 2 + 2 + 1; /* (command+pad) + bitmap + len */
228 if ((ret = afp_openvol(obj, buf, len, rbuf, &rbuflen)) != AFP_OK)
232 memcpy(&bitmap, p, 2);
234 bitmap = ntohs(bitmap);
235 if ( ! (bitmap & 1<<VOLPBIT_VID))