Skip to content
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

Add 'swupd clean' command #383

Merged
merged 1 commit into from
Feb 27, 2018
Merged

Add 'swupd clean' command #383

merged 1 commit into from
Feb 27, 2018

Conversation

cmarcelo
Copy link
Contributor

This command removes old files from statedir. The interface is a bit
opaque so there will be room to modify the policy later.

By default it will delete staged files that are older than
DAYS_TO_KEEP_FILES. For manifests this heuristic is not good as older
manifests might still be used for the current OS version being run, so
also ensure that those do not get deleted. This prevents 'swupd
search' to redownload files.

The default behavior should be suitable to use in combination with
automatic updates, to control the size of state dir. There is also an
--all option to remove all the state files regardless of dates and
usage.

To avoid "disasters" in case some paths are set wrong, the command
explicitly delete patterns of files create by swupd.

@cmarcelo
Copy link
Contributor Author

This is missing Manifest*delta in statedir files. I'll add those later if the idea gets accepted.

@matthewrsj
Copy link
Contributor

@cmarcelo we have conflicts here.

@cmarcelo
Copy link
Contributor Author

Ping

Copy link

@icarus-sparry icarus-sparry left a comment

Choose a reason for hiding this comment

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

Need to fix the coredump and the free_string. Others are optional.

src/clean.c Outdated
struct stat stat;
ret = lstat(file, &stat);
if (ret != 0) {
fprintf(stderr, "couldn't remove file %s: %s\n", file, strerror(errno));

Choose a reason for hiding this comment

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

Misleading error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to "couldn't access" file.

src/clean.c Outdated
if (ret == 0) {
stats.files_removed++;
}
next:

Choose a reason for hiding this comment

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

If you moved "file" out one level of scopt, you could move these 2 lines to the start of the loop and replace "goto next" with "continue".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

src/clean.c Outdated
dir = opendir(path);
if (!dir) {
ret = errno;
goto end;

Choose a reason for hiding this comment

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

"goto end" will cause a segmentation violation.

Just "return errno", and remove the "end:" label.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

src/clean.c Outdated

static bool is_all_digits(const char *s)
{
while (*s) {

Choose a reason for hiding this comment

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

I know I am not going to win the argument, but this should be written as a for loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean for (; *s; s++) or taking the length and using indices?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to for (; *s; s++).

Choose a reason for hiding this comment

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

Thank you.

src/clean.c Outdated
static const size_t prefix_len = sizeof(prefix) - 1;

const char *name = entry->d_name;
if (strlen(name) < prefix_len) {

Choose a reason for hiding this comment

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

I would have used strncmp rather than the 2 separate tests and to show that we are working on string data.

You only need the strlen(name) < prefix_len test to make sure you are not reading beyond the end of the name array,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is much better! Done.

src/clean.c Outdated
goto close_then_end;
}

char *tmp = malloc(size + 1);

Choose a reason for hiding this comment

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

Not sure having tmp adds anything here rather than using contents. The compiler will probably optimize tmp out anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Control flow changed a bit so tmp is gone. It was an attempt to not "fixing" contents for the return call, but had issues.

src/clean.c Outdated

ret = fread(tmp, size, 1, f);
if (ret != 1) {
free_string(&tmp);

Choose a reason for hiding this comment

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

should call free, not free_string. Yes we know how free_string works, and it will do the right thing, but it is breaking the abstraction. One day we might make string_or_die use alloca and make free_string just set the pointer to NULL at which time this will turn into a memory leak.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

src/clean.c Outdated
goto close_then_end;
}

contents = tmp;

Choose a reason for hiding this comment

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

You need to add the terminating NUL to the data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Done.

src/clean.c Outdated
}
}

while (true) {

Choose a reason for hiding this comment

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

while (ret == 0) perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Kept as is. It did not look very good in practice since the test doesn't cover all the breaking conditions (we have a case where the stream is over, and ret == 0).

src/clean.c Outdated
}

end:
free_string(&mom_contents);

Choose a reason for hiding this comment

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

free(mom_contents) rather than free_string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link

@icarus-sparry icarus-sparry left a comment

Choose a reason for hiding this comment

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

Oh and the terminating NUL as well please

@cmarcelo
Copy link
Contributor Author

This is missing Manifest*delta in statedir files.

Removing those now. One improvement would be always remove those regardless of the date.

@cmarcelo
Copy link
Contributor Author

Thanks for the review, Icarus.

if (errno) {
ret = errno;
}
break;

Choose a reason for hiding this comment

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

Optional. Since this is the only break, you could put the closedir() and return here.

Probably not worth it.

ret = unlink(file);
}
if (ret != 0) {
fprintf(stderr, "couldn't remove file %s: %s\n", file, strerror(errno));

Choose a reason for hiding this comment

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

Possibly remove the word "file" from the error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

This command removes old files from statedir. The interface is a bit
opaque so there will be room to modify the policy later.

By default it will delete staged files that are older than
DAYS_TO_KEEP_FILES. For manifests this heuristic is not good as older
manifests might still be used for the current OS version being run, so
also ensure that those do not get deleted. This prevents 'swupd
search' to redownload files.

The default behavior should be suitable to use in combination with
automatic updates, to control the size of state dir. There is also an
--all option to remove all the state files regardless of dates and
usage.

To avoid "disasters" in case some paths are set wrong, the command
explicitly delete patterns of files create by swupd.
Copy link
Contributor

@matthewrsj matthewrsj left a comment

Choose a reason for hiding this comment

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

+1, good code and good interface. Thanks @cmarcelo

@matthewrsj matthewrsj merged commit fe6040c into clearlinux:master Feb 27, 2018
@cmarcelo cmarcelo deleted the clean branch March 26, 2018 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants