示例#1
0
static int modfile_move( INSTANCE * my, int * params )
{
    int r = file_move( string_get( params[0] ), string_get( params[1] ) ) ;
    string_discard( params[1] ) ;
    string_discard( params[0] ) ;
    return r ;
}
示例#2
0
struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
{
	struct file * f;
	struct inode *inode;
	int error;

	error = -ENFILE;
	f = get_empty_filp();
	if (!f)
		goto cleanup_dentry;
	f->f_flags = flags;
	f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
	inode = dentry->d_inode;
	if (f->f_mode & FMODE_WRITE) {
		error = get_write_access(inode);
		if (error)
			goto cleanup_file;
	}

	f->f_mapping = inode->i_mapping;
	f->f_dentry = dentry;
	f->f_vfsmnt = mnt;
	f->f_pos = 0;
	f->f_op = fops_get(inode->i_fop);
	file_move(f, &inode->i_sb->s_files);

	if (f->f_op && f->f_op->open) {
		error = f->f_op->open(inode,f);
		if (error)
			goto cleanup_all;
	}
	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);

	file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);

	/* NB: we're sure to have correct a_ops only after f_op->open */
	if (f->f_flags & O_DIRECT) {
		if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO) {
			fput(f);
			f = ERR_PTR(-EINVAL);
		}
	}

	return f;

cleanup_all:
	fops_put(f->f_op);
	if (f->f_mode & FMODE_WRITE)
		put_write_access(inode);
	file_kill(f);
	f->f_dentry = NULL;
	f->f_vfsmnt = NULL;
cleanup_file:
	put_filp(f);
cleanup_dentry:
	dput(dentry);
	mntput(mnt);
	return ERR_PTR(error);
}
示例#3
0
bool Preferences_Save_Safe( PreferenceDictionary& preferences, const char* filename ){
	Array<char> tmpName( filename, filename + strlen( filename ) + 1 + 3 );
	*( tmpName.end() - 4 ) = 'T';
	*( tmpName.end() - 3 ) = 'M';
	*( tmpName.end() - 2 ) = 'P';
	*( tmpName.end() - 1 ) = '\0';

	return Preferences_Save( preferences, tmpName.data() )
		   && ( !file_exists( filename ) || file_remove( filename ) )
		   && file_move( tmpName.data(), filename );
}
示例#4
0
static bool file_saveBackup (const std::string& path)
{
    if (file_writeable(path)) {
        std::string backup = os::stripExtension(path) + ".bak";

        return (!file_exists(backup) || file_remove(backup)) // remove backup
               && file_move(path, backup); // rename current to backup
    }

    globalErrorStream() << "ERROR: map path is not writeable: " << path << "\n";
    return false;
}
示例#5
0
/**
 *  Move a file.
 *
 *  This function will first attempt to rename the file. If the rename
 *  fails because the file is being moved across file systems, it will
 *  be copied to the destination file and the source file deleted. MD5
 *  checking will be used if the file needs to be copied across file
 *  systems.
 *
 *  This function will also add a "moving file" message to the log file.
 *
 *  If an error occurs in this function it will be appended to the log and
 *  error mail messages, and the process status will be set appropriately.
 *
 *  @param  src_file  - full path to the source file
 *  @param  dest_file - full path to the destination file
 *
 *  @return
 *    - 1 if the file was moved successfully
 *    - 0 if an error occurred
 */
int dsproc_move_file(const char *src_file, const char *dest_file)
{
    LOG( DSPROC_LIB_NAME,
        "Moving:   %s\n"
        " -> to:   %s\n", src_file, dest_file);

    if (!file_move(src_file, dest_file, FC_CHECK_MD5)) {
        dsproc_set_status(DSPROC_EFILEMOVE);
        return(0);
    }

    return(1);
}
示例#6
0
bool file_saveBackup(const char* path)
{
  if(file_writeable(path))
  {
	  StringOutputStream backup(256);
    backup << StringRange(path, path_get_extension(path)) << "bak";

    return (!file_exists(backup.c_str()) || file_remove(backup.c_str())) // remove backup
      && file_move(path, backup.c_str()); // rename current to backup
  }

  globalErrorStream() << "map path is not writeable: " << makeQuoted(path) << "\n";
  return false;
}
示例#7
0
struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
{
	struct file * f;
	struct inode *inode;
	int error;

	error = -ENFILE;
	f = get_empty_filp();
	if (!f)
		goto cleanup_dentry;
	f->f_flags = flags;
	f->f_mode = (flags+1) & O_ACCMODE;
	inode = dentry->d_inode;
	if (f->f_mode & FMODE_WRITE) {
		error = get_write_access(inode);
		if (error)
			goto cleanup_file;
	}

	f->f_dentry = dentry;
	f->f_vfsmnt = mnt;
	f->f_pos = 0;
	f->f_reada = 0;
	f->f_op = fops_get(inode->i_fop);
	if (inode->i_sb)
		file_move(f, &inode->i_sb->s_files);
	if (f->f_op && f->f_op->open) {
		error = f->f_op->open(inode,f);
		if (error)
			goto cleanup_all;
	}
	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);

	return f;

cleanup_all:
	fops_put(f->f_op);
	if (f->f_mode & FMODE_WRITE)
		put_write_access(inode);
	f->f_dentry = NULL;
	f->f_vfsmnt = NULL;
cleanup_file:
	put_filp(f);
cleanup_dentry:
	dput(dentry);
	mntput(mnt);
	return ERR_PTR(error);
}
示例#8
0
static int __ptmx_open(struct inode *inode, struct file *filp)
{
	struct tty_struct *tty;
	int retval;
	int index;

	nonseekable_open(inode, filp);

	/* find a device that is not in use. */
	tty_lock();
	index = devpts_new_index(inode);
	tty_unlock();
	if (index < 0)
		return index;

	mutex_lock(&tty_mutex);
	tty_lock();
	tty = tty_init_dev(ptm_driver, index, 1);
	mutex_unlock(&tty_mutex);

	if (IS_ERR(tty)) {
		retval = PTR_ERR(tty);
		goto out;
	}

	set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
	filp->private_data = tty;
	file_move(filp, &tty->tty_files);

	retval = devpts_pty_new(inode, tty->link);
	if (retval)
		goto out1;

	retval = ptm_driver->ops->open(tty, filp);
	if (!retval)
		return 0;
out1:
	tty_unlock();
	tty_release(inode, filp);
	return retval;
out:
	devpts_kill_index(inode, index);
	tty_unlock();
	return retval;
}
示例#9
0
int my_rename( const char *old_path, const char *new_path )
{
	DISABLE_ERRORS;
	int result = -1;
	auto told_path = tstr(old_path);
	auto tnew_path = tstr(new_path);
	LPCTSTR p_old = MRP(told_path.get());
	LPCTSTR p_new = MRP2(tnew_path.get());

	result = my_access(old_path,0);
	if(result < 0) {
		// my_errno already set
	} else {
		if(is_same_drive(p_old,p_new)) {
			result = _trename( p_old, p_new );
			if(result != 0) { // by definition, rename may also return a positive value to indicate an error
				my_errno = errno;
			} else {
				my_errno = 0;
			}
		} else {
			if(is_dir(p_old)) {
				result = folder_copy( p_old, p_new );
				// my_errno already set
				if(result >= 0) {
					if(myRemoveDirectory( p_old )) {
						my_errno = 0;
						result = 0;
					} else {
						// there is no proper error code for this failure.
						my_errno = EACCES;
						result = -1;
					}
				}
			} else {
				result = file_move( p_old, p_new );
				// my_errno already set
			}
		}
	}
	D(bug(TEXT("rename(%s,%s,%s,%s) = %d\n"), told_path.get(), p_old, tnew_path.get(), p_new, result));
	RESTORE_ERRORS;
	return result;
}
示例#10
0
void
cache_move (const char *src,
	    const char *dest)
{
	char   *cache_src;
	time_t  dest_mtime = get_file_mtime (dest);

	cache_src = cache_get_nautilus_cache_name (src);

	if (path_is_file (cache_src)) {
		char *cache_dest = cache_get_nautilus_cache_name (dest);

		if (path_is_file (cache_dest))
			file_unlink (cache_dest);
		if (file_move (cache_src, cache_dest))
			set_file_mtime (cache_dest, dest_mtime);

		g_free (cache_dest);
	}
	g_free (cache_src);
}
示例#11
0
/*
 * Remove old lines from pref files
 *
 * If you are using setgid, make sure privileges were raised prior
 * to calling this.
 */
static void remove_old_dump(const char *cur_fname, const char *mark)
{
	bool between_marks = FALSE;
	bool changed = FALSE;

	char buf[1024];

	char start_line[1024];
	char end_line[1024];

	char new_fname[1024];

	ang_file *new_file;
	ang_file *cur_file;


	/* Format up some filenames */
	strnfmt(new_fname, sizeof(new_fname), "%s.new", cur_fname);

	/* Work out what we expect to find */
	strnfmt(start_line, sizeof(start_line), "%s begin %s",
			dump_separator, mark);
	strnfmt(end_line, sizeof(end_line), "%s end %s", dump_separator, mark);



	/* Open current file */
	cur_file = file_open(cur_fname, MODE_READ, -1);
	if (!cur_file)
		return;

	/* Open new file */
	new_file = file_open(new_fname, MODE_WRITE, FTYPE_TEXT);
	if (!new_file) {
		msg("Failed to create file %s", new_fname);
		return;
	}

	/* Loop for every line */
	while (file_getl(cur_file, buf, sizeof(buf))) {
		/* If we find the start line, turn on */
		if (!strcmp(buf, start_line)) {
			between_marks = TRUE;
		}

		/* If we find the finish line, turn off */
		else if (!strcmp(buf, end_line)) {
			between_marks = FALSE;
			changed = TRUE;
		}

		if (!between_marks) {
			/* Copy orginal line */
			file_putf(new_file, "%s\n", buf);
		}
	}

	/* Close files */
	file_close(cur_file);
	file_close(new_file);

	/* If there are changes, move things around */
	if (changed) {
		char old_fname[1024];
		strnfmt(old_fname, sizeof(old_fname), "%s.old", cur_fname);

		if (file_move(cur_fname, old_fname)) {
			file_move(new_fname, cur_fname);
			file_delete(old_fname);
		}
	}

	/* Otherwise just destroy the new file */
	else {
		file_delete(new_fname);
	}
}
示例#12
0
static struct file *__dentry_open(struct _dentry *dentry, struct vfsmount *mnt,
					int flags, struct file *f,
					int (*open)(struct _inode *, struct file *))
{
	struct _inode *inode;
	int error;
	struct super_block *sb;
	struct _file *_f = tx_cache_get_file(f);

	_f->f_flags = flags;
	_f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
		 		FMODE_PREAD | FMODE_PWRITE;
	inode = d_get_inode(dentry);
	sb = inode->i_sb;

	if (_f->f_mode & FMODE_WRITE) {
		error = get_write_access(parent(inode));
		if (error)
			goto cleanup_file;
	}

	f->f_mapping = inode->i_mapping;
	_f->f_path.dentry = parent(dentry);
	_f->f_path.mnt = mnt;
	_f->f_pos = 0;
	f->f_op = fops_get(inode->i_fop);
	file_move(f, &sb->s_files);

	if (!open && f->f_op)
		open = f->f_op->open;
	if (open) {
		error = open(inode, f);
		if (error)
			goto cleanup_all;
	}

	_f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);

	file_ra_state_init(&f->f_ra, 
			   tx_cache_get_inode(f->f_mapping->host)->i_mapping);

	/* NB: we're sure to have correct a_ops only after f_op->open */
	if (_f->f_flags & O_DIRECT) {
		if (!f->f_mapping->a_ops ||
		    ((!f->f_mapping->a_ops->direct_IO) &&
		    (!f->f_mapping->a_ops->get_xip_page))) {
			fput(f);
			f = ERR_PTR(-EINVAL);
		}
	}

	return f;

cleanup_all:
	fops_put(f->f_op);
	if (_f->f_mode & FMODE_WRITE)
		put_write_access(parent(inode));
	file_kill(f);
	_f->f_path.dentry = NULL;
	_f->f_path.mnt = NULL;
cleanup_file:
	/* Avoid issues if we recycle this object */
	if(live_transaction())
		early_release(&f->xobj, 1);
	put_filp(f);
	dput(parent(dentry));
	mntput(mnt);
	return ERR_PTR(error);
}
示例#13
0
int ipkg_conf_write_status_files(ipkg_conf_t *conf)
{
     pkg_dest_list_elt_t *iter;
     pkg_dest_t *dest;
     pkg_vec_t *all;
     pkg_t *pkg;
     register int i;
     int err;

     if (conf->noaction)
	  return 0;
     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
	  dest = iter->data;
	  dest->status_file = fopen(dest->status_file_tmp_name, "w");
	  if (dest->status_file == NULL) {
	       fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
		       __FUNCTION__, dest->status_file_name, strerror(errno));
	  }
     }

     all = pkg_vec_alloc();
     pkg_hash_fetch_available(&conf->pkg_hash, all);

     for(i = 0; i < all->len; i++) {
	  pkg = all->pkgs[i];
	  /* We don't need most uninstalled packages in the status file */
	  if (pkg->state_status == SS_NOT_INSTALLED
	      && (pkg->state_want == SW_UNKNOWN
		  || pkg->state_want == SW_DEINSTALL
		  || pkg->state_want == SW_PURGE)) {
	       continue;
	  }
	  if (!pkg) {
	    fprintf(stderr, "Null package\n");
	  }
	  if (pkg->dest == NULL) {
	       fprintf(stderr, "%s: ERROR: Can't write status for "
		       "package %s since it has a NULL dest\n",
		       __FUNCTION__, pkg->name);
	       continue;
	  }
	  if (pkg->dest->status_file) {
	       pkg_print_status(pkg, pkg->dest->status_file);
	  }
     }

     pkg_vec_free(all);

     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
	  dest = iter->data;
	  if (dest->status_file) {
	       err = ferror(dest->status_file);
	       fclose(dest->status_file);
	       dest->status_file = NULL;
	       if (!err) {
		    file_move(dest->status_file_tmp_name, dest->status_file_name);
	       } else {
		    fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
			    "retaining old %s\n", __FUNCTION__, 
			    dest->status_file_tmp_name, dest->status_file_name);
	       }
	  }
     }

     return 0;
}
示例#14
0
/**
 * Actually place an entry into the high score file
 * Return the location (0 is best) or -1 on "failure"
 */
static void highscore_write(const high_score scores[], size_t sz)
{
	size_t n;

	ang_file *lok;
	ang_file *scorefile;

	char old_name[1024];
	char cur_name[1024];
	char new_name[1024];
	char lok_name[1024];

	path_build(old_name, sizeof(old_name), ANGBAND_DIR_SCORES, "scores.old");
	path_build(cur_name, sizeof(cur_name), ANGBAND_DIR_SCORES, "scores.raw");
	path_build(new_name, sizeof(new_name), ANGBAND_DIR_SCORES, "scores.new");
	path_build(lok_name, sizeof(lok_name), ANGBAND_DIR_SCORES, "scores.lok");


	/* Read in and add new score */
	n = highscore_count(scores, sz);


	/* Lock scores */
	if (file_exists(lok_name)) {
		msg("Lock file in place for scorefile; not writing.");
		return;
	}

	safe_setuid_grab();
	lok = file_open(lok_name, MODE_WRITE, FTYPE_RAW);
	file_lock(lok);
	safe_setuid_drop();

	if (!lok) {
		msg("Failed to create lock for scorefile; not writing.");
		return;
	}


	/* Open the new file for writing */
	safe_setuid_grab();
	scorefile = file_open(new_name, MODE_WRITE, FTYPE_RAW);
	safe_setuid_drop();

	if (!scorefile) {
		msg("Failed to open new scorefile for writing.");

		file_close(lok);
		file_delete(lok_name);
		return;
	}

	file_write(scorefile, (const char *)scores, sizeof(high_score)*n);
	file_close(scorefile);

	/* Now move things around */
	safe_setuid_grab();

	if (file_exists(old_name) && !file_delete(old_name))
		msg("Couldn't delete old scorefile");

	if (file_exists(cur_name) && !file_move(cur_name, old_name))
		msg("Couldn't move old scores.raw out of the way");

	if (!file_move(new_name, cur_name))
		msg("Couldn't rename new scorefile to scores.raw");

	/* Remove the lock */
	file_close(lok);
	file_delete(lok_name);

	safe_setuid_drop();
}
示例#15
0
int
opkg_download(const char *src, const char *dest_file_name,
	curl_progress_func cb, void *data, const short hide_error)
{
    int err = 0;

    char *src_basec = xstrdup(src);
    char *src_base = basename(src_basec);
    char *tmp_file_location;

    opkg_msg(NOTICE,"Downloading %s.\n", src);

    if (str_starts_with(src, "file:")) {
	const char *file_src = src + 5;
	opkg_msg(INFO, "Copying %s to %s...", file_src, dest_file_name);
	err = file_copy(file_src, dest_file_name);
	opkg_msg(INFO, "Done.\n");
        free(src_basec);
	return err;
    }

    sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
    free(src_basec);
    err = unlink(tmp_file_location);
    if (err && errno != ENOENT) {
	opkg_perror(ERROR, "Failed to unlink %s", tmp_file_location);
	free(tmp_file_location);
	return -1;
    }

    if (conf->http_proxy) {
	opkg_msg(DEBUG, "Setting environment variable: http_proxy = %s.\n",
		conf->http_proxy);
	setenv("http_proxy", conf->http_proxy, 1);
    }
    if (conf->ftp_proxy) {
	opkg_msg(DEBUG, "Setting environment variable: ftp_proxy = %s.\n",
		conf->ftp_proxy);
	setenv("ftp_proxy", conf->ftp_proxy, 1);
    }
    if (conf->no_proxy) {
	opkg_msg(DEBUG,"Setting environment variable: no_proxy = %s.\n",
		conf->no_proxy);
	setenv("no_proxy", conf->no_proxy, 1);
    }

#ifdef HAVE_CURL
    CURLcode res;
    FILE * file = fopen (tmp_file_location, "w");

    curl = opkg_curl_init (cb, data);
    if (curl)
    {
	curl_easy_setopt (curl, CURLOPT_URL, src);
	curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);

	res = curl_easy_perform (curl);
	fclose (file);
	if (res)
	{
	    long error_code;
	    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
	    opkg_msg(hide_error?DEBUG2:ERROR, "Failed to download %s: %s.\n",
		    src, curl_easy_strerror(res));
	    free(tmp_file_location);
	    return -1;
	}

    }
    else
    {
	free(tmp_file_location);
	return -1;
    }
#else
    {
      int res;
      const char *argv[8];
      int i = 0;

      argv[i++] = "wget";
      argv[i++] = "-q";
      if (conf->http_proxy || conf->ftp_proxy) {
	argv[i++] = "-Y";
	argv[i++] = "on";
      }
      argv[i++] = "-O";
      argv[i++] = tmp_file_location;
      argv[i++] = src;
      argv[i++] = NULL;
      res = xsystem(argv);

      if (res) {
	opkg_msg(ERROR, "Failed to download %s, wget returned %d.\n", src, res);
	free(tmp_file_location);
	return -1;
      }
    }
#endif

    err = file_move(tmp_file_location, dest_file_name);

    free(tmp_file_location);

    return err;
}
示例#16
0
/**
 *  Static: Rename a data file.
 *
 *  This function will rename a raw data file into the datastream directory
 *  using the datastream name and begin_time to give it a fully qualified
 *  ARM name.
 *
 *  The begin_time will be validated using:
 *
 *      dsproc_validate_datastream_data_time()
 *
 *  If the end_time is specified, this function will verify that it is greater
 *  than the begin_time and is not in the future. If only one record was found
 *  in the raw file, the end_time argument must be set to NULL.
 *
 *  If the output file exists and has the same MD5 as the input file,
 *  the input file will be removed and a warning message will be generated.
 *
 *  If the output file exists and has a different MD5 than the input
 *  file, the rename will fail.
 *
 *  If an error occurs in this function it will be appended to the log and
 *  error mail messages, and the process status will be set appropriately.
 *
 *  @param  ds_id      - datastream ID
 *  @param  file_path  - path to the directory the file is in
 *  @param  file_name  - name of the file to rename
 *  @param  begin_time - time of the first record in the file
 *  @param  end_time   - time of the last record in the file
 *  @param  extension  - extension to use when renaming the file, or NULL to
 *                       use the default extension for the datastream format.
 *
 *  @return
 *    - 1 if successful
 *    - 0 if an error occurred
 */
static int _dsproc_rename(
    int              ds_id,
    const char      *file_path,
    const char      *file_name,
    const timeval_t *begin_time,
    const timeval_t *end_time,
    const char      *extension)
{
    DataStream *ds         = _DSProc->datastreams[ds_id];
    time_t      now        = time(NULL);
    int         force_mode = dsproc_get_force_mode();
    char        src_file[PATH_MAX];
    struct stat src_stats;
    char        src_md5[64];
    char        done_dir[PATH_MAX];
    char       *dest_path;
    char        dest_name[256];
    char        dest_file[PATH_MAX];
    char        dest_md5[64];
    struct tm   time_stamp_tm;
    char        time_stamp[32];
    char        time_string1[32];
    char        time_string2[32];
    const char *preserve;
    int         dot_count;
    char        rename_error[2*PATH_MAX];
    int         rename_file;

    memset(&time_stamp_tm, 0, sizeof(struct tm));

    DEBUG_LV1( DSPROC_LIB_NAME,
        "%s: Renaming file: %s/%s\n",
        ds->name, file_path, file_name);

    /************************************************************
    *  Get source file stats
    *************************************************************/

    snprintf(src_file, PATH_MAX, "%s/%s", file_path, file_name);

    if (access(src_file, F_OK) != 0) {

        if (errno == ENOENT) {

            ERROR( DSPROC_LIB_NAME,
                "Could not rename file: %s\n"
                " -> file does not exist\n", src_file);

            dsproc_set_status(DSPROC_ENOSRCFILE);
        }
        else {

            ERROR( DSPROC_LIB_NAME,
                "Could not access file: %s\n"
                " -> %s\n", src_file, strerror(errno));

            dsproc_set_status(DSPROC_EACCESS);
        }

        return(0);
    }

    if (stat(src_file, &src_stats) < 0) {

        ERROR( DSPROC_LIB_NAME,
            "Could not rename file: %s\n"
            " -> stat error: %s\n", src_file, strerror(errno));

        dsproc_set_status(DSPROC_EFILESTATS);
        return(0);
    }

    /************************************************************
    *  Validate datastream times
    *************************************************************/

    /* validate begin time */

    if (!force_mode) {

        if (!begin_time || !begin_time->tv_sec) {

            ERROR( DSPROC_LIB_NAME,
                "Could not rename file: %s\n"
                " -> file time not specified\n", src_file);

            dsproc_set_status(DSPROC_ENOFILETIME);
            return(0);
        }

        if (!dsproc_validate_datastream_data_time(ds_id, begin_time)) {

            ERROR( DSPROC_LIB_NAME,
                "Could not rename file: %s\n"
                " -> time validation error\n", src_file);

            return(0);
        }

        /* validate end time */

        if (end_time && end_time->tv_sec) {

            if (TV_LTEQ(*end_time, *begin_time)) {

                format_timeval(begin_time, time_string2);
                format_timeval(end_time,   time_string1);

                ERROR( DSPROC_LIB_NAME,
                    "Could not rename file: %s\n"
                    " -> end time '%s' <= begin time '%s'\n",
                    src_file, time_string1, time_string2);

                dsproc_set_status(DSPROC_ETIMEORDER);
                return(0);
            }
            else if (end_time->tv_sec > now) {

                format_timeval(end_time, time_string1);
                format_secs1970(now, time_string2);

                ERROR( DSPROC_LIB_NAME,
                    "Could not rename file: %s\n"
                    " -> data time '%s' is in the future (current time is: %s)\n",
                    src_file, time_string1, time_string2);

                dsproc_disable(DSPROC_EFUTURETIME);
                return(0);
            }
        }
    }

    /************************************************************
    *  Determine the portion of the original file name to preserve
    *************************************************************/

    preserve = (const char *)NULL;

    if (ds->preserve_dots > 0) {

        dot_count = 0;

        for (preserve = file_name + strlen(file_name) - 1;
             preserve != file_name;
             preserve--) {

            if (*preserve == '.') {
                dot_count++;
                if (dot_count == ds->preserve_dots) {
                    preserve++;
                    break;
                }
            }
        }
    }

    /************************************************************
    *  Get the full path to the destination directory
    *************************************************************/

    snprintf(done_dir, PATH_MAX, "%s/.done", file_path);

    if (access(done_dir, F_OK) == 0) {
        dest_path = done_dir;
    }
    else if (errno != ENOENT) {

        ERROR( DSPROC_LIB_NAME,
            "Could not access directory: %s\n"
            " -> %s\n", done_dir, strerror(errno));

        dsproc_set_status(DSPROC_EACCESS);
        return(0);
    }
    else {

        dest_path = ds->dir->path;

        /* Make sure the destination directory exists */

        if (!make_path(dest_path, 0775)) {

            ERROR( DSPROC_LIB_NAME,
                "Could not rename file: %s\n"
                " -> make_path failed for: %s\n",
                src_file, dest_path);

            dsproc_set_status(DSPROC_EDESTDIRMAKE);
            return(0);
        }
    }

    /************************************************************
    *  Create the time stamp for the destination file
    *************************************************************/

    if (!gmtime_r(&begin_time->tv_sec, &time_stamp_tm)) {

        ERROR( DSPROC_LIB_NAME,
            "Could not rename file: %s\n"
            " -> gmtime error: %s\n",
            src_file, strerror(errno));

        dsproc_set_status(DSPROC_ETIMECALC);
        return(0);
    }

    strftime(time_stamp, 32, "%Y%m%d.%H%M%S", &time_stamp_tm);

    /************************************************************
    *  Create the output file name
    *************************************************************/

    if (!extension) {
        extension = ds->extension;
    }

    if (preserve) {
        snprintf(dest_name, 256, "%s.%s.%s.%s",
            ds->name, time_stamp, extension, preserve);
    }
    else {
        snprintf(dest_name, 256, "%s.%s.%s",
            ds->name, time_stamp, extension);
    }

    snprintf(dest_file, PATH_MAX, "%s/%s", dest_path, dest_name);

    /************************************************************
    *  Check if the output file already exists
    *************************************************************/

    rename_file = 1;

    if (access(dest_file, F_OK) == 0) {

        sprintf(rename_error,
            "\n"
            "Found existing destination file while renaming source file:\n"
            " -> from: %s\n"
            " -> to:   %s\n",
            src_file, dest_file);

        /* Check the MD5s */

        if (!file_get_md5(src_file, src_md5)) {

            ERROR( DSPROC_LIB_NAME,
                "%s -> could not get source file MD5\n",
                rename_error);

            dsproc_set_status(DSPROC_EFILEMD5);
            return(0);
        }

        if (!file_get_md5(dest_file, dest_md5)) {

            ERROR( DSPROC_LIB_NAME,
                "%s -> could not get destination file MD5\n",
                rename_error);

            dsproc_set_status(DSPROC_EFILEMD5);
            return(0);
        }

        if (strcmp(src_md5, dest_md5) == 0) {

            /* The MD5s match so delete the input file */

            if (unlink(src_file) < 0) {

                ERROR( DSPROC_LIB_NAME,
                    "%s"
                    " -> source and destination files have matching MD5s\n"
                    " -> could not delete source file: %s\n",
                    rename_error, strerror(errno));

                dsproc_set_status(DSPROC_EUNLINK);
                return(0);
            }

            WARNING( DSPROC_LIB_NAME,
                "%s"
                " -> source and destination files have matching MD5s\n"
                " -> deleted source file\n",
                rename_error);

            rename_file = 0;
        }
        else {

            /* The MD5s do not match */

            ERROR( DSPROC_LIB_NAME,
                "%s"
                " -> source and destination files have different MD5s\n",
                rename_error);

            dsproc_set_status(DSPROC_EMD5CHECK);
            return(0);
        }
    }
    else if (errno != ENOENT) {

        ERROR( DSPROC_LIB_NAME,
            "Could not access file: %s\n"
            " -> %s\n", dest_file, strerror(errno));

        dsproc_set_status(DSPROC_EACCESS);
        return(0);
    }

    /************************************************************
    *  Rename the file
    *************************************************************/

    if (rename_file) {

        LOG( DSPROC_LIB_NAME,
            "Renaming:   %s\n"
            " -> to:     %s\n",
            src_file, dest_file);

        if (!file_move(src_file, dest_file, FC_CHECK_MD5)) {
            dsproc_set_status(DSPROC_EFILEMOVE);
            return(0);
        }

        /* Update the access and modification times */

        utime(dest_file, NULL);
    }

    /************************************************************
    *  Update the datastream file stats
    *************************************************************/

    dsproc_update_datastream_file_stats(
        ds_id, (double)src_stats.st_size, begin_time, end_time);

    return(1);
}
示例#17
0
/*
 * Attempt to save the player in a savefile
 */
bool savefile_save(const char *path)
{
	ang_file *file;

	char new_savefile[1024];
	char old_savefile[1024];

	/* New savefile */
	strnfmt(new_savefile, sizeof(new_savefile), "%s.new", path);
	strnfmt(old_savefile, sizeof(old_savefile), "%s.old", path);

	/* Make sure that the savefile doesn't already exist */
	safe_setuid_grab();
	file_delete(new_savefile);
	file_delete(old_savefile);
	safe_setuid_drop();

	/* Open the savefile */
	safe_setuid_grab();
	file = file_open(new_savefile, MODE_WRITE, FTYPE_SAVE);
	safe_setuid_drop();
	
	if (file)
	{
		file_write(file, (char *) &savefile_magic, 4);
		file_write(file, (char *) &savefile_name, 4);

		character_saved = try_save(file);
		file_close(file);
	}

	if (character_saved)
	{
		bool err = FALSE;

		safe_setuid_grab();

		if (file_exists(savefile) && !file_move(savefile, old_savefile))
			err = TRUE;

		if (!err)
		{
			if (!file_move(new_savefile, savefile))
				err = TRUE;

			if (err)
				file_move(old_savefile, savefile);
			else
				file_delete(old_savefile);
		}

		safe_setuid_drop();

		return err ? FALSE : TRUE;
	}
	
	/* Delete temp file */
	safe_setuid_grab();
	file_delete(new_savefile);
	safe_setuid_drop();

	return FALSE;
}
示例#18
0
struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
{
    struct file * f;
    struct inode *inode;
    static LIST_HEAD(kill_list);
    int error;

    error = -ENFILE;
    f = get_empty_filp();
    if (!f)
        goto cleanup_dentry;
    f->f_flags = flags;
    f->f_mode = (flags+1) & O_ACCMODE;
    inode = dentry->d_inode;
    if (f->f_mode & FMODE_WRITE) {
        error = get_write_access(inode);
        if (error)
            goto cleanup_file;
    }

    f->f_dentry = dentry;
    f->f_vfsmnt = mnt;
    f->f_pos = 0;
    f->f_reada = 0;
    f->f_op = fops_get(inode->i_fop);
    file_move(f, &inode->i_sb->s_files);

    /* preallocate kiobuf for O_DIRECT */
    f->f_iobuf = NULL;
    f->f_iobuf_lock = 0;
    if (f->f_flags & O_DIRECT) {
        error = alloc_kiovec(1, &f->f_iobuf);
        if (error)
            goto cleanup_all;
    }

    if (f->f_op && f->f_op->open) {
        error = f->f_op->open(inode,f);
        if (error)
            goto cleanup_all;
    }
    f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);

    return f;

cleanup_all:
    if (f->f_iobuf)
        free_kiovec(1, &f->f_iobuf);
    fops_put(f->f_op);
    if (f->f_mode & FMODE_WRITE)
        put_write_access(inode);
    file_move(f, &kill_list); /* out of the way.. */
    f->f_dentry = NULL;
    f->f_vfsmnt = NULL;
cleanup_file:
    put_filp(f);
cleanup_dentry:
    dput(dentry);
    mntput(mnt);
    return ERR_PTR(error);
}
示例#19
0
static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
					int flags, struct file *f,
					int (*open)(struct inode *, struct file *),
					const struct cred *cred)
{
	struct inode *inode;
	int error;

	f->f_flags = flags;
	f->f_mode = (__force fmode_t)((flags+1) & O_ACCMODE) | FMODE_LSEEK |
				FMODE_PREAD | FMODE_PWRITE;
	inode = dentry->d_inode;
	if (f->f_mode & FMODE_WRITE) {
		error = __get_file_write_access(inode, mnt);
		if (error)
			goto cleanup_file;
		if (!special_file(inode->i_mode))
			file_take_write(f);
	}

	f->f_mapping = inode->i_mapping;
	f->f_path.dentry = dentry;
	f->f_path.mnt = mnt;
	f->f_pos = 0;
	f->f_op = fops_get(inode->i_fop);
	file_move(f, &inode->i_sb->s_files);

	error = security_dentry_open(f, cred);
	if (error)
		goto cleanup_all;

	if (!open && f->f_op)
		open = f->f_op->open;
	if (open) {
		error = open(inode, f);
		if (error)
			goto cleanup_all;
	}

	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);

	file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);

	/* NB: we're sure to have correct a_ops only after f_op->open */
	if (f->f_flags & O_DIRECT) {
		if (!f->f_mapping->a_ops ||
		    ((!f->f_mapping->a_ops->direct_IO) &&
		    (!f->f_mapping->a_ops->get_xip_mem))) {
			fput(f);
			f = ERR_PTR(-EINVAL);
		}
	}

	return f;

cleanup_all:
	fops_put(f->f_op);
	if (f->f_mode & FMODE_WRITE) {
		put_write_access(inode);
		if (!special_file(inode->i_mode)) {
			/*
			 * We don't consider this a real
			 * mnt_want/drop_write() pair
			 * because it all happenend right
			 * here, so just reset the state.
			 */
			file_reset_write(f);
			mnt_drop_write(mnt);
		}
	}
	file_kill(f);
	f->f_path.dentry = NULL;
	f->f_path.mnt = NULL;
cleanup_file:
	put_filp(f);
	dput(dentry);
	mntput(mnt);
	return ERR_PTR(error);
}
示例#20
0
/*
 * Attempt to save the player in a savefile
 */
bool savefile_save(const char *path)
{
	ang_file *file;
	int count = 0;
	char new_savefile[1024];
	char old_savefile[1024];

	/* New savefile */
	strnfmt(old_savefile, sizeof(old_savefile), "%s%u.old", path,Rand_simple(1000000));
	while (file_exists(old_savefile) && (count++ < 100)) {
		strnfmt(old_savefile, sizeof(old_savefile), "%s%u%u.old", path,Rand_simple(1000000),count);
	}
	count = 0;
	/* Make sure that the savefile doesn't already exist */
	/*safe_setuid_grab();
	file_delete(new_savefile);
	file_delete(old_savefile);
	safe_setuid_drop();*/

	/* Open the savefile */
	safe_setuid_grab();
	strnfmt(new_savefile, sizeof(new_savefile), "%s%u.new", path,Rand_simple(1000000));
	while (file_exists(new_savefile) && (count++ < 100)) {
		strnfmt(new_savefile, sizeof(new_savefile), "%s%u%u.new", path,Rand_simple(1000000),count);
	}
	file = file_open(new_savefile, MODE_WRITE, FTYPE_SAVE);
	safe_setuid_drop();

	if (file)
	{
		file_write(file, (char *) &savefile_magic, 4);
		file_write(file, (char *) &savefile_name, 4);

		character_saved = try_save(file);
		file_close(file);
	}

	if (character_saved)
	{
		bool err = FALSE;

		safe_setuid_grab();

		if (file_exists(savefile) && !file_move(savefile, old_savefile))
			err = TRUE;

		if (!err)
		{
			if (!file_move(new_savefile, savefile))
				err = TRUE;

			if (err)
				file_move(old_savefile, savefile);
			else
				file_delete(old_savefile);
		} 

		safe_setuid_drop();

		return err ? FALSE : TRUE;
	}

	/* Delete temp file if the save failed */
	if (file)
	{
		/* file is no longer valid, but it still points to a non zero
		 * value if the file was created above */
		safe_setuid_grab();
		file_delete(new_savefile);
		safe_setuid_drop();
	}
	return FALSE;
}
示例#21
0
static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags, struct file *f)
{
	struct inode *inode;
	static LIST_HEAD(kill_list);
	int error;

	f->f_flags = flags;
	f->f_mode = (flags+1) & O_ACCMODE;
	inode = dentry->d_inode;
	if (f->f_mode & FMODE_WRITE) {
		error = get_write_access(inode);
		if (error)
			goto cleanup_file;
	}

	f->f_dentry = dentry;
	f->f_vfsmnt = mnt;
	f->f_pos = 0;
	f->f_reada = 0;
	f->f_op = fops_get(inode->i_fop);
	file_move(f, &inode->i_sb->s_files);

	/* preallocate kiobuf for O_DIRECT */
	f->f_iobuf = NULL;
	f->f_iobuf_lock = 0;
	if (f->f_flags & O_DIRECT) {
		error = alloc_kiovec(1, &f->f_iobuf);
		if (error)
			goto cleanup_all;
	}

	if (f->f_op && f->f_op->open) {
		error = f->f_op->open(inode,f);
		if (error)
			goto cleanup_all;
	}
	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);

	/* NB: we're sure to have correct a_ops only after f_op->open */
	if (f->f_flags & O_DIRECT) {
		if (!inode->i_mapping || !inode->i_mapping->a_ops ||
		    !(inode->i_mapping->a_ops->direct_IO ||
		      inode->i_mapping->a_ops->direct_sector_IO)) {
			fput(f);
			f = ERR_PTR(-EINVAL);
		}
	}

	return f;

cleanup_all:
	if (f->f_iobuf)
		free_kiovec(1, &f->f_iobuf);
	fops_put(f->f_op);
	if (f->f_mode & FMODE_WRITE)
		put_write_access(inode);
	file_move(f, &kill_list); /* out of the way.. */
	f->f_dentry = NULL;
	f->f_vfsmnt = NULL;
cleanup_file:
	put_filp(f);
	dput(dentry);
	mntput(mnt);
	return ERR_PTR(error);
}