int fsync_file(int fd, int datasync)
{
	int ret;
	if (datasync)
		ret = fdatasync(fd);
	else
		ret = fsync(fd);

	if (ret < 0)
		return -errno;
	return 0;
}
Example #2
0
void CTrace::comment(const char* text)
{
    if (m_enabled) {
        indent();
        // repeat the current scope to make this function easy to use
        write(s_fd, s_scopes.back(), strlen(s_scopes.back()));
        write(s_fd, " [", 2);
        write(s_fd, text, strlen(text));
        write(s_fd, "]\n", 2);
        fdatasync(s_fd);
    }
}
Example #3
0
File: file.c Project: calind/nbdkit
/* Flush the file to disk. */
static int
file_flush (void *handle)
{
  struct handle *h = handle;

  if (fdatasync (h->fd) == -1) {
    nbdkit_error ("fdatasync: %m");
    return -1;
  }

  return 0;
}
Example #4
0
void drop_caches(int *fds, char **file_names, int num_files)
{
	int i;

	for (i = 0; i < num_files; i++) {
		printf("%s... ", file_names[i]);
		fdatasync(fds[i]);
		if (posix_fadvise(fds[i], 0, 0, POSIX_FADV_DONTNEED) < 0)
			die("fadvise failed");
		printf("caches dropped\n");
	}
}
/*!
    \internal
    \since 5.1
*/
bool QFSFileEnginePrivate::nativeSyncToDisk()
{
    Q_Q(QFSFileEngine);
#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
    const int ret = fdatasync(nativeHandle());
#else
    const int ret = fsync(nativeHandle());
#endif
    if (ret != 0)
        q->setError(QFile::WriteError, qt_error_string(errno));
    return ret == 0;
}
Example #6
0
tb_bool_t tb_file_sync(tb_file_ref_t file)
{
    // check
    tb_assert_and_check_return_val(file, tb_false);

    // sync
#ifdef TB_CONFIG_POSIX_HAVE_FDATASYNC
    return !fdatasync(tb_file2fd(file))? tb_true : tb_false;
#else
    return !fsync(tb_file2fd(file))? tb_true : tb_false;
#endif
}
Example #7
0
/** Synchronize file contents
 *
 * If the datasync parameter is non-zero, then only the user data
 * should be flushed, not the meta data.
 *
 * Changed in version 2.2
 */
int bb_fsync(const char *path, int datasync, struct fuse_file_info *fi) {
   int retstat = 0;
    
   log_msg("\nbb_fsync(path=\"%s\", datasync=%d, fi=0x%08x)\n",path,datasync,fi);
   log_fi(fi);
    
   if (datasync) retstat = fdatasync(fi->fh);
   else	retstat = fsync(fi->fh);
    
   if (retstat < 0) bb_error("bb_fsync fsync");
   return retstat;
}
Example #8
0
    void LogFile::writeAt(unsigned long long offset, const void *buf, size_t len) { 
        assert(((size_t)buf)%4096==0); // aligned
        ssize_t written = pwrite(_fd, buf, len, offset);
        if( written != (ssize_t) len ) {
            log() << "writeAt fails " << errnoWithDescription() << endl;
        }
#if defined(__linux__)
        fdatasync(_fd);
#else
        fsync(_fd);
#endif
    }
Example #9
0
int jlog_file_sync(jlog_file *f)
{
  int rv;

#ifdef HAVE_FDATASYNC
  while((rv = fdatasync(f->fd)) == -1 && errno == EINTR) ;
#else
  while((rv = fsync(f->fd)) == -1 && errno == EINTR) ;
#endif
  if (rv == 0) return 1;
  return 0;
}
Example #10
0
static void test_imfs_make_generic_node(void)
{
  int rv = 0;
  int fd = 0;
  const char *path = "generic";
  char buf [1];
  ssize_t n = 0;
  off_t off = 0;

  rv = IMFS_make_generic_node(
    path,
    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
    &node_control,
    &global_state
  );
  rtems_test_assert(rv == 0);

  fd = open(path, O_RDWR);
  rtems_test_assert(fd >= 0);

  n = read(fd, buf, sizeof(buf));
  rtems_test_assert(n == 0);

  n = write(fd, buf, sizeof(buf));
  rtems_test_assert(n == 0);

  rv = ioctl(fd, 0);
  rtems_test_assert(rv == 0);

  off = lseek(fd, off, SEEK_SET);
  rtems_test_assert(off == 0);

  rv = ftruncate(fd, 0);
  rtems_test_assert(rv == 0);

  rv = fsync(fd);
  rtems_test_assert(rv == 0);

  rv = fdatasync(fd);
  rtems_test_assert(rv == 0);

  rv = fcntl(fd, F_GETFD);
  rtems_test_assert(rv >= 0);

  rv = close(fd);
  rtems_test_assert(rv == 0);

  rv = unlink(path);
  rtems_test_assert(rv == 0);

  rtems_test_assert(global_state == TEST_DESTROYED);
}
Example #11
0
int main (void)
{
	int fd;
	mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
	int oflag = O_RDWR | O_APPEND | O_CREAT | O_EXCL;

	fd = open("file_append", oflag, mode);
	if (fd < 0) error_exit();

	// write some characters for test
	char *a = "abcdefg";
	ssize_t s = write(fd, a, 7);
	if (s != 7) error_exit();

	// lseek test, reset file offset
	if (lseek(fd, 0, SEEK_SET) < 0)
		log_err("lseek");

	// read from file of start test
	char b[3];
	if (read(fd, b, 3) == 3)
	{
		log_info("first 3 character");
		write(STDOUT_FILENO, b, 3);
	}
	else
		log_err("read");

	// lseek and override data use write with append flag
	if (write(fd, "DEFG", 4) == 4)
	{
		log_info("file content");
		fdatasync(fd);

		off_t len = lseek(fd, 0, SEEK_END);
		if (len < 0) log_err("lseek");

		char fresult[len];
		lseek(fd, 0, SEEK_SET); // set to begin of file
		read(fd, fresult, len);
		write(STDOUT_FILENO, fresult, len);
	}
	else
		log_err("write");

	return EXIT_SUCCESS;

	/* 
	 * with O_APPEND oflag, seek() and write() can not override data
	 * but can seek with lseek()
	 * */
}
Example #12
0
    int
env_fsync( const char *path )
{
    int		fd;
    int		ret = 0;

    if (( fd = open( path, O_RDONLY )) < 0 ) {
	syslog( LOG_ERR, "Syserror: env_fsync open %s: %m", path );
	return( 1 );
    }

    /* fdatasync() "does not flush modified metadata unless that metadata is
     * needed in order to allow a subsequent data retrieval to be correctly
     * handled." We don't require that all metadata be synced to disk, so if
     * fdatasync() is available it's preferred.
     */
#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
    if ( fdatasync( fd ) < 0 ) {
#else
    if ( fsync( fd ) < 0 ) {
#endif
	syslog( LOG_ERR, "Syserror: env_fsync fsync %s: %m", path );
	ret = 1;
    }
    close( fd );

    return( ret );
}

    /* calling this function updates the attempt time */

    int
env_touch( struct envelope *env )
{
    char			fname[ MAXPATHLEN ];
    struct timeval		tv_now;

    sprintf( fname, "%s/E%s", env->e_dir, env->e_id );

    if ( utime( fname, NULL ) != 0 ) {
	syslog( LOG_ERR, "Syserror: env_touch utime %s: %m", fname );
	return( -1 );
    }

    if ( simta_gettimeofday( &tv_now ) != 0 ) {
	return( -1 );
    }

    env->e_etime.tv_sec = tv_now.tv_sec;

    return( 0 );
}
Example #13
0
/*
 * There is no POSIX fsyncdir - presumably this is just fsync when called on a
 * directory.  Mimicking code from (non-dir) fsync.
 */
static int bru_fsyncdir(const char *path, int datasync, struct fuse_file_info *fi)
{
	SET_CALLER_UID();

	int ret;
	if(datasync)
		ret = fdatasync(fi->fh);
	else
		ret = fsync(fi->fh);

	SET_RET_ERRNO();
	return ret;
}
Example #14
0
		virtual void flush()
		{
			assert(fd >= 0);
			
#ifdef MACOSX
			if (fsync(fd) < 0)
#else
			if (fdatasync(fd) < 0)
#endif
			{
				fail(DashelException::IOError, errno, "File flush error.");
			}
		}
Example #15
0
static int ciopfs_fsync(const char *path, int isdatasync, struct fuse_file_info *fi)
{
	int res;
#ifdef HAVE_FDATASYNC
	if (isdatasync)
		res = fdatasync(fi->fh);
	else
#endif
		res = fsync(fi->fh);
	if (res == -1)
		return -errno;
	return 0;
}
Example #16
0
    int FileOperation::flush_data()
    {
      if (open_flags_ & O_SYNC)
      {
        return 0;
      }

      int fd = check_file();
      if (fd < 0)
        return fd;

      return fdatasync(fd);
    }
Example #17
0
int condor_fdatasync(int fd, const char* /*path*/)
{
	if (!condor_fsync_on)
	{
		return 0;
	}

#ifdef HAVE_FDATASYNC
	return fdatasync(fd);
#else
	return fsync(fd);
#endif
}
Example #18
0
/** Synchronize file contents
 *
 * If the datasync parameter is non-zero, then only the user data
 * should be flushed, not the meta data.
 *
 * Changed in version 2.2
 */
int sfs_fsync(const char *path, int datasync, struct fuse_file_info *fi) {
    int retstat = 0;

    if (datasync)
        retstat = fdatasync(fi->fh);
    else
        retstat = fsync(fi->fh);

    if (retstat < 0)
        sfs_error("sfs_fsync fsync");

    return retstat;
}
static int doOneCmd(
  IoContext *pCtx,
  u8 *aData,
  int pgsz,
  char *zCmd,
  char **pzOut
){
  char c;
  char *z = zCmd;

  while( safe_isspace(*z) ) z++;
  c = *z;

  if( c==0 ){
    if( pzOut ) *pzOut = z;
    return 0;
  }

  if( c=='s' || c=='S' ){
    if( pzOut ) *pzOut = &z[1];
    return fdatasync(pCtx->fd);
  }

  if( safe_isdigit(c) ){
    i64 iOff = 0;
    int nByte = 0;
    int rc = 0;
    int nPg;
    int iPg;

    nByte = getNextSize(z, &z, &rc);
    if( rc || *z!='@' ) goto bad_command;
    z++;
    iOff = getNextSize(z, &z, &rc);
    if( rc || (safe_isspace(*z)==0 && *z!='\0') ) goto bad_command;
    if( pzOut ) *pzOut = z;

    nPg = (nByte+pgsz-1) / pgsz;
    lseek(pCtx->fd, iOff, SEEK_SET);
    for(iPg=0; iPg<nPg; iPg++){
      write(pCtx->fd, aData, pgsz);
    }
    pCtx->nWrite += nByte/1024;

    return 0;
  }

 bad_command:
  testPrintError("unrecognized command: %s", zCmd);
  return 1;
}
Example #20
0
/** \brief Flush data written to the file descriptor to disk.
 *
 *  This prevents freezing up Linux disk access on a running
 *  CFQ, AS, or Deadline as the disk write schedulers. It does
 *  this via two mechanism. One is a data sync using the best
 *  mechanism available (fdatasync then fsync). The second is
 *  by telling the kernel we do not intend to use the data just
 *  written anytime soon so other processes time-slices will
 *  not be used to deal with our excess dirty pages.
 *
 *  \note We used to also use sync_file_range on Linux, however
 *  this is incompatible with newer filesystems such as BRTFS and
 *  does not actually sync any blocks that have not been allocated
 *  yet so it was never really appropriate for ThreadedFileWriter.
 *
 *  \note We use standard posix calls for this, so any operating
 *  system supporting the calls will benefit, but this has been
 *  designed with Linux in mind. Other OS's may benefit from
 *  revisiting this function.
 */
void ThreadedFileWriter::Sync(void)
{
    if (fd >= 0)
    {
#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
        // fdatasync tries to avoid updating metadata, but will in
        // practice always update metadata if any data is written
        // as the file will usually have grown.
        fdatasync(fd);
#else
        fsync(fd);
#endif
    }
}
Example #21
0
bool CSqlLogger::write_log(const std::string& sql)
{
    try
    {
        int32_t length = static_cast<int32_t>(sql.size());
        sys::LockHelper<sys::CLock> lock_helper(_lock);

        if ((-1 == _log_fd) || need_rotate())
        {
            rotate_log();
        }
        if (-1 == _log_fd)
        {
            return false;
        }

        struct iovec iov[2];
        iov[0].iov_base = &length;
        iov[0].iov_len = sizeof(length);
        iov[1].iov_base = const_cast<char*>(sql.data());
        iov[1].iov_len = sql.size();
        ssize_t bytes_written = writev(_log_fd, iov, sizeof(iov)/sizeof(iov[0]));
        if (bytes_written != static_cast<int>(iov[0].iov_len+iov[1].iov_len))
        {
            const int errcode = errno;
            THROW_SYSCALL_EXCEPTION(utils::CStringUtils::format_string("writev %s error: %s", _log_filepath.c_str(), sys::Error::to_string(errcode).c_str()), errcode, "writev");
        }

        // 计数
        ++_total_lines;

        const int32_t lines = argument::lines->value();
        if ((lines > 0) && (++_num_lines >= lines))
        {
            _num_lines = 0;
            if (-1 == fdatasync(_log_fd))
            {
                const int errcode = errno;
                THROW_SYSCALL_EXCEPTION(utils::CStringUtils::format_string("fdatasync %s error: %s", _log_filepath.c_str(), sys::Error::to_string(errcode).c_str()), errno, "fdatasync");
            }
        }

        return true;
    }
    catch (sys::CSyscallException& ex)
    {
        MYLOG_ERROR("[%s] write [%s] to [%s] failed: %s\n", _dbinfo->str().c_str(), sql.c_str(), _log_filepath.c_str(), ex.str().c_str());
        return false;
    }
}
Example #22
0
/** Synchronize file contents
 *
 * If the datasync parameter is non-zero, then only the user data
 * should be flushed, not the meta data.
 *
 * Changed in version 2.2
 */
int bb_fsync(const char *path, int datasync, struct fuse_file_info *fi)
{
    log_msg("\nbb_fsync(path=\"%s\", datasync=%d, fi=0x%08x)\n",
	    path, datasync, fi);
    log_fi(fi);
    
    // some unix-like systems (notably freebsd) don't have a datasync call
#ifdef HAVE_FDATASYNC
    if (datasync)
	return log_syscall("fdatasync", fdatasync(fi->fh), 0);
    else
#endif	
	return log_syscall("fsync", fsync(fi->fh), 0);
}
Example #23
0
/** Synchronize file contents
 *
 * If the datasync parameter is non-zero, then only the user data
 * should be flushed, not the meta data.
 *
 * Changed in version 2.2
 */
int bb_fsync(const char *path, int datasync, struct fuse_file_info *fi)
{
    int retstat = 0;
    
    
    // some unix-like systems (notably freebsd) don't have a datasync call
#ifdef HAVE_FDATASYNC
    if (datasync)
	retstat = fdatasync(fi->fh);
    else
#endif	
	retstat = fsync(fi->fh);
    return retstat;
}
Example #24
0
int thesmurFS_fsync(const char* path, int datasync, struct fuse_file_info* fi) {
    char* physical_path;
    int r;
    //emergency_output("fsync");
    if(get_physical_path(path, &physical_path) == -1) {
        //emergency_output("ENOMEM");
        return -errno;
    }
    if(datasync)
        r = fdatasync(fi->fh);
    else
        r = fsync(fi->fh);
    return r;
}
Example #25
0
int
main(int argc, char **argv)
{
	int lc;				/* loop counter */
	char *msg;			/* message returned from parse_opts */

	/* parse standard options */
	if ((msg = parse_opts(argc, argv, (option_t *)NULL, NULL)) !=
	    (char *)NULL) {
		tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
	}

	setup();

	/* check looping state if -i option is given */
	for (lc = 0; TEST_LOOPING(lc); lc++) {
		/* reset Tst_count in case we are looping */
		Tst_count = 0;

		for (testno = 0; testno < TST_TOTAL; ++testno) {
			if( (tdat[testno].setup) && (tdat[testno].setup()) ) {
				/* setup() failed, skip this test */
				continue;
			}

			/* Test the system call */
			TEST(fdatasync(fd));
			TEST_ERROR_LOG(TEST_ERRNO);
			if ( (TEST_RETURN == EXP_RET_VAL) &&
				(TEST_ERRNO == tdat[testno].experrno) ) {
				tst_resm(TPASS, "Expected failure for %s, "
					"errno: %d", tdat[testno].desc, 
					TEST_ERRNO);
			} else {
				tst_resm(TFAIL, "Unexpected results for %s ; "
					"returned %d (expected %d), errno %d "
					"(expected %d)", tdat[testno].desc,
					TEST_RETURN, EXP_RET_VAL,
					TEST_ERRNO, tdat[testno].experrno);
			}
			if(tdat[testno].cleanup) {
				tdat[testno].cleanup();
			}
		}
	}
	cleanup();

	/*NOTREACHED*/
	return 0;
}
Example #26
0
File: sunlink.c Project: aosm/srm
void flush(int fd) {
  /* force buffered writes to be flushed to disk */
#if defined F_FULLFSYNC
  /* F_FULLFSYNC is equivalent to fsync plus device flush to media */
  if (fcntl(fd, F_FULLFSYNC, NULL) != 0) {
    /* we're not on a fs that supports this; fall back to plain fsync */
    fsync(fd);
  }
#elif HAVE_FDATASYNC
  fdatasync(fd);
#else
  fsync(fd);
#endif
}
Example #27
0
File: file.c Project: wanghaofu/c
/**
  A test about syscall of File I/O
Author:supu@TaobaoDBA
[email protected] http://orczhou.com http://www.taobaodba.com
 */

#include   "stdlib.h"           /* for exit */
#include   "unistd.h"           /* for write fdatasync*/
#include    "fcntl.h"           /* for open  */
int main(void){
	int fd;
	if((fd=open("/test.file",O_WRONLY|O_APPEND|O_DSYNC))<0){
		exit(1);
	}
	char buff[]="\nhello wangtao \n";
	if(write(fd,buff,16)!= 16){
		exit(2);
	}
	if(fdatasync(fd)==-1){
		exit(3);
	}
	exit(0);
}
Example #28
0
File: fd.c Project: hellower/gpdb
/*
 * pg_fdatasync --- same as fdatasync except does nothing if enableFsync is off
 *
 * Not all platforms have fdatasync; treat as fsync if not available.
 */
int
pg_fdatasync(int fd)
{
	if (enableFsync)
	{
#ifdef HAVE_FDATASYNC
		return fdatasync(fd);
#else
		return fsync(fd);
#endif
	}
	else
		return 0;
}
Example #29
0
static int
fd_sync (int fd, u_int32_t flags)
{
    switch (flags & SYNC_TYPE) {
        case 0:         return 0;
        case SYNC_ALL:  return fsync (fd);
#ifndef NO_DSYNC
        case SYNC_DATA: return fdatasync (fd);
#endif
        default:
            assert (0);
    }
    return EINVAL;
}
Example #30
0
/*
 * Write the [eid] & [etime] of the last processed event to the opened
 * [zcp] state_file.  Note that etime[] is an array of size 2.
 * Return 0 on success, -1 on error.
 */
int
zed_conf_write_state(struct zed_conf *zcp, uint64_t eid, int64_t etime[])
{
	ssize_t len;
	struct iovec iov[3];
	ssize_t n;

	if (!zcp) {
		errno = EINVAL;
		zed_log_msg(LOG_ERR,
		    "Failed to write state file: %s", strerror(errno));
		return (-1);
	}
	if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t) -1) {
		zed_log_msg(LOG_WARNING,
		    "Failed to reposition state file offset: %s",
		    strerror(errno));
		return (-1);
	}
	len = 0;
	iov[0].iov_base = &eid;
	len += iov[0].iov_len = sizeof (eid);
	iov[1].iov_base = &etime[0];
	len += iov[1].iov_len = sizeof (etime[0]);
	iov[2].iov_base = &etime[1];
	len += iov[2].iov_len = sizeof (etime[1]);

	n = writev(zcp->state_fd, iov, 3);
	if (n < 0) {
		zed_log_msg(LOG_WARNING,
		    "Failed to write state file \"%s\": %s",
		    zcp->state_file, strerror(errno));
		return (-1);
	}
	if (n != len) {
		errno = EIO;
		zed_log_msg(LOG_WARNING,
		    "Failed to write state file \"%s\": Wrote %d of %d bytes",
		    zcp->state_file, n, len);
		return (-1);
	}
	if (fdatasync(zcp->state_fd) < 0) {
		zed_log_msg(LOG_WARNING,
		    "Failed to sync state file \"%s\": %s",
		    zcp->state_file, strerror(errno));
		return (-1);
	}
	return (0);
}