NS_IMETHODIMP nsFileStream::SetEOF() { if (mFD == nsnull) return NS_BASE_STREAM_CLOSED; #if defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS) // Some system calls require an EOF offset. PRInt64 offset; nsresult rv = Tell(&offset); if (NS_FAILED(rv)) return rv; #endif #if defined(XP_UNIX) || defined(XP_BEOS) if (ftruncate(PR_FileDesc2NativeHandle(mFD), offset) != 0) { NS_ERROR("ftruncate failed"); return NS_ERROR_FAILURE; } #elif defined(XP_WIN) if (!SetEndOfFile((HANDLE) PR_FileDesc2NativeHandle(mFD))) { NS_ERROR("SetEndOfFile failed"); return NS_ERROR_FAILURE; } #elif defined(XP_OS2) if (DosSetFileSize((HFILE) PR_FileDesc2NativeHandle(mFD), offset) != NO_ERROR) { NS_ERROR("DosSetFileSize failed"); return NS_ERROR_FAILURE; } #else // XXX not implemented #endif return NS_OK; }
/* ** Truncate an open file to a specified size */ static int os2Truncate( sqlite3_file *id, i64 nByte ){ APIRET rc = NO_ERROR; os2File *pFile = (os2File*)id; OSTRACE3( "TRUNCATE %d %lld\n", pFile->h, nByte ); SimulateIOError( return SQLITE_IOERR_TRUNCATE ); rc = DosSetFileSize( pFile->h, nByte ); return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE; }
HB_ULONG hb_fsOS2DosSetFileSizeL( HB_FHANDLE hFile, HB_FOFFSET nSize ) { APIRET ret; if( hb_isWSeB() ) ret = s_DosSetFileSizeL( ( HFILE ) hFile, ( LONGLONG ) nSize ); else ret = DosSetFileSize( ( HFILE ) hFile, ( ULONG ) nSize ); hb_fsSetError( ( HB_ERRCODE ) ret ); return ret; }
bool mozilla::fallocate(PRFileDesc *aFD, int64_t aLength) { #if defined(HAVE_POSIX_FALLOCATE) return posix_fallocate(PR_FileDesc2NativeHandle(aFD), 0, aLength) == 0; #elif defined(XP_WIN) int64_t oldpos = PR_Seek64(aFD, 0, PR_SEEK_CUR); if (oldpos == -1) return false; if (PR_Seek64(aFD, aLength, PR_SEEK_SET) != aLength) return false; bool retval = (0 != SetEndOfFile((HANDLE)PR_FileDesc2NativeHandle(aFD))); PR_Seek64(aFD, oldpos, PR_SEEK_SET); return retval; #elif defined(XP_OS2) return aLength <= UINT32_MAX && 0 == DosSetFileSize(PR_FileDesc2NativeHandle(aFD), (uint32_t)aLength); #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. */ int64_t oldpos = PR_Seek64(aFD, 0, PR_SEEK_CUR); if (oldpos == -1) return false; 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() int64_t iWrite = ((buf.st_size + 2 * nBlk - 1) / nBlk) * nBlk - 1; // Next offset to write to while (iWrite < aLength) { nWrite = 0; if (PR_Seek64(aFD, iWrite, PR_SEEK_SET) == iWrite) nWrite = PR_Write(aFD, "", 1); if (nWrite != 1) break; iWrite += nBlk; } PR_Seek64(aFD, oldpos, PR_SEEK_SET); return nWrite == 1; #endif return false; }
int main(VOID) { HFILE hfFileHandle = 0L; /* Handle for file being manipulated */ ULONG ulAction = 0; /* Action taken by DosOpen */ ULONG ulWrote = 0; /* Number of bytes written by DosWrite */ UCHAR uchFileName[20] = "dosman.dat", /* Name of file */ uchFileData[4] = "DATA"; /* Data to write to file */ APIRET rc = NO_ERROR; /* Return code */ /* Open the file dosman.dat. Use an existing file or create a new */ /* one if it doesn't exist. */ rc = DosOpen(uchFileName, &hfFileHandle, &ulAction, 4L, FILE_ARCHIVED | FILE_NORMAL, OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS, OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE, 0L); if (rc != NO_ERROR) { printf("DosOpen error: return code = %u\n", rc); return 1; } rc = DosWrite (hfFileHandle, (PVOID) uchFileData, sizeof (uchFileData), &ulWrote); if (rc != NO_ERROR) { printf("DosWrite error: return code = %u\n", rc); return 1; } rc = DosResetBuffer (hfFileHandle); if (rc != NO_ERROR) { printf("DosResetBuffer error: return code = %u\n", rc); return 1; } /* endif */ rc = DosSetFileSize (hfFileHandle, 8L); /* Change file size */ if (rc != NO_ERROR) { printf("DosSetFileSize error: return code = %u\n", rc); return 1; } return NO_ERROR; }
APIRET XIOfile32::doSetFileSize(int64_t size) { if (size > 0x7ffffff || size < 0) return ERROR_INVALID_PARAMETER; return DosSetFileSize(s_handle, (long)size); }
BOOL WriteConfigFile(HWND hwndParent,PSZ szFileSpec,PVOID pBuffer,ULONG cbCount) { int iIndex; HFILE hFile; char szBackupSpec[CCHMAXPATH + 1]; APIRET rc; ULONG ulStatus; char szMessage[120]; char szCaption[80]; char *pchDot; strcpy(szBackupSpec,szFileSpec); for (iIndex = (strlen(szBackupSpec) - 1);iIndex > 0;iIndex--) if (szBackupSpec[iIndex] == '.') break; iIndex++; if (iIndex <= 2) { iIndex = strlen(szFileSpec); szBackupSpec[iIndex++] = '.'; } pchDot = &szBackupSpec[iIndex]; strcpy(pchDot,"001"); iIndex = 2; while ((rc = DosOpen(szBackupSpec,&hFile,&ulStatus,0L,0,0x0010,0x0020,(PEAOP2)0L)) != 0) { // if (hwndParent != NULLHANDLE) // MessageBox(HWND_DESKTOP,szBackupSpec); sprintf(pchDot,"%03X",iIndex++); if (iIndex > 0xfff) { DosClose(hFile); if (hwndParent != NULLHANDLE) { sprintf(szMessage,"%s was not updated! Could not make backup",szFileSpec,rc); MessageBox(HWND_DESKTOP,szMessage); } return(FALSE); } } DosClose(hFile); DosCopy(szFileSpec,szBackupSpec,DCPY_EXISTING); if (hwndParent != NULLHANDLE) { sprintf(szMessage,"The current system configuration file, %s, was renamed to %s",szFileSpec,szBackupSpec); sprintf(szCaption,"System configuration file was backed up."); WinMessageBox(HWND_DESKTOP, hwndParent, szMessage, szCaption, 0L, (MB_OK | MB_ICONASTERISK)); } if ((rc = DosOpen(szFileSpec,&hFile,&ulStatus,0L,0,1,0x0022,(PEAOP2)0L)) != 0) { if (hwndParent != NULLHANDLE) { sprintf(szMessage,"%s was not updated! Could not reopen - Error = %u",szFileSpec,rc); MessageBox(HWND_DESKTOP,szMessage); } return(FALSE); } DosSetFileSize(hFile,cbCount); DosWrite(hFile,pBuffer,cbCount,&cbCount); DosClose(hFile); return(TRUE); }
//----------------------------- CMD_DosSetFileSize ----------------------------- void CMD_DosSetFileSize(HFILE hFile,LXIOCPA_DMN_CMDPARMPACKET* pParam ,PLXDOSSETFILESIZESTRUCT pfs) { pParam->rc=DosSetFileSize(pfs->hFile,pfs->cbSize); }
STATIC APIRET WriteINI(PXINI pXIni) // in: profile opened with xprfOpenProfile { APIRET arc = NO_ERROR; ULONG ulTotalFileSize = sizeof(INIFILE_HEADER); ULONG ulSet = 0; PBYTE pbData2Write = NULL; // check how much memory we'll need: // one INIFILE_HEADER // + for each app: one INIFILE_APP plus app name length // + for each key: one INIFILE_KEY plus key name length plus data length // go thru all apps PLISTNODE pAppNode = lstQueryFirstNode(&pXIni->llApps); while (pAppNode) { PLISTNODE pKeyNode; PXINIAPPDATA pAppDataThis = (PXINIAPPDATA)pAppNode->pItemData; // one INIFILE_APP plus app name length for each app ulTotalFileSize += sizeof(INIFILE_APP) + strlen(pAppDataThis->pszAppName) + 1; // null terminator // for each app, go thru all keys pKeyNode = lstQueryFirstNode(&pAppDataThis->llKeys); while (pKeyNode) { PXINIKEYDATA pKeyDataThis = (PXINIKEYDATA)pKeyNode->pItemData; ulTotalFileSize += sizeof(INIFILE_KEY) + strlen(pKeyDataThis->pszKeyName) + pKeyDataThis->cbData + 1; // null terminator pKeyNode = pKeyNode->pNext; } pAppNode = pAppNode->pNext; } // allocate buffer for total size if (pbData2Write = (PBYTE)malloc(ulTotalFileSize)) { // set header in buffer PINIFILE_HEADER pHeader = (PINIFILE_HEADER)pbData2Write; // pointer into buffer for current write // (used below) ULONG ulCurOfs = sizeof(INIFILE_HEADER); // 1) set up header pHeader->magic = 0xFFFFFFFF; if (lstCountItems(&pXIni->llApps)) // we have any applications: pHeader->offFirstApp = sizeof(INIFILE_HEADER); // offset of first application in file else // empty INI file: pHeader->offFirstApp = 0; pHeader->lenFile = ulTotalFileSize; pHeader->filler1 = 0; pHeader->filler2 = 0; // 2) for-all-applications loop pAppNode = lstQueryFirstNode(&pXIni->llApps); while (pAppNode) { PXINIAPPDATA pAppDataThis = (PXINIAPPDATA)pAppNode->pItemData; ULONG cbAppName = strlen(pAppDataThis->pszAppName) + 1; // layout of application entry in file: // -- struct PINIFILE_APP (ulCurOfs right now) // -- application name (null-terminated) // -- INIFILE_KEY 1 // -- INIFILE_KEY 2 ... // -- next struct PINIFILE_APP // make pointer to application entry PINIFILE_APP pIniAppCurrent = (PINIFILE_APP)(pbData2Write + ulCurOfs); // write out application entry // pIniAppCurrent->offNextApp = 0; // pIniAppCurrent->offFirstKeyInApp = ulCurOfs + cbAppName; pIniAppCurrent->filler1 = 0; pIniAppCurrent->lenAppName = pIniAppCurrent->_lenAppName = cbAppName; // set offset to application name: put this right after the application ulCurOfs += sizeof(INIFILE_APP); pIniAppCurrent->offAppName = ulCurOfs; // write app name (null-terminated) memcpy(pbData2Write + ulCurOfs, pAppDataThis->pszAppName, cbAppName); ulCurOfs += cbAppName; // ulCurOfs points to INIFILE_KEY entry now if (pAppDataThis->cKeys) { // we have keys: PLISTNODE pKeyNode = lstQueryFirstNode(&pAppDataThis->llKeys); pIniAppCurrent->offFirstKeyInApp = ulCurOfs; // 3) for-all-keys loop per application while (pKeyNode) { PXINIKEYDATA pKeyDataThis = (PXINIKEYDATA)pKeyNode->pItemData; ULONG cbKeyName = strlen(pKeyDataThis->pszKeyName) + 1; PINIFILE_KEY pIniKeyCurrent = (PINIFILE_KEY)(pbData2Write + ulCurOfs); pIniKeyCurrent->filler1 = 0; ulCurOfs += sizeof(INIFILE_KEY); // has offset to key name now // a) key name pIniKeyCurrent->lenKeyName = pIniKeyCurrent->_lenKeyName = cbKeyName; pIniKeyCurrent->offKeyName = ulCurOfs; memcpy(pbData2Write + ulCurOfs, pKeyDataThis->pszKeyName, cbKeyName); ulCurOfs += cbKeyName; // has offset to data now // b) data pIniKeyCurrent->lenKeyData = pIniKeyCurrent->_lenKeyData = pKeyDataThis->cbData; pIniKeyCurrent->offKeyData = ulCurOfs; memcpy(pbData2Write + ulCurOfs, pKeyDataThis->pbData, pKeyDataThis->cbData); ulCurOfs += pKeyDataThis->cbData; // points to after all key data now; // this receives either the next key/data block // or the next application or nothing // ofs of next key: if (pKeyNode->pNext) pIniKeyCurrent->offNextKeyInApp = ulCurOfs; else // last key: pIniKeyCurrent->offNextKeyInApp = 0; pKeyNode = pKeyNode->pNext; } // ulCurOfs points to after the last keys entry now } else // no keys: pIniAppCurrent->offFirstKeyInApp = 0; // done with keys (there may be none!); // now set offset to next app in current application if (pAppNode->pNext) pIniAppCurrent->offNextApp = ulCurOfs; else // this was the last one: pIniAppCurrent->offNextApp = 0; // next app pAppNode = pAppNode->pNext; } // write out everything if (!(arc = DosSetFilePtr(pXIni->hFile, 0, FILE_BEGIN, &ulSet))) { ULONG cbWritten = 0; if (!(arc = DosWrite(pXIni->hFile, pbData2Write, ulTotalFileSize, &cbWritten))) { if (!(arc = DosSetFileSize(pXIni->hFile, ulTotalFileSize))) ; } } free(pbData2Write); } return arc; }