From 965f5f58082ae33ca26282a0b4e17383711c4308 Mon Sep 17 00:00:00 2001 From: nishi Date: Mon, 20 May 2024 05:54:09 +0000 Subject: [PATCH] add mtime field git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@42 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Client/main.c | 7 ++++++- Library/database.c | 20 ++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Client/main.c b/Client/main.c index 0160e4c..0280ac3 100644 --- a/Client/main.c +++ b/Client/main.c @@ -34,6 +34,7 @@ #include #include #include +#include #ifdef __MINGW32__ #include @@ -118,7 +119,11 @@ int main(int argc, char** argv) { printf("Bad database file or non-existent.\n"); return 1; } - printf("Opened the database (Version %d).\n", dataworks_database_get_version(db)); + time_t mtime = (time_t)dataworks_database_get_mtime(db); + struct tm* tm = localtime(&mtime); + char mtimestr[256]; + strftime(mtimestr, 255, "%a %b %d %H:%M:%S %Z %Y", tm); + printf("Opened the database (Version %d, Modified at %s).\n", dataworks_database_get_version(db), mtimestr); printf("\n"); printf("Type a command (.help) for the help\n"); printf("\n"); diff --git a/Library/database.c b/Library/database.c index c27d2db..8a1f1ab 100644 --- a/Library/database.c +++ b/Library/database.c @@ -34,6 +34,7 @@ #include #include #include +#include const char sig[3] = {0x7f, 'D', 'W'}; @@ -48,6 +49,8 @@ int dataworks_database_create(const char* fname) { nul[0] = 0; nul[1] = 1; fwrite(nul, 1, 2, f); + uint64_t t = time(NULL); + __dw_big_endian(t, uint64_t, fwrite(__converted_ptr, 1, 8, f)); for(i = 0; i < 4096; i++) nul[i] = 0; for(i = 0; i < 256; i++) { fwrite(nul, 1, 1, f); @@ -72,23 +75,28 @@ struct dataworks_db* dataworks_database_open(const char* fname) { return NULL; } __dw_lockfile(fp); - char ptrver[2]; + char ptrver[8]; fread(ptrver, 1, 2, fp); uint16_t be_ver = *(uint16_t*)(char*)ptrver; uint16_t ver; __dw_native_endian(be_ver, uint16_t, ver = __converted); + fread(ptrver, 1, 8, fp); + uint64_t be_mtime = *(uint64_t*)(char*)ptrver; + uint64_t mtime; + __dw_native_endian(be_mtime, uint64_t, mtime = __converted); __dw_unlockfile(fp); - if(ver == 1){ + if(ver == 1) { struct dataworks_db* db = malloc(sizeof(*db)); db->fp = fp; db->version = ver; + db->mtime = mtime; return db; - }else{ + } else { fclose(fp); return NULL; } } -int dataworks_database_get_version(struct dataworks_db* db){ - return db->version; -} +int dataworks_database_get_version(struct dataworks_db* db) { return db->version; } + +uint64_t dataworks_database_get_mtime(struct dataworks_db* db) { return db->mtime; } -- 2.43.0