@@ -542,6 +542,8 @@ static uint16_t lfs_fs_disk_version_minor(lfs_t *lfs) {
542542
543543
544544/// Internal operations predeclared here ///
545+ static lfs_ssize_t lfs_dir_path_ (lfs_t * lfs ,
546+ lfs_mdir_t * dir , uint16_t id , char * path , lfs_size_t size );
545547#ifndef LFS_READONLY
546548static int lfs_dir_commit (lfs_t * lfs , lfs_mdir_t * dir ,
547549 const struct lfs_mattr * attrs , int attrcount );
@@ -3833,6 +3835,53 @@ static lfs_soff_t lfs_file_size_(lfs_t *lfs, lfs_file_t *file) {
38333835 return file -> ctz .size ;
38343836}
38353837
3838+ static lfs_ssize_t lfs_dir_path_ (lfs_t * lfs ,
3839+ lfs_mdir_t * dir , uint16_t id , char * path , lfs_size_t size ) {
3840+ struct lfs_info info ;
3841+ char * next = path ;
3842+ lfs_mdir_t parent ;
3843+ lfs_ssize_t len ;
3844+ lfs_stag_t tag ;
3845+ int err ;
3846+
3847+ if (lfs_pair_cmp (lfs -> root , dir -> pair ) != 0 ) {
3848+ tag = lfs_fs_parent (lfs , dir -> pair , & parent );
3849+ if (tag < 0 ) {
3850+ return tag ;
3851+ }
3852+
3853+ len = lfs_dir_path_ (lfs , & parent , lfs_tag_id (tag ), next , size );
3854+ if (len < 0 ) {
3855+ return len ;
3856+ }
3857+
3858+ next += len ;
3859+ size -= len ;
3860+ }
3861+
3862+ err = lfs_dir_getinfo (lfs , dir , id , & info );
3863+ if (err < 0 ) {
3864+ return err ;
3865+ }
3866+
3867+ len = strlen (info .name );
3868+ if (len >= size ) {
3869+ return LFS_ERR_INVAL ;
3870+ }
3871+
3872+ memcpy (next , info .name , len + 1 );
3873+ next += len ;
3874+
3875+ if (info .type == LFS_TYPE_DIR ) {
3876+ * next ++ = '/' ;
3877+ if (++ len >= size ) {
3878+ return LFS_ERR_INVAL ;
3879+ }
3880+ * next = '\0' ;
3881+ }
3882+
3883+ return next - path ;
3884+ }
38363885
38373886/// General fs operations ///
38383887static int lfs_stat_ (lfs_t * lfs , const char * path , struct lfs_info * info ) {
@@ -6235,6 +6284,22 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
62356284 return res ;
62366285}
62376286
6287+ int lfs_file_path (lfs_t * lfs , lfs_file_t * file , char * path , lfs_size_t size ) {
6288+ int err = LFS_LOCK (lfs -> cfg );
6289+ if (err ) {
6290+ return err ;
6291+ }
6292+
6293+ LFS_TRACE ("lfs_file_path(%p, %p)" , (void * )lfs , (void * )file );
6294+ LFS_ASSERT (lfs_mlist_isopen (lfs -> mlist , (struct lfs_mlist * )file ));
6295+
6296+ err = lfs_dir_path_ (lfs , & file -> m , file -> id , path , size );
6297+
6298+ LFS_TRACE ("lfs_file_path -> %d" , err );
6299+ LFS_UNLOCK (lfs -> cfg );
6300+ return err < 0 ? err : 0 ;
6301+ }
6302+
62386303#ifndef LFS_READONLY
62396304int lfs_mkdir (lfs_t * lfs , const char * path ) {
62406305 int err = LFS_LOCK (lfs -> cfg );
@@ -6366,6 +6431,20 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs) {
63666431 return res ;
63676432}
63686433
6434+ int lfs_dir_path (lfs_t * lfs , lfs_dir_t * dir , char * path , lfs_size_t size ) {
6435+ int err = LFS_LOCK (lfs -> cfg );
6436+ if (err ) {
6437+ return err ;
6438+ }
6439+
6440+ LFS_TRACE ("lfs_dir_path(%p, %p)" , (void * )lfs , (void * )dir );
6441+ err = lfs_dir_path_ (lfs , & dir -> m , dir -> id , path , size );
6442+
6443+ LFS_TRACE ("lfs_dir_path -> %d" , err );
6444+ LFS_UNLOCK (lfs -> cfg );
6445+ return err < 0 ? err : 0 ;
6446+ }
6447+
63696448int lfs_fs_traverse (lfs_t * lfs , int (* cb )(void * , lfs_block_t ), void * data ) {
63706449 int err = LFS_LOCK (lfs -> cfg );
63716450 if (err ) {
0 commit comments