Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : * @brief Check whether the given path exists and matches requested filesystem object type
5 : : * @param path Path to verify
6 : : * @param fs_object_type Expected object type: SHOULD_BE_A_FILE or SHOULD_BE_A_DIRECTORY
7 : : * @return EXISTS when path exists and matches requested type, otherwise NOT_FOUND
8 : : * @details The special path ":memory:" is treated as not available and returns NOT_FOUND
9 : : */
10 : 1047 : FileAvailability file_availability(
11 : : const char *path,
12 : : const unsigned char fs_object_type)
13 : : {
14 : : /// The availability status returned by this function
15 : : /// By default, the path is treated as unavailable until verified
16 : 1047 : FileAvailability presence = NOT_FOUND;
17 : :
18 : : // Do nothing if the path is not a real path but in-memory database
19 [ + + ]: 1047 : if(strcmp(path,":memory:") == 0)
20 : : {
21 : 76 : return(presence);
22 : : }
23 : :
24 : : struct stat stats;
25 : :
26 : 971 : slog(TRACE,"Verify that the path %s exists\n",path);
27 : :
28 : : // Check for existence
29 [ + + ]: 971 : if(stat(path,&stats) == 0)
30 : : {
31 : : // Check is it a directory or a file
32 [ + + ]: 840 : if(fs_object_type == SHOULD_BE_A_FILE)
33 : : {
34 : : // Verify that the path points to a regular file
35 [ + - ]: 268 : if(S_ISREG(stats.st_mode))
36 : : {
37 : 268 : slog(TRACE,"The path %s is exists and it is a file\n",path);
38 : 268 : presence = EXISTS;
39 : : }
40 : :
41 [ + - ]: 572 : } else if(fs_object_type == SHOULD_BE_A_DIRECTORY){
42 : : // Verify that the path points to a directory
43 [ + - ]: 572 : if(S_ISDIR(stats.st_mode))
44 : : {
45 : 572 : slog(TRACE,"The path %s is exists and it is a directory\n",path);
46 : 572 : presence = EXISTS;
47 : : }
48 : : }
49 : : }
50 : :
51 [ + + ]: 971 : if(EXISTS != presence)
52 : : {
53 [ + + ]: 131 : if(fs_object_type == SHOULD_BE_A_FILE)
54 : : {
55 : 129 : slog(EVERY,"The path %s doesn't exist or it is not a file\n",path);
56 [ + - ]: 2 : } else if(fs_object_type == SHOULD_BE_A_DIRECTORY){
57 : 2 : slog(EVERY,"The path %s doesn't exist or it is not a directory\n",path);
58 : : }
59 : : }
60 : :
61 : 971 : return(presence);
62 : : }
|