Line data Source code
1 : /* File: u8dir_file.c; Copyright and License: see below */
2 :
3 : #include "u8dir/u8dir_file.h"
4 : #include "u8/u8_trace.h"
5 : #include "u8/u8_log.h"
6 : #include <stdio.h>
7 : #include <sys/stat.h>
8 : #include <stdbool.h>
9 : #include <assert.h>
10 :
11 17 : u8_error_t u8dir_file_remove( u8dir_file_t this_ )
12 : {
13 17 : U8_TRACE_BEGIN();
14 17 : assert( this_ != NULL );
15 17 : u8_error_t err = U8_ERROR_NONE;
16 :
17 17 : const int remove_err = remove( this_ );
18 17 : if ( 0 != remove_err )
19 : {
20 : /* This error may have happened on purpose or by an unexpected condition */
21 2 : if (errno == ENOENT)
22 : {
23 1 : U8_TRACE_INFO_STR( "tried to remove non-existing file:", this_ );
24 1 : U8_LOG_EVENT("remove() called on non-existing file.");
25 1 : err |= U8_ERROR_FILE_ALREADY_REMOVED;
26 : }
27 : else
28 : {
29 1 : U8_TRACE_INFO_STR( "error at removing file:", this_ );
30 1 : U8_LOG_EVENT("remove() failed to remove a file.");
31 1 : err |= U8_ERROR_AT_FILE_WRITE;
32 : }
33 : }
34 : else
35 : {
36 15 : U8_TRACE_INFO_STR( "removed file:", this_ );
37 15 : U8_LOG_EVENT("remove() removed a file.");
38 : }
39 :
40 17 : U8_TRACE_END_ERR(err);
41 17 : return err;
42 : }
43 :
44 4 : bool u8dir_file_is_regular_file( u8dir_file_t this_ )
45 : {
46 4 : U8_TRACE_BEGIN();
47 4 : assert( this_ != NULL );
48 4 : bool result = false;
49 :
50 : struct stat stat_buffer;
51 4 : const int stat_err = stat( this_, &stat_buffer ); /* stat follows symlinks - lstat not */
52 4 : if ( stat_err == -1 )
53 : {
54 4 : result = false;
55 : }
56 : else
57 : {
58 0 : result = S_ISREG( stat_buffer.st_mode );
59 : }
60 :
61 4 : U8_TRACE_END();
62 4 : return result;
63 : }
64 :
65 2 : u8_error_t u8dir_file_get_size( u8dir_file_t this_, uint64_t* out_file_size )
66 : {
67 2 : U8_TRACE_BEGIN();
68 2 : assert( this_ != NULL );
69 2 : assert( out_file_size != NULL );
70 2 : u8_error_t err = U8_ERROR_NONE;
71 :
72 : struct stat stat_buffer;
73 2 : const int stat_err = stat( this_, &stat_buffer );
74 2 : if ( stat_err == -1 )
75 : {
76 1 : err = U8_ERROR_AT_FILE_READ;
77 : }
78 : else
79 : {
80 1 : *out_file_size = stat_buffer.st_size;
81 : }
82 :
83 2 : U8_TRACE_END_ERR(err);
84 2 : return err;
85 : }
86 :
87 3 : u8_error_t u8dir_file_get_modification_time( u8dir_file_t this_, uint64_t* out_mod_time )
88 : {
89 3 : U8_TRACE_BEGIN();
90 3 : assert( this_ != NULL );
91 3 : assert( out_mod_time != NULL );
92 3 : u8_error_t err = U8_ERROR_NONE;
93 :
94 : struct stat stat_buffer;
95 3 : const int stat_err = stat( this_, &stat_buffer );
96 3 : if ( stat_err == -1 )
97 : {
98 1 : err = U8_ERROR_AT_FILE_READ;
99 : }
100 : else
101 : {
102 : #ifdef _WIN32
103 : *out_mod_time = stat_buffer.st_mtime;
104 : #else
105 2 : *out_mod_time = stat_buffer.st_mtim.tv_sec;
106 : #endif
107 : }
108 :
109 3 : U8_TRACE_END_ERR(err);
110 3 : return err;
111 : }
112 :
113 2 : u8_error_t u8dir_file_get_creation_time( u8dir_file_t this_, uint64_t* out_create_time )
114 : {
115 2 : U8_TRACE_BEGIN();
116 2 : assert( this_ != NULL );
117 2 : assert( out_create_time != NULL );
118 2 : u8_error_t err = U8_ERROR_NONE;
119 :
120 : struct stat stat_buffer;
121 2 : const int stat_err = stat( this_, &stat_buffer );
122 2 : if ( stat_err == -1 )
123 : {
124 1 : err = U8_ERROR_AT_FILE_READ;
125 : }
126 : else
127 : {
128 : #ifdef _WIN32
129 : *out_create_time = stat_buffer.st_ctime;
130 : #else
131 1 : *out_create_time = stat_buffer.st_ctim.tv_sec;
132 : #endif
133 : }
134 :
135 2 : U8_TRACE_END_ERR(err);
136 2 : return err;
137 : }
138 :
139 :
140 : /*
141 : Copyright 2022-2026 Andreas Warnke
142 :
143 : Licensed under the Apache License, Version 2.0 (the "License");
144 : you may not use this file except in compliance with the License.
145 : You may obtain a copy of the License at
146 :
147 : http://www.apache.org/licenses/LICENSE-2.0
148 :
149 : Unless required by applicable law or agreed to in writing, software
150 : distributed under the License is distributed on an "AS IS" BASIS,
151 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
152 : See the License for the specific language governing permissions and
153 : limitations under the License.
154 : */
|