Skip to content

Commit

Permalink
usr: do not rely on LFS64 interfaces
Browse files Browse the repository at this point in the history
LFS64 interfaces are non-standard. Relying on them causes multiple
compile errors on systems using musl libc 1.2.4 or later. They could be
worked around by defining _LARGEFILE64_SOURCE, but this only a short
term workaround, and will be removed in a future musl version.

Instead, define _FILE_OFFSET_BITS as 64, which makes all interfaces on
glibc 64-bit by default, and replace the non-standard interfaces with
the standard ones. This replaces all occurences of fstat64 with fstat,
lseek64 with lseek, pread64 with pread, pwrite64 with pwrite, and stat64
with stat.

Signed-off-by: Stijn Tintel <[email protected]>
  • Loading branch information
stintel committed Mar 30, 2024
1 parent eadd823 commit 277320f
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 21 deletions.
1 change: 1 addition & 0 deletions usr/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ endif

INCLUDES += -I.

CFLAGS += -D_FILE_OFFSET_BITS=64
CFLAGS += -D_GNU_SOURCE
CFLAGS += $(INCLUDES)
ifneq ($(DEBUG),)
Expand Down
12 changes: 6 additions & 6 deletions usr/bs_rdwr.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static void bs_rdwr_request(struct scsi_cmd *cmd)
break;
}

ret = pread64(fd, tmpbuf, length, offset);
ret = pread(fd, tmpbuf, length, offset);

if (ret != length) {
set_medium_error(cmd);
Expand Down Expand Up @@ -113,7 +113,7 @@ static void bs_rdwr_request(struct scsi_cmd *cmd)
break;
}

ret = pread64(fd, tmpbuf, length, offset);
ret = pread(fd, tmpbuf, length, offset);

if (ret != length) {
set_medium_error(cmd);
Expand Down Expand Up @@ -175,7 +175,7 @@ static void bs_rdwr_request(struct scsi_cmd *cmd)
length = scsi_get_out_length(cmd);
write_buf = scsi_get_out_buffer(cmd);
write:
ret = pwrite64(fd, write_buf, length,
ret = pwrite(fd, write_buf, length,
offset);
if (ret == length) {
struct mode_pg *pg;
Expand Down Expand Up @@ -233,7 +233,7 @@ static void bs_rdwr_request(struct scsi_cmd *cmd)
break;
}

ret = pwrite64(fd, tmpbuf, blocksize, offset);
ret = pwrite(fd, tmpbuf, blocksize, offset);
if (ret != blocksize)
set_medium_error(cmd);

Expand All @@ -246,7 +246,7 @@ static void bs_rdwr_request(struct scsi_cmd *cmd)
case READ_12:
case READ_16:
length = scsi_get_in_length(cmd);
ret = pread64(fd, scsi_get_in_buffer(cmd), length,
ret = pread(fd, scsi_get_in_buffer(cmd), length,
offset);

if (ret != length)
Expand Down Expand Up @@ -278,7 +278,7 @@ static void bs_rdwr_request(struct scsi_cmd *cmd)
break;
}

ret = pread64(fd, tmpbuf, length, offset);
ret = pread(fd, tmpbuf, length, offset);

if (ret != length)
set_medium_error(cmd);
Expand Down
6 changes: 3 additions & 3 deletions usr/bs_ssc.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ static int append_blk(struct scsi_cmd *cmd, uint8_t *data,

/* Write any data */
if (size) {
ret = pwrite64(fd, data, size,
ret = pwrite(fd, data, size,
curr->curr + SSC_BLK_HDR_SIZE);
if (ret != size) {
eprintf("Write of data failed: %m\n");
Expand Down Expand Up @@ -349,7 +349,7 @@ static int resp_var_read(struct scsi_cmd *cmd, uint8_t *buf, uint32_t length,
}
}

ret = pread64(cmd->dev->fd, buf, length, h->curr + SSC_BLK_HDR_SIZE);
ret = pread(cmd->dev->fd, buf, length, h->curr + SSC_BLK_HDR_SIZE);
if (ret != length) {
sense_data_build(cmd, MEDIUM_ERROR, ASC_READ_ERROR);
result = SAM_STAT_CHECK_CONDITION;
Expand Down Expand Up @@ -404,7 +404,7 @@ static int resp_fixed_read(struct scsi_cmd *cmd, uint8_t *buf, uint32_t length,
goto out;
}

residue = pread64(fd, buf, block_length,
residue = pread(fd, buf, block_length,
h->curr + SSC_BLK_HDR_SIZE);
if (block_length != residue) {
eprintf("Could only read %d bytes, not %d\n",
Expand Down
8 changes: 4 additions & 4 deletions usr/libssc.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int ssc_read_mam_info(int fd, struct MAM_info *i)
if (ret != sizeof(struct MAM))
return 1;

if (lseek64(fd, SSC_1ST_HDR_OFFSET, SEEK_SET) != SSC_1ST_HDR_OFFSET)
if (lseek(fd, SSC_1ST_HDR_OFFSET, SEEK_SET) != SSC_1ST_HDR_OFFSET)
return 1;

SSC_GET_MAM_INFO_VAL(tape_fmt_version, 32);
Expand Down Expand Up @@ -176,7 +176,7 @@ int ssc_write_mam_info(int fd, struct MAM_info *i)
if (ret != sizeof(struct MAM))
return 1;

if (lseek64(fd, SSC_1ST_HDR_OFFSET, SEEK_SET) != SSC_1ST_HDR_OFFSET)
if (lseek(fd, SSC_1ST_HDR_OFFSET, SEEK_SET) != SSC_1ST_HDR_OFFSET)
return 1;

return 0;
Expand All @@ -188,7 +188,7 @@ int ssc_read_blkhdr(int fd, struct blk_header_info *i, loff_t offset)
struct blk_header h, *m = &h;
uint32_t crc = ~0;

count = pread64(fd, m, SSC_BLK_HDR_SIZE, offset);
count = pread(fd, m, SSC_BLK_HDR_SIZE, offset);
if (count != SSC_BLK_HDR_SIZE)
return 1;

Expand Down Expand Up @@ -225,7 +225,7 @@ int ssc_write_blkhdr(int fd, struct blk_header_info *i, loff_t offset)
crc = crc32c(crc, &m->ondisk_sz, SSC_BLK_HDR_SIZE - sizeof(m->h_csum));
*(uint32_t *)m->h_csum = ~crc;

count = pwrite64(fd, m, SSC_BLK_HDR_SIZE, offset);
count = pwrite(fd, m, SSC_BLK_HDR_SIZE, offset);
if (count != SSC_BLK_HDR_SIZE)
return 1;

Expand Down
5 changes: 2 additions & 3 deletions usr/sbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#define _FILE_OFFSET_BITS 64
#define __USE_GNU

#include <errno.h>
Expand Down Expand Up @@ -52,15 +51,15 @@ static unsigned int blk_shift = DEFAULT_BLK_SHIFT;
static off_t find_next_data(struct scsi_lu *dev, off_t offset)
{
#ifdef SEEK_DATA
return lseek64(dev->fd, offset, SEEK_DATA);
return lseek(dev->fd, offset, SEEK_DATA);
#else
return offset;
#endif
}
static off_t find_next_hole(struct scsi_lu *dev, off_t offset)
{
#ifdef SEEK_HOLE
return lseek64(dev->fd, offset, SEEK_HOLE);
return lseek(dev->fd, offset, SEEK_HOLE);
#else
return dev->size;
#endif
Expand Down
2 changes: 1 addition & 1 deletion usr/tgtimg.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ static int ssc_new(int op, char *path, char *barcode, char *capacity,
h->blk_type = BLK_EOD;
h->blk_num = 1;
h->prev = 0;
h->next = lseek64(fd, 0, SEEK_CUR);
h->next = lseek(fd, 0, SEEK_CUR);
h->curr = h->next;

ret = ssc_write_blkhdr(fd, h, h->next);
Expand Down
4 changes: 2 additions & 2 deletions usr/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ int chrdev_open(char *modname, char *devpath, uint8_t minor, int *fd)
int backed_file_open(char *path, int oflag, uint64_t *size, uint32_t *blksize)
{
int fd, err;
struct stat64 st;
struct stat st;

fd = open(path, oflag);
if (fd < 0) {
eprintf("Could not open %s, %m\n", path);
return fd;
}

err = fstat64(fd, &st);
err = fstat(fd, &st);
if (err < 0) {
eprintf("Cannot get stat %d, %m\n", fd);
goto close_fd;
Expand Down
4 changes: 2 additions & 2 deletions usr/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ void concat_buf_release(struct concat_buf *b);
*/
static inline int unmap_file_region(int fd, off_t offset, off_t length)
{
struct stat64 st;
if (fstat64(fd, &st) < 0)
struct stat st;
if (fstat(fd, &st) < 0)
return -1;
if (S_ISREG(st.st_mode)) {
#ifdef FALLOC_FL_PUNCH_HOLE
Expand Down

0 comments on commit 277320f

Please sign in to comment.