diff options
author | teor <teor2345@gmail.com> | 2017-05-28 22:21:27 +1000 |
---|---|---|
committer | teor <teor2345@gmail.com> | 2017-05-28 22:28:43 +1000 |
commit | 69b234a0a8250103e44a486c038eb5e11b3128ec (patch) | |
tree | cf62a452eb45b17b65f3a0de6125f0188160a15f /src/common/storagedir.c | |
parent | 334fe6bb6b124c2f3eb1186610999446d57cde76 (diff) | |
download | tor-69b234a0a8250103e44a486c038eb5e11b3128ec.tar.gz tor-69b234a0a8250103e44a486c038eb5e11b3128ec.zip |
Refactor storage usage reductions into a static function
No behaviour change.
Part of #22424.
Diffstat (limited to 'src/common/storagedir.c')
-rw-r--r-- | src/common/storagedir.c | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/common/storagedir.c b/src/common/storagedir.c index 532fa2a2c8..cc2230d502 100644 --- a/src/common/storagedir.c +++ b/src/common/storagedir.c @@ -406,6 +406,22 @@ storage_dir_read_labeled(storage_dir_t *dir, return result; } +/* Reduce the cached usage amount in <b>d</b> by <b>removed_file_size</b>. + * This function is a no-op if <b>d->usage_known</b> is 0. */ +static void +storage_dir_reduce_usage(storage_dir_t *d, uint64_t removed_file_size) +{ + if (d->usage_known) { + if (! BUG(d->usage < removed_file_size)) { + /* This bug can also be triggered if an external process resized a file + * between the call to storage_dir_get_usage() that last checked + * actual usage (rather than relaying on cached usage), and the call to + * this function. */ + d->usage -= removed_file_size; + } + } +} + /** * Remove the file called <b>fname</b> from <b>d</b>. */ @@ -425,9 +441,7 @@ storage_dir_remove_file(storage_dir_t *d, } } if (unlink(ipath) == 0) { - if (d->usage_known && ! BUG(d->usage < size)) { - d->usage -= size; - } + storage_dir_reduce_usage(d, size); } else { log_warn(LD_FS, "Unable to unlink %s", escaped(path)); tor_free(path); @@ -506,9 +520,7 @@ storage_dir_shrink(storage_dir_t *d, int idx = 0; while ((d->usage > target_size || min_to_remove > 0) && idx < n) { if (unlink(sandbox_intern_string(ents[idx].path)) == 0) { - if (! BUG(d->usage < ents[idx].size)) { - d->usage -= ents[idx].size; - } + storage_dir_reduce_usage(d, ents[idx].size); --min_to_remove; } ++idx; |