Repository navigation
Feature/installdate module #2764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #pragma once | ||
|
|
||
| #include "fastfetch.h" | ||
|
|
||
| typedef struct FFInstallDateResult { | ||
| uint64_t installTime; // milliseconds since epoch | ||
| const char* source; // which probe succeeded | ||
| } FFInstallDateResult; | ||
|
|
||
| const char* ffDetectInstallDate(FFInstallDateResult* result); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| #include "installdate.h" | ||
| #include "common/debug.h" | ||
| #include "common/io.h" | ||
|
|
||
| #include <sys/stat.h> | ||
|
|
||
| #if defined(STATX_BTIME) && !defined(__ANDROID__) | ||
| #include <sys/syscall.h> | ||
| #endif | ||
|
|
||
| /* | ||
| * Detection strategy (priority cascade): | ||
| * | ||
| * 1. statx() birth time of /etc/machine-id | ||
| * The most reliable signal: machine-id is generated once at install time | ||
| * (by systemd-machine-id-setup or equivalent) and never rewritten. | ||
| * statx() with STATX_BTIME retrieves the inode birth timestamp, which | ||
| * survives file copies and permission changes — unlike mtime or ctime. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't think so. Copying files recreates inodes. |
||
| * | ||
| * 2. mtime of /etc/machine-id | ||
| * Fallback for filesystems that do not support birth time (ext3, old XFS). | ||
| * mtime reflects the last write, which for machine-id is typically the | ||
| * initial generation at install time. | ||
| * | ||
| * 3. mtime of /var/log/installer (Debian/Ubuntu) or /root/anaconda-ks.cfg (RHEL/Fedora) | ||
| * Installer artifacts left by distro-specific installers. Less universal | ||
| * but useful when machine-id has been regenerated. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if machine-id has been regenerated, it's still there, and statx(machine-id) still works, so |
||
| */ | ||
|
|
||
| #ifdef SYS_statx | ||
| static bool tryStatxBtime(const char* path, uint64_t* resultMs, const char** source) | ||
| { | ||
| struct statx stx; | ||
| if (syscall(SYS_statx, 0, path, 0, STATX_BTIME, &stx) == 0 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This breaks Android. |
||
| && (stx.stx_mask & STATX_BTIME) | ||
| && stx.stx_btime.tv_sec > 685065600 /* 1991-09-17, birth of Linux */) | ||
| { | ||
| *resultMs = (uint64_t) stx.stx_btime.tv_sec * 1000 + (uint64_t) stx.stx_btime.tv_nsec / 1000000; | ||
| *source = path; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| #endif | ||
|
|
||
| static bool tryMtime(const char* path, uint64_t* resultMs, const char** source) | ||
| { | ||
| struct stat st; | ||
| if (stat(path, &st) == 0 && st.st_mtime > 685065600) | ||
| { | ||
| *resultMs = (uint64_t) st.st_mtime * 1000; | ||
| *source = path; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| const char* ffDetectInstallDate(FFInstallDateResult* result) | ||
| { | ||
| static const char* candidates[] = { | ||
| "/etc/machine-id", | ||
| "/var/log/installer", | ||
| "/root/anaconda-ks.cfg", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This requires root |
||
| }; | ||
|
|
||
| // Probe 1: statx birth time (best signal — inode creation timestamp) | ||
| #ifdef SYS_statx | ||
| for (uint32_t i = 0; i < ARRAY_SIZE(candidates); ++i) | ||
| { | ||
| if (tryStatxBtime(candidates[i], &result->installTime, &result->source)) | ||
| { | ||
| FF_DEBUG("Install date detected via statx btime of %s", result->source); | ||
| return nullptr; | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| // Probe 2: mtime fallback (for filesystems without birth time support) | ||
| for (uint32_t i = 0; i < ARRAY_SIZE(candidates); ++i) | ||
| { | ||
| if (tryMtime(candidates[i], &result->installTime, &result->source)) | ||
| { | ||
| FF_DEBUG("Install date detected via mtime of %s", result->source); | ||
| return nullptr; | ||
| } | ||
| } | ||
|
|
||
| FF_DEBUG("Install date: all probes failed"); | ||
| return "No install date could be determined"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #include "installdate.h" | ||
|
|
||
| const char* ffDetectInstallDate([[maybe_unused]] FFInstallDateResult* result) { | ||
| return "Not supported on this platform"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| #include "common/printing.h" | ||
| #include "common/jsonconfig.h" | ||
| #include "common/time.h" | ||
| #include "detection/installdate/installdate.h" | ||
| #include "modules/installdate/installdate.h" | ||
|
|
||
| bool ffPrintInstallDate(FFInstallDateOptions* options) { | ||
| FFInstallDateResult result = {}; | ||
|
|
||
| const char* error = ffDetectInstallDate(&result); | ||
|
|
||
| if (error) { | ||
| ffPrintError(FF_MODULE_GET_DISPLAY_NAME(InstallDate), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); | ||
| return false; | ||
| } | ||
|
|
||
| uint64_t nowMs = ffTimeGetNow(); | ||
| FFTimeGetAgeResult age = ffTimeGetAge(result.installTime, nowMs); | ||
|
|
||
| if (options->moduleArgs.outputFormat.length == 0) { | ||
| ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(InstallDate), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); | ||
|
|
||
| // Default output: "X years, Y days (since YYYY-MM-DD)" | ||
| if (age.years > 0) { | ||
| printf("%u year%s, %u day%s", age.years, age.years == 1 ? "" : "s", age.daysOfYear, age.daysOfYear == 1 ? "" : "s"); | ||
| } else { | ||
| printf("%u day%s", age.daysOfYear, age.daysOfYear == 1 ? "" : "s"); | ||
| } | ||
| printf(" (since %s)\n", ffTimeToShortStr(result.installTime)); | ||
| } else { | ||
| FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(InstallDate), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { | ||
| FF_ARG(age.years, "years"), | ||
| FF_ARG(age.daysOfYear, "days-of-year"), | ||
| FF_ARG(age.yearsFraction, "years-fraction"), | ||
| { FF_ARG_TYPE_STRING, ffTimeToShortStr(result.installTime), "install-date" }, | ||
| { FF_ARG_TYPE_STRING, ffTimeToFullStr(result.installTime), "install-date-full" }, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ffTimeToShortStr and ffTimeToFullStr share the same static buffer, so in this case they are always the same. |
||
| { FF_ARG_TYPE_STRING, result.source, "source" }, | ||
| })); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void ffParseInstallDateJsonObject(FFInstallDateOptions* options, yyjson_val* module) { | ||
| yyjson_val *key, *val; | ||
| size_t idx, max; | ||
| yyjson_obj_foreach (module, idx, max, key, val) { | ||
| if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) { | ||
| continue; | ||
| } | ||
|
|
||
| ffPrintError(FF_MODULE_GET_DISPLAY_NAME(InstallDate), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key)); | ||
| } | ||
| } | ||
|
|
||
| [[gnu::cold]] | ||
| void ffGenerateInstallDateJsonConfig(FFInstallDateOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { | ||
| ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); | ||
| } | ||
|
|
||
| bool ffGenerateInstallDateJsonResult([[maybe_unused]] FFInstallDateOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { | ||
| FFInstallDateResult result = {}; | ||
| const char* error = ffDetectInstallDate(&result); | ||
|
|
||
| if (error) { | ||
| yyjson_mut_obj_add_str(doc, module, "error", error); | ||
| return false; | ||
| } | ||
|
|
||
| yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); | ||
| yyjson_mut_obj_add_uint(doc, obj, "installTime", result.installTime); | ||
| yyjson_mut_obj_add_strcpy(doc, obj, "installDate", ffTimeToFullStr(result.installTime)); | ||
| yyjson_mut_obj_add_str(doc, obj, "source", result.source); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void ffInitInstallDateOptions(FFInstallDateOptions* options) { | ||
| ffOptionInitModuleArg(&options->moduleArgs, ""); | ||
| } | ||
|
|
||
| void ffDestroyInstallDateOptions(FFInstallDateOptions* options) { | ||
| ffOptionDestroyModuleArg(&options->moduleArgs); | ||
| } | ||
|
|
||
| FFModuleBaseInfo ffInstallDateModuleInfo = { | ||
| .name = "InstallDate", | ||
| .description = "Print when the operating system was installed and how long ago", | ||
| .displayName = { | ||
| .en = "Install Date", | ||
| .ar = "تاريخ التثبيت", | ||
| .cs = "Datum instalace", | ||
| .de = "Installationsdatum", | ||
| .es = "Fecha de instalación", | ||
| .fi = "Asennuspäivä", | ||
| .fr = "Date d'installation", | ||
| .gl = "Data de instalación", | ||
| .he = "תאריך התקנה", | ||
| .id = "Tanggal Instalasi", | ||
| .it = "Data di installazione", | ||
| .ja = "インストール日", | ||
| .ko = "설치 날짜", | ||
| .pl = "Data instalacji", | ||
| .pt = "Data de instalação", | ||
| .ru = "Дата установки", | ||
| .tr = "Kurulum Tarihi", | ||
| .uk = "Дата встановлення", | ||
| .vi = "Ngày cài đặt", | ||
| .zh_CN = "安装日期", | ||
| .zh_TW = "安裝日期", | ||
| }, | ||
| .initOptions = (void*) ffInitInstallDateOptions, | ||
| .destroyOptions = (void*) ffDestroyInstallDateOptions, | ||
| .parseJsonObject = (void*) ffParseInstallDateJsonObject, | ||
| .printModule = (void*) ffPrintInstallDate, | ||
| .generateJsonResult = (void*) ffGenerateInstallDateJsonResult, | ||
| .generateJsonConfig = (void*) ffGenerateInstallDateJsonConfig, | ||
| .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) { | ||
| { "Years since install", "years" }, | ||
| { "Days of year since install", "days-of-year" }, | ||
| { "Years fraction since install", "years-fraction" }, | ||
| { "Install date in short format", "install-date" }, | ||
| { "Install date in full format", "install-date-full" }, | ||
| { "Detection source path", "source" }, | ||
| })), | ||
| .defaultOrder = 12, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #pragma once | ||
|
|
||
| #include "option.h" | ||
|
|
||
| bool ffPrintInstallDate(FFInstallDateOptions* options); | ||
| void ffInitInstallDateOptions(FFInstallDateOptions* options); | ||
| void ffDestroyInstallDateOptions(FFInstallDateOptions* options); | ||
|
|
||
| extern FFModuleBaseInfo ffInstallDateModuleInfo; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #pragma once | ||
|
|
||
| #include "common/option.h" | ||
|
|
||
| typedef struct FFInstallDateOptions { | ||
| FFModuleArgs moduleArgs; | ||
| } FFInstallDateOptions; | ||
|
|
||
| static_assert(sizeof(FFInstallDateOptions) <= FF_OPTION_MAX_SIZE, "FFInstallDateOptions size exceeds maximum allowed size"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this works only on systemd distros.