mirror of
https://github.com/amadvance/snapraid.git
synced 2026-10-26 19:01:57 -04:00
Fixes getting file info in Windows for some special files.
This commit is contained in:
parent
0d304116fb
commit
6bead7d180
2 changed files with 149 additions and 61 deletions
152
portable.h
152
portable.h
|
|
@ -141,7 +141,7 @@ struct windows_stat {
|
|||
};
|
||||
|
||||
/**
|
||||
* Convert Windows info to the Unix stat format.
|
||||
* Converts Windows info to the Unix stat format.
|
||||
*/
|
||||
static inline void windows_info2stat(const BY_HANDLE_FILE_INFORMATION* info, struct windows_stat* st)
|
||||
{
|
||||
|
|
@ -177,6 +177,64 @@ static inline void windows_info2stat(const BY_HANDLE_FILE_INFORMATION* info, str
|
|||
st->st_nlink = info->nNumberOfLinks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts Windows findfirst info to the Unix stat format.
|
||||
*/
|
||||
static inline void windows_finddata2stat(const WIN32_FIND_DATA* info, struct windows_stat* st)
|
||||
{
|
||||
/* Convert special attributes to a char device */
|
||||
if ((info->dwFileAttributes & (FILE_ATTRIBUTE_DEVICE | FILE_ATTRIBUTE_TEMPORARY | FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_REPARSE_POINT)) != 0) {
|
||||
st->st_mode = S_IFCHR;
|
||||
} else if ((info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
|
||||
st->st_mode = S_IFDIR;
|
||||
} else {
|
||||
st->st_mode = S_IFREG;
|
||||
}
|
||||
|
||||
st->st_size = info->nFileSizeHigh;
|
||||
st->st_size <<= 32;
|
||||
st->st_size |= info->nFileSizeLow;
|
||||
|
||||
st->st_mtime = info->ftLastWriteTime.dwHighDateTime;
|
||||
st->st_mtime <<= 32;
|
||||
st->st_mtime |= info->ftLastWriteTime.dwLowDateTime;
|
||||
|
||||
/*
|
||||
* Convert to unix time
|
||||
*
|
||||
* How To Convert a UNIX time_t to a Win32 FILETIME or SYSTEMTIME
|
||||
* http://support.microsoft.com/kb/167296
|
||||
*/
|
||||
st->st_mtime = (st->st_mtime - 116444736000000000LL) / 10000000;
|
||||
|
||||
/* No inode information available */
|
||||
st->st_ino = 0;
|
||||
|
||||
/* No link information available */
|
||||
st->st_nlink = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts Windows error to errno.
|
||||
*/
|
||||
static inline void windows_errno(DWORD error)
|
||||
{
|
||||
switch (error) {
|
||||
case ERROR_INVALID_HANDLE :
|
||||
errno = EBADF;
|
||||
break;
|
||||
case ERROR_FILE_NOT_FOUND :
|
||||
errno = ENOENT;
|
||||
break;
|
||||
case ERROR_ACCESS_DENIED :
|
||||
errno = EACCES;
|
||||
break;
|
||||
default:
|
||||
errno = EIO;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int windows_fstat(int fd, struct windows_stat* st)
|
||||
{
|
||||
BY_HANDLE_FILE_INFORMATION info;
|
||||
|
|
@ -189,7 +247,7 @@ static inline int windows_fstat(int fd, struct windows_stat* st)
|
|||
}
|
||||
|
||||
if (!GetFileInformationByHandle(h, &info)) {
|
||||
errno = EIO;
|
||||
windows_errno(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -198,38 +256,66 @@ static inline int windows_fstat(int fd, struct windows_stat* st)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like the C stat() but without inode information.
|
||||
*/
|
||||
static inline int windows_stat(const char* file, struct windows_stat* st)
|
||||
{
|
||||
HANDLE h;
|
||||
WIN32_FIND_DATA data;
|
||||
|
||||
h = FindFirstFile(file, &data);
|
||||
if (h == INVALID_HANDLE_VALUE) {
|
||||
windows_errno(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!FindClose(h)) {
|
||||
windows_errno(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
windows_finddata2stat(&data, st);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define HAVE_STAT_INODE 1
|
||||
/**
|
||||
* Like the C stat() with inode information.
|
||||
* It doesn't work for all kind of files and directories. For example "\System Volume Information" cannot be opened.
|
||||
*/
|
||||
static inline int stat_inode(const char* file, struct windows_stat* st)
|
||||
{
|
||||
BY_HANDLE_FILE_INFORMATION info;
|
||||
HANDLE h;
|
||||
|
||||
h = CreateFile(file, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
||||
if (h == INVALID_HANDLE_VALUE) {
|
||||
DWORD error = GetLastError();
|
||||
switch (error) {
|
||||
case ERROR_FILE_NOT_FOUND :
|
||||
errno = ENOENT;
|
||||
break;
|
||||
default:
|
||||
errno = EIO;
|
||||
break;
|
||||
}
|
||||
windows_errno(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!GetFileInformationByHandle(h, &info)) {
|
||||
DWORD error = GetLastError();
|
||||
CloseHandle(h);
|
||||
errno = EIO;
|
||||
windows_errno(error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!CloseHandle(h)) {
|
||||
windows_errno(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
windows_info2stat(&info, st);
|
||||
|
||||
CloseHandle(h);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like the C ftruncate().
|
||||
*/
|
||||
static inline int windows_ftruncate(int fd, off64_t off)
|
||||
{
|
||||
HANDLE h;
|
||||
|
|
@ -248,37 +334,21 @@ static inline int windows_ftruncate(int fd, off64_t off)
|
|||
|
||||
pos.QuadPart = off;
|
||||
if (!SetFilePointerEx(h, pos, 0, FILE_BEGIN)) {
|
||||
DWORD error = GetLastError();
|
||||
switch (error) {
|
||||
case ERROR_INVALID_HANDLE :
|
||||
errno = EBADF;
|
||||
break;
|
||||
default:
|
||||
errno = EIO;
|
||||
break;
|
||||
}
|
||||
windows_errno(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!SetEndOfFile(h)) {
|
||||
DWORD error = GetLastError();
|
||||
switch (error) {
|
||||
case ERROR_INVALID_HANDLE :
|
||||
errno = EBADF;
|
||||
break;
|
||||
case ERROR_ACCESS_DENIED :
|
||||
errno = EACCES;
|
||||
break;
|
||||
default:
|
||||
errno = EIO;
|
||||
break;
|
||||
}
|
||||
windows_errno(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like the C rename().
|
||||
*/
|
||||
static inline int windows_rename(const char* a, const char* b)
|
||||
{
|
||||
/*
|
||||
|
|
@ -289,17 +359,7 @@ static inline int windows_rename(const char* a, const char* b)
|
|||
* http://stackoverflow.com/questions/167414/is-an-atomic-file-rename-with-overwrite-possible-on-windows
|
||||
*/
|
||||
if (!MoveFileEx(a, b, MOVEFILE_REPLACE_EXISTING)) {
|
||||
switch (GetLastError()) {
|
||||
case ERROR_ACCESS_DENIED :
|
||||
errno = EACCES;
|
||||
break;
|
||||
case ERROR_FILE_NOT_FOUND :
|
||||
errno = ENOENT;
|
||||
break;
|
||||
default :
|
||||
errno = EIO;
|
||||
break;
|
||||
}
|
||||
windows_errno(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
|||
58
scan.c
58
scan.c
|
|
@ -192,47 +192,72 @@ static void scan_file(struct snapraid_scan* scan, struct snapraid_state* state,
|
|||
static void scan_dir(struct snapraid_scan* scan, struct snapraid_state* state, struct snapraid_disk* disk, const char* dir, const char* sub)
|
||||
{
|
||||
DIR* d;
|
||||
struct dirent* dd;
|
||||
|
||||
d = opendir(dir);
|
||||
if (!d) {
|
||||
fprintf(stderr, "Error accessing directory '%s'. %s.\n", dir, strerror(errno));
|
||||
fprintf(stderr, "Error opening directory '%s'. %s.\n", dir, strerror(errno));
|
||||
fprintf(stderr, "You can exclude it in the config file with:\n\texclude /%s\n", sub);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while ((dd = readdir(d)) != 0) {
|
||||
|
||||
while (1) {
|
||||
char path_next[PATH_MAX];
|
||||
char sub_next[PATH_MAX];
|
||||
struct stat st;
|
||||
const char* name = dd->d_name;
|
||||
const char* name;
|
||||
struct dirent* dd;
|
||||
|
||||
/* clear errno to detect errneous conditions */
|
||||
errno = 0;
|
||||
dd = readdir(d);
|
||||
if (dd == 0 && errno != 0) {
|
||||
fprintf(stderr, "Error reading directory '%s'. %s.\n", dir, strerror(errno));
|
||||
fprintf(stderr, "You can exclude it in the config file with:\n\texclude /%s\n", sub);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (dd == 0 && errno == 0) {
|
||||
break; /* finished */
|
||||
}
|
||||
|
||||
/* skip "." and ".." files */
|
||||
name = dd->d_name;
|
||||
if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
|
||||
continue;
|
||||
|
||||
/* check for not supported file names, limitation derived from the content file format */
|
||||
if (name[0] == 0 || isspace(name[0]) || isspace(name[strlen(name)-1])) {
|
||||
fprintf(stderr, "Unsupported name '%s' in file '%s'.\n", name, path_next);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
pathprint(path_next, sizeof(path_next), "%s%s", dir, name);
|
||||
pathprint(sub_next, sizeof(sub_next), "%s%s", sub, name);
|
||||
|
||||
/* get info about the file */
|
||||
if (stat(path_next, &st) != 0) {
|
||||
fprintf(stderr, "Error in stat file/directory '%s'\n", path_next);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* check for not supported file names, mainly derived from the content file format */
|
||||
if (name[0] == 0 || isspace(name[0]) || isspace(name[strlen(name)-1])) {
|
||||
fprintf(stderr, "Unsupported name '%s' in file '%s'\n", name, path_next);
|
||||
fprintf(stderr, "Error in stat file/directory '%s'. %s.\n", path_next, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (S_ISREG(st.st_mode)) {
|
||||
if (filter_path(&state->filterlist, sub_next, 0) == 0) {
|
||||
/* check for having read permission */
|
||||
|
||||
/* check for read permission */
|
||||
if (access(path_next, R_OK) != 0) {
|
||||
fprintf(stderr, "You do not have read permissions for file '%s'\n", path_next);
|
||||
fprintf(stderr, "You do not have read permissions for file '%s'.\n", path_next);
|
||||
fprintf(stderr, "Fix the permissions or exclude it in the configuration with:\n\texclude /%s\n", sub_next);
|
||||
fprintf(stderr, "or exclude the whole directory with:\n\texclude /%s\n", sub);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
#if HAVE_STAT_INODE
|
||||
/* get inode info about the file, Windows needs an additional step */
|
||||
if (stat_inode(path_next, &st) != 0) {
|
||||
fprintf(stderr, "Error in stat_inode file '%s'. %s.\n", path_next, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
#endif
|
||||
|
||||
scan_file(scan, state, disk, sub_next, &st);
|
||||
} else {
|
||||
if (state->verbose) {
|
||||
|
|
@ -256,7 +281,10 @@ static void scan_dir(struct snapraid_scan* scan, struct snapraid_state* state, s
|
|||
}
|
||||
}
|
||||
|
||||
closedir(d);
|
||||
if (closedir(d) != 0) {
|
||||
fprintf(stderr, "Error closing directory '%s'. %s.\n", dir, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void state_scan(struct snapraid_state* state)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue