@@ -560,6 +560,57 @@ int fs_stat(fs9_file *dir, const char *path, struct stat *buf)
560
560
return r;
561
561
}
562
562
563
+ int fs_rename (fs9_file *dir, const char *origname, const char *newname)
564
+ {
565
+ fs9_file f;
566
+ uint8_t *ptr, *szptr;
567
+ unsigned sz;
568
+ int r = fs_open_relative (dir, &f, origname, 0 );
569
+ if (r != 0 ) {
570
+ #ifdef _DEBUG_9P
571
+ __builtin_printf (" rename: open_relative failed with %d\n " , r);
572
+ #endif
573
+ return r;
574
+ }
575
+ // set up rename command
576
+ ptr = doPut4 (txbuf, 0 ); // space for total message size
577
+ ptr = doPut1 (ptr, t_wstat); // command
578
+ ptr = doPut2 (ptr, NOTAG); // only one command, so no tag needed
579
+ ptr = doPut4 (ptr, (unsigned )&f);
580
+ szptr = ptr; // save pointer to size of wstat struct
581
+ ptr += 4 ; // skip two 2 byte sizes
582
+
583
+ // fill in type[2] dev[4] qid[13] mode[4] atime[4] mtime[4] length[8]
584
+ // so 2 + 4 + 13 + 20 = 39 bytes total
585
+ memset (ptr, 0xff , 39 );
586
+ ptr += 39 ;
587
+
588
+ // now copy in the new file name
589
+ ptr = doPutStr (ptr, newname);
590
+
591
+ // and some empty strings for uid, gid, muid
592
+ ptr = doPutStr (ptr, " " );
593
+ ptr = doPutStr (ptr, " " );
594
+ ptr = doPutStr (ptr, " " );
595
+
596
+ sz = (ptr-szptr) - 2 ;
597
+
598
+ // fill in the earlier size fields
599
+ szptr = doPut2 (szptr, sz);
600
+ szptr = doPut2 (szptr, sz-2 );
601
+
602
+ // now send the request
603
+ r = (*sendRecv)(txbuf, ptr, maxlen);
604
+ if (r < 5 || txbuf[4 ] != r_wstat) {
605
+ #ifdef _DEBUG_9P
606
+ __builtin_printf (" rename: sendRecv failed with r=%d\n " , r);
607
+ #endif
608
+ return -EINVAL;
609
+ }
610
+ return 0 ;
611
+ }
612
+
613
+
563
614
//
564
615
// VFS versions of the above
565
616
//
@@ -825,8 +876,7 @@ static int v_rmdir(const char *name)
825
876
826
877
static int v_rename (const char *oldname, const char *newname)
827
878
{
828
- // FIXME: should use wstat to implement the rename
829
- return -ENOSYS;
879
+ return fs_rename (&rootdir, oldname, newname);
830
880
}
831
881
832
882
static int v_open (vfs_file_t *fil, const char *name, int flags)
0 commit comments