Use a for loop to set timestamps on UNIX.

This commit is contained in:
2021-03-30 04:42:52 +01:00
parent 9569fee882
commit e38a96b4fb
4 changed files with 89 additions and 251 deletions

View File

@@ -65,6 +65,6 @@ if(HAVE_STATFS_NAMEMAX)
add_definitions(-DUSE_STATFS_NAMEMAX)
endif()
set(UNIX_SOURCES attr.c deleted.c dirdepth.c filename.c files.c frag.c links.c os.c perms.c rsrcfork.c sparse.c time.c volume.c xattr.c)
set(UNIX_SOURCES attr.c deleted.c dirdepth.c filename.c files.c frag.c links.c os.c perms.c rsrcfork.c sparse.c time.c volume.c xattr.c time.h)
add_library(unix ${UNIX_SOURCES})

View File

@@ -30,19 +30,18 @@ Copyright (C) 2011-2021 Natalia Portillo
#include <unistd.h>
#include <utime.h>
#include "time.h"
#include "../include/defs.h"
#include "../log.h"
#include "unix.h"
void Timestamps(const char* path)
{
char message[300];
int ret;
time_t ftLastAccessTime;
time_t ftLastWriteTime;
FILE* h;
int rc, wRc, cRc, tRc;
struct utimbuf times;
int i;
ret = chdir(path);
@@ -70,243 +69,30 @@ void Timestamps(const char* path)
log_write("Creating timestamped files.\n");
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("MAXATIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
for(i = 0; i < KNOWN_UNIX_TIMES; i++)
{
memset(message, 0, 300);
sprintf(message, DATETIME_FORMAT, MAXDATETIME, "access");
times.actime = MAXTIMESTAMP;
memset(&times, 0, sizeof(struct utimbuf));
h = fopen(unix_times[i].filename, "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
times.actime = unix_times[i].access;
times.modtime = unix_times[i].modification;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fprintf(h, DATETIME_FORMAT, unix_times[i].message, unix_times[i].type);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("MAXATIME", &times);
if(ret) { tRc = errno; }
ret = utime(unix_times[i].filename, &times);
if(ret) { tRc = errno; }
}
log_write(
"\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", unix_times[i].filename, rc, wRc, cRc, tRc);
}
log_write("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "MAXATIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("MAXMTIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(message, 0, 300);
sprintf(message, DATETIME_FORMAT, MAXDATETIME, "modification");
times.modtime = MAXTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("MAXMTIME", &times);
if(ret) { tRc = errno; }
}
log_write("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "MAXMTIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("MINATIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(message, 0, 300);
sprintf(message, DATETIME_FORMAT, MINDATETIME, "access");
times.actime = MINTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("MINATIME", &times);
if(ret) { tRc = errno; }
}
log_write("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "MINATIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("MINMTIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(message, 0, 300);
sprintf(message, DATETIME_FORMAT, MINDATETIME, "modification");
times.modtime = MINTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("MINMTIME", &times);
if(ret) { tRc = errno; }
}
log_write("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "MINMTIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("Y1KATIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(message, 0, 300);
sprintf(message, DATETIME_FORMAT, Y1KDATETIME, "access");
times.actime = Y1KTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("Y1KATIME", &times);
if(ret) { tRc = errno; }
}
log_write("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "Y1KATIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("Y1KMTIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(message, 0, 300);
sprintf(message, DATETIME_FORMAT, Y1KDATETIME, "modification");
times.modtime = Y1KTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("Y1KMTIME", &times);
if(ret) { tRc = errno; }
}
log_write("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "Y1KMTIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("Y2KATIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(message, 0, 300);
sprintf(message, DATETIME_FORMAT, Y2KDATETIME, "access");
times.actime = Y2KTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("Y2KATIME", &times);
if(ret) { tRc = errno; }
}
log_write("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "Y2KATIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("Y2KMTIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(message, 0, 300);
sprintf(message, DATETIME_FORMAT, Y1KDATETIME, "modification");
times.modtime = Y2KTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("Y2KMTIME", &times);
if(ret) { tRc = errno; }
}
log_write("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "Y2KMTIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("LESSATIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(message, 0, 300);
sprintf(message, DATETIME_FORMAT, LESSDATETIME, "access");
times.actime = LESSTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("LESSATIME", &times);
if(ret) { tRc = errno; }
}
log_write("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "LESSATIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("LESSMTIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(message, 0, 300);
sprintf(message, DATETIME_FORMAT, LESSDATETIME, "modification");
times.modtime = LESSTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("LESSMTIME", &times);
if(ret) { tRc = errno; }
}
log_write("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "LESSMTIME", rc, wRc, cRc, tRc);
}

64
setter/src/unix/time.h Normal file
View File

@@ -0,0 +1,64 @@
/****************************************************************************
Aaru Data Preservation Suite
-----------------------------------------------------------------------------
Author(s) : Natalia Portillo
--[ License ] ---------------------------------------------------------------
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/>.
-----------------------------------------------------------------------------
Copyright (C) 2011-2021 Natalia Portillo
*****************************************************************************/
#ifndef AARU_FSTESTER_SETTER_SRC_UNIX_TIME_H_
#define AARU_FSTESTER_SETTER_SRC_UNIX_TIME_H_
#define DATETIME_FORMAT "This file is dated %s for %s\n"
#define LESSDATETIME "2106/02/07 06:28:15 or unknown"
#define MAXDATETIME "2038/01/19 03:14:07"
#define MINDATETIME "1970/01/01 00:00:00"
#define Y2KDATETIME "2000/01/01 00:00:00"
#define Y1KDATETIME "1999/12/31 23:59:59"
#define LESSTIMESTAMP 0xFFFFFFFF
#define MAXTIMESTAMP 0x7FFFFFFF
#define MINTIMESTAMP 0x00000000
#define Y2KTIMESTAMP 0x386D4380
#define Y1KTIMESTAMP 0x386D437F
typedef struct
{
char* filename;
time_t access;
time_t modification;
char type[13];
char message[32];
} unix_time_tests_t;
#define KNOWN_UNIX_TIMES 10
static const unix_time_tests_t unix_times[KNOWN_UNIX_TIMES] = {
"MAXATIME", MAXTIMESTAMP, 0, "access", MAXDATETIME,
"MAXMTIME", 0, MAXTIMESTAMP, "modification", MAXDATETIME,
"MINATIME", MINTIMESTAMP, 0, "access", MINDATETIME,
"MINMTIME", 0, MINTIMESTAMP, "modification", MINDATETIME,
"Y1KATIME", Y1KTIMESTAMP, 0, "access", Y1KDATETIME,
"Y1KMTIME", 0, Y1KTIMESTAMP, "modification", Y1KDATETIME,
"Y2KATIME", Y2KTIMESTAMP, 0, "access", Y2KDATETIME,
"Y2KMTIME", 0, Y2KTIMESTAMP, "modification", Y2KDATETIME,
"LESSATIME", LESSTIMESTAMP, 0, "access", LESSDATETIME,
"LESSMTIME", 0, LESSTIMESTAMP, "modification", LESSDATETIME
};
#endif // AARU_FSTESTER_SETTER_SRC_UNIX_TIME_H_

View File

@@ -349,16 +349,4 @@ Copyright (C) 2011-2021 Natalia Portillo
#define _XIAFS_SUPER_MAGIC 0x012fd16d
#endif
#define DATETIME_FORMAT "This file is dated %s for %s\n"
#define LESSDATETIME "2106/02/07 06:28:15 or unknown"
#define MAXDATETIME "2038/01/19 03:14:07"
#define MINDATETIME "1970/01/01 00:00:00"
#define Y2KDATETIME "2000/01/01 00:00:00"
#define Y1KDATETIME "1999/12/31 23:59:59"
#define LESSTIMESTAMP 0xFFFFFFFF
#define MAXTIMESTAMP 0x7FFFFFFF
#define MINTIMESTAMP 0x00000000
#define Y2KTIMESTAMP 0x386D4380
#define Y1KTIMESTAMP 0x386D437F
#endif // AARU_FSTESTER_SETTER_UNIX_H