Skip to content

CFE-2815: Added isnewerthantime function#5799

Merged
nickanderson merged 2 commits into
cfengine:masterfrom
jakub-nt:CFE-2815
May 28, 2025
Merged

CFE-2815: Added isnewerthantime function#5799
nickanderson merged 2 commits into
cfengine:masterfrom
jakub-nt:CFE-2815

Conversation

@jakub-nt

@jakub-nt jakub-nt commented May 8, 2025

Copy link
Copy Markdown
Contributor

corresponding docs PR: cfengine/documentation#3430

@CLAassistant

CLAassistant commented May 8, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

int exit_code = stat(arg_file, &file_buf);

if (exit_code == -1)
{

Check warning

Code scanning / CodeQL

Poorly documented large function Warning

Poorly documented function: fewer than 2% comments for a function of 162 lines.

time_t file_mtime = file_buf.st_mtime;

bool result = file_mtime > arg_mtime;

Check notice

Code scanning / CodeQL

Pointer argument is dereferenced without checking for NULL Note

Parameter finalargs in FnCallSelectServers() is dereferenced without an explicit null-check
time_t file_mtime = file_buf.st_mtime;

bool result = file_mtime > arg_mtime;

Check notice

Code scanning / CodeQL

Pointer argument is dereferenced without checking for NULL Note

Parameter finalargs in FnCallSelectServers() is dereferenced without an explicit null-check

bool result = file_mtime > arg_mtime;

return FnReturnContext(result);

Check notice

Code scanning / CodeQL

Pointer argument is dereferenced without checking for NULL Note

Parameter finalargs in FnCallSelectServers() is dereferenced without an explicit null-check
bool result = file_mtime > arg_mtime;

return FnReturnContext(result);
}

Check notice

Code scanning / CodeQL

Pointer argument is dereferenced without checking for NULL Note

Parameter finalargs in FnCallSelectServers() is dereferenced without an explicit null-check

return FnReturnContext(result);
}

Check notice

Code scanning / CodeQL

Pointer argument is dereferenced without checking for NULL Note

Parameter finalargs in FnCallSelectServers() is dereferenced without an explicit null-check
@larsewi larsewi requested review from larsewi and victormlg May 8, 2025 17:53

@larsewi larsewi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Since the title will be part of the changelog you can make it a little bit more specific. E.g., "Added isnewerthantime() policy function". Also backticks in the changelog can cause som issues later on. So better omit them in the case of changelog entries.

It would also be nice if you included an acceptance test. Here you can add more test cases that are not suitable in an example. E.g., what happens if the timestamp is not a string, what happens in the timestamp is CF_NOINT, what happens if timestamp is CF_INFINITY etc.

Comment thread examples/isnewerthan.cf
Comment thread libpromises/evalfunction.c
Comment thread libpromises/evalfunction.c Outdated
Comment thread libpromises/evalfunction.c Outdated
@larsewi

larsewi commented May 12, 2025

Copy link
Copy Markdown
Contributor

You will also have to sign the CLA #5799 (comment)

Changelog: None
Signed-off-by: jakub-nt <175944085+jakub-nt@users.noreply.github.com>
Ticket: CFE-2815

Changelog: Title
Signed-off-by: jakub-nt <175944085+jakub-nt@users.noreply.github.com>

@victormlg victormlg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment thread libpromises/evalfunction.c

if (exit_code == -1)
{
return FnFailure();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could add some user friendly error here?

@olehermanse olehermanse May 22, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it's a good idea to add helpful error messages, but looking at the other similar policy functions below, this seems consistent with them - they also return FnFailure() without printing an error, when stat fails, indicating:

  1. Maybe it's desirable to not print anything? Would adding errors become too noisy?
  2. Maybe it's intentional, since something else is responsible for printing the error automatically when you return FnFailure()?
  3. Maybe it is really a mistake, and we should update all of these to have error messages?

I haven't looked into it further, so I don't know the answer. If you find out, and it's 1. or 2., probably a comment would be nice instead, or a debug level message, or both.

@larsewi larsewi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 🚀

Comment on lines +3 to +4
inputs => { "../../default.cf.sub" };
bundlesequence => { default("$(this.promise_filename)") };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are not using anything from default.cf.sub you can consider not including it and instead write your own bundle sequence.

Suggested change
inputs => { "../../default.cf.sub" };
bundlesequence => { default("$(this.promise_filename)") };
bundlesequence => { "check" };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, you can omit body common control and just have bundle agent main and then use methods promises to call each of the other bundles you want.

bundle agent __main__
{
  methods:
    "init";
    "test";
    "check";
    "cleanup";

So, many ways to skin the cat.

{
vars:
"test_file"
string => "$(this.promise_dirname)/isnewerthantime_file.txt";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can consider creating this file in an init bundle and setting the mtime, all using the files promise.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation is using the derived mtime via filestat()

@nickanderson nickanderson merged commit a7dac04 into cfengine:master May 28, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

8 participants