forked from fxsound2/fxsound-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDate.cpp
More file actions
486 lines (396 loc) · 13.6 KB
/
Copy pathFileDate.cpp
File metadata and controls
486 lines (396 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
FxSound
Copyright (C) 2023 FxSound LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "codedefs.h"
#include <windows.h>
#include "slout.h"
#include "file.h"
#include "u_file.h"
#include "pt_defs.h"
#include "pstr.h"
/*
* FUNCTION: fileSetBackCreateTime()
* DESCRIPTION:
*
* Fill in the passed cp_fullpath string with the path to the DSP
* path, based on the passed parameters. It passes back both the
* path for the hardware DSP, and the software DSP.
*/
int PT_DECLSPEC fileSetBackCreateTime(char *cp_fullpath, long l_time_back_in_secs,
CSlout *hp_slout)
{
wchar_t wcp_fullpath[PT_MAX_PATH_STRLEN];
if(pstrConvertToWideCharString(cp_fullpath, wcp_fullpath, PT_MAX_PATH_STRLEN) != OKAY)
return(NOT_OKAY);
return(fileSetBackCreateTime_Wide(wcp_fullpath, l_time_back_in_secs, hp_slout));
}
/*
* FUNCTION: fileSetBackCreateTime_Wide()
* DESCRIPTION:
*
* Fill in the passed cp_fullpath string with the path to the DSP
* path, based on the passed parameters. It passes back both the
* path for the hardware DSP, and the software DSP.
*/
int PT_DECLSPEC fileSetBackCreateTime_Wide(wchar_t *wcp_fullpath, long l_time_back_in_secs,
CSlout *hp_slout)
{
HANDLE fileh;
FILETIME create_t, access_t, mod_t;
__int64 *newtime64;
#ifdef FILEDATE_DEBUG
char time_str[128];
time_t current_time, new_time;
time(¤t_time);
new_time = current_time - l_time_back_in_secs;
strcpy(time_str, ctime(&new_time));
#endif
/* Note- for the SetFileTime function, need to open file using the
* CreateFile call, parameterized to only open file if it exists
* and not create it.
*/
fileh = fileWin32CreateFile_Wide( wcp_fullpath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL, hp_slout);
if( fileh == INVALID_HANDLE_VALUE )
return(NOT_OKAY);
/* Both of the next functions return zero on failure */
if( GetFileTime(fileh, &create_t, &access_t, &mod_t) == 0 )
{
CloseHandle(fileh);
return(NOT_OKAY);
}
newtime64 = (__int64 *)&create_t;
/* These functions use a LARGE_INTEGER count of 1e7 ticks per second */
*newtime64 -= (__int64)l_time_back_in_secs * (__int64)10000000;
/* Both of the next functions return zero on failure */
if( SetFileTime(fileh, (FILETIME *)newtime64, (FILETIME *)newtime64, (FILETIME *)newtime64) == 0 )
{
CloseHandle(fileh);
return(NOT_OKAY);
}
if( CloseHandle(fileh) == 0 )
return(NOT_OKAY);
return(OKAY);
}
/*
* FUNCTION: fileGetModifiedDate()
* DESCRIPTION:
*
* Passes back the modified date of the passed file.
*/
int PT_DECLSPEC fileGetModifiedDate(char *cp_fullpath, FILETIME *ftp_modified_date,
CSlout *hp_slout)
{
wchar_t wcp_fullpath[PT_MAX_PATH_STRLEN];
if(pstrConvertToWideCharString(cp_fullpath, wcp_fullpath, PT_MAX_PATH_STRLEN) != OKAY)
return(NOT_OKAY);
return(fileGetModifiedDate_Wide(wcp_fullpath, ftp_modified_date, hp_slout));
}
/*
* FUNCTION: fileGetModifiedDate_Wide()
* DESCRIPTION:
*
* Passes back the modified date of the passed file.
*/
int PT_DECLSPEC fileGetModifiedDate_Wide(wchar_t *wcp_fullpath, FILETIME *ftp_modified_date,
CSlout *hp_slout)
{
HANDLE fileh;
FILETIME creation_t, access_t, modified_t;
/* Note- for the SetFileTime function, need to open file using the
* CreateFile call, parameterized to only open file if it exists
* and not create it.
*/
fileh = fileWin32CreateFile_Wide( wcp_fullpath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL, hp_slout);
if( fileh == INVALID_HANDLE_VALUE )
return(NOT_OKAY);
/* Both of the next functions return zero on failure */
if( GetFileTime(fileh, (FILETIME *)&creation_t, (FILETIME *)&access_t,
(FILETIME *)&modified_t) == 0 )
{
CloseHandle(fileh);
return(NOT_OKAY);
}
ftp_modified_date->dwHighDateTime = modified_t.dwHighDateTime;
ftp_modified_date->dwLowDateTime = modified_t.dwLowDateTime;
if( CloseHandle(fileh) == 0 )
return(NOT_OKAY);
return(OKAY);
}
/*
* FUNCTION: fileGetCreationDate()
* DESCRIPTION:
*
* Passes back the creation date of the passed file.
*/
int PT_DECLSPEC fileGetCreationDate(char *cp_fullpath, FILETIME *ftp_modified_date,
CSlout *hp_slout)
{
wchar_t wcp_fullpath[PT_MAX_PATH_STRLEN];
if(pstrConvertToWideCharString(cp_fullpath, wcp_fullpath, PT_MAX_PATH_STRLEN) != OKAY)
return(NOT_OKAY);
return(fileGetCreationDate_Wide(wcp_fullpath, ftp_modified_date, hp_slout));
}
/*
* FUNCTION: fileGetCreationDate_Wide()
* DESCRIPTION:
*
* Passes back the creation date of the passed file.
*/
int PT_DECLSPEC fileGetCreationDate_Wide(wchar_t *wcp_fullpath, FILETIME *ftp_modified_date,
CSlout *hp_slout)
{
HANDLE fileh;
FILETIME creation_t, access_t, modified_t;
/* Note- for the SetFileTime function, need to open file using the
* CreateFile call, parameterized to only open file if it exists
* and not create it.
*/
fileh = fileWin32CreateFile_Wide( wcp_fullpath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL, hp_slout);
if( fileh == INVALID_HANDLE_VALUE )
return(NOT_OKAY);
/* Both of the next functions return zero on failure */
if( GetFileTime(fileh, (FILETIME *)&creation_t, (FILETIME *)&access_t,
(FILETIME *)&modified_t) == 0 )
{
CloseHandle(fileh);
return(NOT_OKAY);
}
ftp_modified_date->dwHighDateTime = creation_t.dwHighDateTime;
ftp_modified_date->dwLowDateTime = creation_t.dwLowDateTime;
if( CloseHandle(fileh) == 0 )
return(NOT_OKAY);
return(OKAY);
}
/*
* FUNCTION: fileSetModifiedDate()
* DESCRIPTION:
*
* Sets the modified date of the passed file.
*/
int PT_DECLSPEC fileSetModifiedDate(char *cp_fullpath, FILETIME *ftp_modified_date,
CSlout *hp_slout)
{
wchar_t wcp_fullpath[PT_MAX_PATH_STRLEN];
if(pstrConvertToWideCharString(cp_fullpath, wcp_fullpath, PT_MAX_PATH_STRLEN) != OKAY)
return(NOT_OKAY);
return(fileSetModifiedDate_Wide(wcp_fullpath, ftp_modified_date, hp_slout));
}
/*
* FUNCTION: fileSetModifiedDate_Wide()
* DESCRIPTION:
*
* Sets the modified date of the passed file.
*/
int PT_DECLSPEC fileSetModifiedDate_Wide(wchar_t *wcp_fullpath, FILETIME *ftp_modified_date,
CSlout *hp_slout)
{
HANDLE fileh;
/* Note- for the SetFileTime function, need to open file using the
* CreateFile call, parameterized to only open file if it exists
* and not create it.
*/
fileh = fileWin32CreateFile_Wide( wcp_fullpath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL, hp_slout);
if( fileh == INVALID_HANDLE_VALUE )
return(NOT_OKAY);
/* Both of the next functions return zero on failure */
if( SetFileTime(fileh, ftp_modified_date, ftp_modified_date, ftp_modified_date) == 0 )
{
CloseHandle(fileh);
return(NOT_OKAY);
}
if( CloseHandle(fileh) == 0 )
return(NOT_OKAY);
return(OKAY);
}
/*
* FUNCTION: fileCompareDates()
* DESCRIPTION:
*
* Compares the modified dates of the passed two files and passes back IS_TRUE or
* IS_FALSE depending on if the first file is newer than the second.
*/
int PT_DECLSPEC fileCompareDates(char *cp_fullpath1, char *cp_fullpath2,
int *ip_first_file_newer, CSlout *hp_slout)
{
wchar_t wcp_fullpath1[PT_MAX_PATH_STRLEN];
wchar_t wcp_fullpath2[PT_MAX_PATH_STRLEN];
if(pstrConvertToWideCharString(cp_fullpath1, wcp_fullpath1, PT_MAX_PATH_STRLEN) != OKAY)
return(NOT_OKAY);
if(pstrConvertToWideCharString(cp_fullpath2, wcp_fullpath2, PT_MAX_PATH_STRLEN) != OKAY)
return(NOT_OKAY);
return(fileCompareDates_Wide(wcp_fullpath1, wcp_fullpath2, ip_first_file_newer, hp_slout));
}
/*
* FUNCTION: fileCompareDates_Wide()
* DESCRIPTION:
*
* Compares the modified dates of the passed two files and passes back IS_TRUE or
* IS_FALSE depending on if the first file is newer than the second.
*/
int PT_DECLSPEC fileCompareDates_Wide(wchar_t *wcp_fullpath1, wchar_t *wcp_fullpath2,
int *ip_first_file_newer, CSlout *hp_slout)
{
FILETIME modified_date1;
FILETIME modified_date2;
FILETIME creation_date1;
FILETIME creation_date2;
FILETIME newer_date1;
FILETIME newer_date2;
long l_result;
if (wcp_fullpath1 == NULL)
return(NOT_OKAY);
if (wcp_fullpath2 == NULL)
return(NOT_OKAY);
if (fileGetModifiedDate_Wide(wcp_fullpath1, &modified_date1, hp_slout) != OKAY)
return(NOT_OKAY);
if (fileGetModifiedDate_Wide(wcp_fullpath2, &modified_date2, hp_slout) != OKAY)
return(NOT_OKAY);
if (fileGetCreationDate_Wide(wcp_fullpath1, &creation_date1, hp_slout) != OKAY)
return(NOT_OKAY);
if (fileGetCreationDate_Wide(wcp_fullpath2, &creation_date2, hp_slout) != OKAY)
return(NOT_OKAY);
/*
* We take whatever is latest between the creation time and the modifified time as the modified date.
* This is because during installation it changes the creation date but not the modified date, but we
* want it to be considered as new.
*/
l_result = CompareFileTime(&creation_date1, &modified_date1);
if(l_result == -1) // creation_t is EARLIER then modified_t
{
newer_date1.dwHighDateTime = modified_date1.dwHighDateTime;
newer_date1.dwLowDateTime = modified_date1.dwLowDateTime;
}
else
{
newer_date1.dwHighDateTime = creation_date1.dwHighDateTime;
newer_date1.dwLowDateTime = creation_date1.dwLowDateTime;
}
l_result = CompareFileTime(&creation_date2, &modified_date2);
if(l_result == -1) // creation_t is EARLIER then modified_t
{
newer_date2.dwHighDateTime = modified_date2.dwHighDateTime;
newer_date2.dwLowDateTime = modified_date2.dwLowDateTime;
}
else
{
newer_date2.dwHighDateTime = creation_date2.dwHighDateTime;
newer_date2.dwLowDateTime = creation_date2.dwLowDateTime;
}
l_result = CompareFileTime(&newer_date1, &newer_date2);
if (l_result == -1) // newer_date1 is EARLIER then newer_date2
*ip_first_file_newer = IS_FALSE;
else
*ip_first_file_newer = IS_TRUE;
return(OKAY);
}
/*
* FUNCTION: fileTouch()
* DESCRIPTION:
*
* Changes all the dates (modified, created, accessed) for the passed file to
* the current time.
*/
int PT_DECLSPEC fileTouch(char *cp_file_fullpath, CSlout *hp_slout)
{
wchar_t wcp_file_fullpath[PT_MAX_PATH_STRLEN];
if(pstrConvertToWideCharString(cp_file_fullpath, wcp_file_fullpath, PT_MAX_PATH_STRLEN) != OKAY)
return(NOT_OKAY);
return(fileTouch_Wide(wcp_file_fullpath, hp_slout));
}
/*
* FUNCTION: fileTouch_Wide()
* DESCRIPTION:
*
* Changes all the dates (modified, created, accessed) for the passed file to
* the current time.
*/
int PT_DECLSPEC fileTouch_Wide(wchar_t *wcp_file_fullpath, CSlout *hp_slout)
{
SYSTEMTIME st_current;
HANDLE hFile;
FILETIME ft_current;
BOOL rc;
if (wcp_file_fullpath == NULL)
return(NOT_OKAY);
GetSystemTime(&st_current);
if (SystemTimeToFileTime(&st_current,&ft_current) == 0)
return(NOT_OKAY);
hFile=fileWin32CreateFile_Wide(wcp_file_fullpath,GENERIC_WRITE,FILE_SHARE_WRITE,
0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0, hp_slout);
if (hFile == INVALID_HANDLE_VALUE)
return(NOT_OKAY);
rc = SetFileTime(hFile,&ft_current,&ft_current,&ft_current);
if (rc == 0)
{
CloseHandle(hFile);
return(NOT_OKAY);
}
CloseHandle(hFile);
return(OKAY);
}
/*
* FUNCTION: fileGetModifiedDate_StringFormatted()
* DESCRIPTION:
*
* Passes back a formatted string of the modified date of the specified file.
* (Ex. "3/1/2006 7:45:11 PM")
*/
int PT_DECLSPEC fileGetModifiedDateString(char *cp_fullpath, char *cp_formatted_datetime, CSlout *hp_slout)
{
wchar_t wcp_fullpath[PT_MAX_PATH_STRLEN];
wchar_t wcp_formatted_datetime[PT_MAX_GENERIC_STRLEN];
if(pstrConvertToWideCharString(cp_fullpath, wcp_fullpath, PT_MAX_PATH_STRLEN) != OKAY)
return(NOT_OKAY);
if (fileGetModifiedDateString_Wide(wcp_fullpath, wcp_formatted_datetime, hp_slout) != OKAY)
return(NOT_OKAY);
if (pstrConvertWideCharStringToAnsiCharString(wcp_formatted_datetime,
cp_formatted_datetime,
PT_MAX_GENERIC_STRLEN) != OKAY)
return(NOT_OKAY);
return(OKAY);
}
/*
* FUNCTION: fileGetModifiedDateString_Wide()
* DESCRIPTION:
*
* Passes back a formatted string of the modified date of the specified file.
* (Ex. "3/1/2006 7:45:11 PM")
*/
int PT_DECLSPEC fileGetModifiedDateString_Wide(wchar_t *wcp_fullpath, wchar_t *wcp_formatted_datetime, CSlout *hp_slout)
{
FILETIME modified_pft;
SYSTEMTIME st;
SYSTEMTIME local_st;
wchar_t wcp_date[PT_MAX_GENERIC_STRLEN];
wchar_t wcp_time[PT_MAX_GENERIC_STRLEN];
TIME_ZONE_INFORMATION tzone;
// Get the modified date structure for the file
if (fileGetModifiedDate_Wide(wcp_fullpath, &modified_pft, hp_slout) != OKAY)
return(NOT_OKAY);
// Convert modified FILETIME to SYSTEMTIME
FileTimeToSystemTime(&modified_pft, &st);
// Get current system time zone
GetTimeZoneInformation(&tzone);
// Translate UTC SYSTEMTIME into local SYSTEMTIME
SystemTimeToTzSpecificLocalTime(&tzone, &st, &local_st);
// Format it
GetDateFormatW(LOCALE_SYSTEM_DEFAULT, LOCALE_NOUSEROVERRIDE , &local_st, NULL, wcp_date, PT_MAX_GENERIC_STRLEN);
GetTimeFormatW(LOCALE_SYSTEM_DEFAULT, LOCALE_NOUSEROVERRIDE , &local_st, NULL, wcp_time, PT_MAX_GENERIC_STRLEN);
swprintf(wcp_formatted_datetime, L"%s %s", wcp_date, wcp_time);
return(OKAY);
}