2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
7 #include <sys/syslog.h>
10 #include <sys/param.h>
11 #include <netatalk/endian.h>
12 #include <atalk/adouble.h>
13 #include <atalk/afp.h>
14 #include <atalk/util.h>
15 #include <atalk/cnid.h>
22 #include "directory.h"
30 int afp_getfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
33 int ibuflen, *rbuflen;
41 u_int16_t fbitmap, dbitmap, vid;
46 memcpy( &vid, ibuf, sizeof( vid ));
47 ibuf += sizeof( vid );
48 if (( vol = getvolbyvid( vid )) == NULL ) {
49 return( AFPERR_PARAM );
52 memcpy( &did, ibuf, sizeof( did ));
53 ibuf += sizeof( did );
55 if (( dir = dirsearch( vol, did )) == NULL ) {
56 return( AFPERR_NOOBJ );
59 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
60 fbitmap = ntohs( fbitmap );
61 ibuf += sizeof( fbitmap );
62 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
63 dbitmap = ntohs( dbitmap );
64 ibuf += sizeof( dbitmap );
66 if (( path = cname( vol, dir, &ibuf )) == NULL) {
67 return( AFPERR_NOOBJ );
70 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
71 return( AFPERR_NOOBJ );
75 if (S_ISDIR(st.st_mode)) {
76 if (dbitmap && ( ret = getdirparams(vol, dbitmap, ".", curdir,
77 &st, rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
80 /* this is a directory */
81 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISDIR;
83 if (fbitmap && ( ret = getfilparams(vol, fbitmap, path, curdir, &st,
84 rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
88 *(rbuf + 2 * sizeof( u_int16_t )) = FILDIRBIT_ISFILE;
90 *rbuflen = buflen + 3 * sizeof( u_int16_t );
91 fbitmap = htons( fbitmap );
92 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
93 rbuf += sizeof( fbitmap );
94 dbitmap = htons( dbitmap );
95 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
96 rbuf += sizeof( dbitmap ) + sizeof( u_char );
102 int afp_setfildirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
105 int ibuflen, *rbuflen;
111 u_int16_t vid, bitmap;
116 memcpy( &vid, ibuf, sizeof(vid));
117 ibuf += sizeof( vid );
119 if (( vol = getvolbyvid( vid )) == NULL ) {
120 return( AFPERR_PARAM );
123 if (vol->v_flags & AFPVOL_RO)
126 memcpy( &did, ibuf, sizeof( did));
127 ibuf += sizeof( did);
129 if (( dir = dirsearch( vol, did )) == NULL ) {
130 return( AFPERR_NOOBJ );
133 memcpy( &bitmap, ibuf, sizeof( bitmap ));
134 bitmap = ntohs( bitmap );
135 ibuf += sizeof( bitmap );
137 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
138 return( AFPERR_NOOBJ );
141 if ( stat( mtoupath(vol, path ), &st ) < 0 ) {
142 return( AFPERR_NOOBJ );
146 * If ibuf is odd, make it even.
148 if ((u_long)ibuf & 1 ) {
152 if (S_ISDIR(st.st_mode)) {
153 rc = setdirparams(vol, path, bitmap, ibuf );
155 rc = setfilparams(vol, path, bitmap, ibuf );
157 if ( rc == AFP_OK ) {
158 setvoltime(obj, vol );
163 int afp_rename(obj, ibuf, ibuflen, rbuf, rbuflen )
166 int ibuflen, *rbuflen;
171 struct dir *dir, *odir = NULL;
172 char *path, *buf, *upath, *newpath;
177 #if AD_VERSION > AD_VERSION1
184 memcpy( &vid, ibuf, sizeof( vid ));
185 ibuf += sizeof( vid );
186 if (( vol = getvolbyvid( vid )) == NULL ) {
187 return( AFPERR_PARAM );
190 if (vol->v_flags & AFPVOL_RO)
193 memcpy( &did, ibuf, sizeof( did ));
194 ibuf += sizeof( did );
195 if (( dir = dirsearch( vol, did )) == NULL ) {
196 return( AFPERR_NOOBJ );
199 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
200 return( AFPERR_NOOBJ );
203 /* another place where we know about the path type */
204 if ( *ibuf++ != 2 ) {
205 return( AFPERR_PARAM );
207 plen = (unsigned char) *ibuf++;
208 *( ibuf + plen ) = '\0';
210 if ( *path == '\0' ) {
211 if ( curdir->d_parent == NULL ) { /* root directory */
212 return( AFPERR_NORENAME );
215 path = curdir->d_name;
216 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
217 return( AFPERR_NOOBJ );
222 if ( strcasecmp( path, ibuf ) == 0 ) {
227 /* if a curdir/newname ofork exists, return busy */
228 if (of_findname(vol, curdir, ibuf))
231 /* source == destination. just say okay. */
232 if (strcmp(path, ibuf) == 0)
235 /* check for illegal characters */
236 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
237 strpbrk(ibuf, MSWINDOWS_BADCHARS))
240 newpath = obj->oldtmp;
241 strcpy( newpath, mtoupath(vol, ibuf ));
243 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(newpath, '/'))
246 if (!validupath(vol, newpath))
249 /* the strdiacasecmp deals with case-insensitive, case preserving
251 if (stat( newpath, &st ) == 0 && strdiacasecmp(path, ibuf))
252 return( AFPERR_EXIST );
254 upath = mtoupath(vol, path);
256 #if AD_VERSION > AD_VERSION1
257 id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
260 if ( rename( upath, newpath ) < 0 ) {
263 return( AFPERR_NOOBJ );
265 return( AFPERR_ACCESS );
267 return( AFPERR_PARAM );
271 #if AD_VERSION > AD_VERSION1
272 if (stat(newpath, &st) < 0) /* this shouldn't fail */
274 cnid_update(vol->v_db, id, &st, curdir->d_did, newpath, strlen(newpath));
278 newadpath = obj->newtmp;
279 strcpy( newadpath, ad_path( newpath, 0 ));
280 if ( rename( ad_path( upath, 0 ), newadpath ) < 0 ) {
281 if ( errno == ENOENT ) { /* no adouble header file */
282 if (( unlink( newadpath ) < 0 ) && ( errno != ENOENT )) {
283 return( AFPERR_PARAM );
287 return( AFPERR_PARAM );
290 memset(&ad, 0, sizeof(ad));
291 if ( ad_open( newpath, ADFLAGS_HF, O_RDWR|O_CREAT, 0666,
293 return( AFPERR_PARAM );
298 memset(&ad, 0, sizeof(ad));
299 if ( ad_open( newpath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
300 O_RDWR|O_CREAT, 0666, &ad) < 0 ) {
301 if (!((errno == ENOENT) && vol_noadouble(vol)))
302 return( AFPERR_PARAM );
305 if ((buf = realloc( odir->d_name, plen + 1 )) == NULL ) {
306 syslog( LOG_ERR, "afp_rename: realloc: %m" );
308 ad_flush(&ad, ADFLAGS_HF); /* in case of create */
309 ad_close(&ad, ADFLAGS_HF);
314 strcpy( odir->d_name, ibuf );
319 ad_setentrylen( &ad, ADEID_NAME, plen );
320 memcpy( ad_entry( &ad, ADEID_NAME ), ibuf, plen );
321 ad_flush( &ad, ADFLAGS_HF );
322 ad_close( &ad, ADFLAGS_HF );
325 setvoltime(obj, vol );
327 /* if it's still open, rename the ofork as well. */
328 if (of_rename(vol, curdir, path, curdir, ibuf) < 0)
335 int afp_delete(obj, ibuf, ibuflen, rbuf, rbuflen )
338 int ibuflen, *rbuflen;
349 memcpy( &vid, ibuf, sizeof( vid ));
350 ibuf += sizeof( vid );
351 if (( vol = getvolbyvid( vid )) == NULL ) {
352 return( AFPERR_PARAM );
355 if (vol->v_flags & AFPVOL_RO)
358 memcpy( &did, ibuf, sizeof( did ));
359 ibuf += sizeof( int );
360 if (( dir = dirsearch( vol, did )) == NULL ) {
361 return( AFPERR_NOOBJ );
364 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
365 return( AFPERR_NOOBJ );
368 if ( *path == '\0' ) {
369 rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
370 } else if (of_findname(vol, curdir, path)) {
372 } else if ((rc = deletefile( upath = mtoupath(vol, path ))) == AFP_OK) {
373 #if AD_VERSION > AD_VERSION1 /* get rid of entry */
374 cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
375 cnid_delete(vol->v_db, id);
378 if ( rc == AFP_OK ) {
379 setvoltime(obj, vol );
384 char *ctoupath( vol, dir, name )
385 const struct vol *vol;
390 static char path[ MAXPATHLEN + 1];
394 p = path + sizeof( path ) - 1;
396 u = mtoupath(vol, name );
399 strncpy( p, u, len );
400 for ( d = dir; d->d_parent; d = d->d_parent ) {
402 u = mtoupath(vol, d->d_name );
405 strncpy( p, u, len );
408 len = strlen( vol->v_path );
410 strncpy( p, vol->v_path, len );
416 int afp_moveandrename(obj, ibuf, ibuflen, rbuf, rbuflen )
419 int ibuflen, *rbuflen;
422 struct dir *sdir, *ddir, *odir = NULL;
424 char *oldname, *newname;
425 char *path, *p, *upath;
429 #if AD_VERSION > AD_VERSION1
436 memcpy( &vid, ibuf, sizeof( vid ));
437 ibuf += sizeof( vid );
438 if (( vol = getvolbyvid( vid )) == NULL ) {
439 return( AFPERR_PARAM );
442 if (vol->v_flags & AFPVOL_RO)
445 /* source did followed by dest did */
446 memcpy( &did, ibuf, sizeof( did ));
447 ibuf += sizeof( int );
448 if (( sdir = dirsearch( vol, did )) == NULL ) {
449 return( AFPERR_PARAM );
452 memcpy( &did, ibuf, sizeof( did ));
453 ibuf += sizeof( int );
455 /* source pathname */
456 if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
457 return( AFPERR_NOOBJ );
461 newname = obj->newtmp;
462 oldname = obj->oldtmp;
463 if ( *path != '\0' ) {
464 /* not a directory */
465 strcpy(newname, path);
466 strcpy(oldname, path); /* an extra copy for of_rename */
467 #if AD_VERSION > AD_VERSION1
468 p = mtoupath(vol, path);
469 id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
471 p = ctoupath( vol, sdir, newname );
474 strcpy( newname, odir->d_name );
475 strcpy(oldname, odir->d_name);
476 p = ctoupath( vol, odir->d_parent, newname );
477 #if AD_VERSION > AD_VERSION1
478 id = curdir->d_did; /* we already have the CNID */
482 * p now points to the full pathname of the source fs object.
485 /* get the destination directory */
486 if (( ddir = dirsearch( vol, did )) == NULL ) {
487 return( AFPERR_PARAM );
489 if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
490 return( AFPERR_NOOBJ );
492 if ( *path != '\0' ) {
493 return( AFPERR_BADTYPE );
496 /* one more place where we know about path type */
497 if ( *ibuf++ != 2 ) {
498 return( AFPERR_PARAM );
501 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
502 strncpy( newname, ibuf, plen );
503 newname[ plen ] = '\0';
506 /* check for illegal characters */
507 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
508 strpbrk(newname, MSWINDOWS_BADCHARS))
511 upath = mtoupath(vol, newname);
513 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
516 if (!validupath(vol, upath))
519 /* source == destination. we just silently accept this. */
520 if (curdir == sdir) {
521 if (strcmp(oldname, newname) == 0)
524 /* deal with case insensitive, case-preserving filesystems. */
525 if ((stat(upath, &st) == 0) && strdiacasecmp(oldname, newname))
528 } else if (stat(upath, &st ) == 0)
529 return( AFPERR_EXIST );
532 if (of_findname(vol, curdir, newname)) {
534 } else if ((rc = renamefile( p, upath, newname,
535 vol_noadouble(vol) )) == AFP_OK) {
536 /* if it's still open, rename the ofork as well. */
537 rc = of_rename(vol, sdir, oldname, curdir, newname);
540 rc = renamedir(p, upath, odir, curdir, newname, vol_noadouble(vol));
543 if ( rc == AFP_OK ) {
544 #if AD_VERSION > AD_VERSION1
545 /* renaming may have moved the file/dir across a filesystem */
546 if (stat(upath, &st) < 0)
549 /* fix up the catalog entry */
550 cnid_update(vol->v_db, id, &st, curdir->d_did, upath, strlen(upath));
552 setvoltime(obj, vol );