@@ -1739,87 +1739,87 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
1739
1739
}
1740
1740
1741
1741
int lfs_file_close (lfs_t * lfs , lfs_file_t * file ) {
1742
- int err = lfs_file_sync (lfs , file );
1742
+ int err = lfs_file_sync (lfs , file );
1743
1743
1744
- // remove from list of files
1745
- for (lfs_file_t * * p = & lfs -> files ; * p ; p = & (* p )-> next ) {
1746
- if (* p == file ) {
1747
- * p = file -> next ;
1748
- break ;
1749
- }
1744
+ // remove from list of files
1745
+ for (lfs_file_t * * p = & lfs -> files ; * p ; p = & (* p )-> next ) {
1746
+ if (* p == file ) {
1747
+ * p = file -> next ;
1748
+ break ;
1750
1749
}
1750
+ }
1751
1751
1752
- // clean up memory
1753
- if (!lfs -> cfg -> file_buffer ) {
1754
- lfs_free (file -> cache .buffer );
1755
- }
1752
+ // clean up memory
1753
+ if (!lfs -> cfg -> file_buffer ) {
1754
+ lfs_free (file -> cache .buffer );
1755
+ }
1756
1756
1757
+ return err ;
1758
+ }
1759
+
1760
+ static int lfs_file_relocate (lfs_t * lfs , lfs_file_t * file ) {
1761
+ relocate :;
1762
+ // just relocate what exists into new block
1763
+ lfs_block_t nblock ;
1764
+ int err = lfs_alloc (lfs , & nblock );
1765
+ if (err ) {
1757
1766
return err ;
1758
1767
}
1759
1768
1760
- static int lfs_file_relocate (lfs_t * lfs , lfs_file_t * file ) {
1761
- relocate :;
1762
- // just relocate what exists into new block
1763
- lfs_block_t nblock ;
1764
- int err = lfs_alloc (lfs , & nblock );
1769
+ err = lfs_bd_erase (lfs , nblock );
1770
+ if (err ) {
1771
+ if (err == LFS_ERR_CORRUPT ) {
1772
+ goto relocate ;
1773
+ }
1774
+ return err ;
1775
+ }
1776
+
1777
+ // either read from dirty cache or disk
1778
+ for (lfs_off_t i = 0 ; i < file -> off ; i ++ ) {
1779
+ uint8_t data ;
1780
+ err = lfs_cache_read (lfs , & lfs -> rcache , & file -> cache ,
1781
+ file -> block , i , & data , 1 );
1765
1782
if (err ) {
1766
1783
return err ;
1767
1784
}
1768
1785
1769
- err = lfs_bd_erase (lfs , nblock );
1786
+ err = lfs_cache_prog (lfs , & lfs -> pcache , & lfs -> rcache ,
1787
+ nblock , i , & data , 1 );
1770
1788
if (err ) {
1771
1789
if (err == LFS_ERR_CORRUPT ) {
1772
1790
goto relocate ;
1773
1791
}
1774
1792
return err ;
1775
1793
}
1794
+ }
1776
1795
1777
- // either read from dirty cache or disk
1778
- for (lfs_off_t i = 0 ; i < file -> off ; i ++ ) {
1779
- uint8_t data ;
1780
- err = lfs_cache_read (lfs , & lfs -> rcache , & file -> cache ,
1781
- file -> block , i , & data , 1 );
1782
- if (err ) {
1783
- return err ;
1784
- }
1785
-
1786
- err = lfs_cache_prog (lfs , & lfs -> pcache , & lfs -> rcache ,
1787
- nblock , i , & data , 1 );
1788
- if (err ) {
1789
- if (err == LFS_ERR_CORRUPT ) {
1790
- goto relocate ;
1791
- }
1792
- return err ;
1793
- }
1794
- }
1796
+ // copy over new state of file
1797
+ memcpy (file -> cache .buffer , lfs -> pcache .buffer , lfs -> cfg -> prog_size );
1798
+ file -> cache .block = lfs -> pcache .block ;
1799
+ file -> cache .off = lfs -> pcache .off ;
1800
+ lfs -> pcache .block = 0xffffffff ;
1795
1801
1796
- // copy over new state of file
1797
- memcpy (file -> cache .buffer , lfs -> pcache .buffer , lfs -> cfg -> prog_size );
1798
- file -> cache .block = lfs -> pcache .block ;
1799
- file -> cache .off = lfs -> pcache .off ;
1800
- lfs -> pcache .block = 0xffffffff ;
1802
+ file -> block = nblock ;
1803
+ return 0 ;
1804
+ }
1801
1805
1802
- file -> block = nblock ;
1803
- return 0 ;
1806
+ static int lfs_file_flush (lfs_t * lfs , lfs_file_t * file ) {
1807
+ if (file -> flags & LFS_F_READING ) {
1808
+ file -> flags &= ~LFS_F_READING ;
1804
1809
}
1805
1810
1806
- static int lfs_file_flush (lfs_t * lfs , lfs_file_t * file ) {
1807
- if (file -> flags & LFS_F_READING ) {
1808
- file -> flags &= ~LFS_F_READING ;
1809
- }
1810
-
1811
- if (file -> flags & LFS_F_WRITING ) {
1812
- lfs_off_t pos = file -> pos ;
1811
+ if (file -> flags & LFS_F_WRITING ) {
1812
+ lfs_off_t pos = file -> pos ;
1813
1813
1814
- if (!(file -> flags & LFS_F_INLINE )) {
1815
- // copy over anything after current branch
1816
- lfs_file_t orig = {
1817
- .head = file -> head ,
1818
- .size = file -> size ,
1819
- .flags = LFS_O_RDONLY ,
1820
- .pos = file -> pos ,
1821
- .cache = lfs -> rcache ,
1822
- };
1814
+ if (!(file -> flags & LFS_F_INLINE )) {
1815
+ // copy over anything after current branch
1816
+ lfs_file_t orig = {
1817
+ .head = file -> head ,
1818
+ .size = file -> size ,
1819
+ .flags = LFS_O_RDONLY ,
1820
+ .pos = file -> pos ,
1821
+ .cache = lfs -> rcache ,
1822
+ };
1823
1823
lfs -> rcache .block = 0xffffffff ;
1824
1824
1825
1825
while (file -> pos < file -> size ) {
@@ -2270,6 +2270,7 @@ int lfs_file_getattrs(lfs_t *lfs, lfs_file_t *file,
2270
2270
return LFS_ERR_RANGE ;
2271
2271
}
2272
2272
2273
+ memset (attrs [j ].buffer , 0 , attrs [j ].size );
2273
2274
memcpy (attrs [j ].buffer ,
2274
2275
file -> attrs [i ].buffer , file -> attrs [i ].size );
2275
2276
}
@@ -2281,9 +2282,9 @@ int lfs_file_getattrs(lfs_t *lfs, lfs_file_t *file,
2281
2282
2282
2283
int lfs_file_setattrs (lfs_t * lfs , lfs_file_t * file ,
2283
2284
const struct lfs_attr * attrs , int count ) {
2284
- // just tack to the file, will be written at sync time
2285
- file -> attrs = attrs ;
2286
- file -> attrcount = count ;
2285
+ if (( file -> flags & 3 ) == LFS_O_RDONLY ) {
2286
+ return LFS_ERR_BADF ;
2287
+ }
2287
2288
2288
2289
// at least make sure attributes fit
2289
2290
if (!lfs_pairisnull (file -> pair )) {
@@ -2306,6 +2307,11 @@ int lfs_file_setattrs(lfs_t *lfs, lfs_file_t *file,
2306
2307
}
2307
2308
}
2308
2309
2310
+ // just tack to the file, will be written at sync time
2311
+ file -> attrs = attrs ;
2312
+ file -> attrcount = count ;
2313
+ file -> flags |= LFS_F_DIRTY ;
2314
+
2309
2315
return 0 ;
2310
2316
}
2311
2317
@@ -2432,6 +2438,10 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
2432
2438
return LFS_ERR_NAMETOOLONG ;
2433
2439
}
2434
2440
2441
+ if (oldentry .size - oldentry .d .nlen + nlen > lfs -> cfg -> block_size ) {
2442
+ return LFS_ERR_NOSPC ;
2443
+ }
2444
+
2435
2445
// must have same type
2436
2446
if (prevexists && preventry .d .type != oldentry .d .type ) {
2437
2447
return LFS_ERR_ISDIR ;
0 commit comments