Skip to content

Commit

Permalink
Merge pull request #1450 from greenbone/mergify/bp/master/pr-1447
Browse files Browse the repository at this point in the history
Fix size calculation in `--optimize vacuum` (bp #1447)
  • Loading branch information
timopollmeier authored Mar 17, 2021
2 parents a13791d + fe06c79 commit 01e94f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fix SQL escaping when adding VT references [#1429](https://github.com/greenbone/gvmd/pull/1429)
- Update report run status more consistently [#1434](https://github.com/greenbone/gvmd/pull/1434)
- Improve modify_override errors, fix no NVT case [#1435](https://github.com/greenbone/gvmd/pull/1435)
- Fix size calculation in `--optimize vacuum` [#1447](https://github.com/greenbone/gvmd/pull/1447)

### Removed

Expand Down
53 changes: 20 additions & 33 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -55247,50 +55247,37 @@ manage_optimize (GSList *log_config, const db_conn_info_t *database,
ret = 0;
if (strcasecmp (name, "vacuum") == 0)
{
struct stat state;
long long int old_size, new_size;
gchar *quoted_db_name;
unsigned long long int old_size, new_size;

old_size = 0LL;
new_size = 0LL;
ret = stat (database->name, &state);
if (ret)
switch (errno)
{
case ENOENT:
break;
default:
g_warning ("%s: failed to stat database: %s",
__func__,
strerror (errno));
}
else
old_size = state.st_size;
quoted_db_name = sql_quote (sql_database ());

old_size = sql_int64_0 ("SELECT pg_database_size ('%s')",
quoted_db_name);

sql ("VACUUM;");

ret = stat (database->name, &state);
if (ret)
switch (errno)
{
case ENOENT:
break;
default:
g_warning ("%s: failed to stat database: %s",
__func__,
strerror (errno));
}
else
new_size = state.st_size;
new_size = sql_int64_0 ("SELECT pg_database_size ('%s')",
quoted_db_name);

g_free (quoted_db_name);

if (old_size && new_size)
if (old_size <= 0 || new_size <= 0)
success_text = g_strdup_printf ("Optimized: vacuum.");
else if (new_size <= old_size)
success_text = g_strdup_printf ("Optimized: vacuum."
" Database file size reduced by"
" %lld MiB (%0.1f %%).\n",
" %llu MiB (%0.1f %%).\n",
(old_size - new_size) / (1024 * 1024),
(old_size - new_size)
* 100.0 / old_size);
else
success_text = g_strdup_printf ("Optimized: vacuum.");
success_text = g_strdup_printf ("Optimized: vacuum."
" Database file size *increased* by"
" %llu MiB (%0.1f %%).\n",
(new_size - old_size) / (1024 * 1024),
(new_size - old_size)
* 100.0 / old_size);
}
else if (strcasecmp (name, "analyze") == 0)
{
Expand Down

0 comments on commit 01e94f8

Please sign in to comment.