[PATCH 2/2 v5] truncate: new applet

Bernhard Reutner-Fischer rep.dot.nop at gmail.com
Fri Mar 6 13:06:40 UTC 2015


On 6 March 2015 at 13:29, Ari Sundholm <ari at tuxera.com> wrote:
> On Thu, 2015-03-05 at 23:11 +0100, Bernhard Reutner-Fischer wrote:
>> On March 5, 2015 8:57:05 PM GMT+01:00, Ari Sundholm <ari at tuxera.com> wrote:
>>>+      opt_complementary = "s";
>>>+      opts = getopt32(argv, "cs:", &size_str);
>>>+
>> >+    if (!*argv)
>> >+            bb_error_msg_and_die("no files specified!");
>>
>> opt_complementary = "-1"; at least one non-option arg
>
> Thanks, changed to "s:-1", as the presence of the -s option has to be
> checked as well.
>
>> >+
>> >+    if (opts & OPT_NOCREATE)
>> >+            flags = O_RDWR;
>>
>> Wasteful.
>
> Do you have a suggestion how to make this less wasteful? The presence of
> the -c option has to be checked and its effect on the open() flags put
> in effect somehow. Or do you think the entire -c option is unneeded?

setting flags twice is most likely wasteful.
Maybe int flags = O_RDWR;
if (!(opts & OPT_NOCREATE))
  flags |= O_CREAT;

or
int flags = O_RDWR | (1 - (opts & OPT_NOCREATE)) * O_CREAT

or something like that. You have to experiment.
>
>> >+    while (*argv) {
>> >+            fd = xopen(*argv, flags);
>> >+            if (ftruncate(fd, size) == -1)
>> >+                    bb_perror_msg_and_die("ftruncate failed");
>>
>> " failed" is redundant.
>
> Indeed. Thanks.
>
>> I would only accept this as a shell script, but that's Denys call.
>
> This applet was borne out of a real need and, for us, was one of the
> important things that was missing but needed of busybox. Having to
> maintain separate wrapper scripts gets old pretty fast when the
> alternative would be to be able to just deploy a busybox with all tools
> needed. Of course, we have no desire to bloat busybox unnecessarily.
> This applet was judged generally useful and thus submitted for mainline
> inclusion.
>
> BTW, I took a look at my dd wrapper script and it is considerably bigger
> than the applet and doesn't do as much as the applet does. And the

give me 3 minutes..

$ wc -c ftruncate.sh
255 ftruncate.sh
$ cat !$
cat ftruncate.sh
c=1
s=0
for a in $@; do
if [ "$s" = "-1" ];then s=${a}; continue; fi
case /$a in
/-c) c=0; continue;;
/-s) s=-1; continue;;
esac
if=${a}
if [ ! -f ${a} ]; then
  [ $c -eq 0 ] && continue
  if="/dev/zero"
fi
dd if=${if} bs=1 seek=${s} count=1 of=${a}
done
$ dmesg | tee /tmp/1 > /tmp/2
$ sh ./ftruncate.sh -s 4096c /tmp/1 /tmp/2

> applet is of course entirely optional even if accepted.

sure


More information about the busybox mailing list