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 21 : u8_error_t u8dir_file_remove( u8dir_file_t this_ )
12 : {
13 21 : U8_TRACE_BEGIN();
14 21 : assert( this_ != NULL );
15 21 : u8_error_t err = U8_ERROR_NONE;
16 :
17 21 : const int remove_err = remove( this_ );
18 21 : if ( 0 != remove_err )
19 : {
20 : /* This error may have happened on purpose or by an unexpected condition */
21 5 : if (errno == ENOENT)
22 : {
23 4 : U8_TRACE_INFO_STR( "tried to remove non-existing file:", this_ );
24 4 : U8_LOG_EVENT("remove() called on non-existing file.");
25 4 : 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 16 : U8_TRACE_INFO_STR( "removed file:", this_ );
37 16 : U8_LOG_EVENT("remove() removed a file.");
38 : }
39 :
40 21 : U8_TRACE_END_ERR(err);
41 21 : return err;
42 : }
43 :
44 2 : u8_error_t u8dir_file_get_size( u8dir_file_t this_, uint64_t* out_file_size )
45 : {
46 2 : U8_TRACE_BEGIN();
47 2 : assert( this_ != NULL );
48 2 : assert( out_file_size != NULL );
49 2 : u8_error_t err = U8_ERROR_NONE;
50 :
51 : struct stat stat_buffer;
52 2 : const int stat_err = stat( this_, &stat_buffer );
53 2 : if ( stat_err == -1 )
54 : {
55 1 : err = U8_ERROR_AT_FILE_READ;
56 : }
57 : else
58 : {
59 1 : *out_file_size = stat_buffer.st_size;
60 : }
61 :
62 2 : U8_TRACE_END_ERR(err);
63 2 : return err;
64 : }
65 :
66 3 : u8_error_t u8dir_file_get_modification_time( u8dir_file_t this_, uint64_t* out_mod_time )
67 : {
68 3 : U8_TRACE_BEGIN();
69 3 : assert( this_ != NULL );
70 3 : assert( out_mod_time != NULL );
71 3 : u8_error_t err = U8_ERROR_NONE;
72 :
73 : struct stat stat_buffer;
74 3 : const int stat_err = stat( this_, &stat_buffer );
75 3 : if ( stat_err == -1 )
76 : {
77 1 : err = U8_ERROR_AT_FILE_READ;
78 : }
79 : else
80 : {
81 : #ifdef _WIN32
82 : *out_mod_time = stat_buffer.st_mtime;
83 : #else
84 2 : *out_mod_time = stat_buffer.st_mtim.tv_sec;
85 : #endif
86 : }
87 :
88 3 : U8_TRACE_END_ERR(err);
89 3 : return err;
90 : }
91 :
92 2 : u8_error_t u8dir_file_get_creation_time( u8dir_file_t this_, uint64_t* out_create_time )
93 : {
94 2 : U8_TRACE_BEGIN();
95 2 : assert( this_ != NULL );
96 2 : assert( out_create_time != NULL );
97 2 : u8_error_t err = U8_ERROR_NONE;
98 :
99 : struct stat stat_buffer;
100 2 : const int stat_err = stat( this_, &stat_buffer );
101 2 : if ( stat_err == -1 )
102 : {
103 1 : err = U8_ERROR_AT_FILE_READ;
104 : }
105 : else
106 : {
107 : #ifdef _WIN32
108 : *out_create_time = stat_buffer.st_ctime;
109 : #else
110 1 : *out_create_time = stat_buffer.st_ctim.tv_sec;
111 : #endif
112 : }
113 :
114 2 : U8_TRACE_END_ERR(err);
115 2 : return err;
116 : }
117 :
118 :
119 : /*
120 : Copyright 2022-2025 Andreas Warnke
121 :
122 : Licensed under the Apache License, Version 2.0 (the "License");
123 : you may not use this file except in compliance with the License.
124 : You may obtain a copy of the License at
125 :
126 : http://www.apache.org/licenses/LICENSE-2.0
127 :
128 : Unless required by applicable law or agreed to in writing, software
129 : distributed under the License is distributed on an "AS IS" BASIS,
130 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131 : See the License for the specific language governing permissions and
132 : limitations under the License.
133 : */
|