2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
11 #include <sys/syslog.h>
12 #include <sys/types.h>
14 #include <sys/param.h>
15 #include <netatalk/endian.h>
16 #include <atalk/adouble.h>
17 #include <atalk/afp.h>
18 #include <atalk/util.h>
19 #include <atalk/cnid.h>
26 #include "directory.h"
34 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
37 int ibuflen, *rbuflen;
45 u_int16_t fbitmap, dbitmap, vid;
50 memcpy( &vid, ibuf, sizeof( vid ));
51 ibuf += sizeof( vid );
52 if (( vol = getvolbyvid( vid )) == NULL ) {
53 return( AFPERR_PARAM );
56 memcpy( &did, ibuf, sizeof( did ));
57 ibuf += sizeof( did );
59 if (( dir = dirsearch( vol, did )) == NULL ) {
60 return( AFPERR_NOOBJ );
63 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
64 fbitmap = ntohs( fbitmap );
65 ibuf += sizeof( fbitmap );
66 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
67 dbitmap = ntohs( dbitmap );
68 ibuf += sizeof( dbitmap );
70 if (( path = cname( vol, dir, &ibuf )) == NULL) {
71 return( AFPERR_NOOBJ );
74 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
75 return( AFPERR_NOOBJ );
79 if (S_ISDIR(st.st_mode)) {
80 if (dbitmap && ( ret = getdirparams(vol, dbitmap, ".", curdir,
81 &st, rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
84 /* this is a directory */
85 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISDIR;
87 if (fbitmap && ( ret = getfilparams(vol, fbitmap, path, curdir, &st,
88 rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
92 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
94 *rbuflen = buflen + 3 * sizeof( u_int16_t );
95 fbitmap = htons( fbitmap );
96 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
97 rbuf += sizeof( fbitmap );
98 dbitmap = htons( dbitmap );
99 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
100 rbuf += sizeof( dbitmap ) + sizeof( u_char );
106 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
109 int ibuflen, *rbuflen;
115 u_int16_t vid, bitmap;
120 memcpy( &vid, ibuf, sizeof(vid));
121 ibuf += sizeof( vid );
123 if (( vol = getvolbyvid( vid )) == NULL ) {
124 return( AFPERR_PARAM );
127 if (vol->v_flags & AFPVOL_RO)
130 memcpy( &did, ibuf, sizeof( did));
131 ibuf += sizeof( did);
133 if (( dir = dirsearch( vol, did )) == NULL ) {
134 return( AFPERR_NOOBJ );
137 memcpy( &bitmap, ibuf, sizeof( bitmap ));
138 bitmap = ntohs( bitmap );
139 ibuf += sizeof( bitmap );
141 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
142 return( AFPERR_NOOBJ );
145 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
146 return( AFPERR_NOOBJ );
150 * If ibuf is odd, make it even.
152 if ((u_long)ibuf & 1 ) {
156 if (S_ISDIR(st.st_mode)) {
157 rc = setdirparams(vol, path, bitmap, ibuf );
159 rc = setfilparams(vol, path, bitmap, ibuf );
161 if ( rc == AFP_OK ) {
162 setvoltime(obj, vol );
167 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
170 int ibuflen, *rbuflen;
175 struct dir *dir, *odir = NULL;
176 char *path, *buf, *upath, *newpath;
181 #if AD_VERSION > AD_VERSION1
188 memcpy( &vid, ibuf, sizeof( vid ));
189 ibuf += sizeof( vid );
190 if (( vol = getvolbyvid( vid )) == NULL ) {
191 return( AFPERR_PARAM );
194 if (vol->v_flags & AFPVOL_RO)
197 memcpy( &did, ibuf, sizeof( did ));
198 ibuf += sizeof( did );
199 if (( dir = dirsearch( vol, did )) == NULL ) {
200 return( AFPERR_NOOBJ );
203 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
204 return( AFPERR_NOOBJ );
207 /* another place where we know about the path type */
208 if ( *ibuf++ != 2 ) {
209 return( AFPERR_PARAM );
211 plen = (unsigned char) *ibuf++;
212 *( ibuf + plen ) = '\0';
214 if ( *path == '\0' ) {
215 if ( curdir->d_parent == NULL ) { /* root directory */
216 return( AFPERR_NORENAME );
219 path = curdir->d_name;
220 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
221 return( AFPERR_NOOBJ );
226 if ( strcasecmp( path, ibuf ) == 0 ) {
231 /* if a curdir/newname ofork exists, return busy */
232 if (of_findname(vol, curdir, ibuf))
235 /* source == destination. just say okay. */
236 if (strcmp(path, ibuf) == 0)
239 /* check for illegal characters */
240 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
241 strpbrk(ibuf, MSWINDOWS_BADCHARS))
244 newpath = obj->oldtmp;
245 strcpy( newpath, mtoupath(vol, ibuf ));
247 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(newpath, '/'))
250 if (!validupath(vol, newpath))
253 /* the strdiacasecmp deals with case-insensitive, case preserving
255 if (stat( newpath, &st ) == 0 && strdiacasecmp(path, ibuf))
256 return( AFPERR_EXIST );
258 upath = mtoupath(vol, path);
260 #if AD_VERSION > AD_VERSION1
261 id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
264 if ( rename( upath, newpath ) < 0 ) {
267 return( AFPERR_NOOBJ );
269 return( AFPERR_ACCESS );
271 return( AFPERR_PARAM );
275 #if AD_VERSION > AD_VERSION1
276 if (stat(newpath, &st) < 0) /* this shouldn't fail */
278 cnid_update(vol->v_db, id, &st, curdir->d_did, newpath, strlen(newpath));
282 newadpath = obj->newtmp;
283 strcpy( newadpath, ad_path( newpath, 0 ));
284 if ( rename( ad_path( upath, 0 ), newadpath ) < 0 ) {
285 if ( errno == ENOENT ) { /* no adouble header file */
286 if (( unlink( newadpath ) < 0 ) && ( errno != ENOENT )) {
287 return( AFPERR_PARAM );
291 return( AFPERR_PARAM );
294 memset(&ad, 0, sizeof(ad));
295 if ( ad_open( newpath, ADFLAGS_HF, O_RDWR|O_CREAT, 0666,
297 return( AFPERR_PARAM );
302 memset(&ad, 0, sizeof(ad));
303 if ( ad_open( newpath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
304 O_RDWR|O_CREAT, 0666, &ad) < 0 ) {
305 if (!((errno == ENOENT) && vol_noadouble(vol)))
306 return( AFPERR_PARAM );
309 if ((buf = realloc( odir->d_name, plen + 1 )) == NULL ) {
310 syslog( LOG_ERR, "afp_rename: realloc: %m" );
312 ad_flush(&ad, ADFLAGS_HF); /* in case of create */
313 ad_close(&ad, ADFLAGS_HF);
318 strcpy( odir->d_name, ibuf );
323 ad_setentrylen( &ad, ADEID_NAME, plen );
324 memcpy( ad_entry( &ad, ADEID_NAME ), ibuf, plen );
325 ad_flush( &ad, ADFLAGS_HF );
326 ad_close( &ad, ADFLAGS_HF );
329 setvoltime(obj, vol );
331 /* if it's still open, rename the ofork as well. */
332 if (of_rename(vol, curdir, path, curdir, ibuf) < 0)
339 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
342 int ibuflen, *rbuflen;
353 memcpy( &vid, ibuf, sizeof( vid ));
354 ibuf += sizeof( vid );
355 if (( vol = getvolbyvid( vid )) == NULL ) {
356 return( AFPERR_PARAM );
359 if (vol->v_flags & AFPVOL_RO)
362 memcpy( &did, ibuf, sizeof( did ));
363 ibuf += sizeof( int );
364 if (( dir = dirsearch( vol, did )) == NULL ) {
365 return( AFPERR_NOOBJ );
368 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
369 return( AFPERR_NOOBJ );
372 if ( *path == '\0' ) {
373 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
374 } else if (of_findname(vol, curdir, path)) {
376 } else if ((rc = deletefile( upath = mtoupath(vol, path ))) == AFP_OK) {
377 #if AD_VERSION > AD_VERSION1 /* get rid of entry */
378 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
379 cnid_delete(vol->v_db, id);
382 if ( rc == AFP_OK ) {
383 setvoltime(obj, vol );
388 char *ctoupath( vol, dir, name )
389 const struct vol *vol;
394 static char path[ MAXPATHLEN + 1];
398 p = path + sizeof( path ) - 1;
400 u = mtoupath(vol, name );
403 strncpy( p, u, len );
404 for ( d = dir; d->d_parent; d = d->d_parent ) {
406 u = mtoupath(vol, d->d_name );
409 strncpy( p, u, len );
412 len = strlen( vol->v_path );
414 strncpy( p, vol->v_path, len );
420 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
423 int ibuflen, *rbuflen;
426 struct dir *sdir, *ddir, *odir = NULL;
428 char *oldname, *newname;
429 char *path, *p, *upath;
433 #if AD_VERSION > AD_VERSION1
440 memcpy( &vid, ibuf, sizeof( vid ));
441 ibuf += sizeof( vid );
442 if (( vol = getvolbyvid( vid )) == NULL ) {
443 return( AFPERR_PARAM );
446 if (vol->v_flags & AFPVOL_RO)
449 /* source did followed by dest did */
450 memcpy( &did, ibuf, sizeof( did ));
451 ibuf += sizeof( int );
452 if (( sdir = dirsearch( vol, did )) == NULL ) {
453 return( AFPERR_PARAM );
456 memcpy( &did, ibuf, sizeof( did ));
457 ibuf += sizeof( int );
459 /* source pathname */
460 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
461 return( AFPERR_NOOBJ );
465 newname = obj->newtmp;
466 oldname = obj->oldtmp;
467 if ( *path != '\0' ) {
468 /* not a directory */
469 strcpy(newname, path);
470 strcpy(oldname, path); /* an extra copy for of_rename */
471 #if AD_VERSION > AD_VERSION1
472 p = mtoupath(vol, path);
473 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
475 p = ctoupath( vol, sdir, newname );
478 strcpy( newname, odir->d_name );
479 strcpy(oldname, odir->d_name);
480 p = ctoupath( vol, odir->d_parent, newname );
481 #if AD_VERSION > AD_VERSION1
482 id = curdir->d_did; /* we already have the CNID */
486 * p now points to the full pathname of the source fs object.
489 /* get the destination directory */
490 if (( ddir = dirsearch( vol, did )) == NULL ) {
491 return( AFPERR_PARAM );
493 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
494 return( AFPERR_NOOBJ );
496 if ( *path != '\0' ) {
497 return( AFPERR_BADTYPE );
500 /* one more place where we know about path type */
501 if ( *ibuf++ != 2 ) {
502 return( AFPERR_PARAM );
505 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
506 strncpy( newname, ibuf, plen );
507 newname[ plen ] = '\0';
510 /* check for illegal characters */
511 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
512 strpbrk(newname, MSWINDOWS_BADCHARS))
515 upath = mtoupath(vol, newname);
517 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
520 if (!validupath(vol, upath))
523 /* source == destination. we just silently accept this. */
524 if (curdir == sdir) {
525 if (strcmp(oldname, newname) == 0)
528 /* deal with case insensitive, case-preserving filesystems. */
529 if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
532 } else if (stat(upath, &st ) == 0)
533 return( AFPERR_EXIST );
536 if (of_findname(vol, curdir, newname)) {
538 } else if ((rc = renamefile( p, upath, newname,
539 vol_noadouble(vol) )) == AFP_OK) {
540 /* if it's still open, rename the ofork as well. */
541 rc = of_rename(vol, sdir, oldname, curdir, newname);
544 rc = renamedir(p, upath, odir, curdir, newname, vol_noadouble(vol));
547 if ( rc == AFP_OK ) {
548 #if AD_VERSION > AD_VERSION1
549 /* renaming may have moved the file/dir across a filesystem */
550 if (stat(upath, &st) < 0)
553 /* fix up the catalog entry */
554 cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
556 setvoltime(obj, vol );