[Toybox] [PATCH] fallocate

Felix Janda felix.janda at posteo.de
Fri Aug 9 11:57:35 PDT 2013


hhm wrote:
> Here is a request for a new toy:
> 
> fallocate
> 
> This would be similar to the "readlink" toy; a wrapper for a syscall.
> 
> A similar effect can be procured by making a sparse file using
> "truncate" or a zeroed file by using "dd" and /dev/zero as input
> ("if="). However, "truncate" does something different (sparse file
> instead of real file), and "dd" takes much longer, since it writes the
> file in addition to allocating it.
> 
> See <https://wiki.archlinux.org/index.php/Swap#Swap_file_creation> for
> an example.
> 
> It is likely simple to implement this, at least the basic (no optional
> args; only mandatory filename/size params) functionality.
> 
> Someone more familiar with toybox source and C can likely do a good
> job for this :-).

Below is a patch to add a simple version like you proposed. It would
be very simple to add the -o and the -k options (if there is need).
I'm not sure about the correct permissions of the possibly newly
created file.

Felix

# HG changeset patch
# User Felix Janda <felix.janda at posteo.de>
# Date 1376073962 -7200
# Node ID 4837af10cc3690c7c56d9ab7c02db1c01f44bd31
# Parent  910f35ff76be14699fc65299b0f08dee4c0c5440
New toy: fallocate

diff -r 910f35ff76be -r 4837af10cc36 toys/other/fallocate.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/toys/other/fallocate.c	Fri Aug 09 20:46:02 2013 +0200
@@ -0,0 +1,30 @@
+/* fallocate.c - Preallocate space to a file
+ *
+ * Copyright 2013 Felix Janda <felix.janda at posteo.de>
+ *
+ * No standard
+
+USE_FALLOCATE(NEWTOY(fallocate, ">1l#|", TOYFLAG_USR|TOYFLAG_BIN))
+
+config FALLOCATE
+  bool "fallocate"
+  default n
+  help
+    usage: fallocate [-l size] file
+
+    Tell the filesystem to allocate space for a file.
+*/
+
+#define FOR_fallocate
+#include "toys.h"
+
+GLOBALS(
+  long size;
+)
+
+void fallocate_main(void)
+{
+  int fd = xcreate(*toys.optargs, O_RDWR | O_CREAT, 0644);
+  if (posix_fallocate(fd, 0, TT.size)) error_exit("Not enough space");
+  if (CFG_TOYBOX_FREE) close(fd);
+}

 1376074655.0


More information about the Toybox mailing list