Linux OSX中的fallocate()命令等价物?

Linux OSX中的fallocate()命令等价物?,linux,macos,Linux,Macos,OSX中是否有类似的fallocate() 我想把OSX中的所有等价问题汇总到一些文档/表格中,或者任何适合每个人的东西中。有人知道一些熟悉的东西吗?fallocate()在OSX上不存在。你可以“假装”它;Mozilla在FileUtils类中伪造了它。请参阅此文件: 以下是代码,以防链接过时: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- * ***** BEGIN LICENSE

OSX中是否有类似的
fallocate()

我想把OSX中的所有等价问题汇总到一些文档/表格中,或者任何适合每个人的东西中。有人知道一些熟悉的东西吗?

fallocate()
在OSX上不存在。你可以“假装”它;Mozilla在FileUtils类中伪造了它。请参阅此文件:

以下是代码,以防链接过时:

/* -*- 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;
}
<代码> /*--模式:C++;标签宽度:2;缩进制表符模式:无;c-基本偏移量:2-*- *******开始许可证阻止***** *版本:MPL 1.1/GPL 2.0/LGPL 2.1 * *此文件的内容以Mozilla公共许可证版本为准 *1.1(“许可证”);除非符合以下要求,否则不得使用此文件 *执照。您可以通过以下方式获得许可证副本: * http://www.mozilla.org/MPL/ * *根据许可证分发的软件按“原样”分发, *无任何明示或暗示的担保。看到许可证了吗 *适用于本协议项下权利和限制的具体语言 *执照。 * *原始代码是Mozilla代码。 * *原始代码的初始开发人员是 * Mozilla基金会。 *最初开发人员创建的部分版权归(C)2010所有 *最初的开发者。版权所有。 * *投稿人: *塔拉斯格里克酒店 * *或者,本文件的内容可根据以下条款使用: *GNU通用公共许可证版本2或更高版本(“GPL”),或 *GNU Lesser通用公共许可证2.1版或更高版本(“LGPL”), *在这种情况下,GPL或LGPL的规定适用 *上面的那些。如果您希望仅允许使用此文件的版本 *根据GPL或LGPL的条款,不允许其他人 *根据MPL条款使用此文件的版本,并指出您的 *决定删除上述规定,并以通知取代 *以及GPL或LGPL要求的其他规定。如果不删除 *根据上述规定,收件人可在以下情况下使用您的本文件版本: *MPL、GPL或LGPL中任何一项的条款。 * *****结束许可证阻止*******/ #如果已定义(XP\U UNIX) #包括 #包括 #包括 #包括 #定义的elif(XP_WIN) #包括 #恩迪夫 #包括“nscore.h” #包括“private/pprio.h” #包括“mozilla/FileUtils.h” 布尔 mozilla::fallocate(PRFileDesc*aFD,PRInt64 aLength) { #如果已定义(使其处于空闲状态) 返回posix_fallocate(PR_FileDesc2NativeHandle(aFD),0,aLength)=0; #定义的elif(XP_WIN) 返回PR_Seek64(aFD、aLength、PR_SEEK_SET)=aLength &&0!=SetEndOfFile((句柄)PR_FileDesc2NativeHandle(aFD)); #定义的elif(XP_MACOSX) int fd=PR_FileDesc2NativeHandle(aFD); fstore_t store={F_ALLOCATECONTIG,F_peoposmode,0,aLength}; //尝试获取一块连续的磁盘空间 int ret=fcntl(fd、F_预分配和存储); 如果(-1==ret){ //好吧,也许我们太支离破碎了,分配不连续 store.fst_flags=F_allocatell; ret=fcntl(fd、F_预分配和存储); 如果(-1==ret) 返回false; } 返回0==ftruncate(fd,aLength); #已定义的elif(XP\U UNIX) //下面是从sqlite中的fcntlSizeHint复制的 /*如果操作系统没有posix_fallocate(),请伪造它。首先使用 **ftruncate()设置文件大小,然后向 **扩展区域内每个块中的最后一个字节 **glibc实现posix_fallocate()所使用的技术是否相同 **在没有真正的fallocate()系统调用的系统上。 */ 结构统计buf; int fd=PR_FileDesc2NativeHandle(aFD); if(fstat(fd和buf)) 返回false; 如果(基本尺寸>=长度) 返回false; const int nBlk=buf.st_blksize; 如果(!nBlk) 返回false; if(ftruncate(fd,aLength)) 返回false; int nWrite;//从write()返回值 PRInt64 iWrite=((buf.st_size+2*nBlk-1)/nBlk)*nBlk-1;//要写入的下一个偏移量 做{ nWrite=0; if(PR_Seek64(aFD、iWrite、PR_SEEK_SET)==iWrite) nWrite=PR_Write(aFD,“,1); iWrite+=nBlk; }而(nWrite==1&&iWrite如何使用:

mkfile 1m test.tmp

这不是同一个命令,而是为了同一个目的


对于那些想要创建用于测试的假数据文件的人来说,
mkfile
非常优雅。另一种方法是使用
dd

dd if=/dev/zero of=zfile count=1024 bs=1024
正如您在
od-bzfile
中看到的,它充满了零。如果您需要随机数据(例如,您可能需要使用数据压缩测试工作流),请使用“/dev/random”代替“/dev/zero”:


您知道当
fcntl
由于无法分配请求的内存量而失败时,会返回哪个错误代码吗?目前,由于其他原因(例如,错误的文件描述符),代码没有区分这一点和失败。这方面的问题还不是很清楚。这是不是邀请我们搜索兔子洞,寻找真正可以实现的东西?我目前无法连接到mozilla源代码repo,但我认为上面的代码片段有两个问题:首先,
fstore\u t
应使用
F|u allocateltig | F|u allocatell
初始化(这些是标志)。其次,第五个out参数应该初始化为0,以防止出现编译器警告:
fstore_t store={F_ALLOCATECONTIG | F_allocatell,F_peoposmode,0,aLength,0}
mkfile
似乎用零填充文件,而
fallocate
将不会将内容设置为任何值(因此文件将充满垃圾)。好的,那么它不是完全相同的东西:D,但用于相同的目的,回答编辑和添加的手册页您知道此文件的头文件吗?使用
-n
标记
mkfile
使其只分配文件,而不将
-n的头文件置零+1dd if=/dev/random of=randfile count=1024 bs=1024