1
1

* src/ext.c (exec_extension): Use mc_mkstemps().

* src/user.c (execute_menu_command): Use mc_mkstemps().
* src/util.c (mc_mkstemps): New function - safely create and
open temporary file. Return the handle and the name.
* src/util.h: Declarations for init_tmpdir() and mc_mkstemps().
Define TMPDIR_DEFAULT and SCRIPT_SUFFIX.
* vfs/direntry.c (vfs_s_open): Use mc_mkstemps. Create and close
temporary file to reserve its name on the filesystem.
(vfs_s_retrieve_file): Use mc_mkstemps().
(g_tempnam): Remove.
* vfs/extfs.c (extfs_open): Use mc_mkstemps().
* vfs/sfs.c (redirect): Likewise.
* vfs/shared_ftp_fish.c (_get_file_entry): Likewise.
(retrieve_file): Likewise.
* vfs/vfs.c (mc_def_getlocalcopy): Likewise.
* vfs/xdirentry.h: Remove declaration of g_tempnam().
Этот коммит содержится в:
Pavel Roskin 2001-05-21 16:21:07 +00:00
родитель f501a07de8
Коммит bea09b932d
12 изменённых файлов: 224 добавлений и 139 удалений

Просмотреть файл

@ -1,3 +1,12 @@
2001-05-21 Pavel Roskin <proski@gnu.org>
* src/ext.c (exec_extension): Use mc_mkstemps().
* src/user.c (execute_menu_command): Use mc_mkstemps().
* src/util.c (mc_mkstemps): New function - safely create and
open temporary file. Return the handle and the name.
* src/util.h: Declarations for init_tmpdir() and mc_mkstemps().
Define TMPDIR_DEFAULT and SCRIPT_SUFFIX.
2001-05-18 Pavel Roskin <proski@gnu.org> 2001-05-18 Pavel Roskin <proski@gnu.org>
* TODO: Remove Tk-related entries. * TODO: Remove Tk-related entries.

Просмотреть файл

@ -139,14 +139,10 @@ exec_extension (const char *filename, const char *data, char **drops, int *move_
else else
do_local_copy = 0; do_local_copy = 0;
if ((file_name = tempnam (NULL, "mcext")) == 0) { cmd_file_fd = mc_mkstemps(&file_name, "mcext", SCRIPT_SUFFIX);
message (1, MSG_ERROR, _(" Can't generate unique filename \n %s "),
unix_error_string (errno));
return;
}
/* #warning FIXME: this is ugly */ /* #warning FIXME: this is ugly */
if ((cmd_file_fd = open (file_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600)) == -1){ if (cmd_file_fd == -1){
message (1, MSG_ERROR, _(" Can't create temporary command file \n %s "), message (1, MSG_ERROR, _(" Can't create temporary command file \n %s "),
unix_error_string (errno)); unix_error_string (errno));
free (file_name); free (file_name);

Просмотреть файл

@ -540,19 +540,9 @@ execute_menu_command (char *commands)
return; return;
} }
if ((file_name = tempnam (NULL, "mcusr")) == 0) { cmd_file_fd = mc_mkstemps(&file_name, "mcusr", SCRIPT_SUFFIX);
message (1, MSG_ERROR, _(" Can't generate unique filename \n %s "),
unix_error_string (errno));
return;
}
#ifdef OS2_NT if (cmd_file_fd == -1){
/* OS/2 and NT requires the command to end in .cmd */
p = file_name;
file_name = g_strconcat (file_name, ".cmd", NULL);
free (p);
#endif
if ((cmd_file_fd = open (file_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600)) == -1){
message (1, MSG_ERROR, _(" Can't create temporary command file \n %s "), message (1, MSG_ERROR, _(" Can't create temporary command file \n %s "),
unix_error_string (errno)); unix_error_string (errno));
free (file_name); free (file_name);

Просмотреть файл

@ -1272,3 +1272,96 @@ concat_dir_and_file (const char *dir, const char *file)
return g_strconcat (dir, PATH_SEP_STR, file, NULL); return g_strconcat (dir, PATH_SEP_STR, file, NULL);
} }
/* Following code heavily borrows from libiberty, mkstemps.c */
/* Use 64-bit "enthropy" if available */
#ifdef __GNUC__
__extension__ typedef unsigned long long gcc_uint64_t;
#else
typedef unsigned long gcc_uint64_t;
#endif
/* Number of attempts to create a temporary file */
#ifndef TMP_MAX
#define TMP_MAX 16384
#endif
/*
* Arguments:
* pname - if not NULL, put the filename here.
* prefix - filename only, temporary directory is prepended.
* suffix - if not NULL, appended after the random part.
*
* Result:
* handle of the open file or -1 if couldn't open any.
*/
int mc_mkstemps(char **pname, const char *prefix, const char *suffix)
{
static const char letters[]
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static gcc_uint64_t value;
struct timeval tv;
char *tmpdir;
char *tmpbase;
char *tmpname;
char *XXXXXX;
int count;
tmpdir = getenv("TMPDIR");
if (!tmpdir) {
tmpdir = TMPDIR_DEFAULT;
}
/* Add prefix first to find the position of XXXXXX */
tmpbase = concat_dir_and_file (tmpdir, prefix);
tmpname = g_strconcat (tmpbase, "XXXXXX", suffix, NULL);
if (pname)
*pname = tmpname;
XXXXXX = &tmpname[strlen (tmpbase)];
g_free(tmpbase);
/* Get some more or less random data. */
gettimeofday (&tv, NULL);
value += (tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
for (count = 0; count < TMP_MAX; ++count) {
gcc_uint64_t v = value;
int fd;
/* Fill in the random bits. */
XXXXXX[0] = letters[v % 62];
v /= 62;
XXXXXX[1] = letters[v % 62];
v /= 62;
XXXXXX[2] = letters[v % 62];
v /= 62;
XXXXXX[3] = letters[v % 62];
v /= 62;
XXXXXX[4] = letters[v % 62];
v /= 62;
XXXXXX[5] = letters[v % 62];
fd = open (tmpname, O_RDWR|O_CREAT|O_EXCL, 0600);
if (fd >= 0) {
/* Successfully created. */
if (!pname)
g_free (tmpname);
return fd;
}
/* This is a random value. It is only necessary that the next
TMP_MAX values generated by adding 7777 to VALUE are different
with (module 2^32). */
value += 7777;
}
/* We return the null string if we can't find a unique file name.
Of course, only if the caller wants any string. */
if (!pname)
g_free (tmpname);
else
tmpname[0] = '\0';
return -1;
}

Просмотреть файл

@ -146,6 +146,10 @@ void remove_dash (void);
extern char app_text []; extern char app_text [];
/* Creating temporary files safely */
void init_tmpdir(void);
int mc_mkstemps(char **pname, const char *prefix, const char *suffix);
enum { enum {
ISGUNZIPABLE_GUNZIP, ISGUNZIPABLE_GUNZIP,
ISGUNZIPABLE_BZIP, ISGUNZIPABLE_BZIP,
@ -179,6 +183,8 @@ int max_open_files (void);
# define PATH_SEP '\\' # define PATH_SEP '\\'
# define PATH_SEP_STR "\\" # define PATH_SEP_STR "\\"
# define PATH_ENV_SEP ';' # define PATH_ENV_SEP ';'
# define TMPDIR_DEFAULT "c:\\temp"
# define SCRIPT_SUFFIX ".cmd"
# define OS_SORT_CASE_SENSITIVE_DEFAULT 0 # define OS_SORT_CASE_SENSITIVE_DEFAULT 0
# define STRCOMP stricmp # define STRCOMP stricmp
# define STRNCOMP strnicmp # define STRNCOMP strnicmp
@ -190,6 +196,8 @@ int max_open_files (void);
# define PATH_SEP '/' # define PATH_SEP '/'
# define PATH_SEP_STR "/" # define PATH_SEP_STR "/"
# define PATH_ENV_SEP ':' # define PATH_ENV_SEP ':'
# define TMPDIR_DEFAULT "/tmp"
# define SCRIPT_SUFFIX ""
# define get_default_editor() "vi" # define get_default_editor() "vi"
# define OS_SORT_CASE_SENSITIVE_DEFAULT 1 # define OS_SORT_CASE_SENSITIVE_DEFAULT 1
# define STRCOMP strcmp # define STRCOMP strcmp

Просмотреть файл

@ -1,3 +1,16 @@
2001-05-21 Pavel Roskin <proski@gnu.org>
* vfs/direntry.c (vfs_s_open): Use mc_mkstemps. Create and close
temporary file to reserve its name on the filesystem.
(vfs_s_retrieve_file): Use mc_mkstemps().
(g_tempnam): Remove.
* vfs/extfs.c (extfs_open): Use mc_mkstemps().
* vfs/sfs.c (redirect): Likewise.
* vfs/shared_ftp_fish.c (_get_file_entry): Likewise.
(retrieve_file): Likewise.
* vfs/vfs.c (mc_def_getlocalcopy): Likewise.
* vfs/xdirentry.h: Remove declaration of g_tempnam().
2001-05-18 Pavel Roskin <proski@gnu.org> 2001-05-18 Pavel Roskin <proski@gnu.org>
* vfs.c (vfs_addstamp): Rename st1 to last_stamp. Initialize it * vfs.c (vfs_addstamp): Rename st1 to last_stamp. Initialize it

Просмотреть файл

@ -754,6 +754,7 @@ vfs_s_open (vfs *me, char *file, int flags, int mode)
char *dirname, *name, *save; char *dirname, *name, *save;
vfs_s_entry *ent; vfs_s_entry *ent;
vfs_s_inode *dir; vfs_s_inode *dir;
int tmp_handle;
if (!(flags & O_CREAT)) if (!(flags & O_CREAT))
return NULL; return NULL;
@ -766,7 +767,10 @@ vfs_s_open (vfs *me, char *file, int flags, int mode)
ent = vfs_s_generate_entry (me, name, dir, 0755); ent = vfs_s_generate_entry (me, name, dir, 0755);
ino = ent->ino; ino = ent->ino;
vfs_s_insert_entry (me, dir, ent); vfs_s_insert_entry (me, dir, ent);
ino->localname = g_tempnam (NULL, me->name); tmp_handle = mc_mkstemps (&ino->localname, me->name, NULL);
if (tmp_handle == -1)
return NULL;
close (tmp_handle);
was_changed = 1; was_changed = 1;
} }
@ -935,9 +939,8 @@ vfs_s_retrieve_file(vfs *me, struct vfs_s_inode *ino)
memset(&fh, 0, sizeof(fh)); memset(&fh, 0, sizeof(fh));
fh.ino = ino; fh.ino = ino;
if (!(ino->localname = g_tempnam (NULL, me->name))) ERRNOR (ENOMEM, 0);
handle = open(ino->localname, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600); handle = mc_mkstemps (&ino->localname, me->name, NULL);
if (handle == -1) { if (handle == -1) {
me->verrno = errno; me->verrno = errno;
goto error_4; goto error_4;
@ -1185,20 +1188,3 @@ vfs_s_get_line_interruptible (vfs *me, char *buffer, int size, int fd)
buffer [size-1] = 0; buffer [size-1] = 0;
return 0; return 0;
} }
/* Roland: on most non-GNU/Linux platforms malloc()!=g_malloc() which
* may cause crashes. This wrapper ensures that memory from tempnam
* can safely free'ed with g_free()
*/
char *
g_tempnam( const char *dir, const char *prefix )
{
char *tmp = (char *)tempnam( dir, prefix );
char *name;
name = g_strdup( tmp );
free( tmp );
return( name );
}

Просмотреть файл

@ -603,11 +603,10 @@ static void *extfs_open (vfs *me, char *file, int flags, int mode)
char *cmd; char *cmd;
char *archive_name, *p; char *archive_name, *p;
entry->inode->local_filename = g_tempnam (NULL, "extfs");
{ {
int handle; int handle;
handle = mc_mkstemps (&entry->inode->local_filename, "extfs", NULL);
handle = open(entry->inode->local_filename, O_RDWR | O_CREAT | O_EXCL, 0600);
if (handle == -1) if (handle == -1)
return NULL; return NULL;
close(handle); close(handle);

Просмотреть файл

@ -126,8 +126,8 @@ redirect (vfs *me, char *name)
cur = cur->next; cur = cur->next;
} }
cache = tempnam (NULL, "sfs"); handle = mc_mkstemps (&cache, "sfs", NULL);
handle = open (cache, O_RDWR | O_CREAT | O_EXCL, 0600);
if (handle == -1) { if (handle == -1) {
g_free (cache); g_free (cache);
return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US"; return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US";

Просмотреть файл

@ -330,9 +330,7 @@ _get_file_entry(struct connection *bucket, char *file_name,
ent->local_filename = NULL; ent->local_filename = NULL;
} }
if (flags & O_TRUNC) { if (flags & O_TRUNC) {
ent->local_filename = g_tempnam (NULL, X "fs"); handle = mc_mkstemps (&ent->local_filename, X "fs", NULL);
if (ent->local_filename == NULL) ERRNOR (ENOMEM, NULL);
handle = open(ent->local_filename, O_CREAT | O_TRUNC | O_RDWR | O_EXCL, 0600);
if (handle < 0) ERRNOR (EIO, NULL); if (handle < 0) ERRNOR (EIO, NULL);
close(handle); close(handle);
if (stat (ent->local_filename, &ent->local_stat) < 0) if (stat (ent->local_filename, &ent->local_stat) < 0)
@ -368,12 +366,8 @@ _get_file_entry(struct connection *bucket, char *file_name,
ent->bucket = bucket; ent->bucket = bucket;
ent->name = g_strdup(p); ent->name = g_strdup(p);
ent->remote_filename = g_strdup(file_name); ent->remote_filename = g_strdup(file_name);
ent->local_filename = g_tempnam (NULL, X "fs");
if (!ent->name || !ent->remote_filename || !ent->local_filename) { handle = mc_mkstemps (&ent->local_filename, X "fs", NULL);
direntry_destructor(ent);
ERRNOR (ENOMEM, NULL);
}
handle = open (ent->local_filename, O_CREAT | O_EXCL | O_RDWR | O_TRUNC, 0700);
if (handle == -1) { if (handle == -1) {
my_errno = EIO; my_errno = EIO;
goto error; goto error;
@ -841,10 +835,9 @@ static int retrieve_file(struct direntry *fe)
if (fe->local_filename) if (fe->local_filename)
return 1; return 1;
if (!(fe->local_filename = g_tempnam (NULL, X))) ERRNOR (ENOMEM, 0);
fe->local_is_temp = 1; fe->local_is_temp = 1;
local_handle = open(fe->local_filename, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600); local_handle = mc_mkstemps (&fe->local_filename, X, NULL);
if (local_handle == -1) { if (local_handle == -1) {
my_errno = EIO; my_errno = EIO;
goto error_4; goto error_4;

Просмотреть файл

@ -1062,8 +1062,8 @@ mc_def_getlocalcopy (vfs *vfs, char *filename)
fdin = mc_open (filename, O_RDONLY); fdin = mc_open (filename, O_RDONLY);
if (fdin == -1) if (fdin == -1)
return NULL; return NULL;
tmp = g_tempnam (NULL, "mclocalcopy");
fdout = open (tmp, O_CREAT|O_WRONLY|O_TRUNC|O_EXCL, 0600); fdout = mc_mkstemps (&tmp, "mclocalcopy", NULL);
if (fdout == -1) if (fdout == -1)
goto fail; goto fail;
while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0){ while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0){

Просмотреть файл

@ -254,8 +254,6 @@ int vfs_s_get_line_interruptible (vfs *me, char *buffer, int size, int fd);
/* misc */ /* misc */
int vfs_s_retrieve_file (vfs *me, struct vfs_s_inode *ino); int vfs_s_retrieve_file (vfs *me, struct vfs_s_inode *ino);
/* alloc temp name which can be safely free'd with g_free() */
char *g_tempnam( const char *dir, const char *prefix );
#if 0 #if 0
/* If non-null, FREE */ /* If non-null, FREE */