@@ -18,37 +18,17 @@ You should have received a copy of the GNU General Public License
18
18
along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
*/
20
20
21
+ #include <stdint.h>
22
+
21
23
#include "trashinfo.h"
22
24
#include "utils.h"
23
25
#include "messages.h"
24
26
25
- #define TI_LINE_COUNT 3
26
-
27
- enum
28
- {
29
- TI_HEADER ,
30
- TI_PATH_LINE ,
31
- TI_DATE_LINE
32
- };
33
-
34
- static const char ti_header [] = "[Trash Info]" ;
35
- static const char ti_path [] = "Path=" ;
36
- static const char ti_date [] = "DeletionDate=" ;
27
+ const size_t LEN_MAX_TRASHINFO_PATH_LINE =
28
+ sizeof "Path=" + LEN_MAX_ESCAPED_PATH - 1 ;
37
29
38
- static const struct st__trashinfo st_trashinfo_template [] = {
39
- {ti_header , sizeof ti_header - 1 },
40
- {ti_path , sizeof ti_path - 1 },
41
- {ti_date , sizeof ti_date - 1 },
42
- };
43
-
44
- const char trashinfo_ext [] = ".trashinfo" ;
45
- const int len_trashinfo_ext = sizeof trashinfo_ext - 1 ; /* Subtract 1 for the terminating NULL */
46
- const int LEN_MAX_TRASHINFO_PATH_LINE =
47
- (sizeof ti_path - 1 ) + LEN_MAX_ESCAPED_PATH ;
48
-
49
- const char * lit_info = "info" ;
50
- const char * path_key = "Path" ;
51
- const char * deletion_date_key = "DeletionDate" ;
30
+ const struct trashinfo_template trashinfo_template =
31
+ { "[Trash Info]" , "Path=" , "DeletionDate=" };
52
32
53
33
54
34
int
@@ -105,13 +85,32 @@ create_trashinfo(rmw_target *st_f_props, st_waste *waste_curr,
105
85
}
106
86
}
107
87
108
- fprintf (fp , "%s\n" , st_trashinfo_template [TI_HEADER ].str );
109
- fprintf (fp , "%s%s\n" , st_trashinfo_template [TI_PATH_LINE ].str ,
110
- escaped_path_ptr );
88
+ ssize_t want_size = strlen (trashinfo_template .header ) + 1 +
89
+ strlen (trashinfo_template .path_key ) +
90
+ strlen (escaped_path_ptr ) + 1 +
91
+ strlen (trashinfo_template .deletion_date_key ) +
92
+ strlen (st_time_var -> deletion_date ) + 1 ;
93
+
94
+ int n = fprintf (fp , "%s\n%s%s\n%s%s\n" , trashinfo_template .header ,
95
+ trashinfo_template .path_key , escaped_path_ptr ,
96
+ trashinfo_template .deletion_date_key ,
97
+ st_time_var -> deletion_date );
98
+
111
99
free (escaped_path );
112
- fprintf (fp , "%s%s\n" , st_trashinfo_template [TI_DATE_LINE ].str ,
113
- st_time_var -> deletion_date );
114
100
101
+ if (n < 0 )
102
+ {
103
+ print_msg_error ();
104
+ fprintf (stderr , "fprintf() failed due to an error writing to %s\n" ,
105
+ final_info_dest );
106
+ }
107
+ else if (n != want_size )
108
+ {
109
+ print_msg_error ();
110
+ fprintf (stderr ,
111
+ "Expected to write %zu bytes, but wrote %d bytes to %s\n" ,
112
+ want_size , n , final_info_dest );
113
+ }
115
114
return close_file (& fp , final_info_dest , __func__ );
116
115
}
117
116
else
@@ -122,108 +121,96 @@ create_trashinfo(rmw_target *st_f_props, st_waste *waste_curr,
122
121
}
123
122
124
123
125
- /*
126
- * name: parse_trashinfo_file
127
- *
128
- * Checks the integrity of a trashinfo file and returns req_value for
129
- * either the Path or DeletionDate key
130
- *
131
- */
132
124
char *
133
- parse_trashinfo_file (const char * file , const char * req_value )
125
+ validate_and_get_value (const char * file , ti_key key )
134
126
{
135
- struct trashinfo_field trashinfo_field ;
136
- if (strcmp (req_value , path_key ) != 0
137
- && strcmp (req_value , deletion_date_key ) != 0 )
127
+ const uint8_t LEN_DELETION_DATE_KEY_WITH_VALUE = 32 ;
128
+ struct
138
129
{
139
- print_msg_error ();
140
- fprintf (stderr , "Required arg for %s can be either \"%s\" or \"%s\"." ,
141
- __func__ , path_key , deletion_date_key );
142
- return NULL ;
143
- }
130
+ bool header_ok ;
131
+ bool path_ok ;
132
+ bool date_ok ;
133
+ } ti_status = { false, false, false };
144
134
145
- int line_no = 0 ;
146
135
FILE * fp = fopen (file , "r" );
147
136
if (fp != NULL )
148
137
{
149
- trashinfo_field .value = NULL ;
150
- bool res = true;
138
+ char * key_value = NULL ;
139
+ uint8_t line_n = 0 ;
140
+
151
141
char fp_line [LEN_MAX_TRASHINFO_PATH_LINE ];
152
142
while (fgets (fp_line , LEN_MAX_TRASHINFO_PATH_LINE , fp ) != NULL
153
- && res == true )
143
+ && line_n < TI_LINE_MAX )
154
144
{
155
145
trim_whitespace (fp_line );
156
-
157
- switch (line_no )
146
+ char * val_ptr ;
147
+ switch (line_n )
158
148
{
159
149
case TI_HEADER :
160
- res =
161
- strncmp (fp_line , st_trashinfo_template [TI_HEADER ].str ,
162
- st_trashinfo_template [TI_HEADER ].len ) == 0 ;
150
+ ti_status .header_ok =
151
+ (strcmp (fp_line , trashinfo_template .header ) == 0 );
163
152
break ;
164
- case TI_PATH_LINE :
165
- res =
166
- strncmp (fp_line , st_trashinfo_template [TI_PATH_LINE ].str ,
167
- st_trashinfo_template [TI_PATH_LINE ].len ) == 0 ;
168
- if (res && strcmp (req_value , path_key ) == 0 )
153
+ case PATH_KEY :
154
+ ti_status .path_ok =
155
+ (strncmp
156
+ (fp_line , trashinfo_template .path_key ,
157
+ strlen (trashinfo_template .path_key )) == 0 );
158
+ if (ti_status .path_ok && key == PATH_KEY )
169
159
{
170
- trashinfo_field .f .path_ptr = strchr (fp_line , '=' );
171
- trashinfo_field .f .path_ptr ++ ; /* move past the '=' sign */
172
- char * unescaped_path = unescape_url (trashinfo_field .f .path_ptr );
173
- trashinfo_field .value = unescaped_path ;
160
+ val_ptr = strchr (fp_line , '=' );
161
+ if (val_ptr )
162
+ {
163
+ val_ptr ++ ; /* move past the '=' sign */
164
+ char * unescaped_path = unescape_url (val_ptr );
165
+ if (!unescaped_path )
166
+ fatal_malloc ();
167
+ key_value = unescaped_path ;
168
+ }
174
169
}
175
170
break ;
176
- case TI_DATE_LINE :
177
- res =
178
- strncmp (fp_line , st_trashinfo_template [TI_DATE_LINE ].str ,
179
- st_trashinfo_template [TI_DATE_LINE ].len ) == 0
180
- && strlen (fp_line ) == 32 ;
181
-
182
- if (res && strcmp (req_value , deletion_date_key ) == 0 )
171
+ case DELETIONDATE_KEY :
172
+ ti_status .date_ok =
173
+ (strncmp (fp_line , trashinfo_template .deletion_date_key ,
174
+ strlen (trashinfo_template .deletion_date_key )) == 0 )
175
+ && strlen (fp_line ) == LEN_DELETION_DATE_KEY_WITH_VALUE ;
176
+ if (ti_status .date_ok && key == DELETIONDATE_KEY )
183
177
{
184
- trashinfo_field .f .date_str_ptr = strchr (fp_line , '=' );
185
- trashinfo_field .f .date_str_ptr ++ ;
186
- trashinfo_field .value = strdup (trashinfo_field .f .date_str_ptr );
187
- if (!trashinfo_field .value )
188
- fatal_malloc ();
178
+ val_ptr = strchr (fp_line , '=' );
179
+ if (val_ptr )
180
+ {
181
+ val_ptr ++ ;
182
+ key_value = strdup (val_ptr );
183
+ if (!key_value )
184
+ fatal_malloc ();
185
+ }
189
186
}
190
187
break ;
191
- default :
192
- res = false;
193
- break ;
194
188
}
195
- line_no ++ ;
189
+ line_n ++ ;
196
190
}
197
191
close_file (& fp , file , __func__ );
198
192
199
- if (res && line_no == TI_LINE_COUNT )
200
- return trashinfo_field .value ;
193
+ if (ti_status .header_ok && ti_status .path_ok && ti_status .date_ok
194
+ && key_value )
195
+ return key_value ;
201
196
202
- if (trashinfo_field . value != NULL )
203
- free (trashinfo_field . value );
197
+ if (key_value != NULL )
198
+ free (key_value );
204
199
display_dot_trashinfo_error (file );
205
200
return NULL ;
206
201
}
207
- else
208
- {
209
- open_err (file , __func__ );
210
- return NULL ;
211
- }
202
+ open_err (file , __func__ );
203
+ return NULL ;
212
204
}
213
205
214
- ///////////////////////////////////////////////////////////////////////
215
- #ifdef TEST_LIB
216
-
217
- #include "test.h"
218
-
219
- int
220
- main ()
221
- {
222
- assert (strcmp (st_trashinfo_template [TI_HEADER ].str , "[Trash Info]" ) == 0 );
223
- assert (strcmp (st_trashinfo_template [TI_PATH_LINE ].str , "Path=" ) == 0 );
224
- assert (strcmp (st_trashinfo_template [TI_DATE_LINE ].str , "DeletionDate=" ) ==
225
- 0 );
226
-
227
- return 0 ;
228
- }
229
- #endif
206
+ //const char *ti_key_to_string(ti_key key)
207
+ //{
208
+ //switch (key)
209
+ //{
210
+ //case TI_HEADER: return "TI_HEADER";
211
+ //case PATH_KEY: return "PATH_KEY";
212
+ //case DELETIONDATE_KEY: return "DELETIONDATE_KEY";
213
+ //case TI_LINE_MAX: return "TI_LINE_MAX";
214
+ //default: return "UNKNOWN_KEY";
215
+ //}
216
+ //}
0 commit comments