Uses the NOFOLLOW option in open() to ensure to do not follow symbolic links.

This commit is contained in:
Andrea Mazzoleni 2011-08-25 18:41:27 +02:00
parent 322110672e
commit b299f9d368
3 changed files with 15 additions and 4 deletions

View file

@ -54,7 +54,7 @@ int handle_close_if_different(struct snapraid_handle* handle, struct snapraid_fi
return 0; return 0;
} }
static int handle_ancestor(const char* file) int handle_ancestor(const char* file)
{ {
char dir[PATH_MAX]; char dir[PATH_MAX];
char* c; char* c;
@ -111,12 +111,13 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file)
} }
/* opening in sequential mode in Windows */ /* opening in sequential mode in Windows */
handle->f = open(handle->path, O_RDWR | O_CREAT | O_BINARY | O_SEQUENTIAL, 0600); /* do not follow links to ensure to open the real file */
handle->f = open(handle->path, O_RDWR | O_CREAT | O_BINARY | O_SEQUENTIAL | O_NOFOLLOW, 0600);
/* if failed for missing permission */ /* if failed for missing permission */
if (handle->f == -1 && errno == EACCES) { if (handle->f == -1 && errno == EACCES) {
/* retry without requesting write permission */ /* retry without requesting write permission */
handle->f = open(handle->path, O_RDONLY | O_BINARY | O_SEQUENTIAL); handle->f = open(handle->path, O_RDONLY | O_BINARY | O_SEQUENTIAL | O_NOFOLLOW);
} }
if (handle->f == -1) { if (handle->f == -1) {
@ -195,8 +196,9 @@ int handle_open(struct snapraid_handle* handle, struct snapraid_file* file)
} }
/* opening in sequential mode in Windows */ /* opening in sequential mode in Windows */
/* do not follow links to ensure to open the real file */
pathprint(handle->path, sizeof(handle->path), "%s%s", handle->disk->dir, file->sub); pathprint(handle->path, sizeof(handle->path), "%s%s", handle->disk->dir, file->sub);
handle->f = open(handle->path, O_RDONLY | O_BINARY | O_SEQUENTIAL); handle->f = open(handle->path, O_RDONLY | O_BINARY | O_SEQUENTIAL | O_NOFOLLOW);
if (handle->f == -1) { if (handle->f == -1) {
/* invalidate for error */ /* invalidate for error */

View file

@ -29,6 +29,11 @@ struct snapraid_handle {
struct stat st; /**< Stat info of the opened file. */ struct stat st; /**< Stat info of the opened file. */
}; };
/**
* Creates all the required directories if missing.
*/
int handle_ancestor(const char* file);
/** /**
* Closes a file, if it's different from the specified one. * Closes a file, if it's different from the specified one.
*/ */

View file

@ -48,6 +48,10 @@
#define opendir windows_opendir #define opendir windows_opendir
#define readdir windows_readdir #define readdir windows_readdir
#define closedir windows_closedir #define closedir windows_closedir
#define O_NOFOLLOW 0
#define S_ISLNK(mode) 0
#define readlink(a,b,c) -1
#define symlink(a,b) -1
/** /**
* Generic stat information. * Generic stat information.