Lolly 1.4.28
Loading...
Searching...
No Matches
Classes | Functions
file.cpp File Reference
#include "file.hpp"
#include "analyze.hpp"
#include "lolly/hash/uuid.hpp"
#include "string.hpp"
#include "sys_utils.hpp"
#include "tbox/tbox.h"
Include dependency graph for file.cpp:

Go to the source code of this file.

Classes

struct  file_status
 

Functions

bool is_local_and_single (url u)
 
static string as_local_path (url u)
 
bool is_directory (url u)
 
bool is_regular (url u)
 
bool is_symbolic_link (url u)
 
bool is_newer (url which, url than)
 
bool is_of_type (url name, string filter)
 
int file_size (url u)
 
int last_modified (url u)
 
static tb_long_t tb_directory_walk_func (tb_char_t const *path, tb_file_info_t const *info, tb_cpointer_t priv)
 
array< stringread_directory (url u, bool &error_flag)
 
url subdirectories (url u)
 
void mkdir (url u)
 
void make_dir (url which)
 
void rmdir (url u)
 
void chdir (url u)
 
url url_temp (string suffix)
 
url url_temp_dir_sub ()
 
url url_temp_dir ()
 
void move (url u1, url u2)
 
void copy (url u1, url u2)
 
void remove (url u)
 
static url find_the_first_exist (const url &u)
 lookup the first exist path in a bunch of url, if all url does not exist, then the last one will be used.
 
static bool cleanup_and_return_finally (const file_status &status, const url &u, bool fatal, const string &reason)
 
file_status load_string_try (url u, string &s)
 
bool load_string (url u, string &s, bool fatal)
 
string string_load (url u)
 
file_status save_string_try (url u, const string &s)
 
bool save_string (url u, const string &s, bool fatal)
 
void string_save (const string &s, url u)
 
file_status append_string_try (url u, const string &s)
 
bool append_string (url u, const string &s, bool fatal)
 
void string_append_to_file (const string &s, url u)
 
void append_to (url what, url to)
 

Detailed Description

Unitests for file.

Author
Darcy
Date
2019-2023

Definition in file file.cpp.

Function Documentation

◆ is_local_and_single()

bool is_local_and_single ( url u)

Definition at line 22 of file file.cpp.

22 {
23 string label = u.label ();
24 string protocol= u.protocol ();
25 return ((label == "") || (label == "concat") || (label == "root")) &&
26 (protocol == "default" || protocol == "file");
27}
The list class represents a linked list.
Definition list.hpp:48

◆ as_local_path()

static string as_local_path ( url u)
static

Definition at line 30 of file file.cpp.

30 {
31 if (is_rooted (u, "file")) {
32 return as_string (reroot (u, "default"));
33 }
34 return as_string (u);
35}
string as_string(int16_t i)
Definition string.cpp:272
url reroot(url u, string protocol)
Definition url.cpp:490
bool is_rooted(url u)
Definition url.cpp:686

◆ is_directory()

bool is_directory ( url u)

Definition at line 38 of file file.cpp.

38 {
39 if (!is_local_and_single (u)) return false;
40
41 c_string path (as_local_path (u));
43 if (tb_file_info (path, &info)) {
44 switch (info.type) {
46 return true;
47 default:
48 return false;
49 }
50 }
51 else {
52 return false;
53 }
54}
bool is_local_and_single(url u)
Definition file.cpp:22
static string as_local_path(url u)
Definition file.cpp:30

◆ is_regular()

bool is_regular ( url u)

Definition at line 57 of file file.cpp.

57 {
58 if (!is_local_and_single (u)) return false;
59
60 c_string path (as_local_path (u));
62 if (tb_file_info (path, &info)) {
63 switch (info.type) {
65 return true;
66 default:
67 return false;
68 }
69 }
70 else {
71 return false;
72 }
73}

◆ is_symbolic_link()

bool is_symbolic_link ( url u)

Definition at line 76 of file file.cpp.

76 {
77 if (!is_local_and_single (u)) return false;
78
79 c_string path (as_local_path (u));
81 if (tb_file_info (path, &info)) {
82 return (info.flags & TB_FILE_FLAG_LINK) != 0;
83 }
84 else {
85 return false;
86 }
87}

◆ is_newer()

bool is_newer ( url which,
url than )

Definition at line 90 of file file.cpp.

90 {
91 if (!is_local_and_single (which)) return false;
92 if (!is_local_and_single (than)) return false;
93
97 return info1.mtime > info2.mtime;
98 }
99 else {
100 return false;
101 }
102}

◆ is_of_type()

bool is_of_type ( url name,
string filter )

Definition at line 105 of file file.cpp.

105 {
106 if (is_ramdisc (name)) return true;
107
108 if (!is_local_and_single (name)) return false;
109
110 if (filter == "") return true;
111 int i, n= N (filter);
112
113 // Normal files
114 if (os_win () || os_mingw ()) {
115 string suf;
116 if (filter == "x") {
117 suf= suffix (name);
118 if ((suf != "exe") && (suf != "bat") && (suf != "com")) {
119 name= glue (name, ".exe");
120 suf = "exe";
121 }
122 }
123 }
124 c_string path (as_local_path (name));
126 if (!tb_file_info (path, &info)) {
127 return false;
128 }
129 for (i= 0; i < n; i++)
130 switch (filter[i]) {
131 // FIXME: should check user id and group id for r, w and x
132 case 'f':
133 if (info.type != TB_FILE_TYPE_FILE) return false;
134 break;
135 case 'd':
136 if (info.type != TB_FILE_TYPE_DIRECTORY) return false;
137 break;
138 case 'l':
139 if (info.flags != TB_FILE_FLAG_LINK) return false;
140 break;
141 case 'r':
142 if (!tb_file_access (path, TB_FILE_MODE_RO)) return false;
143 break;
144 case 'w':
145 if (!tb_file_access (path, TB_FILE_MODE_WO)) return false;
146 break;
147 case 'x':
148 if (!tb_file_access (path, TB_FILE_MODE_EXEC)) return false;
149 break;
150 }
151 return true;
152}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
bool os_mingw()
bool os_win()
Definition sys_utils.cpp:94
string suffix(url u, bool use_locase)
Definition url.cpp:381
bool is_ramdisc(url u)
Definition url.cpp:724
url glue(url u, string s)
Definition url.cpp:421

◆ file_size()

int file_size ( url u)

Definition at line 155 of file file.cpp.

155 {
156 if (!is_local_and_single (u)) return -1;
157
158 c_string path (as_local_path (u));
160 if (tb_file_info (path, &info)) {
161 return info.size;
162 }
163 else {
164 return -1;
165 }
166}

◆ last_modified()

int last_modified ( url u)

Definition at line 169 of file file.cpp.

169 {
170 if (!is_local_and_single (u)) return -1;
171
172 c_string path (as_local_path (u));
174 if (tb_file_info (path, &info)) {
175 return info.mtime;
176 }
177 else {
178 return -1;
179 }
180}

◆ tb_directory_walk_func()

static tb_long_t tb_directory_walk_func ( tb_char_t const * path,
tb_file_info_t const * info,
tb_cpointer_t priv )
static

Definition at line 183 of file file.cpp.

184 {
185 // check
187
189 (*p_arr_result) << as_string (tail (url_system (string (path))));
191}
list< T > tail(list< T > l, int n=1)
Get all but the first n items of a list.
Definition list.ipp:193
url url_system(string name)
Definition url.cpp:306

◆ read_directory()

array< string > read_directory ( url u,
bool & error_flag )

Definition at line 194 of file file.cpp.

194 {
195 if (!is_local_and_single (u)) {
196 error_flag= false;
197 return array<string> ();
198 }
199
200 c_string path (as_local_path (u));
203 if (error_flag) {
204 return arr_result;
205 }
207 return arr_result;
208}
bool is_directory(url u)
Definition file.cpp:38
static tb_long_t tb_directory_walk_func(tb_char_t const *path, tb_file_info_t const *info, tb_cpointer_t priv)
Definition file.cpp:183

◆ subdirectories()

url subdirectories ( url u)

Definition at line 211 of file file.cpp.

211 {
212 if (is_or (u)) return subdirectories (u[1]) | subdirectories (u[2]);
213 else if (is_directory (u)) {
214 url ret= u;
215 bool error_flag;
217 for (int i= 0; i < N (dir); i++)
218 if (!starts (dir[i], ".") && is_directory (u * dir[i]))
219 ret= ret | subdirectories (u * dir[i]);
220 return ret;
221 }
222 else return url_none ();
223}
bool starts(string s, const char *what)
Definition analyze.cpp:566
Definition url.hpp:37
url subdirectories(url u)
Definition file.cpp:211
array< string > read_directory(url u, bool &error_flag)
Definition file.cpp:194
url url_none()
Definition url.cpp:91
bool is_or(url u)
Definition url.hpp:178

◆ mkdir()

void mkdir ( url u)

Definition at line 226 of file file.cpp.

226 {
227 string label= u.label ();
228 if (label == "none" || label == "root" || label == "wildcard") return;
229 if (is_local_and_single (u)) { // label == "" or label == "concat"
230 c_string path (as_local_path (u));
231 tb_directory_create (path);
232 }
233 if (is_or (u)) { // label == "or"
234 mkdir (u[1]);
235 mkdir (u[2]);
236 }
237}
void mkdir(url u)
Definition file.cpp:226

◆ make_dir()

void make_dir ( url which)

Definition at line 240 of file file.cpp.

240 {
241 if (is_none (which)) return;
242 if (!is_directory (which)) {
243 make_dir (head (which));
244 mkdir (which);
245 }
246}
void make_dir(url which)
Definition file.cpp:240
list< T > head(list< T > l, int n=1)
Get the first n items of a list.
Definition list.ipp:185
bool is_none(url u)
Definition url.hpp:166

◆ rmdir()

void rmdir ( url u)

Definition at line 249 of file file.cpp.

249 {
250 string label= u.label ();
251 if (label == "none" || label == "root" || label == "wildcard") return;
252 if (is_local_and_single (u)) { // label == "" or label == "concat"
253 c_string path (as_local_path (u));
254 tb_directory_remove (path);
255 }
256 if (is_or (u)) { // label == "or"
257 rmdir (u[1]);
258 rmdir (u[2]);
259 }
260}
void rmdir(url u)
Definition file.cpp:249

◆ chdir()

void chdir ( url u)

Definition at line 263 of file file.cpp.

263 {
264 if (is_local_and_single (u)) {
265 c_string path (as_local_path (u));
266 if (tb_directory_current_set (path) != tb_true) {
267 TM_FAILED ("Failed to change the dir");
268 }
269 }
270 else TM_FAILED ("file path invalid");
271}
#define TM_FAILED(msg)
Macro used to throw an exception with a specified error message.
Definition basic.hpp:93

◆ url_temp()

url url_temp ( string suffix)

Definition at line 274 of file file.cpp.

274 {
275 string file_name= replace (lolly::hash::uuid_make (), "-", "_");
276 if (!is_empty (suffix)) {
277 file_name= file_name * string (".") * suffix;
278 }
280 if (file_size (u) == -1) {
281 return u;
282 }
283 else {
284 return url_temp (suffix);
285 }
286}
string replace(string s, string what, string by)
Definition analyze.cpp:899
url url_temp(string suffix)
Definition file.cpp:274
url url_temp_dir()
Definition file.cpp:300
int file_size(url u)
Definition file.cpp:155
string uuid_make()
Definition uuid.cpp:20
bool is_empty(string s)
Definition string.cpp:316

◆ url_temp_dir_sub()

url url_temp_dir_sub ( )

Definition at line 289 of file file.cpp.

289 {
290#if defined(OS_WIN) || defined(OS_MINGW)
291 url main_tmp_dir= url_system ("$TMP") * url (".lolly");
292#else
293 url main_tmp_dir= url_system ("/tmp") * url (".lolly");
294#endif
296 return (tmp_dir);
297}
SN get_process_id()

◆ url_temp_dir()

url url_temp_dir ( )

Definition at line 300 of file file.cpp.

300 {
301 static url u;
302 if (u == url_none ()) {
304 make_dir (u);
305 }
306 return u;
307}
url url_temp_dir_sub()
Definition file.cpp:289

◆ move()

void move ( url u1,
url u2 )

Definition at line 310 of file file.cpp.

310 {
313
315}

◆ copy()

void copy ( url u1,
url u2 )

Definition at line 318 of file file.cpp.

◆ remove()

void remove ( url u)

Definition at line 326 of file file.cpp.

326 {
327 string label= u.label ();
328 if (label == "none" || label == "root" || label == "wildcard") return;
329 if (is_local_and_single (u)) {
330 c_string path (as_local_path (u));
331 tb_file_remove (path);
332 }
333 else if (is_or (u)) { // label == "or"
334 remove (u[1]);
335 remove (u[2]);
336 }
337}
void remove(url u)
Definition file.cpp:326

◆ find_the_first_exist()

static url find_the_first_exist ( const url & u)
static

lookup the first exist path in a bunch of url, if all url does not exist, then the last one will be used.

Definition at line 359 of file file.cpp.

359 {
360 url u_iter= expand (u);
361 // iterate to find the first existed file
362 while (is_or (u_iter)) {
363 if (is_regular (u_iter[1])) {
364 return u_iter[1];
365 }
366 u_iter= u_iter[2];
367 }
368 // if u_target does not exist, just return the last url
369 return u_iter;
370}
bool is_regular(url u)
Definition file.cpp:57
static url expand(url u1, url u2)
Definition url.cpp:498

◆ cleanup_and_return_finally()

static bool cleanup_and_return_finally ( const file_status & status,
const url & u,
bool fatal,
const string & reason )
static

Definition at line 373 of file file.cpp.

374 {
375
376 if (status.buffer != NULL) {
377 tm_delete_array (status.buffer);
378 }
379 if (status.path != NULL) {
380 tm_delete_array (status.path);
381 }
382
383 if (!status.failed) {
384 return false;
385 }
386 cerr << "Failed to " << reason << " in [" << as_local_path (u) << "]" << LF;
387 if (fatal) {
388 TM_FAILED (status.error_msg);
389 }
390 return true;
391}
@ LF
Definition basic.hpp:287
void tm_delete_array(C *Ptr)
const tb_byte_t * buffer
Definition file.cpp:348
const char * path
Definition file.cpp:347
const char * error_msg
Definition file.cpp:346
bool failed
Definition file.cpp:345
tm_ostream & cerr

◆ load_string_try()

file_status load_string_try ( url u,
string & s )

Definition at line 394 of file file.cpp.

394 {
395 if (!is_local_and_single (u)) {
396 return file_status (true, "Must be a local and single file");
397 }
399 const char* path = as_charp (as_local_path (u_target));
400 if (!tb_file_access (path, TB_FILE_MODE_RO)) {
401 return file_status (true, "File is not readable", path);
402 }
404 if (file == tb_null) {
405 return file_status (true, "Failed to init the file", path);
406 }
407 tb_file_sync (file); // lock file
408 tb_size_t size= tb_file_size (file);
409 if (size == 0) {
410 s= "";
411 tb_file_exit (file);
412 return file_status (false, "", path);
413 }
414 tb_byte_t* buffer = tm_new_array<tb_byte_t> (size);
415 tb_size_t real_size = tb_file_read (file, buffer, size);
416 bool read_sz_equ= (real_size == size);
417 bool exit_suc = tb_file_exit (file); // exit file
418 if (read_sz_equ && exit_suc) {
419 s->resize (size);
420 // Copying char by char has roughly the same efficiency as constructing new
421 // string with buffer.
422 for (size_t seek= 0; seek < size; seek++) {
423 s[seek]= buffer[seek];
424 }
425 return file_status (false, "", path, buffer);
426 }
427 else {
428 return file_status (true, "Unexpected behavior during reading", path,
429 buffer);
430 }
431}
static url find_the_first_exist(const url &u)
lookup the first exist path in a bunch of url, if all url does not exist, then the last one will be u...
Definition file.cpp:359
char * as_charp(string s)
Definition string.cpp:256

◆ load_string()

bool load_string ( url u,
string & s,
bool fatal )

Definition at line 434 of file file.cpp.

434 {
436 return cleanup_and_return_finally (stat, u, fatal, "load url");
437}
static bool cleanup_and_return_finally(const file_status &status, const url &u, bool fatal, const string &reason)
Definition file.cpp:373
file_status load_string_try(url u, string &s)
Definition file.cpp:394

◆ string_load()

string string_load ( url u)

Definition at line 440 of file file.cpp.

440 {
441 string s;
442 // file_url f= u;
443 (void) load_string (u, s, false);
444 return s;
445}
bool load_string(url u, string &s, bool fatal)
Definition file.cpp:434

◆ save_string_try()

file_status save_string_try ( url u,
const string & s )

Definition at line 448 of file file.cpp.

448 {
449 ASSERT (sizeof (tb_byte_t) == sizeof (char),
450 "invalid cast from tb_byte_t* to char*");
451 if (!is_local_and_single (u)) {
452 return file_status (true, "Must be an absolute path");
453 }
455 const char* path = as_charp (as_local_path (u_target));
456
457 // tb_file_access cannot check TB_FILE_MODE_CREAT on windows, so create
458 // directly
461 if (fout == tb_null) {
462 return file_status (true, "File not writeable", path);
463 }
464
465 // lock file
470 return file_status (true, "Fail to lock file", path);
471 }
472
474 const tb_byte_t* content = reinterpret_cast<const tb_byte_t*> (as_charp (s));
479 bool exit_suc= tb_file_exit (fout);
480 if (writ_sz_equ && exit_suc && release_suc) {
481 return file_status (false, "", path, content);
482 }
483 else {
484 return file_status (true, "Unexpected behavior during writting", path,
485 content);
486 }
487}
#define ASSERT(cond, msg)
Macro used to assert that a condition is true, and throw an exception with an error message if the co...
Definition basic.hpp:85

◆ save_string()

bool save_string ( url u,
const string & s,
bool fatal )

Definition at line 490 of file file.cpp.

490 {
492 return cleanup_and_return_finally (stat, u, fatal, "save to url");
493}
file_status save_string_try(url u, const string &s)
Definition file.cpp:448

◆ string_save()

void string_save ( const string & s,
url u )

Definition at line 496 of file file.cpp.

496 {
497 (void) save_string (u, s, false);
498}
bool save_string(url u, const string &s, bool fatal)
Definition file.cpp:490

◆ append_string_try()

file_status append_string_try ( url u,
const string & s )

Definition at line 501 of file file.cpp.

501 {
502 if (!is_local_and_single (u)) {
503 return file_status (true, "Must be a local and single file");
504 }
506 const char* path = as_charp (as_local_path (u_target));
507
508 // open the file
511 if (fout == NULL) {
512 return file_status (true, "File to append is not found or not appendable",
513 path);
514 }
515
516 // lock file
521 return file_status (true, "Fail to lock file", path);
522 }
523
524 // append string to file
526 const tb_byte_t* content = reinterpret_cast<const tb_byte_t*> (as_charp (s));
531 bool exit_suc= tb_file_exit (fout);
532
533 if (writ_sz_equ && exit_suc && release_suc) {
534 return file_status (false, "", path, content);
535 }
536 else {
537 return file_status (true, "Unexpected behavior during appending", path,
538 content);
539 }
540}

◆ append_string()

bool append_string ( url u,
const string & s,
bool fatal )

Definition at line 543 of file file.cpp.

543 {
545 return cleanup_and_return_finally (stat, u, fatal, "append to url");
546}
file_status append_string_try(url u, const string &s)
Definition file.cpp:501

◆ string_append_to_file()

void string_append_to_file ( const string & s,
url u )

Definition at line 549 of file file.cpp.

549 {
550 (void) append_string (u, s, false);
551}
bool append_string(url u, const string &s, bool fatal)
Definition file.cpp:543

◆ append_to()

void append_to ( url what,
url to )

Definition at line 554 of file file.cpp.

554 {
555 string what_s;
556 if (load_string (what, what_s, false) || append_string (to, what_s, false))
557 cerr << "Append failed for " << to << LF;
558}