コード例 #1
0
bool System::fileTruncate(FILE *f ,_I64 s)
{
  int fno=fileno(f);
  int ret = _chsize_s(fno,s);

  return ret == 0;
}
コード例 #2
0
ファイル: HawkDiskFile.cpp プロジェクト: YunYi/hawkutil
	Bool  HawkDiskFile::Chsize(Int64 iSize)
	{
		if(!m_pFile) return false;

		if(m_eOpenType == OPEN_WRITE || m_eOpenType == OPEN_RW || m_eOpenType == OPEN_APPEND)
		{
			Seek(0);

#ifdef PLATFORM_WINDOWS
			return _chsize_s(m_pFile->_file, iSize) == 0? true:false;
#else
			return _chsize_s(m_pFile->_fileno, iSize) == 0? true:false;
#endif
		}
		return false;
	}
コード例 #3
0
xint32 xi_file_ftruncate(xint32 fd, xoff64 len) {
	xint32 ret = _chsize_s(fd, len);
	if (ret) {
		return XI_FILE_RV_ERR_ARGS;
	}
	return ret;
}
コード例 #4
0
ファイル: posix_w32.c プロジェクト: DaneTheory/libgit2
int p_ftruncate(int fd, long size)
{
#if defined(_MSC_VER) && _MSC_VER >= 1500
	return _chsize_s(fd, size);
#else
	return _chsize(fd, size);
#endif
}
コード例 #5
0
	static bool TruncateFileDefault(Stream& file, uint64 size)
	{
		// Default implementation

		// Getting the current end of file
		if (not file.seekFromEndOfFile(0))
			return false;
		ssize_t end = (ssize_t) file.tell();

		#	ifndef YUNI_OS_MSVC
		bool result = (0 == ::ftruncate(FILENO(file.nativeHandle()), (off_t) size));
		#	else
		bool result = (0 == _chsize_s(FILENO(file.nativeHandle()), (sint64) size));
		#	endif
		if (result)
		{
			// if the file was already bigger than the new size, there is nothing to do
			if ((uint64)end >= size)
				return true;

			if (not file.seekFromBeginning(end))
				return false;

			enum
			{
				bufferSize = 1024 * 1024
			};
			size -= (uint64) end;

			if (size)
			{
				char* zero = new char[bufferSize];
				(void)::memset(zero, '\0', sizeof(char) * bufferSize);

				while (size > bufferSize)
				{
					if (bufferSize != file.write(zero, bufferSize))
					{
						delete[] zero;
						return false;
					}
					size -= bufferSize;
				}

				if (size)
				{
					if (size != file.write(zero, size))
					{
						delete[] zero;
						return false;
					}
				}
				delete[] zero;
			}
			return true;
		}
		return false;
	}
コード例 #6
0
ファイル: ZipPlatform.cpp プロジェクト: KerwinMa/QuickLog
bool ZipPlatform::TruncateFile(int iDes, DWORD iSize)
{
#if _MSC_VER >= 1400	
	return _chsize_s(iDes, iSize) == 0;
#else
	return chsize(iDes, iSize) == 0;
#endif

}
コード例 #7
0
ファイル: pl-nt.c プロジェクト: imccoy/prechac-droid
int
ftruncate(int fileno, int64_t length)
{ errno_t e;

  if ( (e=_chsize_s(fileno, length)) == 0 )
    return 0;

  errno = e;
  return -1;
}
コード例 #8
0
size_t
HostFileHandle::truncate()
{
   decaf_check(mHandle);
   decaf_check((mMode & File::Write) || (mMode & File::Update));
   auto length = size();

   if (_chsize_s(_fileno(mHandle), static_cast<long long>(length))) {
      return 0;
   }

   return length;
}
コード例 #9
0
ファイル: BufferedFile.cpp プロジェクト: demonlife/infinidb
int BufferedFile::truncate(off64_t length)
{
	int ret = 0;
#ifdef _MSC_VER
	ret = _chsize_s(_fileno(m_fp), length);
#else
	ret = ftruncate(fileno(m_fp),length);
#endif
	if( IDBLogger::isEnabled() )
		IDBLogger::logTruncate(m_fname, this, length, ret);

	return ret;
}
コード例 #10
0
  /**
   * truncates a file to the given length, if the file exists and can be opened
   * for writing.
   */
  inline void truncate(const std::string& filename, size_t length)
  {
#if defined(_WIN32)
    int fd = -1;
    _sopen_s(&fd, filename.c_str(), _O_WRONLY | _O_BINARY, _SH_DENYNO, _S_IWRITE);
    if (fd != -1)
    {
      _chsize_s(fd, static_cast<__int64>(length));
      _close(fd);
    }
#else
    ::truncate(filename.c_str(), static_cast<off_t>(length));
#endif
  }
コード例 #11
0
ファイル: file_util.cpp プロジェクト: DaMan69/citra
bool IOFile::Resize(u64 size) {
    if (!IsOpen() || 0 !=
#ifdef _WIN32
                         // ector: _chsize sucks, not 64-bit safe
                         // F|RES: changed to _chsize_s. i think it is 64-bit safe
                         _chsize_s(_fileno(m_file), size)
#else
                         // TODO: handle 64bit and growing
                         ftruncate(fileno(m_file), size)
#endif
    )
        m_good = false;

    return m_good;
}
コード例 #12
0
	bool Stream::truncate(uint64 size, bool ensureAllocation)
	{
		if (pFd)
		{
			int fd = FILENO(pFd);

			if (not ensureAllocation)
			{
				# ifndef YUNI_OS_MSVC
				return (0 == ::ftruncate(fd, (off_t) size));
				# else
				return (0 == _chsize_s(fd, (sint64) size));
				# endif
			}
			else
			{
				# ifdef YUNI_HAS_POSIX_FALLOCATE
				return (0 == posix_fallocate(fd, 0, (off_t) size));
				# else

				# ifdef YUNI_OS_MAC
				// On OS X, we can use fcntl(F_PREALLOCATE) to emulate posix_fallocate
				// (but ftruncate must be called anyway)
				fstore_t store;
				memset(&store, 0, sizeof(store));
				store.fst_flags    = F_ALLOCATECONTIG;
				store.fst_posmode  = F_PEOFPOSMODE;
				store.fst_length   = (off_t) size;

				if (-1 == fcntl(fd, F_PREALLOCATE, &store))
				{
					// OK, perhaps we are too fragmented, allocate non-continuous
					store.fst_flags = F_ALLOCATEALL;
					if (-1 == fcntl(fd, F_PREALLOCATE, &store))
						return false;
				}
				return (0 == ::ftruncate(fd, (off_t) size));

				# else

				return TruncateFileDefault(*this, size);

				# endif // OS X
				# endif // POSIX_FALLOCATE
			}
		}
		return false;
	}
コード例 #13
0
ファイル: os_windows.c プロジェクト: peluse/nvml
/*
 * os_posix_fallocate -- allocate file space
 */
int
os_posix_fallocate(int fd, os_off_t offset, os_off_t len)
{
	/*
	 * From POSIX:
	 * "EINVAL -- The len argument was zero or the offset argument was
	 * less than zero."
	 *
	 * From Linux man-page:
	 * "EINVAL -- offset was less than 0, or len was less than or
	 * equal to 0"
	 */
	if (offset < 0 || len <= 0)
		return EINVAL;

	/*
	 * From POSIX:
	 * "EFBIG -- The value of offset+len is greater than the maximum
	 * file size."
	 *
	 * Overflow can't be checked for by _chsize_s, since it only gets
	 * the sum.
	 */
	if (offset + len < offset)
		return EFBIG;

	/*
	 * posix_fallocate should not clobber errno, but
	 * _filelengthi64 might set errno.
	 */
	int orig_errno = errno;

	__int64 current_size = _filelengthi64(fd);

	int file_length_errno = errno;
	errno = orig_errno;

	if (current_size < 0)
		return file_length_errno;

	__int64 requested_size = offset + len;

	if (requested_size <= current_size)
		return 0;

	return _chsize_s(fd, requested_size);
}
コード例 #14
0
ファイル: posix_w32.c プロジェクト: 1336/libgit2
/**
 * Truncate or extend file.
 *
 * We now take a "git_off_t" rather than "long" because
 * files may be longer than 2Gb.
 */
int p_ftruncate(int fd, git_off_t size)
{
	if (size < 0) {
		errno = EINVAL;
		return -1;
	}

#if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
	return ((_chsize_s(fd, size) == 0) ? 0 : -1);
#else
	/* TODO MINGW32 Find a replacement for _chsize() that handles big files. */
	if (size > INT32_MAX) {
		errno = EFBIG;
		return -1;
	}
	return _chsize(fd, (long)size);
#endif
}
コード例 #15
0
/**
 * Truncate the file.
 * @param size New size. (default is 0)
 * @return 0 on success; -1 on error.
 */
int RpFile::truncate(int64_t size)
{
	if (!m_file || !(m_mode & FM_WRITE)) {
		// Either the file isn't open,
		// or it's read-only.
		m_lastError = EBADF;
		return -1;
	} else if (size < 0) {
		m_lastError = EINVAL;
		return -1;
	}

	// Get the current position.
	int64_t pos = ftello(m_file.get());
	if (pos < 0) {
		m_lastError = errno;
		return -1;
	}

	// Truncate the file.
	fflush(m_file.get());
#ifdef _WIN32
	int ret = _chsize_s(fileno(m_file.get()), size);
#else
	int ret = ftruncate(fileno(m_file.get()), size);
#endif
	if (ret != 0) {
		m_lastError = errno;
		return -1;
	}

	// If the previous position was past the new
	// file size, reset the pointer.
	if (pos > size) {
		ret = fseeko(m_file.get(), size, SEEK_SET);
		if (ret != 0) {
			m_lastError = errno;
			return -1;
		}
	}

	// File truncated.
	return 0;
}
コード例 #16
0
bool os_file_truncate(const std::string &fn, int64 fsize)
{
	int fh;
	if( _wsopen_s ( &fh, ConvertToWchar(fn).c_str(), _O_RDWR | _O_CREAT, _SH_DENYNO,
            _S_IREAD | _S_IWRITE ) == 0 )
	{
		if( _chsize_s( fh, fsize ) != 0 )
		{
			_close( fh );
			return false;
		}
		_close( fh );
		return true;
	}
	else
	{
		return false;
	}
}
コード例 #17
0
ファイル: futil.cpp プロジェクト: pjohansson/gromacs
int gmx_truncate(const char *filename, gmx_off_t length)
{
#ifdef GMX_NATIVE_WINDOWS
    FILE *fp = fopen(filename, "rb+");
    if (fp == NULL)
    {
        return -1;
    }
#ifdef _MSC_VER
    int rc = _chsize_s(fileno(fp), length);
#else
    int rc = _chsize(fileno(fp), length);
#endif
    fclose(fp);
    return rc;
#else
    return truncate(filename, length);
#endif
}
コード例 #18
0
ファイル: CFile.cpp プロジェクト: StrongZhu/amule
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;
}
コード例 #19
0
bool ZipPlatform::TruncateFile(int iDes, ULONGLONG uSize)
{	
#if (_MSC_VER >= 1400)
	return _chsize_s(iDes, uSize) == 0;
#else
	if (uSize > _I64_MAX)
		CZipException::Throw(CZipException::tooBigSize);
	else
	{
		HANDLE handle = (HANDLE)GetFileSystemHandle(iDes);
		ULARGE_INTEGER li;
		li.QuadPart = uSize;
		li.LowPart = SetFilePointer(handle, li.LowPart, (LONG*)&li.HighPart, FILE_BEGIN);
		if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
			return false;
		return SetEndOfFile(handle) == TRUE;
	}
	return false; // for the compiler
#endif

}
コード例 #20
0
ファイル: file_operations.hpp プロジェクト: 1901/libmoost
  /**
   * \brief Change the file of a size.  Smaller sizes will truncate the file, larger sizes will pad
   *        the file with zeroes.
   */
  static void change_size(const std::string & path, boost::int64_t length)
  {
#if defined(__GNUC__)
    if (truncate(path.c_str(), length) != 0)
      throw std::runtime_error("could not change size: " + get_error_string(errno));
#elif defined(_MSC_VER)
    int fh;

    if( _sopen_s( &fh, path.c_str(), _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE ) == 0 )
    {
      int result = _chsize_s( fh, length );
      if (result != 0)
      {
        _close( fh );
        throw std::runtime_error( std::string("could not change size: ") + get_error_string(errno) );
      }
      _close( fh );
    }
    else
      throw std::runtime_error("could not open file: " + get_error_string(errno));
#endif
  }
コード例 #21
0
ファイル: demo.c プロジェクト: jspenguin/lab3d-sdl
static void truncate_file(FILE* f) {
    int fd = _fileno(f);
    fflush(f);
    _chsize_s(fd, _ftelli64(fd));
}
コード例 #22
0
/**
 * Write offset to offset file.
 *
 * Locality: toppar's broker thread
 */
static rd_kafka_resp_err_t
rd_kafka_offset_file_commit (rd_kafka_toppar_t *rktp) {
	rd_kafka_itopic_t *rkt = rktp->rktp_rkt;
	int attempt;
        rd_kafka_resp_err_t err = RD_KAFKA_RESP_ERR_NO_ERROR;
        int64_t offset = rktp->rktp_stored_offset;

	for (attempt = 0 ; attempt < 2 ; attempt++) {
		char buf[22];
		int len;

		if (!rktp->rktp_offset_fp)
			if (rd_kafka_offset_file_open(rktp) == -1)
				continue;

		if (fseek(rktp->rktp_offset_fp, 0, SEEK_SET) == -1) {
			rd_kafka_op_err(rktp->rktp_rkt->rkt_rk,
					RD_KAFKA_RESP_ERR__FS,
					"%s [%"PRId32"]: "
					"Seek failed on offset file %s: %s",
					rktp->rktp_rkt->rkt_topic->str,
					rktp->rktp_partition,
					rktp->rktp_offset_path,
					rd_strerror(errno));
                        err = RD_KAFKA_RESP_ERR__FS;
			rd_kafka_offset_file_close(rktp);
			continue;
		}

		len = rd_snprintf(buf, sizeof(buf), "%"PRId64"\n", offset);

		if (fwrite(buf, 1, len, rktp->rktp_offset_fp) < 1) {
			rd_kafka_op_err(rktp->rktp_rkt->rkt_rk,
					RD_KAFKA_RESP_ERR__FS,
					"%s [%"PRId32"]: "
					"Failed to write offset %"PRId64" to "
					"offset file %s: %s",
					rktp->rktp_rkt->rkt_topic->str,
					rktp->rktp_partition,
					offset,
					rktp->rktp_offset_path,
					rd_strerror(errno));
                        err = RD_KAFKA_RESP_ERR__FS;
			rd_kafka_offset_file_close(rktp);
			continue;
		}

                /* Need to flush before truncate to preserve write ordering */
                (void)fflush(rktp->rktp_offset_fp);

		/* Truncate file */
#ifdef _MSC_VER
		if (_chsize_s(_fileno(rktp->rktp_offset_fp), len) == -1)
			; /* Ignore truncate failures */
#else
		if (ftruncate(fileno(rktp->rktp_offset_fp), len) == -1)
			; /* Ignore truncate failures */
#endif
		rd_kafka_dbg(rktp->rktp_rkt->rkt_rk, TOPIC, "OFFSET",
			     "%s [%"PRId32"]: wrote offset %"PRId64" to "
			     "file %s",
			     rktp->rktp_rkt->rkt_topic->str,
			     rktp->rktp_partition, offset,
			     rktp->rktp_offset_path);

		rktp->rktp_committed_offset = offset;

		/* If sync interval is set to immediate we sync right away. */
		if (rkt->rkt_conf.offset_store_sync_interval_ms == 0)
			rd_kafka_offset_file_sync(rktp);


		return RD_KAFKA_RESP_ERR_NO_ERROR;
	}


	return err;
}
コード例 #23
0
int EbrIOFile::Truncate64(__int64 size) {
    return _chsize_s(filefd, size);
}
コード例 #24
0
ファイル: MMAPFile.cpp プロジェクト: gabloe/RapidStash
int ftruncate(int fd, size_t len) {
	return _chsize_s(fd, len);
}
コード例 #25
0
ファイル: NFS3Prog.cpp プロジェクト: philr/winnfsd
nfsstat3 CNFS3Prog::ProcedureSETATTR(void)
{
    char *path;
    sattr3 new_attributes;
    sattrguard3 guard;
    wcc_data obj_wcc;
    nfsstat3 stat;
    int nMode;
    FILE *pFile;
    HANDLE hFile;
    FILETIME fileTime;
    SYSTEMTIME systemTime;

    PrintLog("SETATTR");
    path = GetPath();
    Read(&new_attributes);
    Read(&guard);
    stat = CheckFile(path);
    obj_wcc.before.attributes_follow = GetFileAttributesForNFS(path, &obj_wcc.before.attributes);

    if (stat == NFS3_OK) {
        if (new_attributes.mode.set_it) {
            nMode = 0;

            if ((new_attributes.mode.mode & 0x100) != 0) {
                nMode |= S_IREAD;
            }

            // Always set read and write permissions (deliberately implemented this way)
            // if ((new_attributes.mode.mode & 0x80) != 0) {
            nMode |= S_IWRITE;
            // }

            // S_IEXEC is not availabile on windows
            // if ((new_attributes.mode.mode & 0x40) != 0) {
            //     nMode |= S_IEXEC;
            // }

            if (_chmod(path, nMode) != 0) {
                stat = NFS3ERR_INVAL;
            } else {

            }
        }   

        // deliberately not implemented because we cannot reflect uid/gid on windows (easliy)
        if (new_attributes.uid.set_it){}
        if (new_attributes.gid.set_it){}

        // deliberately not implemented
        if (new_attributes.mtime.set_it == SET_TO_CLIENT_TIME){}
        if (new_attributes.atime.set_it == SET_TO_CLIENT_TIME){}

        if (new_attributes.mtime.set_it == SET_TO_SERVER_TIME || new_attributes.atime.set_it == SET_TO_SERVER_TIME){
            hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
            if (hFile != INVALID_HANDLE_VALUE) {
                GetSystemTime(&systemTime);
                SystemTimeToFileTime(&systemTime, &fileTime);
                if (new_attributes.mtime.set_it == SET_TO_SERVER_TIME){
                    SetFileTime(hFile, NULL, NULL, &fileTime);
                }
                if (new_attributes.atime.set_it == SET_TO_SERVER_TIME){
                    SetFileTime(hFile, NULL, &fileTime, NULL);
                }
            }
            CloseHandle(hFile);
        }

        if (new_attributes.size.set_it){
            pFile = _fsopen(path, "r+b", _SH_DENYWR);
            int filedes = _fileno(pFile);
            _chsize_s(filedes, new_attributes.size.size);
            fclose(pFile);
        }
    }

    obj_wcc.after.attributes_follow = GetFileAttributesForNFS(path, &obj_wcc.after.attributes);

    Write(&stat);
    Write(&obj_wcc);

    return stat;
}
コード例 #26
0
ファイル: file.c プロジェクト: finnianr/Eiffel-Loop
static int ftruncate (int fd, __int64 length){
	int result = _chsize_s (fd, length);
	if (result != 0) result = -1;
	return result;
}
コード例 #27
0
ファイル: os_windows.c プロジェクト: peluse/nvml
/*
 * os_ftruncate -- truncate a file to a specified length
 */
int
os_ftruncate(int fd, os_off_t length)
{
	return _chsize_s(fd, length);
}
コード例 #28
0
ファイル: win32compat.c プロジェクト: MauPlay/friidump
int ftruncate (int fd, __int64 size) {
	return (_chsize_s (fd, size));
}