int our_truncate(char *path, off_t size) { int ret = -1; #if defined(_WINDOWS) || !defined(HAVE_TRUNCATE) int fdes; #endif #ifdef _WINDOWS if((fdes = our_open(path, O_RDWR | O_CREAT | S_IREAD | S_IWRITE | _O_U8TEXT, 0600)) != -1){ if(chsize(fdes, size) == 0) ret = 0; close(fdes); } #else /* UNIX */ #ifdef HAVE_TRUNCATE ret = truncate(fname_to_locale(path), size); #else /* !HAVE_TRUNCATE */ if((fdes = our_open(path, O_RDWR, 0600)) != -1){ ret = chsize(fdes, size) ; if(close(fdes)) ret = -1; } #endif /* !HAVE_TRUNCATE */ #endif /* UNIX */ return ret; }
VOID TUser::Pack () { int fdNew; if (fdDat == -1) fdDat = sopen (DatFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE); if (fdIdx == -1) fdIdx = sopen (IdxFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE); fdNew = sopen ("users.new", O_RDWR|O_BINARY|O_CREAT|O_TRUNC, SH_DENYNO, S_IREAD|S_IWRITE); if (fdNew != -1 && fdDat != -1 && fdIdx != -1) { lseek (fdIdx, 0L, SEEK_SET); lseek (fdDat, 0L, SEEK_SET); while (read (fdIdx, &Idx, sizeof (Idx)) == sizeof (Idx)) { if (Idx.Deleted == FALSE) { lseek (fdDat, Idx.Position, SEEK_SET); read (fdDat, &Usr, sizeof (Usr)); write (fdNew, &Usr, sizeof (Usr)); } } lseek (fdIdx, 0L, SEEK_SET); lseek (fdDat, 0L, SEEK_SET); lseek (fdNew, 0L, SEEK_SET); while (read (fdNew, &Usr, sizeof (Usr)) == sizeof (Usr)) { memset (&Idx, 0, sizeof (Idx)); Idx.Deleted = FALSE; Idx.NameCrc = StringCrc32 (Usr.Name, 0xFFFFFFFFL); Idx.RealNameCrc = StringCrc32 (Usr.RealName, 0xFFFFFFFFL); Idx.Position = tell (fdDat); write (fdDat, &Usr, sizeof (Usr)); write (fdIdx, &Idx, sizeof (Idx)); } chsize (fdDat, tell (fdDat)); chsize (fdIdx, tell (fdIdx)); } if (fdNew != -1) { close (fdNew); unlink ("users.new"); } if (fdIdx != -1) { close (fdIdx); fdIdx = -1; } if (fdDat != -1) { close (fdDat); fdDat = -1; } }
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { int filelength = 0; for(int count = 0;count < 20;count++) { filelength += numobjpersec[count]; } filelength *= (27*4); filelength += (20*4); //reset file to 4 bytes to insure file is no larger than neccesary int handle; handle = open((*mainfile).c_str(),O_TRUNC | O_RDWR | O_BINARY); if(handle != -1) { chsize(handle, 4); close(handle); } //save all info into memory mapped file filehandle = CreateFile((*mainfile).c_str(),GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); filemaphandle = CreateFileMapping(filehandle,NULL,PAGE_READWRITE,0,filelength,NULL); filepointer = MapViewOfFile(filemaphandle,FILE_MAP_WRITE,0,0,0); void* tempfilepntr = filepointer; for(int count = 0;count < 20;count++) { sectors[count].writeinfo(tempfilepntr); } UnmapViewOfFile(filepointer); CloseHandle(filemaphandle); CloseHandle(filehandle); delete mainfile; delete[] sectors; }
int imchsize(IMFILE *imf, long length) { if (length < 0) { errno = EINVAL; return -1; } if (((unsigned long) length) > imf->length) imReserve(imf, (unsigned long) length - imf->length); if (imf->buffer == NULL) return chsize(fileno(imf->stream), length); if ((unsigned long) length > imf->inUse) memset(imf + imf->inUse, (int) (length - (long) imf->inUse), 0); imf->inUse = (unsigned long) length; if (imf->position > imf->inUse) imf->position = imf->inUse; return 0; } /* imchsize */
void status_flush(struct status_output *so) { if (so && so->fd >= 0 && (so->flags & STATUS_OUTPUT_WRITE)) { #if defined(HAVE_FTRUNCATE) { const off_t off = lseek(so->fd, (off_t)0, SEEK_CUR); if (ftruncate(so->fd, off) != 0) { msg(M_WARN, "Failed to truncate status file: %s", strerror(errno)); } } #elif defined(HAVE_CHSIZE) { const long off = (long) lseek(so->fd, (off_t)0, SEEK_CUR); chsize(so->fd, off); } #else /* if defined(HAVE_FTRUNCATE) */ #warning both ftruncate and chsize functions appear to be missing from this OS #endif /* clear read buffer */ if (buf_defined(&so->read_buf)) { ASSERT(buf_init(&so->read_buf, 0)); } } }
/* Riduce il file f di len byte dalla posizione pos */ void shrink(FILE *f, ULONG pos, ULONG len) { #define BLOCK 65535 char buf[BLOCK]; ULONG start, end, tomove, blks; end = filesize(f); tomove = end-pos-len; /* byte da spostare */ start = pos+len; /* posizione iniziale del blocco da spostare */ for (blks=tomove/BLOCK; blks; start+=BLOCK,blks--) { fseek(f,start,SEEK_SET); fread(buf,1,BLOCK,f); fseek(f,start-len,SEEK_SET); fwrite(buf,1,BLOCK,f); } /* Sposta gli ultimi byte */ tomove %= BLOCK; fseek(f,(long)-tomove,SEEK_END); fread(buf,1,tomove,f); fseek(f,(long)-(tomove+len),SEEK_END); fwrite(buf,1,tomove,f); /* Come si imposta la fine di un file in modo ANSIsh??? */ chsize(fileno(f),end-len); }
void truncate(char *fname) { #ifdef __TURBOC__ struct ftime Ftime; #else unsigned short date, time; #endif fd = open(fname, O_WRONLY); #ifdef __TURBOC__ /* Save the time/date */ getftime(fd, &Ftime); #else GETFTIME(fd, (FTIME_T *)&date, (FTIME_T *)&time); #endif chsize(fd, 0L); /* Truncate the file */ #ifdef __TURBOC__ /* Restore the time/date */ setftime(fd, &Ftime); #else SETFTIME(fd, (FTIME_T)date, (FTIME_T)time); #endif close(fd); }
static void resizeTagFile (const long newSize) { int result; #ifdef USE_REPLACEMENT_TRUNCATE result = replacementTruncate (TagFile.name, newSize); #else # ifdef HAVE_TRUNCATE result = truncate (TagFile.name, (off_t) newSize); # else const int fd = open (TagFile.name, O_RDWR); if (fd == -1) result = -1; else { # ifdef HAVE_FTRUNCATE result = ftruncate (fd, (off_t) newSize); # else # ifdef HAVE_CHSIZE result = chsize (fd, newSize); # endif # endif close (fd); } # endif #endif if (result == -1) fprintf (stderr, "Cannot shorten tag file: errno = %d\n", errno); }
void getnodemsg() { char str[81], *buf; int file; ulong length; nodesync(); sprintf(str,"message.%d",node_num); if(flength(str)<1L) /* v1.02 fix */ return; if((file=nopen(str,O_RDWR))==-1) { bprintf("Couldn't open %s\r\n",str); return; } length=filelength(file); if((buf=malloc(length+1L))==NULL) { close(file); bprintf("\7\r\ngetnodemsg: Error allocating %lu bytes of memory for %\r\n" ,length+1L,str); return; } buf[read(file,buf,length)]=0; chsize(file,0); close(file); if(!symbols) strip_symbols(buf); bputs(buf); free(buf); }
void sort_index(smb_t* smb) { ulong l; idxrec_t* idx; printf("Sorting index... "); if((idx=malloc(sizeof(idxrec_t)*smb->status.total_msgs))==NULL) { perror("malloc"); return; } rewind(smb->sid_fp); for(l=0;l<smb->status.total_msgs;l++) if(fread(&idx[l],sizeof(idxrec_t),1,smb->sid_fp)<1) { perror("reading index"); break; } qsort(idx,l,sizeof(idxrec_t) ,(int(*)(const void*, const void*))compare_index); rewind(smb->sid_fp); chsize(fileno(smb->sid_fp),0L); /* Truncate the index */ printf("\nRe-writing index... \n"); smb->status.total_msgs=l; for(l=0;l<smb->status.total_msgs;l++) if(fwrite(&idx[l],sizeof(idxrec_t),1,smb->sid_fp)<1) { perror("writing index"); break; } free(idx); printf("\n"); }
bool ZipPlatform::TruncateFile(int iDes, DWORD iSize) { #if _MSC_VER >= 1400 return _chsize_s(iDes, iSize) == 0; #else return chsize(iDes, iSize) == 0; #endif }
void File::ChangeSize(unsigned long nNewSize) { if(m_pStream!=NULL) { #ifdef WIN32 chsize(fileno(m_pStream),nNewSize) ; #else ftruncate(fileno(m_pStream),nNewSize) ; #endif } }
/* ** Resize a file ** returns 0 if okay -1 if failed. */ Int16 Chsize(int handle,Int32 newsize) { #if DT_OS == 'd' # if DT_CC == 'd' return (Int16)ftruncate(handle, newsize); # elif DT_CC == 'b' || DT_CC == 'm' return (Int16)chsize(handle,newsize); # else # error Chsize unimplemented # endif #elif DT_OS == 'o' # if DT_CC == 'b' return (Int16)chsize(handle,newsize); # else return (Int16)_chsize(handle,newsize); # endif #else return (Int16)ftruncate(handle, newsize); #endif }
int my_chsize( int fd, size_t sz ) { DISABLE_ERRORS; int result = chsize(fd,sz); if(result < 0) { my_errno = errno; } else { my_errno = 0; } RESTORE_ERRORS; return result; }
/*-------------------------------------------------------------------------------------------------- Truncate the file. --------------------------------------------------------------------------------------------------*/ void utTruncateFile( char *fileName, uint64 length) { int file = open(fileName, 0); if(file == -1) { utWarning("utTruncateFile: could not open file %s", fileName); return; } chsize(file, length); close(file); }
void main() { int handle; handle = open( "file", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); if( handle != -1 ) { if( chsize( handle, 32 * 1024L ) != 0 ) { printf( "Error extending file\n" ); } close( handle ); } }
void main(void) { int handle; if ((handle = creat("100ZEROS.DAT", S_IWRITE)) == -1) fprintf(stderr, "Error opening 100ZEROS.DAT"); else { if (chsize(handle, 100L)) printf("Error changing file size\n"); close(handle); } }
int win_truncate(const char *path, off_t length) { int fd, ret, saved_errno; fd = win_open(path, O_WRONLY); if (fd < 0) return -1; ret = chsize(fd, length); saved_errno = errno; win_close(fd); errno = saved_errno; return ret; }
/******************************************************************* ftruncate for operating systems that don't have it ********************************************************************/ int ftruncate(int f,SMB_OFF_T l) { #ifdef HAVE_CHSIZE return chsize(f,l); #else struct flock fl; fl.l_whence = 0; fl.l_len = 0; fl.l_start = l; fl.l_type = F_WRLCK; return fcntl(f, F_FREESP, &fl); #endif }
bool CFile::SetLength(size_t new_len) { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot set length when no file is open.")); #ifdef __WXMSW__ int result = chsize(m_fd, new_len); #else int result = ftruncate(m_fd, new_len); #endif SYSCALL_CHECK((result != -1), wxT("truncating file")); return (result != -1); }
VOID TProtocol::Delete () { int fdNew; USHORT DoClose = FALSE; ULONG Position; if (fdDat == -1) { fdDat = sopen (DataFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE); DoClose = TRUE; } fdNew = sopen ("temp.dat", O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE); if (fdDat != -1 && fdNew != -1) { if ((Position = tell (fdDat)) > 0L) Position -= sizeof (PROTOCOL); lseek (fdDat, 0L, SEEK_SET); while (read (fdDat, &prot, sizeof (PROTOCOL)) == sizeof (PROTOCOL)) { if (strcmp (Key, prot.Key)) write (fdNew, &prot, sizeof (PROTOCOL)); } lseek (fdDat, 0L, SEEK_SET); lseek (fdNew, 0L, SEEK_SET); while (read (fdNew, &prot, sizeof (PROTOCOL)) == sizeof (PROTOCOL)) write (fdDat, &prot, sizeof (PROTOCOL)); chsize (fdDat, tell (fdDat)); lseek (fdDat, Position, SEEK_SET); if (Next () == FALSE) { if (Previous () == FALSE) New (); } } if (fdNew != -1) { close (fdNew); unlink ("temp.dat"); } if (fdDat != -1 && DoClose == TRUE) { close (fdDat); fdDat = -1; } }
/******************************************************************* ftruncate for operating systems that don't have it ********************************************************************/ int rep_ftruncate(int f, off_t l) { #ifdef HAVE_CHSIZE return chsize(f,l); #elif defined(F_FREESP) struct flock fl; fl.l_whence = 0; fl.l_len = 0; fl.l_start = l; fl.l_type = F_WRLCK; return fcntl(f, F_FREESP, &fl); #else #error "you must have a ftruncate function" #endif }
/* Win32 */ int psf_ftruncate (int fd, sf_count_t len) { /* Returns 0 on success, non-zero on failure. */ if (len < 0) return 1 ; /* The global village idiots at micorsoft decided to implement ** nearly all the required 64 bit file offset functions except ** for one, truncate. The fscking morons! ** ** This is not 64 bit file offset clean. Somone needs to clean ** this up. */ if (len > 0x7FFFFFFF) return 1 ; return chsize (fd, len) ; } /* psf_ftruncate */
Boolean System::truncateFile( const char* path, size_t newSize) { Boolean rv = false; int fd = open(path, O_RDWR); if (fd != -1) { if (chsize(fd, (long)newSize) == 0) { rv = true; } close(fd); } return rv; }
bool CFile::SetLength(uint64 new_len) { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot set length when no file is open.")); #ifdef __WINDOWS__ #ifdef _MSC_VER // MSVC has a 64bit version bool result = _chsize_s(m_fd, new_len) == 0; #else // MingW has an old runtime without it bool result = chsize(m_fd, new_len) == 0; #endif #else bool result = ftruncate(m_fd, new_len) != -1; #endif syscall_check(result, m_filePath, wxT("truncating file")); return result; }
BOOL dataWriteFile(FILE* fp, const str_list_t records[], const str_list_t columns, const char* separator ,dataLineCreator_t lineCreator) { size_t count,total; str_list_t list; rewind(fp); if(chsize(fileno(fp),0)!=0) /* truncate */ return(FALSE); if((list=dataCreateList(records,columns,lineCreator))==NULL) return(FALSE); total = strListCount(list); count = strListWriteFile(fp,list,separator); strListFree(&list); return(count == total); }
void set_gat_section(int f, int section) { long l,l1; int i; if (gat_section!=section) { l=filelength(f); l1=((long)section)*GATSECLEN; if (l<l1) { chsize(f,l1); l=l1; } sh_lseek(f,l1,SEEK_SET); if (l<(l1+4096L)) { for (i=0; i<2048; i++) gat[i]=0; sh_write(f,(void *)gat, 4096); } else { sh_read(f,(void *)gat, 4096); } gat_section=section; } }
static int lockfile_write_pid(int fd, pid_t pid) { int ret = -1; char buf[50]; /* * Resets file size to 0. */ #ifdef HAVE_FTRUNCATE ret = ftruncate(fd, 0); #elif HAVE_CHSIZE ret = chsize(fd, 0); #endif if ( ret < 0 ) return prelude_error_from_errno(errno); snprintf(buf, sizeof(buf), "%d\n", (int) pid); ret = write(fd, buf, strlen(buf)); if ( ret < 0 ) return prelude_error_from_errno(errno); return 0; }
void rcs_shiftFile(long offset,long shift,int fdes) { long fileSize; char buffer[4096]; long currentPos; long segSize; fileSize=rcs_numRecords(fdes,1); if (shift>=0) { currentPos=fileSize; while (currentPos>offset) { segSize=(currentPos-offset>=4096) ? 4096 : currentPos-offset; if (segSize==0) return; currentPos-=segSize; lseek(fdes,currentPos,SEEK_SET); read(fdes,buffer,segSize); lseek(fdes,currentPos+shift,SEEK_SET); write(fdes,buffer,segSize); } } else { shift=-shift; currentPos=offset; while (currentPos<fileSize) { segSize=(fileSize-currentPos>4096) ? 4096 : fileSize-currentPos; if (segSize==0) return; lseek(fdes,currentPos+shift,SEEK_SET); read(fdes,buffer,segSize); lseek(fdes,currentPos,SEEK_SET); write(fdes,buffer,segSize); currentPos+=segSize; } #ifdef UNIX ftruncate(fdes,fileSize-shift); #else chsize(fdes,fileSize-shift); #endif } }
/* Win32 */ int psf_ftruncate (SF_PRIVATE *psf, sf_count_t len) { int retval ; /* Returns 0 on success, non-zero on failure. */ if (len < 0) return 1 ; /* The global village idiots at micorsoft decided to implement ** nearly all the required 64 bit file offset functions except ** for one, truncate. The fscking morons! ** ** This is not 64 bit file offset clean. Somone needs to clean ** this up. */ if (len > 0x7FFFFFFF) return -1 ; retval = chsize (psf->filedes, len) ; if (retval == -1) psf_log_syserr (psf, errno) ; return retval ; } /* psf_ftruncate */