Prefer signed types in blocking_read etc

* src/compare.c (process_noop, process_rawdata):
Return bool, not int.
* src/compare.c (process_noop, process_rawdata):
* src/create.c (dump_regular_file):
* src/extract.c (extract_file):
* src/misc.c (blocking_read, blocking_write):
* src/sparse.c (sparse_scan_file_raw, sparse_extract_region):
Prefer signed types like idx_t to unsigned ones like size_t.
(sparse_scan_file_raw): Diagnose read errors.
This commit is contained in:
Paul Eggert
2024-08-09 00:22:53 -07:00
parent 88c2aa1616
commit b3992e4ef8
6 changed files with 62 additions and 57 deletions

View File

@@ -740,8 +740,8 @@ void undo_last_backup (void);
int deref_stat (char const *name, struct stat *buf);
size_t blocking_read (int fd, void *buf, size_t count);
size_t blocking_write (int fd, void const *buf, size_t count);
ptrdiff_t blocking_read (int fd, void *buf, idx_t count);
idx_t blocking_write (int fd, void const *buf, idx_t count);
extern idx_t chdir_current;
extern int chdir_fd;
@@ -1031,7 +1031,7 @@ extern int warning_option;
#define WARNOPT(opt,args) \
do \
{ \
if (WARNING_ENABLED(opt)) WARN (args); \
if (WARNING_ENABLED (opt)) WARN (args); \
} \
while (0)

View File

@@ -76,20 +76,20 @@ report_difference (struct tar_stat_info *st, const char *fmt, ...)
}
/* Take a buffer returned by read_and_process and do nothing with it. */
static int
process_noop (MAYBE_UNUSED size_t size, MAYBE_UNUSED char *data)
static bool
process_noop (MAYBE_UNUSED idx_t size, MAYBE_UNUSED char *data)
{
return 1;
return true;
}
static int
process_rawdata (size_t bytes, char *buffer)
static bool
process_rawdata (idx_t bytes, char *buffer)
{
size_t status = blocking_read (diff_handle, diff_buffer, bytes);
ptrdiff_t status = blocking_read (diff_handle, diff_buffer, bytes);
if (status != bytes)
{
if (status == SAFE_READ_ERROR)
if (status < 0)
{
read_error (current_stat_info.file_name);
report_difference (&current_stat_info, NULL);
@@ -97,31 +97,31 @@ process_rawdata (size_t bytes, char *buffer)
else
{
report_difference (&current_stat_info,
ngettext ("Could only read %lu of %lu byte",
"Could only read %lu of %lu bytes",
ngettext ("Could read only %td of %td byte",
"Could read only %td of %td bytes",
bytes),
(unsigned long) status, (unsigned long) bytes);
status, bytes);
}
return 0;
return false;
}
if (memcmp (buffer, diff_buffer, bytes))
{
report_difference (&current_stat_info, _("Contents differ"));
return 0;
return false;
}
return 1;
return true;
}
/* Some other routine wants SIZE bytes in the archive. For each chunk
of the archive, call PROCESSOR with the size of the chunk, and the
address of the chunk it can work with. The PROCESSOR should return
nonzero for success. Once it returns error, continue skipping
without calling PROCESSOR anymore. */
address of the chunk it can work with. PROCESSOR should return
true for success. Once it fails, continue skipping without calling
PROCESSOR anymore. */
static void
read_and_process (struct tar_stat_info *st, int (*processor) (size_t, char *))
read_and_process (struct tar_stat_info *st, bool (*processor) (idx_t, char *))
{
union block *data_block;
size_t data_size;

View File

@@ -1047,28 +1047,32 @@ dump_regular_file (int fd, struct tar_stat_info *st)
mv_begin_write (st->file_name, st->stat.st_size, st->stat.st_size);
while (size_left > 0)
{
size_t bufsize, count;
blk = find_next_block ();
bufsize = available_space_after (blk);
idx_t bufsize = available_space_after (blk);
if (size_left < bufsize)
{
/* Last read -- zero out area beyond. */
bufsize = size_left;
count = bufsize % BLOCKSIZE;
if (count)
memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
idx_t beyond = bufsize % BLOCKSIZE;
if (beyond)
memset (blk->buffer + size_left, 0, BLOCKSIZE - beyond);
}
count = (fd <= 0) ? bufsize : blocking_read (fd, blk->buffer, bufsize);
if (count == SAFE_READ_ERROR)
ptrdiff_t count;
if (fd <= 0)
count = bufsize;
else
{
read_diag_details (st->orig_file_name,
st->stat.st_size - size_left, bufsize);
pad_archive (size_left);
return dump_status_short;
count = blocking_read (fd, blk->buffer, bufsize);
if (count < 0)
{
read_diag_details (st->orig_file_name,
st->stat.st_size - size_left, bufsize);
pad_archive (size_left);
return dump_status_short;
}
}
size_left -= count;
set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);

View File

@@ -1281,7 +1281,6 @@ extract_file (char *file_name, int typeflag)
off_t size;
union block *data_block;
int status;
size_t count;
size_t written;
bool interdir_made = false;
mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
@@ -1351,7 +1350,7 @@ extract_file (char *file_name, int typeflag)
if (written > size)
written = size;
errno = 0;
count = blocking_write (fd, data_block->buffer, written);
idx_t count = blocking_write (fd, data_block->buffer, written);
size -= written;
set_next_block_after ((union block *)

View File

@@ -843,9 +843,9 @@ deref_stat (char const *name, struct stat *buf)
opened O_NONBLOCK for security reasons, and on some file systems
this can cause read to fail with errno == EAGAIN. Return the
actual number of bytes read, zero for EOF, or
SAFE_READ_ERROR upon error. */
size_t
blocking_read (int fd, void *buf, size_t count)
-1 upon error. */
ptrdiff_t
blocking_read (int fd, void *buf, idx_t count)
{
size_t bytes = full_read (fd, buf, count);
@@ -859,9 +859,7 @@ blocking_read (int fd, void *buf, size_t count)
}
#endif
if (bytes == 0 && errno != 0)
bytes = SAFE_READ_ERROR;
return bytes;
return bytes == SAFE_READ_ERROR || (bytes == 0 && errno != 0) ? -1 : bytes;
}
/* Write to FD from the buffer BUF with COUNT bytes. Do a full write.
@@ -869,11 +867,11 @@ blocking_read (int fd, void *buf, size_t count)
files are opened O_NONBLOCK for security reasons, and on some file
systems this can cause write to fail with errno == EAGAIN. Return
the actual number of bytes written, setting errno if that is less
than COUNT. */
size_t
blocking_write (int fd, void const *buf, size_t count)
than COUNT. Return -1 on write error. */
idx_t
blocking_write (int fd, void const *buf, idx_t count)
{
size_t bytes = full_write (fd, buf, count);
idx_t bytes = full_write (fd, buf, count);
#if defined F_SETFL && O_NONBLOCK
if (bytes < count && errno == EAGAIN)
@@ -1118,7 +1116,7 @@ close_diag (char const *name)
{
if (ignore_failed_read_option)
{
if (WARNING_ENABLED(WARN_FAILED_READ))
if (WARNING_ENABLED (WARN_FAILED_READ))
close_warn (name);
}
else
@@ -1130,7 +1128,7 @@ open_diag (char const *name)
{
if (ignore_failed_read_option)
{
if (WARNING_ENABLED(WARN_FAILED_READ))
if (WARNING_ENABLED (WARN_FAILED_READ))
open_warn (name);
}
else
@@ -1142,7 +1140,7 @@ read_diag_details (char const *name, off_t offset, size_t size)
{
if (ignore_failed_read_option)
{
if (WARNING_ENABLED(WARN_FAILED_READ))
if (WARNING_ENABLED (WARN_FAILED_READ))
read_warn_details (name, offset, size);
}
else
@@ -1154,7 +1152,7 @@ readlink_diag (char const *name)
{
if (ignore_failed_read_option)
{
if (WARNING_ENABLED(WARN_FAILED_READ))
if (WARNING_ENABLED (WARN_FAILED_READ))
readlink_warn (name);
}
else
@@ -1166,7 +1164,7 @@ savedir_diag (char const *name)
{
if (ignore_failed_read_option)
{
if (WARNING_ENABLED(WARN_FAILED_READ))
if (WARNING_ENABLED (WARN_FAILED_READ))
savedir_warn (name);
}
else
@@ -1178,7 +1176,7 @@ seek_diag_details (char const *name, off_t offset)
{
if (ignore_failed_read_option)
{
if (WARNING_ENABLED(WARN_FAILED_READ))
if (WARNING_ENABLED (WARN_FAILED_READ))
seek_warn_details (name, offset);
}
else
@@ -1190,7 +1188,7 @@ stat_diag (char const *name)
{
if (ignore_failed_read_option)
{
if (WARNING_ENABLED(WARN_FAILED_READ))
if (WARNING_ENABLED (WARN_FAILED_READ))
stat_warn (name);
}
else

View File

@@ -216,7 +216,6 @@ sparse_scan_file_raw (struct tar_sparse_file *file)
struct tar_stat_info *st = file->stat_info;
int fd = file->fd;
char buffer[BLOCKSIZE];
size_t count = 0;
off_t offset = 0;
struct sp_array sp = {0, 0};
@@ -225,9 +224,16 @@ sparse_scan_file_raw (struct tar_sparse_file *file)
if (!tar_sparse_scan (file, scan_begin, NULL))
return false;
while ((count = blocking_read (fd, buffer, sizeof buffer)) != 0
&& count != SAFE_READ_ERROR)
while (true)
{
ptrdiff_t count = blocking_read (fd, buffer, sizeof buffer);
if (count <= 0)
{
if (count < 0)
read_diag_details (st->orig_file_name, offset, sizeof buffer);
break;
}
/* Analyze the block. */
if (zero_block_p (buffer, count))
{
@@ -258,7 +264,6 @@ sparse_scan_file_raw (struct tar_sparse_file *file)
sp.offset = offset;
sparse_add_map (st, &sp);
st->archive_file_size += count;
return tar_sparse_scan (file, scan_end, NULL);
}
@@ -489,7 +494,6 @@ sparse_extract_region (struct tar_sparse_file *file, size_t i)
}
else while (write_size > 0)
{
size_t count;
size_t wrbytes = (write_size > BLOCKSIZE) ? BLOCKSIZE : write_size;
union block *blk = find_next_block ();
if (!blk)
@@ -499,7 +503,7 @@ sparse_extract_region (struct tar_sparse_file *file, size_t i)
}
set_next_block_after (blk);
file->dumped_size += BLOCKSIZE;
count = blocking_write (file->fd, blk->buffer, wrbytes);
idx_t count = blocking_write (file->fd, blk->buffer, wrbytes);
write_size -= count;
mv_size_left (file->stat_info->archive_file_size - file->dumped_size);
file->offset += count;