dd: iflag=fullblock now read full blocks if possible (#431997, #449263) ls:

--color now highlights files with capabilities (#449985)
This commit is contained in:
Kamil Dudka 2008-07-24 13:17:42 +00:00
parent 0e06139f50
commit c7a6abf853
6 changed files with 454 additions and 1 deletions

View File

@ -0,0 +1,181 @@
From 9f8be4b0b83d1e0cbf1326f8cb7e077d026d9b0b Mon Sep 17 00:00:00 2001
From: Kamil Dudka <kdudka@redhat.com>
Date: Wed, 23 Jul 2008 11:29:21 +0200
Subject: [PATCH] dd: iflag=fullblock now read full blocks if possible
* src/dd.c (iread_fullblock): New function for reading full blocks.
(scanargs): Check for new parameter iflag=fullblock.
(skip): Use iread_fnc pointer instead of iread function.
(dd_copy): Use iread_fnc pointer instead of iread function.
* tests/dd/misc: Add test for dd - read full blocks.
* doc/coretuils.texi: Mention new parameter iflag=fullblock.
* NEWS: Mentioned the change.
---
NEWS | 4 ++++
doc/coreutils.texi | 6 ++++++
src/dd.c | 39 +++++++++++++++++++++++++++++++++++++--
tests/dd/misc | 9 +++++++++
4 files changed, 56 insertions(+), 2 deletions(-)
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 81e3b91..b95f8dc 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -7719,6 +7719,12 @@ platforms that distinguish binary from text I/O.
Use text I/O. Like @samp{binary}, this option has no effect on
standard platforms.
+@item fullblock
+@opindex fullblock
+Read full blocks from input if possible. read() may return early
+if a full block is not available, so retry until data is available
+or end of file is reached. This flag can be used only for the iflag option.
+
@end table
These flags are not supported on all systems, and @samp{dd} rejects
diff --git a/src/dd.c b/src/dd.c
index ead9574..1b620df 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -225,6 +225,9 @@ static sig_atomic_t volatile interrupt_signal;
/* A count of the number of pending info signals that have been received. */
static sig_atomic_t volatile info_signal_count;
+/* Function used for read (to handle iflag=fullblock parameter) */
+static ssize_t (*iread_fnc) (int fd, char *buf, size_t size);
+
/* A longest symbol in the struct symbol_values tables below. */
#define LONGEST_SYMBOL "fdatasync"
@@ -257,6 +260,7 @@ static struct symbol_value const conversions[] =
};
/* Flags, for iflag="..." and oflag="...". */
+#define O_FULLBLOCK 010000000 /* Read only full blocks from input */
static struct symbol_value const flags[] =
{
{"append", O_APPEND},
@@ -271,6 +275,7 @@ static struct symbol_value const flags[] =
{"nonblock", O_NONBLOCK},
{"sync", O_SYNC},
{"text", O_TEXT},
+ {"fullblock", O_FULLBLOCK}, /* Read only full blocks from input */
{"", 0}
};
@@ -762,6 +767,27 @@ iread (int fd, char *buf, size_t size)
}
}
+/* Wrapper around iread function which reads full blocks if possible */
+static ssize_t
+iread_fullblock (int fd, char *buf, size_t size)
+{
+ ssize_t nread = 0;
+
+ while (0 < size)
+ {
+ ssize_t ncurr = iread(fd, buf, size);
+ if (ncurr < 0)
+ return ncurr;
+ if (ncurr == 0)
+ break;
+ nread += ncurr;
+ buf += ncurr;
+ size -= ncurr;
+ }
+
+ return nread;
+}
+
/* Write to FD the buffer BUF of size SIZE, processing any signals
that arrive. Return the number of bytes written, setting errno if
this is less than SIZE. Keep trying if there are partial
@@ -1000,6 +1026,15 @@ scanargs (int argc, char *const *argv)
if (input_flags & (O_DSYNC | O_SYNC))
input_flags |= O_RSYNC;
+ if (output_flags & O_FULLBLOCK)
+ {
+ error (0, 0, "%s: %s", _("invalid output flag"), "'fullblock'");
+ usage (EXIT_FAILURE);
+ }
+ iread_fnc = (input_flags & O_FULLBLOCK)?
+ iread_fullblock:
+ iread;
+
if (multiple_bits_set (conversions_mask & (C_ASCII | C_EBCDIC | C_IBM)))
error (EXIT_FAILURE, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
if (multiple_bits_set (conversions_mask & (C_BLOCK | C_UNBLOCK)))
@@ -1197,7 +1232,7 @@ skip (int fdesc, char const *file, uintmax_t records, size_t blocksize,
do
{
- ssize_t nread = iread (fdesc, buf, blocksize);
+ ssize_t nread = iread_fnc (fdesc, buf, blocksize);
if (nread < 0)
{
if (fdesc == STDIN_FILENO)
@@ -1508,7 +1543,7 @@ dd_copy (void)
(conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
input_blocksize);
- nread = iread (STDIN_FILENO, ibuf, input_blocksize);
+ nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
if (nread == 0)
break; /* EOF. */
diff --git a/tests/dd/misc b/tests/dd/misc
index d54fbfa..24e5eba 100755
--- a/tests/dd/misc
+++ b/tests/dd/misc
@@ -88,6 +88,15 @@ fi
outbytes=`echo x | dd bs=3 ibs=10 obs=10 conv=sync 2>/dev/null | wc -c`
test "$outbytes" -eq 3 || fail=1
+(echo a; sleep .1; echo b) \
+ | LC_ALL=C dd bs=4 status=noxfer iflag=fullblock >out 2>err || fail=1
+echo "a
+b" > out_ok
+echo "1+0 records in
+1+0 records out" > err_ok
+compare out out_ok || fail=1
+compare err err_ok || fail=1
+
test $fail -eq 0 && fail=$warn
(exit $fail); exit $fail
diff -ruN coreutils-6.12.old/doc/coreutils.info coreutils-6.12/doc/coreutils.info
--- coreutils-6.12.old/doc/coreutils.info 2008-07-24 12:49:57.000000000 +0200
+++ coreutils-6.12/doc/coreutils.info 2008-07-24 12:52:17.000000000 +0200
@@ -6112,6 +6112,12 @@
Use text I/O. Like `binary', this option has no effect on
standard platforms.
+ 'fullblock'
+ Read full blocks from input if possible. read() may return
+ early if a full block is not available, so retry until data
+ is available or end of file is reached. This flag can be used
+ only for the iflag option.
+
These flags are not supported on all systems, and `dd' rejects
attempts to use them when they are not supported. When reading
diff -ruN coreutils-6.12.old/man/dd.1 coreutils-6.12/man/dd.1
--- coreutils-6.12.old/man/dd.1 2008-07-24 12:51:06.000000000 +0200
+++ coreutils-6.12/man/dd.1 2008-07-24 12:59:06.000000000 +0200
@@ -111,6 +111,13 @@
.TP
direct
use direct I/O for data
+.PP
+FLAG symbols only for iflag option:
+.TP
+fullblock
+Read full blocks from input if possible. read() may return early
+if a full block is not available, so retry until data is available
+or end of file is reached.
.IP
directory fail unless a directory
dsync use synchronized I/O for data

View File

@ -0,0 +1,257 @@
From 7634188624dc7f48c047b29fab3715dc7a468059 Mon Sep 17 00:00:00 2001
From: Kamil Dudka <kdudka@redhat.com>
Date: Wed, 23 Jul 2008 09:52:05 +0200
Subject: [PATCH] ls: --color now highlights files with capabilities, too
* configure.ac: New option: --disable-libcap. Check for libcap usability.
* src/Makefile.am (dir_LDADD, ls_LDADD, ...): Append $(LIB_CAP).
* src/ls.c: [HAVE_CAP] Include <sys/capability.h>.
(has_capability): New function for capability detection.
(print_color_indicator): Colorize file with capability.
* src/dircolors.c: Update color lists.
* src/dircolors.hin: Mention new CAPABILITY color attribute.
* tests/ls/capability: Test for ls - colorize file with capability.
* tests/Makefile.am (TESTS): Add ls/capability.
* NEWS: Mention the change.
---
NEWS | 2 ++
configure.ac | 13 +++++++++++++
src/Makefile.am | 6 +++---
src/dircolors.c | 4 ++--
src/dircolors.hin | 1 +
src/ls.c | 43 +++++++++++++++++++++++++++++++++++++++++--
tests/Makefile.am | 1 +
tests/ls/capability | 43 +++++++++++++++++++++++++++++++++++++++++++
8 files changed, 106 insertions(+), 7 deletions(-)
create mode 100755 tests/ls/capability
diff -ruN coreutils-6.12.old/configure.ac coreutils-6.12/configure.ac
--- coreutils-6.12.old/configure.ac 2008-07-24 14:16:32.000000000 +0200
+++ coreutils-6.12/configure.ac 2008-07-24 14:18:51.000000000 +0200
@@ -58,6 +58,19 @@
LIB_SELINUX="-lselinux"
AC_SUBST(LIB_SELINUX)])
+dnl Check whether libcap is usable
+AC_ARG_ENABLE([libcap],
+ AC_HELP_STRING([--disable-libcap], [disable libcap support]),
+ AC_MSG_WARN([libcap support disabled by user]),
+ [AC_CHECK_LIB([cap], [cap_get_file],
+ [AC_CHECK_HEADER([sys/capability.h],
+ [LIB_CAP="-lcap" AC_DEFINE(HAVE_CAP, 1, [libcap usability])],
+ [AC_MSG_WARN([header sys/capability.h was not found, support for libcap will not be built])]
+ )],
+ [AC_MSG_WARN([libcap library was not found or not usable, support for libcap will not be built])])
+ ])
+AC_SUBST([LIB_CAP])
+
AC_FUNC_FORK
optional_bin_progs=
diff -ruN coreutils-6.12.orig/src/Makefile.am coreutils-6.12/src/Makefile.am
--- coreutils-6.12.orig/src/Makefile.am 2008-07-10 12:30:03.000000000 +0200
+++ coreutils-6.12/src/Makefile.am 2008-07-24 13:18:43.000000000 +0200
@@ -98,15 +98,15 @@
# for clock_gettime and fdatasync
dd_LDADD = $(LDADD) $(LIB_GETHRXTIME) $(LIB_FDATASYNC)
-dir_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_SELINUX)
+dir_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_SELINUX) $(LIB_CAP)
id_LDADD = $(LDADD) $(LIB_SELINUX)
-ls_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_SELINUX)
+ls_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_SELINUX) $(LIB_CAP)
mktemp_LDADD = $(LDADD) $(LIB_GETHRXTIME)
pr_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME)
shred_LDADD = $(LDADD) $(LIB_GETHRXTIME) $(LIB_FDATASYNC)
shuf_LDADD = $(LDADD) $(LIB_GETHRXTIME)
tac_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME)
-vdir_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_SELINUX)
+vdir_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_SELINUX) $(LIB_CAP)
tac_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME)
## If necessary, add -lm to resolve use of pow in lib/strtod.c.
diff --git a/src/dircolors.c b/src/dircolors.c
index 56194f7..79109b9 100644
--- a/src/dircolors.c
+++ b/src/dircolors.c
@@ -63,14 +63,14 @@ static const char *const slack_codes[] =
"SYMLINK", "ORPHAN", "MISSING", "FIFO", "PIPE", "SOCK", "BLK", "BLOCK",
"CHR", "CHAR", "DOOR", "EXEC", "LEFT", "LEFTCODE", "RIGHT", "RIGHTCODE",
"END", "ENDCODE", "SUID", "SETUID", "SGID", "SETGID", "STICKY",
- "OTHER_WRITABLE", "OWR", "STICKY_OTHER_WRITABLE", "OWT", NULL
+ "OTHER_WRITABLE", "OWR", "STICKY_OTHER_WRITABLE", "OWT", "CAPABILITY", NULL
};
static const char *const ls_codes[] =
{
"no", "no", "fi", "rs", "di", "ln", "ln", "ln", "or", "mi", "pi", "pi",
"so", "bd", "bd", "cd", "cd", "do", "ex", "lc", "lc", "rc", "rc", "ec", "ec",
- "su", "su", "sg", "sg", "st", "ow", "ow", "tw", "tw", NULL
+ "su", "su", "sg", "sg", "st", "ow", "ow", "tw", "tw", "ca", NULL
};
#define array_len(Array) (sizeof (Array) / sizeof *(Array))
verify (array_len (slack_codes) == array_len (ls_codes));
diff --git a/src/dircolors.hin b/src/dircolors.hin
index 38914c8..5137cc6 100644
--- a/src/dircolors.hin
+++ b/src/dircolors.hin
@@ -77,6 +77,7 @@ CHR 40;33;01 # character device driver
ORPHAN 40;31;01 # symlink to nonexistent file, or non-stat'able file
SETUID 37;41 # file that is setuid (u+s)
SETGID 30;43 # file that is setgid (g+s)
+CAPABILITY 30;41 # file with capability
STICKY_OTHER_WRITABLE 30;42 # dir that is sticky and other-writable (+t,o+w)
OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable
diff --git a/src/ls.c b/src/ls.c
index 4b69f7d..9bc66a1 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -38,6 +38,10 @@
#include <config.h>
#include <sys/types.h>
+#ifdef HAVE_CAP
+# include <sys/capability.h>
+#endif
+
#if HAVE_TERMIOS_H
# include <termios.h>
#endif
@@ -513,14 +517,14 @@ enum indicator_no
C_LEFT, C_RIGHT, C_END, C_RESET, C_NORM, C_FILE, C_DIR, C_LINK,
C_FIFO, C_SOCK,
C_BLK, C_CHR, C_MISSING, C_ORPHAN, C_EXEC, C_DOOR, C_SETUID, C_SETGID,
- C_STICKY, C_OTHER_WRITABLE, C_STICKY_OTHER_WRITABLE
+ C_STICKY, C_OTHER_WRITABLE, C_STICKY_OTHER_WRITABLE, C_CAP
};
static const char *const indicator_name[]=
{
"lc", "rc", "ec", "rs", "no", "fi", "di", "ln", "pi", "so",
"bd", "cd", "mi", "or", "ex", "do", "su", "sg", "st",
- "ow", "tw", NULL
+ "ow", "tw", "ca", NULL
};
struct color_ext_type
@@ -553,6 +557,7 @@ static struct bin_str color_indicator[] =
{ LEN_STR_PAIR ("37;44") }, /* st: sticky: black on blue */
{ LEN_STR_PAIR ("34;42") }, /* ow: other-writable: blue on green */
{ LEN_STR_PAIR ("30;42") }, /* tw: ow w/ sticky: black on green */
+ { LEN_STR_PAIR ("30;41") }, /* capability: black on red */
};
/* FIXME: comment */
@@ -3896,6 +3901,38 @@ print_type_indicator (bool stat_ok, mode_t mode, enum filetype type)
DIRED_PUTCHAR (c);
}
+#ifdef HAVE_CAP
+static bool
+/* Return true if NAME has a capability (see linux/capability.h) */
+has_capability (const char *name)
+{
+ cap_t cap_d;
+ char *result;
+ bool has_cap;
+
+ cap_d = cap_get_file (name);
+ if (cap_d == NULL)
+ return false;
+
+ result = cap_to_text (cap_d, NULL);
+ cap_free (cap_d);
+ if (!result)
+ return false;
+
+ /* check if human-readable capability string is empty */
+ has_cap = !!*result;
+
+ cap_free (result);
+ return has_cap;
+}
+#else
+static bool
+has_capability (const char *name)
+{
+ return false;
+}
+#endif
+
/* Returns whether any color sequence was printed. */
static bool
print_color_indicator (const char *name, mode_t mode, int linkok,
@@ -3923,6 +3960,8 @@ print_color_indicator (const char *name, mode_t mode, int linkok,
type = C_SETUID;
else if ((mode & S_ISGID) != 0)
type = C_SETGID;
+ else if (has_capability (name))
+ type = C_CAP;
else if ((mode & S_IXUGO) != 0)
type = C_EXEC;
}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c2da630..309d174 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -313,6 +313,7 @@ TESTS = \
ln/misc \
ln/sf-1 \
ln/target-1 \
+ ls/capability \
ls/color-dtype-dir \
ls/dangle \
ls/dired \
diff --git a/tests/ls/capability b/tests/ls/capability
new file mode 100755
index 0000000..549e06b
--- /dev/null
+++ b/tests/ls/capability
@@ -0,0 +1,43 @@
+#!/bin/sh
+# Ensure "ls --color" properly colorizes file with capability.
+
+# Copyright (C) 2008 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ ls --version
+fi
+
+. $srcdir/test-lib.sh
+require_root_
+
+(setcap --help) 2>&1 |grep 'usage: setcap' > /dev/null \
+ || skip_test_ "setcap utility not found"
+fail=0
+
+# Don't let a different umask perturb the results.
+umask 22
+
+touch test
+setcap cap_net_bind_service=ep test \
+ || framework_failure
+code='30;41'
+LS_COLORS="ca=$code" \
+ ls --color=always test > out || fail=1
+printf "\033[0m\033[${code}mtest\033[0m\n\033[m" > out_ok || fail=1
+compare out out_ok || fail=1
+
+(exit $fail); exit $fail
--
1.5.4.1

View File

@ -83,6 +83,7 @@ ORPHAN 40;31;01 # symlink to nonexistent file, or non-stat'able file
MISSING 01;05;37;41 # ... and the files they point to
SETUID 37;41 # file that is setuid (u+s)
SETGID 30;43 # file that is setgid (g+s)
CAPABILITY 30;41 # file with capability
STICKY_OTHER_WRITABLE 30;42 # dir that is sticky and other-writable (+t,o+w)
OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable

View File

@ -62,6 +62,7 @@ ORPHAN 01;48;5;232;38;5;9 # symlink to nonexistent file, or non-stat'able file
MISSING 01;05;48;5;232;38;5;15 # ... and the files they point to
SETUID 48;5;196;38;5;15 # file that is setuid (u+s)
SETGID 48;5;11;38;5;16 # file that is setgid (g+s)
CAPABILITY 48;5;196;38;5;226 # file with capability
STICKY_OTHER_WRITABLE 48;5;10;38;5;16 # dir that is sticky and other-writable (+t,o+w)
OTHER_WRITABLE 48;5;10;38;5;21 # dir that is other-writable (o+w) and not sticky
STICKY 48;5;21;38;5;15 # dir with the sticky bit set (+t) and not other-writable

View File

@ -65,6 +65,7 @@ ORPHAN 40;31;01 # symlink to nonexistent file, or non-stat'able file
MISSING 01;05;37;41 # ... and the files they point to
SETUID 37;41 # file that is setuid (u+s)
SETGID 30;43 # file that is setgid (g+s)
CAPABILITY 30;41 # file with capability
STICKY_OTHER_WRITABLE 30;42 # dir that is sticky and other-writable (+t,o+w)
OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable

View File

@ -1,7 +1,7 @@
Summary: The GNU core utilities: a set of tools commonly used in shell scripts
Name: coreutils
Version: 6.12
Release: 6%{?dist}
Release: 7%{?dist}
License: GPLv3+
Group: System Environment/Base
Url: http://www.gnu.org/software/coreutils/
@ -54,6 +54,8 @@ Patch916: coreutils-getfacl-exit-code.patch
Patch950: coreutils-selinux.patch
Patch951: coreutils-selinuxmanpages.patch
Patch952: coreutils-6.11-matchpathconinstall.patch
Patch953: coreutils-6.12-dd-fullblock.patch
Patch954: coreutils-6.12-ls-libcap.patch
BuildRequires: libselinux-devel >= 1.25.6-1
BuildRequires: libacl-devel
@ -64,6 +66,7 @@ BuildRequires: autoconf >= 2.58
#dist-lzma required
BuildRequires: automake >= 1.10.1
%{?!nopam:BuildRequires: pam-devel}
BuildRequires: libcap-devel >= 2.0.6
Requires(post): libselinux >= 1.25.6-1
Requires(pre): /sbin/install-info
@ -71,6 +74,7 @@ Requires(preun): /sbin/install-info
Requires(post): /sbin/install-info
Requires(post): grep
%{?!nopam:Requires: pam >= 0.66-12}
Requires(post): libcap >= 2.0.6
# Require a C library that doesn't put LC_TIME files in our way.
Conflicts: glibc < 2.2
@ -133,11 +137,14 @@ cd %name-%version
#SELinux
%patch950 -p1 -b .selinux
%patch951 -p1 -b .selinuxman
%patch953 -p1 -b .dd-fullblock
%patch954 -p1 -b .ls-libcap
chmod a+x tests/misc/sort-mb-tests
chmod a+x tests/misc/id-context
chmod a+x tests/misc/utimensat-touchcp
chmod a+x tests/ls/capability
#fix typos/mistakes in localized documentation(#439410, #440056)
for pofile in $(find ./po/*.p*)
@ -316,6 +323,11 @@ fi
/sbin/runuser
%changelog
* Wed Jul 24 2008 Kamil Dudka <kdudka@redhat.com> - 6.12-7
- dd: iflag=fullblock now read full blocks if possible
(#431997, #449263)
- ls: --color now highlights files with capabilities (#449985)
* Wed Jul 16 2008 Ondrej Vasik <ovasik@redhat.com> - 6.12-6
- Get rid off fuzz in patches