@@ -31,7 +31,7 @@ static int allocate_new_file(struct fs_file_t *file);
3131static int del_oldest_log (void );
3232static int get_log_file_id (struct fs_dirent * ent );
3333
34- static int check_log_dir_available (void )
34+ static int check_log_volumen_available (void )
3535{
3636 int index = 0 ;
3737 char const * name ;
@@ -42,7 +42,7 @@ static int check_log_dir_available(void)
4242 if (rc == 0 ) {
4343 if (strncmp (CONFIG_LOG_BACKEND_FS_DIR ,
4444 name ,
45- strlen (CONFIG_LOG_BACKEND_FS_DIR ))
45+ strlen (name ))
4646 == 0 ) {
4747 return 0 ;
4848 }
@@ -52,6 +52,57 @@ static int check_log_dir_available(void)
5252 return - ENOENT ;
5353}
5454
55+ static int create_log_dir (const char * path )
56+ {
57+ const char * next ;
58+ const char * last = path + (strlen (path ) - 1 );
59+ char w_path [MAX_PATH_LEN ];
60+ int rc , len ;
61+ struct fs_dir_t dir ;
62+
63+ fs_dir_t_init (& dir );
64+
65+ /* the fist directory name is the mount point*/
66+ /* the firs path's letter might be meaningless `/`, let's skip it */
67+ next = strchr (path + 1 , '/' );
68+ if (!next ) {
69+ return 0 ;
70+ }
71+
72+ while (1 ) {
73+ next ++ ;
74+ if (next > last ) {
75+ return 0 ;
76+ }
77+ next = strchr (next , '/' );
78+ if (!next ) {
79+ next = last ;
80+ len = last - path + 1 ;
81+ } else {
82+ len = next - path ;
83+ }
84+
85+ memcpy (w_path , path , len );
86+ w_path [len ] = 0 ;
87+
88+ rc = fs_opendir (& dir , w_path );
89+ if (rc ) {
90+ /* assume directory doesn't exist */
91+ rc = fs_mkdir (w_path );
92+ if (rc ) {
93+ break ;
94+ }
95+ }
96+ rc = fs_closedir (& dir );
97+ if (rc ) {
98+ break ;
99+ }
100+ }
101+
102+ return rc ;
103+
104+ }
105+
55106static int check_log_file_exist (int num )
56107{
57108 struct fs_dir_t dir ;
@@ -93,10 +144,13 @@ int write_log_to_file(uint8_t *data, size_t length, void *ctx)
93144 struct fs_file_t * f = & file ;
94145
95146 if (backend_state == BACKEND_FS_NOT_INITIALIZED ) {
96- if (check_log_dir_available ()) {
147+ if (check_log_volumen_available ()) {
97148 return length ;
98149 }
99- rc = allocate_new_file (& file );
150+ rc = create_log_dir (CONFIG_LOG_BACKEND_FS_DIR );
151+ if (!rc ) {
152+ rc = allocate_new_file (& file );
153+ }
100154 backend_state = (rc ? BACKEND_FS_CORRUPTED : BACKEND_FS_OK );
101155 }
102156
0 commit comments