1 /*
2 * Copyright (c) 2014 Dave Vasilevsky <dave@vasilevsky.ca>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 #include "util.h"
26
27 #include "fs.h"
28
29 #include <stdio.h>
30
31 #ifdef _WIN32
32 #include <win32.h>
33
sqfs_fd_open(const char * path,sqfs_fd_t * fd,bool print)34 sqfs_err sqfs_fd_open(const char *path, sqfs_fd_t *fd, bool print) {
35 *fd = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
36 if (*fd != INVALID_HANDLE_VALUE)
37 return SQFS_OK;
38
39 // FIXME: Better error handling
40 if (print)
41 fprintf(stderr, "CreateFile error: %d\n", GetLastError());
42 return SQFS_ERR;
43 }
44
sqfs_fd_close(sqfs_fd_t fd)45 void sqfs_fd_close(sqfs_fd_t fd) {
46 CloseHandle(fd);
47 }
48 #else
49 #include <fcntl.h>
50 #include <unistd.h>
51
sqfs_fd_open(const char * path,sqfs_fd_t * fd,bool print)52 sqfs_err sqfs_fd_open(const char *path, sqfs_fd_t *fd, bool print) {
53 *fd = open(path, O_RDONLY);
54 if (*fd != -1)
55 return SQFS_OK;
56
57 if (print)
58 perror("Can't open squashfs image");
59 return SQFS_ERR;
60 }
61
sqfs_fd_close(sqfs_fd_t fd)62 void sqfs_fd_close(sqfs_fd_t fd) {
63 close(fd);
64 }
65 #endif
66
67
68 /* TODO: WIN32 implementation of open/close */
69 /* TODO: i18n of error messages */
sqfs_open_image(sqfs * fs,const char * image,size_t offset)70 sqfs_err sqfs_open_image(sqfs *fs, const char *image, size_t offset) {
71 sqfs_err err;
72 sqfs_fd_t fd;
73
74 if ((err = sqfs_fd_open(image, &fd, stderr)))
75 return err;
76
77 err = sqfs_init(fs, fd, offset);
78 switch (err) {
79 case SQFS_OK:
80 break;
81 case SQFS_BADFORMAT:
82 fprintf(stderr, "This doesn't look like a squashfs image.\n");
83 break;
84 case SQFS_BADVERSION: {
85 int major, minor, mj1, mn1, mj2, mn2;
86 sqfs_version(fs, &major, &minor);
87 sqfs_version_supported(&mj1, &mn1, &mj2, &mn2);
88 fprintf(stderr, "Squashfs version %d.%d detected, only version",
89 major, minor);
90 if (mj1 == mj2 && mn1 == mn2)
91 fprintf(stderr, " %d.%d", mj1, mn1);
92 else
93 fprintf(stderr, "s %d.%d to %d.%d", mj1, mn1, mj2, mn2);
94 fprintf(stderr, " supported.\n");
95 break;
96 }
97 case SQFS_BADCOMP: {
98 bool first = true;
99 int i;
100 sqfs_compression_type sup[SQFS_COMP_MAX],
101 comp = sqfs_compression(fs);
102 sqfs_compression_supported(sup);
103 fprintf(stderr, "Squashfs image uses %s compression, this version "
104 "supports only ", sqfs_compression_name(comp));
105 for (i = 0; i < SQFS_COMP_MAX; ++i) {
106 if (sup[i] == SQFS_COMP_UNKNOWN)
107 continue;
108 if (!first)
109 fprintf(stderr, ", ");
110 fprintf(stderr, "%s", sqfs_compression_name(sup[i]));
111 first = false;
112 }
113 fprintf(stderr, ".\n");
114 break;
115 }
116 default:
117 fprintf(stderr, "Something went wrong trying to read the squashfs "
118 "image.\n");
119 }
120
121 if (err)
122 sqfs_fd_close(fd);
123 return err;
124 }
125
126