LCOV - code coverage report
Current view: directory - objdir/xpcom/build - FileUtils.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 2 2 100.0 %
Date: 2012-06-02 Functions: 1 1 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
       2                 :  * ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is Mozilla code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Mozilla Foundation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2010
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Taras Glek <tglek@mozilla.com>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : #if defined(XP_UNIX)
      39                 : #include <fcntl.h>
      40                 : #include <unistd.h>
      41                 : #include <sys/types.h>
      42                 : #include <sys/stat.h>
      43                 : #elif defined(XP_WIN)
      44                 : #include <windows.h>
      45                 : #elif defined(XP_OS2)
      46                 : #define INCL_DOSFILEMGR
      47                 : #include <os2.h>
      48                 : #endif
      49                 : 
      50                 : #include "nscore.h"
      51                 : #include "private/pprio.h"
      52                 : #include "mozilla/FileUtils.h"
      53                 : #include "mozilla/FunctionTimer.h"
      54                 : 
      55                 : bool 
      56             172 : mozilla::fallocate(PRFileDesc *aFD, PRInt64 aLength) 
      57                 : {
      58                 :   NS_TIME_FUNCTION;
      59                 : #if defined(HAVE_POSIX_FALLOCATE)
      60             172 :   return posix_fallocate(PR_FileDesc2NativeHandle(aFD), 0, aLength) == 0;
      61                 : #elif defined(XP_WIN)
      62                 :   PROffset64 oldpos = PR_Seek64(aFD, 0, PR_SEEK_CUR);
      63                 :   if (oldpos == -1)
      64                 :     return false;
      65                 : 
      66                 :   if (PR_Seek64(aFD, aLength, PR_SEEK_SET) != aLength)
      67                 :     return false;
      68                 : 
      69                 :   bool retval = (0 != SetEndOfFile((HANDLE)PR_FileDesc2NativeHandle(aFD)));
      70                 : 
      71                 :   PR_Seek64(aFD, oldpos, PR_SEEK_SET);
      72                 :   return retval;
      73                 : #elif defined(XP_OS2)
      74                 :   return aLength <= PR_UINT32_MAX
      75                 :     && 0 == DosSetFileSize(PR_FileDesc2NativeHandle(aFD), (PRUint32)aLength);
      76                 : #elif defined(XP_MACOSX)
      77                 :   int fd = PR_FileDesc2NativeHandle(aFD);
      78                 :   fstore_t store = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, aLength};
      79                 :   // Try to get a continous chunk of disk space
      80                 :   int ret = fcntl(fd, F_PREALLOCATE, &store);
      81                 :   if (-1 == ret) {
      82                 :     // OK, perhaps we are too fragmented, allocate non-continuous
      83                 :     store.fst_flags = F_ALLOCATEALL;
      84                 :     ret = fcntl(fd, F_PREALLOCATE, &store);
      85                 :     if (-1 == ret)
      86                 :       return false;
      87                 :   }
      88                 :   return 0 == ftruncate(fd, aLength);
      89                 : #elif defined(XP_UNIX)
      90                 :   // The following is copied from fcntlSizeHint in sqlite
      91                 :   /* If the OS does not have posix_fallocate(), fake it. First use
      92                 :   ** ftruncate() to set the file size, then write a single byte to
      93                 :   ** the last byte in each block within the extended region. This
      94                 :   ** is the same technique used by glibc to implement posix_fallocate()
      95                 :   ** on systems that do not have a real fallocate() system call.
      96                 :   */
      97                 :   PROffset64 oldpos = PR_Seek64(aFD, 0, PR_SEEK_CUR);
      98                 :   if (oldpos == -1)
      99                 :     return false;
     100                 : 
     101                 :   struct stat buf;
     102                 :   int fd = PR_FileDesc2NativeHandle(aFD);
     103                 :   if (fstat(fd, &buf))
     104                 :     return false;
     105                 : 
     106                 :   if (buf.st_size >= aLength)
     107                 :     return false;
     108                 : 
     109                 :   const int nBlk = buf.st_blksize;
     110                 : 
     111                 :   if (!nBlk)
     112                 :     return false;
     113                 : 
     114                 :   if (ftruncate(fd, aLength))
     115                 :     return false;
     116                 : 
     117                 :   int nWrite; // Return value from write()
     118                 :   PRInt64 iWrite = ((buf.st_size + 2 * nBlk - 1) / nBlk) * nBlk - 1; // Next offset to write to
     119                 :   do {
     120                 :     nWrite = 0;
     121                 :     if (PR_Seek64(aFD, iWrite, PR_SEEK_SET) == iWrite)
     122                 :       nWrite = PR_Write(aFD, "", 1);
     123                 :     iWrite += nBlk;
     124                 :   } while (nWrite == 1 && iWrite < aLength);
     125                 : 
     126                 :   PR_Seek64(aFD, oldpos, PR_SEEK_SET);
     127                 :   return nWrite == 1;
     128                 : #endif
     129                 :   return false;
     130                 : }

Generated by: LCOV version 1.7