@@ -2016,6 +2016,21 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
2016
2016
2017
2017
2018
2018
/// Filesystem operations ///
2019
+ static void lfs_deinit (lfs_t * lfs ) {
2020
+ // free allocated memory
2021
+ if (!lfs -> cfg -> read_buffer ) {
2022
+ lfs_free (lfs -> rcache .buffer );
2023
+ }
2024
+
2025
+ if (!lfs -> cfg -> prog_buffer ) {
2026
+ lfs_free (lfs -> pcache .buffer );
2027
+ }
2028
+
2029
+ if (!lfs -> cfg -> lookahead_buffer ) {
2030
+ lfs_free (lfs -> free .buffer );
2031
+ }
2032
+ }
2033
+
2019
2034
static int lfs_init (lfs_t * lfs , const struct lfs_config * cfg ) {
2020
2035
lfs -> cfg = cfg ;
2021
2036
@@ -2025,7 +2040,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
2025
2040
} else {
2026
2041
lfs -> rcache .buffer = lfs_malloc (lfs -> cfg -> read_size );
2027
2042
if (!lfs -> rcache .buffer ) {
2028
- return LFS_ERR_NOMEM ;
2043
+ goto cleanup ;
2029
2044
}
2030
2045
}
2031
2046
@@ -2035,7 +2050,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
2035
2050
} else {
2036
2051
lfs -> pcache .buffer = lfs_malloc (lfs -> cfg -> prog_size );
2037
2052
if (!lfs -> pcache .buffer ) {
2038
- return LFS_ERR_NOMEM ;
2053
+ goto cleanup ;
2039
2054
}
2040
2055
}
2041
2056
@@ -2051,7 +2066,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
2051
2066
} else {
2052
2067
lfs -> free .buffer = lfs_malloc (lfs -> cfg -> lookahead /8 );
2053
2068
if (!lfs -> free .buffer ) {
2054
- return LFS_ERR_NOMEM ;
2069
+ goto cleanup ;
2055
2070
}
2056
2071
}
2057
2072
@@ -2071,23 +2086,10 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
2071
2086
lfs -> deorphaned = false;
2072
2087
2073
2088
return 0 ;
2074
- }
2075
2089
2076
- static int lfs_deinit (lfs_t * lfs ) {
2077
- // free allocated memory
2078
- if (!lfs -> cfg -> read_buffer ) {
2079
- lfs_free (lfs -> rcache .buffer );
2080
- }
2081
-
2082
- if (!lfs -> cfg -> prog_buffer ) {
2083
- lfs_free (lfs -> pcache .buffer );
2084
- }
2085
-
2086
- if (!lfs -> cfg -> lookahead_buffer ) {
2087
- lfs_free (lfs -> free .buffer );
2088
- }
2089
-
2090
- return 0 ;
2090
+ cleanup :
2091
+ lfs_deinit (lfs );
2092
+ return LFS_ERR_NOMEM ;
2091
2093
}
2092
2094
2093
2095
int lfs_format (lfs_t * lfs , const struct lfs_config * cfg ) {
@@ -2107,19 +2109,19 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
2107
2109
lfs_dir_t superdir ;
2108
2110
err = lfs_dir_alloc (lfs , & superdir );
2109
2111
if (err ) {
2110
- return err ;
2112
+ goto cleanup ;
2111
2113
}
2112
2114
2113
2115
// write root directory
2114
2116
lfs_dir_t root ;
2115
2117
err = lfs_dir_alloc (lfs , & root );
2116
2118
if (err ) {
2117
- return err ;
2119
+ goto cleanup ;
2118
2120
}
2119
2121
2120
2122
err = lfs_dir_commit (lfs , & root , NULL , 0 );
2121
2123
if (err ) {
2122
- return err ;
2124
+ goto cleanup ;
2123
2125
}
2124
2126
2125
2127
lfs -> root [0 ] = root .pair [0 ];
@@ -2150,24 +2152,28 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
2150
2152
& superblock .d , sizeof (superblock .d )}
2151
2153
}, 1 );
2152
2154
if (err && err != LFS_ERR_CORRUPT ) {
2153
- return err ;
2155
+ goto cleanup ;
2154
2156
}
2155
2157
2156
2158
valid = valid || !err ;
2157
2159
}
2158
2160
2159
2161
if (!valid ) {
2160
- return LFS_ERR_CORRUPT ;
2162
+ err = LFS_ERR_CORRUPT ;
2163
+ goto cleanup ;
2161
2164
}
2162
2165
2163
2166
// sanity check that fetch works
2164
2167
err = lfs_dir_fetch (lfs , & superdir , (const lfs_block_t [2 ]){0 , 1 });
2165
2168
if (err ) {
2166
- return err ;
2169
+ goto cleanup ;
2167
2170
}
2168
2171
2169
2172
lfs_alloc_ack (lfs );
2170
- return lfs_deinit (lfs );
2173
+
2174
+ cleanup :
2175
+ lfs_deinit (lfs );
2176
+ return err ;
2171
2177
}
2172
2178
2173
2179
int lfs_mount (lfs_t * lfs , const struct lfs_config * cfg ) {
@@ -2187,15 +2193,15 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2187
2193
lfs_superblock_t superblock ;
2188
2194
err = lfs_dir_fetch (lfs , & dir , (const lfs_block_t [2 ]){0 , 1 });
2189
2195
if (err && err != LFS_ERR_CORRUPT ) {
2190
- return err ;
2196
+ goto cleanup ;
2191
2197
}
2192
2198
2193
2199
if (!err ) {
2194
2200
err = lfs_bd_read (lfs , dir .pair [0 ], sizeof (dir .d ),
2195
2201
& superblock .d , sizeof (superblock .d ));
2196
2202
lfs_superblock_fromle32 (& superblock .d );
2197
2203
if (err ) {
2198
- return err ;
2204
+ goto cleanup ;
2199
2205
}
2200
2206
2201
2207
lfs -> root [0 ] = superblock .d .root [0 ];
@@ -2204,22 +2210,30 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2204
2210
2205
2211
if (err || memcmp (superblock .d .magic , "littlefs" , 8 ) != 0 ) {
2206
2212
LFS_ERROR ("Invalid superblock at %d %d" , 0 , 1 );
2207
- return LFS_ERR_CORRUPT ;
2213
+ err = LFS_ERR_CORRUPT ;
2214
+ goto cleanup ;
2208
2215
}
2209
2216
2210
2217
uint16_t major_version = (0xffff & (superblock .d .version >> 16 ));
2211
2218
uint16_t minor_version = (0xffff & (superblock .d .version >> 0 ));
2212
2219
if ((major_version != LFS_DISK_VERSION_MAJOR ||
2213
2220
minor_version > LFS_DISK_VERSION_MINOR )) {
2214
2221
LFS_ERROR ("Invalid version %d.%d" , major_version , minor_version );
2215
- return LFS_ERR_INVAL ;
2222
+ err = LFS_ERR_INVAL ;
2223
+ goto cleanup ;
2216
2224
}
2217
2225
2218
2226
return 0 ;
2227
+
2228
+ cleanup :
2229
+
2230
+ lfs_deinit (lfs );
2231
+ return err ;
2219
2232
}
2220
2233
2221
2234
int lfs_unmount (lfs_t * lfs ) {
2222
- return lfs_deinit (lfs );
2235
+ lfs_deinit (lfs );
2236
+ return 0 ;
2223
2237
}
2224
2238
2225
2239
0 commit comments