libbb: introduce and use xfstat_mode() and xfstat_size()
function old new delta xfstat_size - 28 +28 add_remove_shell_main 312 302 -10 mkfs_ext2_main 2299 2285 -14 mesg_main 157 136 -21 ar_main 565 527 -38 ubi_tools_main 1164 1125 -39 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/5 up/down: 28/-122) Total: -94 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
1 parent
10c9c7faec
commit
8eea53bb7a
13 files changed
+47
-44
No files matched your search
+4
-4
@@ -151,15 +151,15 @@ static int write_ar_header(archive_handle_t *handle)
|
||||
|
||||
static int write_ar_archive(archive_handle_t *handle)
|
||||
{
|
||||
struct stat st;
|
||||
off_t st_size;
|
||||
archive_handle_t *out_handle;
|
||||
|
||||
xfstat(handle->src_fd, &st, handle->ar__name);
|
||||
st_size = xfstat_size(handle->src_fd, handle->ar__name);
|
||||
|
||||
/* if archive exists, create a new handle for output.
|
||||
* we create it in place of the old one.
|
||||
*/
|
||||
if (st.st_size != 0) {
|
||||
if (st_size != 0) {
|
||||
out_handle = init_handle();
|
||||
xunlink(handle->ar__name);
|
||||
out_handle->src_fd = xopen(handle->ar__name, O_WRONLY | O_CREAT | O_TRUNC);
|
||||
@@ -174,7 +174,7 @@ static int write_ar_archive(archive_handle_t *handle)
|
||||
out_handle->offset += AR_MAGIC_LEN + 1;
|
||||
|
||||
/* skip to the end of the archive if we have to append stuff */
|
||||
if (st.st_size != 0) {
|
||||
if (st_size != 0) {
|
||||
handle->filter = filter_replaceable;
|
||||
handle->action_data = copy_data;
|
||||
unpack_ar_archive(handle);
|
||||
|
||||
@@ -681,6 +681,8 @@ void bb_unsetenv_and_free(char *key) FAST_FUNC;
|
||||
void xunlink(const char *pathname) FAST_FUNC;
|
||||
void xstat(const char *pathname, struct stat *buf) FAST_FUNC;
|
||||
void xfstat(int fd, struct stat *buf, const char *errmsg) FAST_FUNC;
|
||||
mode_t FAST_FUNC xfstat_mode(int fd, const char *file_name);
|
||||
off_t FAST_FUNC xfstat_size(int fd, const char *file_name);
|
||||
int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC;
|
||||
int open_or_warn(const char *pathname, int flags) FAST_FUNC;
|
||||
int xopen3(const char *pathname, int flags, int mode) FAST_FUNC;
|
||||
|
||||
@@ -549,6 +549,19 @@ void FAST_FUNC xfstat(int fd, struct stat *stat_buf, const char *errmsg)
|
||||
bb_simple_perror_msg_and_die(errmsg);
|
||||
}
|
||||
|
||||
mode_t FAST_FUNC xfstat_mode(int fd, const char *file_name)
|
||||
{
|
||||
struct stat st;
|
||||
xfstat(fd, &st, file_name);
|
||||
return st.st_mode;
|
||||
}
|
||||
off_t FAST_FUNC xfstat_size(int fd, const char *file_name)
|
||||
{
|
||||
struct stat st;
|
||||
xfstat(fd, &st, file_name);
|
||||
return st.st_size;
|
||||
}
|
||||
|
||||
#if ENABLE_SELINUX
|
||||
// selinux_or_die() - die if SELinux is disabled.
|
||||
void FAST_FUNC selinux_or_die(void)
|
||||
|
||||
@@ -51,18 +51,17 @@ int add_remove_shell_main(int argc UNUSED_PARAM, char **argv)
|
||||
FILE *orig_fp;
|
||||
char *orig_fn;
|
||||
char *new_fn;
|
||||
struct stat sb;
|
||||
|
||||
sb.st_mode = 0666;
|
||||
mode_t st_mode;
|
||||
|
||||
argv++;
|
||||
|
||||
orig_fn = xmalloc_follow_symlinks(SHELLS_FILE);
|
||||
if (!orig_fn)
|
||||
return EXIT_FAILURE;
|
||||
st_mode = 0666;
|
||||
orig_fp = fopen_for_read(orig_fn);
|
||||
if (orig_fp)
|
||||
xfstat(fileno(orig_fp), &sb, orig_fn);
|
||||
st_mode = xfstat_mode(fileno(orig_fp), orig_fn);
|
||||
|
||||
new_fn = xasprintf("%s.tmp", orig_fn);
|
||||
/*
|
||||
@@ -73,7 +72,7 @@ int add_remove_shell_main(int argc UNUSED_PARAM, char **argv)
|
||||
* after which it should revert to O_TRUNC.
|
||||
* For now, I settle for O_TRUNC instead.
|
||||
*/
|
||||
xmove_fd(xopen3(new_fn, O_WRONLY | O_CREAT | O_TRUNC, sb.st_mode), STDOUT_FILENO);
|
||||
xmove_fd(xopen3(new_fn, O_WRONLY | O_CREAT | O_TRUNC, st_mode), STDOUT_FILENO);
|
||||
/* TODO?
|
||||
xfchown(STDOUT_FILENO, sb.st_uid, sb.st_gid);
|
||||
*/
|
||||
|
||||
@@ -326,9 +326,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
|
||||
xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
|
||||
|
||||
if (!(opts & OPTION_s)) {
|
||||
struct stat st;
|
||||
xfstat(STDIN_FILENO, &st, *argv);
|
||||
size_bytes = st.st_size;
|
||||
size_bytes = xfstat_size(STDIN_FILENO, *argv);
|
||||
}
|
||||
|
||||
bytes64 = size_bytes;
|
||||
|
||||
+2
-2
@@ -491,8 +491,8 @@ static int tftp_protocol(
|
||||
fstat(local_fd, &st);
|
||||
//Do we need this instead? Example?
|
||||
//if (local_fd >= 0 && (!ENABLE_TFTP || local_file)) {
|
||||
// xfstat(local_fd, &st, local_file);
|
||||
// if (!S_ISREG(st.st_mode))
|
||||
// mode_t m = xfstat_mode(local_fd, local_file);
|
||||
// if (!S_ISREG(m))
|
||||
// bb_error_msg_and_die("'%s' is not a regular file", local_file);
|
||||
//}
|
||||
cp = stpcpy(cp, "tsize") + 1;
|
||||
|
||||
@@ -61,8 +61,7 @@ int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
fd = xopen(argv[0], O_RDWR|O_EXCL);
|
||||
//Why bother, BLK[SEC]DISCARD will fail on non-blockdevs anyway?
|
||||
// xfstat(fd, &st);
|
||||
// if (!S_ISBLK(st.st_mode))
|
||||
// if (!S_ISBLK(xfstat_mode(fd)))
|
||||
// bb_error_msg_and_die("%s: not a block device", argv[0]);
|
||||
|
||||
offset = xatoull_sfx(offset_str, kMG_suffixes);
|
||||
|
||||
+5
-5
@@ -47,7 +47,7 @@
|
||||
int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int mesg_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
struct stat sb;
|
||||
mode_t st_mode;
|
||||
mode_t m;
|
||||
char c = 0;
|
||||
|
||||
@@ -67,13 +67,13 @@ int mesg_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (!isatty(STDIN_FILENO))
|
||||
bb_simple_error_msg_and_die("not a tty");
|
||||
|
||||
xfstat(STDIN_FILENO, &sb, "stdin");
|
||||
st_mode = xfstat_mode(STDIN_FILENO, "stdin");
|
||||
if (c == 0) {
|
||||
puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
|
||||
puts((st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
|
||||
: sb.st_mode & ~(S_IWGRP|S_IWOTH);
|
||||
m = (c == 'y') ? st_mode | S_IWGRP_OR_S_IWOTH
|
||||
: st_mode & ~(S_IWGRP|S_IWOTH);
|
||||
if (fchmod(STDIN_FILENO, m) != 0)
|
||||
bb_perror_nomsg_and_die();
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
@@ -237,7 +237,6 @@ int mkfs_ext2_main(int argc UNUSED_PARAM, char **argv)
|
||||
uint32_t lost_and_found_blocks;
|
||||
time_t timestamp;
|
||||
const char *label = "";
|
||||
struct stat st;
|
||||
struct ext2_super_block *sb; // superblock
|
||||
struct ext2_group_desc *gd; // group descriptors
|
||||
struct ext2_inode *inode;
|
||||
@@ -257,9 +256,11 @@ int mkfs_ext2_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
// open the device, check the device is a block device
|
||||
xmove_fd(xopen(argv[0], O_RDWR), fd);
|
||||
xfstat(fd, &st, argv[0]);
|
||||
if (!S_ISBLK(st.st_mode) && !(option_mask32 & OPT_F))
|
||||
bb_error_msg_and_die("%s: not a block device", argv[0]);
|
||||
if (!(option_mask32 & OPT_F)) {
|
||||
mode_t st_mode = xfstat_mode(fd, argv[0]);
|
||||
if (!S_ISBLK(st_mode))
|
||||
bb_error_msg_and_die("%s: not a block device", argv[0]);
|
||||
}
|
||||
|
||||
// check if it is mounted
|
||||
// N.B. what if we format a file? find_mount_point will return false negative since
|
||||
|
||||
@@ -671,10 +671,8 @@ int mkfs_minix_main(int argc UNUSED_PARAM, char **argv)
|
||||
} else if (G.total_blocks > 65535)
|
||||
G.total_blocks = 65535;
|
||||
#if 0
|
||||
struct stat statbuf;
|
||||
xfstat(dev_fd, &statbuf, G.device_name);
|
||||
/* why? */
|
||||
if (!S_ISBLK(statbuf.st_mode))
|
||||
if (!S_ISBLK(xfstat_mode(dev_fd, G.device_name)))
|
||||
opt &= ~1; // clear -c (check)
|
||||
#if 0
|
||||
/* I don't know why someone has special code to prevent mkfs.minix
|
||||
|
||||
@@ -154,7 +154,6 @@ int mkfs_reiser_main(int argc UNUSED_PARAM, char **argv)
|
||||
unsigned blocks, bitmap_blocks, i, block;
|
||||
time_t timestamp;
|
||||
const char *label = "";
|
||||
struct stat st;
|
||||
int fd;
|
||||
uint8_t *buf;
|
||||
struct reiserfs_super_block *sb;
|
||||
@@ -169,9 +168,11 @@ int mkfs_reiser_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
// check the device is a block device
|
||||
fd = xopen(argv[0], O_RDWR | O_EXCL);
|
||||
xfstat(fd, &st, argv[0]);
|
||||
if (!S_ISBLK(st.st_mode) && !(option_mask32 & OPT_f))
|
||||
bb_error_msg_and_die("%s: not a block device", argv[0]);
|
||||
if (!(option_mask32 & OPT_f)) {
|
||||
mode_t st_mode = xfstat_mode(fd, argv[0]);
|
||||
if (!S_ISBLK(st_mode))
|
||||
bb_error_msg_and_die("%s: not a block device", argv[0]);
|
||||
}
|
||||
|
||||
// check if it is mounted
|
||||
// N.B. what if we format a file? find_mount_point will return false negative since
|
||||
|
||||
@@ -200,14 +200,6 @@ static const char boot_code[] ALIGN1 =
|
||||
/* 077: message_txt: */
|
||||
"This is not a bootable disk\r\n";
|
||||
|
||||
// In order to reduce stack usage in main (-440 bytes on x86)
|
||||
static NOINLINE mode_t xfstat_mode(int fd, const char *file_name)
|
||||
{
|
||||
struct stat st;
|
||||
xfstat(fd, &st, file_name);
|
||||
return st.st_mode;
|
||||
}
|
||||
|
||||
/* compat:
|
||||
* mkdosfs 2.11 (12 Mar 2005)
|
||||
* Usage: mkdosfs [-A] [-c] [-C] [-v] [-I] [-l bad-block-file]
|
||||
|
||||
+4
-4
@@ -42,13 +42,13 @@
|
||||
#if ENABLE_SELINUX
|
||||
static void mkswap_selinux_setcontext(int fd, const char *path)
|
||||
{
|
||||
struct stat stbuf;
|
||||
mode_t st_mode;
|
||||
|
||||
if (!is_selinux_enabled())
|
||||
return;
|
||||
|
||||
xfstat(fd, &stbuf, path);
|
||||
if (S_ISREG(stbuf.st_mode)) {
|
||||
st_mode = xfstat_mode(fd, path);
|
||||
if (S_ISREG(st_mode)) {
|
||||
const char *newcon;
|
||||
char *oldcon = NULL;
|
||||
context_t context;
|
||||
@@ -56,7 +56,7 @@ static void mkswap_selinux_setcontext(int fd, const char *path)
|
||||
if (fgetfilecon(fd, &oldcon) < 0) {
|
||||
if (errno != ENODATA)
|
||||
goto error;
|
||||
if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
|
||||
if (matchpathcon(path, st_mode, &oldcon) < 0)
|
||||
goto error;
|
||||
}
|
||||
context = context_new(oldcon);
|
||||
|
||||
Reference in new issue
Block a user