FIO: Add support to set file access and modify times

This commit is contained in:
Mark Pizzolato 2021-10-31 04:20:39 -07:00
parent 0bc929222f
commit 079e4780d2
3 changed files with 54 additions and 2 deletions

1
scp.c
View file

@ -7145,6 +7145,7 @@ if (strcmp (ctx->LastDir, directory)) {
strcpy (ctx->LastDir, directory);
}
local = localtime (&filestat->st_mtime);
if (local)
sim_printf ("%02d/%02d/%04d %02d:%02d %s ", local->tm_mon+1, local->tm_mday, 1900+local->tm_year, local->tm_hour%12, local->tm_min, (local->tm_hour >= 12) ? "PM" : "AM");
if (filestat->st_mode & S_IFDIR) {
++ctx->DirCount;

View file

@ -508,6 +508,40 @@ if (CopyFileA (sourcename, destname, !overwrite_existing))
return sim_messagef (SCPE_ARG, "Error Copying '%s' to '%s': %s\n", source_file, dest_file, sim_get_os_error_text (GetLastError ()));
}
static void _time_t_to_filetime (time_t ttime, FILETIME *filetime)
{
t_uint64 time64;
time64 = 134774; /* Days betwen Jan 1, 1601 and Jan 1, 1970 */
time64 *= 24; /* Hours */
time64 *= 3600; /* Seconds */
time64 += (t_uint64)ttime; /* include time_t seconds */
time64 *= 10000000; /* Convert seconds to 100ns units */
filetime->dwLowDateTime = (DWORD)time64;
filetime->dwHighDateTime = (DWORD)(time64 >> 32);
}
t_stat sim_set_file_times (const char *file_name, time_t access_time, time_t write_time)
{
char filename[PATH_MAX + 1];
FILETIME accesstime, writetime;
HANDLE hFile;
BOOL bStat;
_time_t_to_filetime (access_time, &accesstime);
_time_t_to_filetime (write_time, &writetime);
if (NULL == _sim_expand_homedir (file_name, filename, sizeof (filename)))
return sim_messagef (SCPE_ARG, "Error Setting File Times - Problem Source Filename '%s'\n", filename);
hFile = CreateFileA (filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return sim_messagef (SCPE_ARG, "Can't open file '%s' to set it's times: %s\n", filename, sim_get_os_error_text (GetLastError ()));
bStat = SetFileTime (hFile, NULL, &accesstime, &writetime);
CloseHandle (hFile);
return bStat ? SCPE_OK : sim_messagef (SCPE_ARG, "Error setting file '%s' times: %s\n", filename, sim_get_os_error_text (GetLastError ()));
}
#include <io.h>
#include <direct.h>
int sim_set_fsize (FILE *fptr, t_addr size)
@ -611,7 +645,7 @@ return ftruncate(fileno(fptr), (off_t)size);
#include <sys/stat.h>
#include <fcntl.h>
#if HAVE_UTIME
#if defined (HAVE_UTIME)
#include <utime.h>
#endif
@ -666,6 +700,22 @@ if (st == SCPE_OK) {
return st;
}
t_stat sim_set_file_times (const char *file_name, time_t access_time, time_t write_time)
{
t_stat st = SCPE_IOERR;
#if defined (HAVE_UTIME)
struct utimbuf utim;
utim.actime = access_time;
utim.modtime = write_time;
if (!utime (file_name, &utim))
st = SCPE_OK;
#else
st = SCPE_NOFNC;
#endif
return st;
}
int sim_set_fifo_nonblock (FILE *fptr)
{
struct stat stbuf;

View file

@ -64,6 +64,7 @@ int sim_fseek (FILE *st, t_addr offset, int whence);
int sim_fseeko (FILE *st, t_offset offset, int whence);
t_bool sim_can_seek (FILE *st);
int sim_set_fsize (FILE *fptr, t_addr size);
t_stat sim_set_file_times (const char *file_name, time_t access_time, time_t write_time);
int sim_set_fifo_nonblock (FILE *fptr);
size_t sim_fread (void *bptr, size_t size, size_t count, FILE *fptr);
size_t sim_fwrite (const void *bptr, size_t size, size_t count, FILE *fptr);