librazor/uri-io.c
changeset 488 7c6d932f291f
parent 485 5e309e37906e
child 489 9d0a04089d22
     1.1 --- a/librazor/uri-io.c	Fri Jul 08 17:52:02 2016 +0100
     1.2 +++ b/librazor/uri-io.c	Mon Jul 11 13:54:54 2016 +0100
     1.3 @@ -56,7 +56,7 @@
     1.4  				 (s1) ? 1 : (s2) ? -1 : 0)
     1.5  
     1.6  static void *
     1.7 -razor_archive_get_file_contents(const char *filename, int fd, const char *path,
     1.8 +razor_archive_get_file_contents(const char *filename, const char *path,
     1.9  				size_t *length, struct razor_error **error)
    1.10  {
    1.11  #if HAVE_LIBARCHIVE
    1.12 @@ -73,7 +73,7 @@
    1.13  	archive_read_support_compression_all(a);
    1.14  	archive_read_support_format_all(a);
    1.15  
    1.16 -	r = archive_read_open_fd(a, fd, 10240);
    1.17 +	r = archive_read_open_filename(a, filename, 10240);
    1.18  
    1.19  	if (r) {
    1.20  		errmsg = archive_error_string(a);
    1.21 @@ -161,7 +161,7 @@
    1.22  razor_file_get_contents_archive(const char *filename, size_t *length,
    1.23  				struct razor_error **error)
    1.24  {
    1.25 -	int fd;
    1.26 +	int is_dir;
    1.27  	char *path, *slash, *s;
    1.28  	void *addr;
    1.29  	struct razor_error *tmp_error = NULL;
    1.30 @@ -171,37 +171,23 @@
    1.31  
    1.32  	while (slash) {
    1.33  		*slash = '\0';
    1.34 -		fd = open(path, O_RDONLY | O_BINARY);
    1.35 -		if (fd >= 0) {
    1.36 -			addr = razor_archive_get_file_contents(path, fd,
    1.37 -							       slash + 1,
    1.38 -							       length,
    1.39 -							       error);
    1.40 +		is_dir = razor_file_is_directory(path, NULL);
    1.41 +		if (is_dir < 0) {
    1.42 +			s = strrchr(path, '/');
    1.43 +			*slash = '/';
    1.44 +			slash = s;
    1.45 +		} else if (!is_dir) {
    1.46 +			addr = razor_archive_get_file_contents(path, slash + 1,
    1.47 +							       length, error);
    1.48  			free(path);
    1.49 -			close(fd);
    1.50  			return addr;
    1.51 -#ifdef MSWIN_API
    1.52 -		} else if (errno != ENOTDIR && errno != ENOENT) {
    1.53 -#else
    1.54 -		} else if (errno != ENOTDIR) {
    1.55 -#endif
    1.56 -			free(path);
    1.57 -			razor_set_error_posix(error, filename);
    1.58 -			return NULL;
    1.59 -		}
    1.60 -		s = strrchr(path, '/');
    1.61 -		*slash = '/';
    1.62 -		slash = s;
    1.63 +		} else
    1.64 +			break;
    1.65  	}
    1.66  
    1.67  	free(path);
    1.68 -#ifdef MSWIN_API
    1.69 -	razor_set_error(error, RAZOR_POSIX_ERROR, ENOENT, filename,
    1.70 -			strerror(ENOENT));
    1.71 -#else
    1.72  	razor_set_error(error, RAZOR_POSIX_ERROR, ENOTDIR, filename,
    1.73  			strerror(ENOTDIR));
    1.74 -#endif
    1.75  	return NULL;
    1.76  }
    1.77  
    1.78 @@ -219,7 +205,7 @@
    1.79  void *razor_file_get_contents(const char *filename, size_t *length, int private,
    1.80  			      struct razor_error **error)
    1.81  {
    1.82 -	int fd;
    1.83 +	int fd, saved_errno;
    1.84  	struct stat st;
    1.85  	void *addr = NULL;
    1.86  	size_t nb, size;
    1.87 @@ -228,17 +214,28 @@
    1.88  
    1.89  	fd = open(filename, O_RDONLY | O_BINARY);
    1.90  	if (fd < 0) {
    1.91 +		saved_errno = errno;
    1.92  #ifdef MSWIN_API
    1.93 -		if (errno != ENOTDIR && errno != ENOENT) {
    1.94 +		if (saved_errno != ENOTDIR && saved_errno != ENOENT) {
    1.95  #else
    1.96 -		if (errno != ENOTDIR) {
    1.97 +		if (saved_errno != ENOTDIR) {
    1.98  #endif
    1.99  			razor_set_error_posix(error, filename);
   1.100  			return NULL;
   1.101  		}
   1.102  		addr = razor_file_get_contents_archive(filename, &size, error);
   1.103 -		if (!addr)
   1.104 +		if (!addr) {
   1.105 +			if (error && saved_errno != ENOTDIR &&
   1.106 +			    razor_error_matches(error, RAZOR_POSIX_ERROR,
   1.107 +						ENOTDIR)) {
   1.108 +				razor_error_free(*error);
   1.109 +				*error = NULL;
   1.110 +				razor_set_error(error, RAZOR_POSIX_ERROR,
   1.111 +						saved_errno, filename,
   1.112 +						strerror(saved_errno));
   1.113 +			}
   1.114  			return NULL;
   1.115 +		}
   1.116  	} else {
   1.117  		if (fstat(fd, &st) < 0) {
   1.118  			razor_set_error_posix(error, filename);