32

Is there a fallocate() equivalent in OS X?

I would like to aggregate all of those equivalent in OS X questions into some doc/table or whatever for everyone. Anybody knows something familiar?

Inoperable
  • 1,429
  • 5
  • 17
  • 33
  • It's unfortunate that OS X does not have a `fallocate()` system call. `ftruncate()` takes way more time to extend a file, since it needs to clear the content in the extended area. – Franklin Yu Jul 29 '16 at 16:32

4 Answers4

40

What about using:

mkfile -n 1m test.tmp

It's not the same command but serves the same purpose.

Note that fallocate uses decimal multipliers, whereas mkfile uses binary multipliers.

mkfile man

Gerhard
  • 518
  • 5
  • 9
Panthro
  • 3,247
  • 2
  • 32
  • 37
  • 2
    `mkfile` seems to pad the file with zeroes, while `fallocate` will not set the content to any value (so the file will be full of trash). – Franklin Yu Jul 28 '16 at 21:43
  • 1
    ok, then it's not the exactly same thing :D But, serves the same purpose, answer edited and added man page – Panthro Jul 29 '16 at 08:49
  • Do you know the header file for this? – Sohaib May 02 '17 at 00:08
  • 8
    using the `-n` flag for `mkfile` makes it just allocate the file without zero-ing it – hintss Oct 31 '17 at 22:54
  • 1
    +1 for `-n`. The great thing about fallocate is that you can make enormous files for testing purposes in no time at all, because nothing needs to be written to disk. Perhaps the `-n` flag can be added to this answer? – Andy Foster Mar 30 '21 at 16:53
  • At least on CentOS 7 fallocate do use binary multiplier suffixes. – Lester Cheung Oct 07 '22 at 06:58
17

fallocate() doesn't exist on OSX. You can "fake" it though; Mozilla fakes it in their FileUtils class. See this file:

http://hg.mozilla.org/mozilla-central/file/3d846420a907/xpcom/glue/FileUtils.cpp#l61

Here's the code, in case that link goes stale:

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * ***** BEGIN LICENSE BLOCK *****
  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  *
  * The contents of this file are subject to the Mozilla Public License Version
  * 1.1 (the "License"); you may not use this file except in compliance with
  * the License. You may obtain a copy of the License at
  * http://www.mozilla.org/MPL/
  *
  * Software distributed under the License is distributed on an "AS IS" basis,
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  * for the specific language governing rights and limitations under the
  * License.
  *
  * The Original Code is Mozilla code.
  *
  * The Initial Developer of the Original Code is
  * Mozilla Foundation.
  * Portions created by the Initial Developer are Copyright (C) 2010
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Taras Glek <tglek@mozilla.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
#if defined(XP_UNIX)
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#elif defined(XP_WIN)
#include <windows.h>
#endif

#include "nscore.h"
#include "private/pprio.h"
#include "mozilla/FileUtils.h"

bool 
mozilla::fallocate(PRFileDesc *aFD, PRInt64 aLength) 
{
#if defined(HAVE_POSIX_FALLOCATE)
  return posix_fallocate(PR_FileDesc2NativeHandle(aFD), 0, aLength) == 0;
#elif defined(XP_WIN)
  return PR_Seek64(aFD, aLength, PR_SEEK_SET) == aLength
    && 0 != SetEndOfFile((HANDLE)PR_FileDesc2NativeHandle(aFD));
#elif defined(XP_MACOSX)
  int fd = PR_FileDesc2NativeHandle(aFD);
  fstore_t store = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, aLength};
  // Try to get a continous chunk of disk space
  int ret = fcntl(fd, F_PREALLOCATE, &store);
  if(-1 == ret){
    // OK, perhaps we are too fragmented, allocate non-continuous
    store.fst_flags = F_ALLOCATEALL;
    ret = fcntl(fd, F_PREALLOCATE, &store);
    if (-1 == ret)
      return false;
  }
  return 0 == ftruncate(fd, aLength);
#elif defined(XP_UNIX)
  // The following is copied from fcntlSizeHint in sqlite
  /* If the OS does not have posix_fallocate(), fake it. First use
  ** ftruncate() to set the file size, then write a single byte to
  ** the last byte in each block within the extended region. This
  ** is the same technique used by glibc to implement posix_fallocate()
  ** on systems that do not have a real fallocate() system call.
  */
  struct stat buf;
  int fd = PR_FileDesc2NativeHandle(aFD);
  if (fstat(fd, &buf))
    return false;

  if (buf.st_size >= aLength)
    return false;

  const int nBlk = buf.st_blksize;

  if (!nBlk)
    return false;

  if (ftruncate(fd, aLength))
    return false;
  
  int nWrite; // Return value from write()
  PRInt64 iWrite = ((buf.st_size + 2 * nBlk - 1) / nBlk) * nBlk - 1; // Next offset to write to
  do {
    nWrite = 0;
    if (PR_Seek64(aFD, iWrite, PR_SEEK_SET) == iWrite)
      nWrite = PR_Write(aFD, "", 1);
    iWrite += nBlk;
  } while (nWrite == 1 && iWrite < aLength);
  return nWrite == 1;
#endif
  return false;
}
ZachB
  • 13,051
  • 4
  • 61
  • 89
allquixotic
  • 1,481
  • 2
  • 18
  • 37
  • Do you know which error code is returned when `fcntl` fails because it could not allocate the requested amount of memory? Right now, the code does not distinguish between this and failure because of other reasons (e.g. bad file descriptor). The [man pages](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/fcntl.2.html) are not very clear in this regard. – void-pointer Jul 08 '14 at 05:02
  • Is this an invitation to search the rabbit hole for something that can actually be implemented? – gone Dec 17 '19 at 14:14
  • 1
    I cannot connect to the mozilla source code repo at the moment, but the above snippet has two issues I believe: First of all, the `fstore_t` should be initialized with `F_ALLOCATECONTIG | F_ALLOCATEALL` (these are flags). Secondly, the fifth out param should be initialized to 0 to prevent a compiler warning: `fstore_t store = {F_ALLOCATECONTIG | F_ALLOCATEALL, F_PEOFPOSMODE, 0, aLength, 0};` – milianw Jan 31 '20 at 08:50
1

For those wanting to create fake data files for testing, mkfile is pretty elegant. An alternative is to use dd:

dd if=/dev/zero of=zfile count=1024 bs=1024

As you can see with od -b zfile, it's full of zeros. If you want random data (which you may want for testing workflows with data compression, for example), then use "/dev/random" in place of "/dev/zero":

dd if=/dev/random of=randfile count=1024 bs=1024
Ken Tanaka
  • 35
  • 1
  • 6
  • Does not seem to fill the space in APFS volumes. File is displayed quickly as created with the expected size (checking with `ls -l`) but displaying Information in Finder or `df -h` still reports a lot of free space. – Michael S. Nov 13 '19 at 12:04
  • @MichaelS. It's possible that the OS does some [compression](https://en.wikipedia.org/wiki/Apple_File_System#Compression) but a `df -h .` shows a decrease by 10 GB on my system if I run `df -h .; date; dd if=/dev/zero of=zfile count=10240000 bs=1024 ; date; df -h .` (Allow up to 20 minutes and 100GB free space if you increase the count by a factor of 10. I bracketed the "dd" command with the "date" just to give timing for commands that take longer.) – Ken Tanaka Mar 18 '20 at 20:33
1

I know this question is ten years old, but here's how to do it with dd. If the filesystem supports sparse files, these commands are instantaneous and won't actually claim the space (yet) in the filesystem. If it doesn't, it'll take some time as the filesystem creates the empty file.

NOTE: If you have MacPorts or Homebrew installed, you can also install coreutils and simply use truncate. I'll assume you don't have truncate, but see the end for the same operations using that.

  1. Create a 1000-byte file:

dd if=/dev/zero of=1000-byte-file count=0 bs=1 seek=1000

  1. Create a 20TiB file:

dd if=/dev/zero of=20-tib-file count=0 bs=1 seek=$((20*1024*1024*1024*1024))

  1. Enlarge the 1000-byte file to a 20-TiB one. NOTE: I'm using the oflag=append option here, because I'm using a GNU version of dd, installed via MacPorts. If you're using the native BSD-based dd bundled with macOS, you need to omit that bit. Basically, if you get the error dd: unknown operand oflag, just remove oflag=append and try again.

dd if=/dev/zero of=1000-byte-file count=0 bs=1 seek=$((20*1024*1024*1024*1024)) oflag=append

  1. Shrink (truncate) to a 10-TiB one:

dd if=/dev/zero of=1000-byte-file count=0 bs=1 seek=$((10*1024*1024*1024*1024))

In all examples, we take advantage of the seek feature in dd. It directs the output to start writing at a certain position, in a multiple of the block size bs=1, i.e. one byte. count=0 means "copy zero bs-sized blocks."

So we're saying, "copy zero blocks of 1 byte from /dev/zero and write them to '1000-byte-file' starting 1000 bytes into that output file." When the output file doesn't exist, dd will create it (sparsely) at a size of seek*bs, here 1000*1 bytes.

In the third example where we enlarge a file, we just increase the seek value way beyond the current size of the file, and use the append output flag, telling dd to append to the existing file.

To truncate a file as in the fourth example, simply set a seek value lower than the current size of the file. The default behavior when dd writes to a specific location in an existing file is to truncate whatever data is beyond what's being written, which is why this works for truncating a file. This will of course destroy any data beyond that point.

As promised, here are the same four operations using truncate:

  1. truncate -s 1000 1000-byte-file
  2. truncate -s $((20*1024*1024*1024*1024)) 20-tib-file
  3. truncate -s $((20*1024*1024*1024*1024)) 1000-byte-file
  4. truncate -s $((10*1024*1024*1024*1024)) 1000-byte-file
DanielSmedegaardBuus
  • 977
  • 1
  • 10
  • 18