Define grub_util_is_directory/regular/special_file and

use OS-dependent versions rather than to rely on stat().
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-10-15 20:27:58 +02:00
parent ec16e02639
commit df1e64c98e
6 changed files with 113 additions and 31 deletions

View file

@ -1,3 +1,8 @@
2013-10-15 Vladimir Serbinenko <phcoder@gmail.com>
Define grub_util_is_directory/regular/special_file and
use OS-dependent versions rather than to rely on stat().
2013-10-15 Vladimir Serbinenko <phcoder@gmail.com> 2013-10-15 Vladimir Serbinenko <phcoder@gmail.com>
* util/grub-mkimage.c: Move backend part to ... * util/grub-mkimage.c: Move backend part to ...

View file

@ -30,11 +30,8 @@
#include <grub/emu/hostdisk.h> #include <grub/emu/hostdisk.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <dirent.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <sys/stat.h>
#include <string.h> #include <string.h>
static int static int
@ -52,10 +49,7 @@ is_dir (const char *path, const char *name)
strcat (pathname, name); strcat (pathname, name);
struct stat st; return grub_util_is_directory (pathname);
if (stat (pathname, &st))
return 0;
return S_ISDIR (st.st_mode);
} }
struct grub_hostfs_data struct grub_hostfs_data

View file

@ -216,4 +216,36 @@ grub_util_fopen (const char *path, const char *mode)
return fopen (path, mode); return fopen (path, mode);
} }
int
grub_util_is_directory (const char *path)
{
struct stat st;
if (stat (path, &st) == -1)
return 0;
return S_ISDIR (st.st_mode);
}
int
grub_util_is_regular (const char *path)
{
struct stat st;
if (stat (path, &st) == -1)
return 0;
return S_ISREG (st.st_mode);
}
int
grub_util_is_special_file (const char *path)
{
struct stat st;
if (lstat (path, &st) == -1)
return 1;
return (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode));
}
#endif #endif

View file

@ -410,6 +410,39 @@ grub_util_unlink (const char *name)
return ret; return ret;
} }
int
grub_util_is_directory (const char *name)
{
LPTSTR name_windows;
DWORD attr;
name_windows = grub_util_get_windows_path (name);
if (!name_windows)
return 0;
attr = GetFileAttributes (name_windows);
grub_free (name_windows);
return !!(attr & FILE_ATTRIBUTE_DIRECTORY);
}
int
grub_util_is_regular (const char *name)
{
LPTSTR name_windows;
DWORD attr;
name_windows = grub_util_get_windows_path (name);
if (!name_windows)
return 0;
attr = GetFileAttributes (name_windows);
grub_free (name_windows);
return !(attr & FILE_ATTRIBUTE_DIRECTORY)
&& !(attr & FILE_ATTRIBUTE_REPARSE_POINT) && attr;
}
#ifdef __MINGW32__ #ifdef __MINGW32__
FILE * FILE *
@ -438,6 +471,22 @@ int fsync (int fno)
return 0; return 0;
} }
int
grub_util_is_special_file (const char *name)
{
LPTSTR name_windows;
DWORD attr;
name_windows = grub_util_get_windows_path (name);
if (!name_windows)
return 1;
attr = GetFileAttributes (name_windows);
grub_free (name_windows);
return !!(attr & FILE_ATTRIBUTE_REPARSE_POINT) || !attr;
}
#else #else
FILE * FILE *
@ -446,4 +495,14 @@ grub_util_fopen (const char *path, const char *mode)
return fopen (path, mode); return fopen (path, mode);
} }
int
grub_util_is_special_file (const char *path)
{
struct stat st;
if (lstat (destnew, &st) == -1)
return 1;
return (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode));
}
#endif #endif

View file

@ -24,6 +24,13 @@
#include <sys/types.h> #include <sys/types.h>
#include <grub/osdep/hostfile.h> #include <grub/osdep/hostfile.h>
int
grub_util_is_directory (const char *path);
int
grub_util_is_special_file (const char *path);
int
grub_util_is_regular (const char *path);
int int
grub_util_fd_seek (grub_util_fd_t fd, grub_uint64_t off); grub_util_fd_seek (grub_util_fd_t fd, grub_uint64_t off);
ssize_t ssize_t

View file

@ -34,18 +34,11 @@
#include <grub/command.h> #include <grub/command.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/zfs/zfs.h> #include <grub/zfs/zfs.h>
#include <grub/osdep/hostfile.h> #include <grub/emu/hostfile.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "progname.h" #include "progname.h"
#include "argp.h" #include "argp.h"
@ -265,22 +258,17 @@ cmd_cmp (char *src, char *dest)
{ {
FILE *ff; FILE *ff;
struct stat st; if (grub_util_is_directory (dest))
if (stat (dest, &st) == -1)
grub_util_error (_("OS file %s open error: %s"), dest,
strerror (errno));
if (S_ISDIR (st.st_mode))
{ {
DIR *dir = opendir (dest); grub_util_fd_dir_t dir = grub_util_fd_opendir (dest);
struct dirent *entry; grub_util_fd_dirent_t entry;
if (dir == NULL) if (dir == NULL)
{ {
grub_util_error (_("OS file %s open error: %s"), dest, grub_util_error (_("OS file %s open error: %s"), dest,
strerror (errno)); grub_util_fd_strerror ());
return; return;
} }
while ((entry = readdir (dir))) while ((entry = grub_util_fd_readdir (dir)))
{ {
char *srcnew, *destnew; char *srcnew, *destnew;
char *ptr; char *ptr;
@ -298,15 +286,12 @@ cmd_cmp (char *src, char *dest)
*ptr++ = '/'; *ptr++ = '/';
strcpy (ptr, entry->d_name); strcpy (ptr, entry->d_name);
#if !defined (_WIN32) || defined (__CYGWIN__) if (grub_util_is_special_file (destnew))
if (lstat (destnew, &st) == -1 || (!S_ISREG (st.st_mode)
&& !S_ISDIR (st.st_mode)))
continue; continue;
#endif
cmd_cmp (srcnew, destnew); cmd_cmp (srcnew, destnew);
} }
closedir (dir); grub_util_fd_closedir (dir);
return; return;
} }