Skip to content

Commit

Permalink
Replace fprintf's with log_error's
Browse files Browse the repository at this point in the history
git grep -l -E '%s\\n", strerror'|xargs sed -i 's/%s\\n"\(.*\), strerror([^)]*)/%m"\1/'
parallel spatch --in-place --sp-file coccinelle/fprintf.cocci ::: $(git ls-files '*.[ch]')
git grep -e 'log_error.*\\n' -l|xargs sed -i -r 's/(log_error.*) "\\n\"/\1/; s/(log_error.*)\\n/\1/;'
+ manual fixups
  • Loading branch information
keszybz committed Nov 24, 2017
1 parent e6569ac commit fb2252c
Show file tree
Hide file tree
Showing 13 changed files with 614 additions and 973 deletions.
18 changes: 18 additions & 0 deletions coccinelle/fprintf.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: LGPL-2.1+ */

@@
expression e;
local idexpression r;
expression list s;
@@
- if (e) {
- fprintf(stderr, s);
- return r;
- }
+ if (e)
+ return log_error_errno(r, s);
@@
expression list s;
@@
- fprintf(stderr, s);
+ log_error(s);
7 changes: 4 additions & 3 deletions src/cadecoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2189,7 +2189,7 @@ static int ca_decoder_parse_entry(CaDecoder *d, CaDecoderNode *n) {
break;

default:
fprintf(stderr, "Got unexpected object: %016" PRIx64 "\n", t);
log_error("Got unexpected object: %016" PRIx64, t);
return -EBADMSG;
}

Expand Down Expand Up @@ -2580,7 +2580,7 @@ static int ca_decoder_parse_filename(CaDecoder *d, CaDecoderNode *n) {
return CA_DECODER_STEP;

default:
fprintf(stderr, "Got unexpected object: %016" PRIx64 "\n", t);
log_error("Got unexpected object: %016" PRIx64, t);
return -EBADMSG;
}
}
Expand Down Expand Up @@ -3200,7 +3200,8 @@ static int ca_decoder_node_reflink(CaDecoder *d, CaDecoderNode *n) {

source_fd = ca_location_open(l);
if (source_fd == -ENOENT) {
fprintf(stderr, "Can't open reflink source %s: %s\n", ca_location_format(l), strerror(-source_fd));
log_error_errno(source_fd,
"Can't open reflink source %s: %m", ca_location_format(l));
goto next;
}
if (source_fd < 0)
Expand Down
154 changes: 54 additions & 100 deletions src/cafuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ static int iterate_until_file(CaSync *s) {
int step;

step = ca_sync_step(s);
if (step < 0) {
fprintf(stderr, "Failed to run synchronizer: %s\n", strerror(-step));
return step;
}
if (step < 0)
return log_error_errno(step, "Failed to run synchronizer: %m");

switch (step) {

case CA_SYNC_FINISHED:
fprintf(stderr, "Premature end of file.\n");
log_error("Premature end of file.");
return -EIO;

case CA_SYNC_NEXT_FILE:
Expand All @@ -61,10 +59,8 @@ static int iterate_until_file(CaSync *s) {
r = sync_poll_sigset(s);
if (r == -ESHUTDOWN) /* Quit */
return r;
if (r < 0) {
fprintf(stderr, "Failed to poll: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to poll: %m");

break;

Expand All @@ -82,10 +78,8 @@ static int seek_to_path(CaSync *s, const char *path) {
assert(path);

r = ca_sync_seek_path(s, path);
if (r < 0) {
fprintf(stderr, "Failed to seek for stat to %s: %s\n", path, strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to seek for stat to %s: %m", path);

return iterate_until_file(s);
}
Expand All @@ -103,10 +97,8 @@ static int fill_stat(CaSync *s, struct stat *stbuf) {
assert(stbuf);

r = ca_sync_current_mode(s, &mode);
if (r < 0) {
fprintf(stderr, "Failed to get current mode: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to get current mode: %m");

(void) ca_sync_current_uid(s, &uid);
(void) ca_sync_current_gid(s, &gid);
Expand Down Expand Up @@ -180,10 +172,8 @@ static int casync_readlink(
return r;

r = ca_sync_current_target(instance, &target);
if (r < 0) {
fprintf(stderr, "failed to get symlink target: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "failed to get symlink target: %m");

strncpy(ret, target, size);

Expand Down Expand Up @@ -211,25 +201,19 @@ static int casync_readdir(
return -ENOBUFS;

r = ca_sync_set_payload(instance, false);
if (r < 0) {
fprintf(stderr, "Failed to turn off payload: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to turn off payload: %m");

r = ca_sync_seek_path(instance, path);
if (r < 0) {
fprintf(stderr, "Failed to seek to path %s: %s\n", path, strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to seek to path %s: %m", path);

for (;;) {
int step;

step = ca_sync_step(instance);
if (step < 0) {
fprintf(stderr, "Failed to run synchronizer: %s\n", strerror(-step));
return step;
}
if (step < 0)
return log_error_errno(step, "Failed to run synchronizer: %m");

switch (step) {

Expand All @@ -246,10 +230,8 @@ static int casync_readdir(
}

r = ca_sync_current_path(instance, &name);
if (r < 0) {
fprintf(stderr, "Failed to get current path: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to get current path: %m");

r = fill_stat(instance, &stbuf);
if (r < 0) {
Expand All @@ -265,10 +247,8 @@ static int casync_readdir(
free(name);

r = ca_sync_seek_next_sibling(instance);
if (r < 0) {
fprintf(stderr, "Failed to seek to next sibling: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to seek to next sibling: %m");

break;
}
Expand All @@ -285,10 +265,8 @@ static int casync_readdir(
r = sync_poll_sigset(instance);
if (r == -ESHUTDOWN) /* Quit */
return r;
if (r < 0) {
fprintf(stderr, "Failed to poll: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to poll: %m");

break;

Expand Down Expand Up @@ -334,16 +312,12 @@ static int casync_read(const char *path, char *buf, size_t size, off_t offset, s
/* fprintf(stderr, "Got request for read(%s@%" PRIu64 ").\n", path, (uint64_t) offset); */

r = ca_sync_set_payload(instance, true);
if (r < 0) {
fprintf(stderr, "Failed to turn on payload: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to turn on payload: %m");

r = ca_sync_seek_path_offset(instance, path, offset);
if (r < 0) {
fprintf(stderr, "Failed to seek to path %s@%" PRIu64 ": %s\n", path, (uint64_t) offset, strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to seek to path %s@%" PRIu64 ": %m", path, (uint64_t)offset);

for (;;) {
bool eof = false;
Expand All @@ -353,10 +327,8 @@ static int casync_read(const char *path, char *buf, size_t size, off_t offset, s
break;

step = ca_sync_step(instance);
if (step < 0) {
fprintf(stderr, "Failed to run synchronizer: %s\n", strerror(-step));
return step;
}
if (step < 0)
return log_error_errno(step, "Failed to run synchronizer: %m");

switch (step) {

Expand All @@ -369,10 +341,8 @@ static int casync_read(const char *path, char *buf, size_t size, off_t offset, s
size_t n;

r = ca_sync_get_payload(instance, &p, &n);
if (r < 0) {
fprintf(stderr, "Failed to acquire payload: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to acquire payload: %m");

if (n > size)
n = size;
Expand All @@ -398,10 +368,8 @@ static int casync_read(const char *path, char *buf, size_t size, off_t offset, s
r = sync_poll_sigset(instance);
if (r == -ESHUTDOWN) /* Quit */
return r;
if (r < 0) {
fprintf(stderr, "Failed to poll: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to poll: %m");

break;

Expand Down Expand Up @@ -431,21 +399,17 @@ static int casync_statfs(const char *path, struct statvfs *sfs) {
r = ca_sync_get_archive_size(instance, &size);
if (r >= 0)
break;
if (r != -EAGAIN) {
fprintf(stderr, "Failed to acquire archive size: %s\n", strerror(-r));
return r;
}
if (r != -EAGAIN)
return log_error_errno(r, "Failed to acquire archive size: %m");

step = ca_sync_step(instance);
if (step < 0) {
fprintf(stderr, "Failed to run synchronizer: %s\n", strerror(-step));
return step;
}
if (step < 0)
return log_error_errno(step, "Failed to run synchronizer: %m");

switch (step) {

case CA_SYNC_FINISHED:
fprintf(stderr, "Premature end of file.\n");
log_error("Premature end of file.");
return -EIO;

case CA_SYNC_STEP:
Expand All @@ -462,10 +426,8 @@ static int casync_statfs(const char *path, struct statvfs *sfs) {
r = sync_poll_sigset(instance);
if (r == -ESHUTDOWN) /* Quit */
return r;
if (r < 0) {
fprintf(stderr, "Failed to poll: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to poll: %m");
break;

default:
Expand Down Expand Up @@ -643,21 +605,17 @@ static int feature_flags_warning(CaSync *s) {
r = ca_sync_get_feature_flags(s, &ff);
if (r >= 0)
break;
if (r != -ENODATA) {
fprintf(stderr, "Failed to retrieve feature flags: %m\n");
return r;
}
if (r != -ENODATA)
return log_error_errno(r, "Failed to retrieve feature flags: %m");

step = ca_sync_step(instance);
if (step < 0) {
fprintf(stderr, "Failed to run synchronizer: %s\n", strerror(-step));
return step;
}
if (step < 0)
return log_error_errno(step, "Failed to run synchronizer: %m");

switch (step) {

case CA_SYNC_FINISHED:
fprintf(stderr, "Premature end of file.\n");
log_error("Premature end of file.");
return -EIO;

case CA_SYNC_STEP:
Expand All @@ -674,10 +632,8 @@ static int feature_flags_warning(CaSync *s) {
r = sync_poll_sigset(s);
if (r == -ESHUTDOWN) /* Quit */
return r;
if (r < 0) {
fprintf(stderr, "Failed to poll: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to poll: %m");
break;

default:
Expand All @@ -690,12 +646,10 @@ static int feature_flags_warning(CaSync *s) {
return 0;

r = ca_with_feature_flags_format(unsupported, &t);
if (r < 0) {
fprintf(stderr, "Failed to format feature flags: %s\n", strerror(-r));
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to format feature flags: %m");

fprintf(stderr, "The following feature flags are not exposed in the mounted file system: %s\n", t);
log_error("The following feature flags are not exposed in the mounted file system: %s", t);
free(t);

return 0;
Expand Down Expand Up @@ -740,7 +694,7 @@ int ca_fuse_run(CaSync *s, const char *what, const char *where, bool do_mkdir) {
if (r == -ENOENT && do_mkdir) {
if (mkdir(where, 0777) < 0) {
r = -errno;
fprintf(stderr, "Failed to create mount directory %s: %s\n", where, strerror(-r));
log_error("Failed to create mount directory %s: %m", where);
goto finish;
}

Expand All @@ -750,7 +704,7 @@ int ca_fuse_run(CaSync *s, const char *what, const char *where, bool do_mkdir) {
}

if (r < 0) {
fprintf(stderr, "Failed to establish FUSE mount: %s\n", strerror(-r));
log_error("Failed to establish FUSE mount: %m");
goto finish;
}
}
Expand All @@ -759,7 +713,7 @@ int ca_fuse_run(CaSync *s, const char *what, const char *where, bool do_mkdir) {
fuse = fuse_new(fc, NULL, &ops, sizeof(ops), s);
if (!fuse) {
r = errno != 0 ? -abs(errno) : -ENOMEM;
fprintf(stderr, "Failed to allocate FUSE object: %s\n", strerror(-r));
log_error("Failed to allocate FUSE object: %m");
goto finish;
}

Expand All @@ -782,7 +736,7 @@ int ca_fuse_run(CaSync *s, const char *what, const char *where, bool do_mkdir) {

r = fuse_loop(fuse);
if (r < 0) {
fprintf(stderr, "Failed to run FUSE loop: %s\n", strerror(-r));
log_error("Failed to run FUSE loop: %m");
goto finish;
}

Expand Down
Loading

0 comments on commit fb2252c

Please sign in to comment.