@@ -29,6 +29,7 @@ static int file_ctr, newest, oldest;
2929
3030static int allocate_new_file (struct fs_file_t * file );
3131static int del_oldest_log (void );
32+ static int get_log_file_id (struct fs_dirent * ent );
3233
3334static int check_log_dir_available (void )
3435{
@@ -51,6 +52,41 @@ static int check_log_dir_available(void)
5152 return - ENOENT ;
5253}
5354
55+ static int check_log_file_exist (int num )
56+ {
57+ struct fs_dir_t dir ;
58+ struct fs_dirent ent ;
59+ int rc ;
60+
61+ fs_dir_t_init (& dir );
62+
63+ rc = fs_opendir (& dir , CONFIG_LOG_BACKEND_FS_DIR );
64+ if (rc ) {
65+ return - EIO ;
66+ }
67+
68+ while (1 ) {
69+ rc = fs_readdir (& dir , & ent );
70+ if (rc < 0 ) {
71+ (void ) fs_closedir (& dir );
72+ return - EIO ;
73+ }
74+ if (ent .name [0 ] == 0 ) {
75+ break ;
76+ }
77+
78+ rc = get_log_file_id (& ent );
79+
80+ if (rc == num ) {
81+ return 1 ;
82+ }
83+ }
84+
85+ (void ) fs_closedir (& dir );
86+
87+ return 0 ;
88+ }
89+
5490int write_log_to_file (uint8_t * data , size_t length , void * ctx )
5591{
5692 int rc ;
@@ -84,18 +120,33 @@ int write_log_to_file(uint8_t *data, size_t length, void *ctx)
84120 }
85121
86122 rc = fs_write (f , data , length );
87- if (rc == - ENOSPC ) {
88- if (IS_ENABLED (CONFIG_LOG_BACKEND_FS_OVERWRITE )) {
123+ if (rc >= 0 ) {
124+ if (IS_ENABLED (CONFIG_LOG_BACKEND_FS_OVERWRITE ) &&
125+ (rc != length )) {
89126 del_oldest_log ();
90127
91128 return 0 ;
92129 }
93130 /* If overwrite is disabled, full memory
94- * is equivalent of corrupted backend .
131+ * cause the log record abandonment .
95132 */
96- goto on_error ;
97- } else if (rc < 0 ) {
98- return 0 ;
133+ length = rc ;
134+ } else {
135+ rc = check_log_file_exist (newest );
136+ if (rc == 0 ) {
137+ /* file was lost somehow
138+ * try to get a new one
139+ */
140+ file_ctr -- ;
141+ rc = allocate_new_file (f );
142+ if (rc < 0 ) {
143+ goto on_error ;
144+ }
145+ } else if (rc < 0 ) {
146+ /* fs is corrupted*/
147+ goto on_error ;
148+ }
149+ length = 0 ;
99150 }
100151
101152 fs_sync (f );
0 commit comments