2 * $Id: appl.c,v 1.18.4.1 2010-02-01 10:56:08 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #include <sys/param.h>
18 #include <atalk/logger.h>
21 #include <atalk/adouble.h>
22 #include <atalk/afp.h>
23 #include <atalk/bstrlib.h>
24 #include <atalk/bstradd.h>
25 #include <atalk/globals.h>
28 #include "directory.h"
32 static struct savedt sa = { { 0, 0, 0, 0 }, -1, 0, 0};
34 static int pathcmp(char *p, int plen, char *q, int qlen)
36 return (( plen == qlen && memcmp( p, q, plen ) == 0 ) ? 0 : 1 );
39 static int applopen(struct vol *vol, u_char creator[ 4 ], int flags, int mode)
41 char *dtf, *adt, *adts;
43 if ( sa.sdt_fd != -1 ) {
44 if ( !(flags & ( O_RDWR | O_WRONLY )) &&
45 memcmp( sa.sdt_creator, creator, sizeof( CreatorType )) == 0 &&
46 sa.sdt_vid == vol->v_vid ) {
53 dtf = dtfile( vol, creator, ".appl" );
55 if (( sa.sdt_fd = open( dtf, flags, ad_mode( dtf, mode ))) < 0 ) {
56 if ( errno == ENOENT && ( flags & O_CREAT )) {
57 if (( adts = strrchr( dtf, '/' )) == NULL ) {
58 return( AFPERR_PARAM );
61 if (( adt = strrchr( dtf, '/' )) == NULL ) {
62 return( AFPERR_PARAM );
65 (void) ad_mkdir( dtf, DIRBITS | 0777 );
67 (void) ad_mkdir( dtf, DIRBITS | 0777 );
70 if (( sa.sdt_fd = open( dtf, flags, ad_mode( dtf, mode ))) < 0 ) {
71 return( AFPERR_PARAM );
74 return( AFPERR_PARAM );
77 memcpy( sa.sdt_creator, creator, sizeof( CreatorType ));
78 sa.sdt_vid = vol->v_vid;
84 * copy appls to new file, deleting any matching (old) appl entries
86 static int copyapplfile(int sfd, int dfd, char *mpath, u_short mplen)
92 char buf[ MAXPATHLEN ];
94 while (( cc = read( sfd, buf, sizeof(appltag) + sizeof( u_short ))) > 0 ) {
95 p = buf + sizeof(appltag);
96 memcpy( &len, p, sizeof(len));
99 if (( cc = read( sa.sdt_fd, p, len )) < len ) {
102 if ( pathcmp( mpath, mplen, p, len ) != 0 ) {
104 if ( write( dfd, buf, p - buf ) != p - buf ) {
114 * build mac. path (backwards) by traversing the directory tree
116 * The old way: dir and path refer to an app, path is a mac format
117 * pathname. makemacpath() builds something that looks like a cname,
118 * but uses upaths instead of mac format paths.
120 * The new way: dir and path refer to an app, path is a mac format
121 * pathname. makemacpath() builds a cname. (zero is a path separator
122 * and it's not \0 terminated).
124 * See afp_getappl() for the backward compatiblity code.
127 makemacpath(const struct vol *vol, char *mpath, int mpathlen, struct dir *dir, char *path)
131 p = mpath + mpathlen;
133 memcpy( p, path, strlen( path ));
135 while ( dir->d_did != DIRDID_ROOT ) {
136 p -= blength(dir->d_m_name) + 1;
138 /* FIXME: pathname too long */
141 memcpy(p, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1);
142 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
149 char buffer[12 + MAXPATHLEN + 1];
150 int buflen = 12 + MAXPATHLEN + 1;
154 struct bstrList *pathlist = NULL;
155 cnid_t cnid = dir->d_pdid;
157 /* Create list for path elements, request 16 list elements for now*/
158 if ((pathlist = bstListCreateMin(16)) == NULL) {
159 LOG(log_error, logtype_afpd, "makemacpath: OOM: %s", strerror(errno));
163 while ( cnid != DIRDID_ROOT ) {
165 /* construct path, copy already found uname to path element list*/
166 if ((bstrListPush(pathlist, bfromcstr(path))) != BSTR_OK) {
167 afp_errno = AFPERR_MISC;
173 if ((uname = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) {
174 afp_errno = AFPERR_NOOBJ;
179 if ((path = utompath(vol, uname, cnid, utf8_encoding())) == NULL) {
180 afp_errno = AFPERR_MISC;
190 bstrListDestroy(pathlist);
197 int afp_addappl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
203 u_int16_t vid, mplen;
208 char *mpath, *tempfile;
213 memcpy( &vid, ibuf, sizeof( vid ));
214 ibuf += sizeof( vid );
215 if (NULL == ( vol = getvolbyvid( vid ))) {
216 return( AFPERR_PARAM );
219 memcpy( &did, ibuf, sizeof( did ));
220 ibuf += sizeof( did );
221 if (NULL == ( dir = dirlookup( vol, did )) ) {
225 memcpy( creator, ibuf, sizeof( creator ));
226 ibuf += sizeof( creator );
228 memcpy( appltag, ibuf, sizeof( appltag ));
229 ibuf += sizeof( appltag );
231 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
232 return get_afp_errno(AFPERR_PARAM);
234 if ( path_isadir(path) ) {
235 return( AFPERR_BADTYPE );
238 if ( applopen( vol, creator, O_RDWR|O_CREAT, 0666 ) != AFP_OK ) {
239 return( AFPERR_PARAM );
241 if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) {
242 return( AFPERR_PARAM );
244 dtf = dtfile( vol, creator, ".appl.temp" );
245 tempfile = obj->oldtmp;
246 strcpy( tempfile, dtf );
247 if (( tfd = open( tempfile, O_RDWR|O_CREAT, 0666 )) < 0 ) {
248 return( AFPERR_PARAM );
251 mp = makemacpath( vol, mpath, AFPOBJ_TMPSIZ, curdir, path->m_name );
256 mplen = mpath + AFPOBJ_TMPSIZ - mp;
258 /* write the new appl entry at start of temporary file */
259 p = mp - sizeof( u_short );
260 mplen = htons( mplen );
261 memcpy( p, &mplen, sizeof( mplen ));
262 mplen = ntohs( mplen );
263 p -= sizeof( appltag );
264 memcpy(p, appltag, sizeof( appltag ));
265 cc = mpath + AFPOBJ_TMPSIZ - p;
266 if ( write( tfd, p, cc ) != cc ) {
269 return( AFPERR_PARAM );
271 cc = copyapplfile( sa.sdt_fd, tfd, mp, mplen );
278 return( AFPERR_PARAM );
280 if ( rename( tempfile, dtfile( vol, creator, ".appl" )) < 0 ) {
281 return( AFPERR_PARAM );
286 int afp_rmvappl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
292 u_int16_t vid, mplen;
296 char *tempfile, *mpath;
301 memcpy( &vid, ibuf, sizeof( vid ));
302 ibuf += sizeof( vid );
303 if (NULL == ( vol = getvolbyvid( vid ))) {
304 return( AFPERR_PARAM );
307 memcpy( &did, ibuf, sizeof( did ));
308 ibuf += sizeof( did );
309 if (NULL == ( dir = dirlookup( vol, did )) ) {
313 memcpy( creator, ibuf, sizeof( creator ));
314 ibuf += sizeof( creator );
316 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
317 return get_afp_errno(AFPERR_PARAM);
319 if ( path_isadir(path) ) {
320 return( AFPERR_BADTYPE );
323 if ( applopen( vol, creator, O_RDWR, 0666 ) != AFP_OK ) {
324 return( AFPERR_NOOBJ );
326 if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) {
327 return( AFPERR_PARAM );
329 dtf = dtfile( vol, creator, ".appl.temp" );
330 tempfile = obj->oldtmp;
331 strcpy( tempfile, dtf );
332 if (( tfd = open( tempfile, O_RDWR|O_CREAT, 0666 )) < 0 ) {
333 return( AFPERR_PARAM );
336 mp = makemacpath( vol, mpath, AFPOBJ_TMPSIZ, curdir, path->m_name );
338 return AFPERR_PARAM ;
341 mplen = mpath + AFPOBJ_TMPSIZ - mp;
342 cc = copyapplfile( sa.sdt_fd, tfd, mp, mplen );
349 return( AFPERR_PARAM );
351 if ( rename( tempfile, dtfile( vol, creator, ".appl" )) < 0 ) {
352 return( AFPERR_PARAM );
357 int afp_getappl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
363 u_int16_t vid, aindex, bitmap, len;
371 memcpy( &vid, ibuf, sizeof( vid ));
372 ibuf += sizeof( vid );
373 if (NULL == ( vol = getvolbyvid( vid )) ) {
375 return( AFPERR_PARAM );
378 memcpy( creator, ibuf, sizeof( creator ));
379 ibuf += sizeof( creator );
381 memcpy( &aindex, ibuf, sizeof( aindex ));
382 ibuf += sizeof( aindex );
383 aindex = ntohs( aindex );
384 if ( aindex ) { /* index 0 == index 1 */
388 memcpy( &bitmap, ibuf, sizeof( bitmap ));
389 bitmap = ntohs( bitmap );
390 ibuf += sizeof( bitmap );
392 if ( applopen( vol, creator, O_RDONLY, 0666 ) != AFP_OK ) {
394 return( AFPERR_NOITEM );
396 if ( aindex < sa.sdt_index ) {
397 if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) {
399 return( AFPERR_PARAM );
404 /* position to correct spot within appl file */
406 while (( cc = read( sa.sdt_fd, buf, sizeof( appltag )
407 + sizeof( u_short ))) > 0 ) {
408 p = buf + sizeof( appltag );
409 memcpy( &len, p, sizeof( len ));
411 p += sizeof( u_short );
412 if (( cc = read( sa.sdt_fd, p, len )) < len ) {
415 if ( sa.sdt_index == aindex ) {
420 if ( cc <= 0 || sa.sdt_index != aindex ) {
422 return( AFPERR_NOITEM );
428 * Check to see if this APPL mapping has an mpath or a upath. If
429 * there are any ':'s in the name, it is a upath and must be converted
430 * to an mpath. Hopefully, this code will go away.
433 #define hextoint( c ) ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
434 #define islxdigit(x) (!isupper(x)&&isxdigit(x))
436 char utomname[ MAXPATHLEN + 1];
444 if ( *u == ':' && *(u+1) != '\0' && islxdigit( *(u+1)) &&
445 *(u+2) != '\0' && islxdigit( *(u+2))) {
447 h = hextoint( *u ) << 4;
460 if ( p[ len - 1 ] == '\0' ) {
464 #endif /* APPLCNAME */
466 /* fake up a cname */
469 *q++ = 2; /* long path type */
470 *q++ = (unsigned char)len;
474 if (( path = cname( vol, vol->v_root, &q )) == NULL ) {
476 return( AFPERR_NOITEM );
479 if ( path_isadir(path) || path->st_errno ) {
481 return( AFPERR_NOITEM );
483 buflen = *rbuflen - sizeof( bitmap ) - sizeof( appltag );
484 if ( getfilparams(vol, bitmap, path, curdir, rbuf + sizeof( bitmap ) +
485 sizeof( appltag ), &buflen ) != AFP_OK ) {
487 return( AFPERR_BITMAP );
490 *rbuflen = buflen + sizeof( bitmap ) + sizeof( appltag );
491 bitmap = htons( bitmap );
492 memcpy( rbuf, &bitmap, sizeof( bitmap ));
493 rbuf += sizeof( bitmap );
494 memcpy( rbuf, appltag, sizeof( appltag ));
495 rbuf += sizeof( appltag );