2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
16 #include <sys/types.h>
18 #include <sys/param.h>
19 #include <sys/syslog.h>
22 #include <netatalk/endian.h>
23 #include <atalk/adouble.h>
24 #include <atalk/afp.h>
28 #include "directory.h"
32 static struct savedt sa = { { 0, 0, 0, 0 }, -1, 0 };
34 static __inline__ int pathcmp( p, plen, q, qlen )
40 return (( plen == qlen && memcmp( p, q, plen ) == 0 ) ? 0 : 1 );
43 static int applopen( vol, creator, flags, mode )
47 char *dtf, *adt, *adts;
49 if ( sa.sdt_fd != -1 ) {
50 if ( !(flags & ( O_RDWR | O_WRONLY )) &&
51 memcmp( sa.sdt_creator, creator, sizeof( CreatorType )) == 0 &&
52 sa.sdt_vid == vol->v_vid ) {
59 dtf = dtfile( vol, creator, ".appl" );
61 if (( sa.sdt_fd = open( dtf, flags, ad_mode( dtf, mode ))) < 0 ) {
62 if ( errno == ENOENT && ( flags & O_CREAT )) {
63 if (( adts = strrchr( dtf, '/' )) == NULL ) {
64 return( AFPERR_PARAM );
67 if (( adt = strrchr( dtf, '/' )) == NULL ) {
68 return( AFPERR_PARAM );
71 (void) ad_mkdir( dtf, DIRBITS | 0777 );
73 (void) ad_mkdir( dtf, DIRBITS | 0777 );
76 if (( sa.sdt_fd = open( dtf, flags, ad_mode( dtf, mode ))) < 0 ) {
77 return( AFPERR_PARAM );
80 return( AFPERR_PARAM );
83 memcpy( sa.sdt_creator, creator, sizeof( CreatorType ));
84 sa.sdt_vid = vol->v_vid;
90 * copy appls to new file, deleting any matching (old) appl entries
92 static int copyapplfile( sfd, dfd, mpath, mplen )
102 char buf[ MAXPATHLEN ];
104 while (( cc = read( sfd, buf, sizeof(appltag) + sizeof( u_short ))) > 0 ) {
105 p = buf + sizeof(appltag);
106 memcpy( &len, p, sizeof(len));
109 if (( cc = read( sa.sdt_fd, p, len )) < len ) {
112 if ( pathcmp( mpath, mplen, p, len ) != 0 ) {
114 if ( write( dfd, buf, p - buf ) != p - buf ) {
124 * build mac. path (backwards) by traversing the directory tree
126 * The old way: dir and path refer to an app, path is a mac format
127 * pathname. makemacpath() builds something that looks like a cname,
128 * but uses upaths instead of mac format paths.
130 * The new way: dir and path refer to an app, path is a mac format
131 * pathname. makemacpath() builds a cname.
133 * See afp_getappl() for the backward compatiblity code.
136 makemacpath( mpath, mpathlen, dir, path )
144 p = mpath + mpathlen;
146 strncpy( p, path, strlen( path ));
148 while ( dir->d_parent != NULL ) {
149 p -= strlen( dir->d_name ) + 1;
150 strcpy( p, dir->d_name );
157 int afp_addappl(obj, ibuf, ibuflen, rbuf, rbuflen )
160 int ibuflen, *rbuflen;
166 u_int16_t vid, mplen;
167 char *path, *dtf, *p, *mp;
170 char *mpath, *tempfile;
175 memcpy( &vid, ibuf, sizeof( vid ));
176 ibuf += sizeof( vid );
177 if (( vol = getvolbyvid( vid )) == NULL ) {
178 return( AFPERR_PARAM );
181 memcpy( &did, ibuf, sizeof( did ));
182 ibuf += sizeof( did );
183 if (( dir = dirsearch( vol, did )) == NULL ) {
184 return( AFPERR_NOOBJ );
187 memcpy( creator, ibuf, sizeof( creator ));
188 ibuf += sizeof( creator );
190 memcpy( appltag, ibuf, sizeof( appltag ));
191 ibuf += sizeof( appltag );
193 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
194 return( AFPERR_NOOBJ );
196 if ( *path == '\0' ) {
197 return( AFPERR_BADTYPE );
200 if ( applopen( vol, creator, O_RDWR|O_CREAT, 0666 ) != AFP_OK ) {
201 return( AFPERR_PARAM );
203 if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) {
204 return( AFPERR_PARAM );
206 dtf = dtfile( vol, creator, ".appl.temp" );
207 tempfile = obj->oldtmp;
208 strcpy( tempfile, dtf );
209 if (( tfd = open( tempfile, O_RDWR|O_CREAT, 0666 )) < 0 ) {
210 return( AFPERR_PARAM );
213 mp = makemacpath( mpath, AFPOBJ_TMPSIZ, curdir, path );
214 mplen = mpath + AFPOBJ_TMPSIZ - mp;
216 /* write the new appl entry at start of temporary file */
217 p = mp - sizeof( u_short );
218 mplen = htons( mplen );
219 memcpy( p, &mplen, sizeof( mplen ));
220 mplen = ntohs( mplen );
221 p -= sizeof( appltag );
222 memcpy(p, appltag, sizeof( appltag ));
223 cc = mpath + AFPOBJ_TMPSIZ - p;
224 if ( write( tfd, p, cc ) != cc ) {
226 return( AFPERR_PARAM );
228 cc = copyapplfile( sa.sdt_fd, tfd, mp, mplen );
235 return( AFPERR_PARAM );
237 if ( rename( tempfile, dtfile( vol, creator, ".appl" )) < 0 ) {
238 return( AFPERR_PARAM );
243 int afp_rmvappl(obj, ibuf, ibuflen, rbuf, rbuflen )
246 int ibuflen, *rbuflen;
252 u_int16_t vid, mplen;
253 char *path, *dtf, *mp;
255 char *tempfile, *mpath;
260 memcpy( &vid, ibuf, sizeof( vid ));
261 ibuf += sizeof( vid );
262 if (( vol = getvolbyvid( vid )) == NULL ) {
263 return( AFPERR_PARAM );
266 memcpy( &did, ibuf, sizeof( did ));
267 ibuf += sizeof( did );
268 if (( dir = dirsearch( vol, did )) == NULL ) {
269 return( AFPERR_NOOBJ );
272 memcpy( creator, ibuf, sizeof( creator ));
273 ibuf += sizeof( creator );
275 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
276 return( AFPERR_NOOBJ );
278 if ( *path == '.' ) {
279 return( AFPERR_BADTYPE );
282 if ( applopen( vol, creator, O_RDWR, 0666 ) != AFP_OK ) {
283 return( AFPERR_NOOBJ );
285 if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) {
286 return( AFPERR_PARAM );
288 dtf = dtfile( vol, creator, ".appl.temp" );
289 tempfile = obj->oldtmp;
290 strcpy( tempfile, dtf );
291 if (( tfd = open( tempfile, O_RDWR|O_CREAT, 0666 )) < 0 ) {
292 return( AFPERR_PARAM );
295 mp = makemacpath( mpath, AFPOBJ_TMPSIZ, curdir, path );
296 mplen = mpath + AFPOBJ_TMPSIZ - mp;
297 cc = copyapplfile( sa.sdt_fd, tfd, mp, mplen );
304 return( AFPERR_PARAM );
306 if ( rename( tempfile, dtfile( vol, creator, ".appl" )) < 0 ) {
307 return( AFPERR_PARAM );
312 int afp_getappl(obj, ibuf, ibuflen, rbuf, rbuflen )
315 int ibuflen, *rbuflen;
321 u_int16_t vid, aindex, bitmap, len;
328 memcpy( &vid, ibuf, sizeof( vid ));
329 ibuf += sizeof( vid );
330 if (( vol = getvolbyvid( vid )) == NULL ) {
332 return( AFPERR_PARAM );
335 memcpy( creator, ibuf, sizeof( creator ));
336 ibuf += sizeof( creator );
338 memcpy( &aindex, ibuf, sizeof( aindex ));
339 ibuf += sizeof( aindex );
340 aindex = ntohs( aindex );
341 if ( aindex ) { /* index 0 == index 1 */
345 memcpy( &bitmap, ibuf, sizeof( bitmap ));
346 bitmap = ntohs( bitmap );
347 ibuf += sizeof( bitmap );
349 if ( applopen( vol, creator, O_RDONLY, 0666 ) != AFP_OK ) {
351 return( AFPERR_NOITEM );
353 if ( aindex < sa.sdt_index ) {
354 if ( lseek( sa.sdt_fd, 0L, SEEK_SET ) < 0 ) {
356 return( AFPERR_PARAM );
361 /* position to correct spot within appl file */
363 while (( cc = read( sa.sdt_fd, buf, sizeof( appltag )
364 + sizeof( u_short ))) > 0 ) {
365 p = buf + sizeof( appltag );
366 memcpy( &len, p, sizeof( len ));
368 p += sizeof( u_short );
369 if (( cc = read( sa.sdt_fd, p, len )) < len ) {
372 if ( sa.sdt_index == aindex ) {
377 if ( cc <= 0 || sa.sdt_index != aindex ) {
379 return( AFPERR_NOITEM );
385 * Check to see if this APPL mapping has an mpath or a upath. If
386 * there are any ':'s in the name, it is a upath and must be converted
387 * to an mpath. Hopefully, this code will go away.
390 #define hextoint( c ) ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
391 #define islxdigit(x) (!isupper(x)&&isxdigit(x))
393 char utomname[ MAXPATHLEN + 1];
401 if ( *u == ':' && *(u+1) != '\0' && islxdigit( *(u+1)) &&
402 *(u+2) != '\0' && islxdigit( *(u+2))) {
404 h = hextoint( *u ) << 4;
417 if ( p[ len - 1 ] == '\0' ) {
423 /* fake up a cname */
426 *q++ = 2; /* long path type */
427 *q++ = (unsigned char)len;
431 if (( p = cname( vol, vol->v_dir, &q )) == NULL ) {
433 return( AFPERR_NOITEM );
436 if ( stat( mtoupath(vol, p), &st ) < 0 ) {
438 return( AFPERR_NOITEM );
440 buflen = *rbuflen - sizeof( bitmap ) - sizeof( appltag );
441 if ( getfilparams(vol, bitmap, p, curdir, &st, rbuf + sizeof( bitmap ) +
442 sizeof( appltag ), &buflen ) != AFP_OK ) {
444 return( AFPERR_BITMAP );
447 *rbuflen = buflen + sizeof( bitmap ) + sizeof( appltag );
448 bitmap = htons( bitmap );
449 memcpy( rbuf, &bitmap, sizeof( bitmap ));
450 rbuf += sizeof( bitmap );
451 memcpy( rbuf, appltag, sizeof( appltag ));
452 rbuf += sizeof( appltag );