Update .travis.yml
[appimagekit/gsi.git] / fuseiso.c
blob3616f34514a34cb86f3e11bc70fdbdf04cb80bb1
1 /***************************************************************************
2 * Copyright (c) 2005, 2006 by Dmitry Morozhnikov <dmiceman@mail.ru > *
3 * Copyright (c) 2005-10 Simon Peter *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <linux/stat.h>
31 #include <mntent.h>
32 #include <sys/param.h>
33 #include <linux/iso_fs.h>
35 #define FUSE_USE_VERSION 22
36 #include <fuse.h>
37 #include "isofs.h"
39 #ifdef __GNUC__
40 # define UNUSED(x) x __attribute__((unused))
41 #else
42 # define UNUSED(x) x
43 #endif
45 static char *imagefile = NULL;
46 static char *mount_point = NULL;
47 static int image_fd = -1;
49 int maintain_mount_point = 1;
51 char* iocharset;
53 char* normalize_name(const char* fname) {
54 char* abs_fname = (char *) malloc(PATH_MAX);
55 realpath(fname, abs_fname);
56 // ignore errors from realpath()
57 return abs_fname;
60 int check_mount_point() {
61 struct stat st;
62 int rc = lstat(mount_point, &st);
63 if(rc == -1 && errno == ENOENT) {
64 // directory does not exists, createcontext
65 rc = mkdir(mount_point, 0777); // let`s underlying filesystem manage permissions
66 if(rc != 0) {
67 perror("Can't create mount point");
68 return -EIO;
70 } else if(rc == -1) {
71 perror("Can't check mount point");
72 return -1;
74 return 0;
77 void del_mount_point() {
78 int rc = rmdir(mount_point);
79 if(rc != 0) {
80 perror("Can't delete mount point");
84 static int isofs_getattr(const char *path, struct stat *stbuf)
86 return isofs_real_getattr(path, stbuf);
89 static int isofs_readlink(const char *path, char *target, size_t size) {
90 return isofs_real_readlink(path, target, size);
93 static int isofs_open(const char *path, struct fuse_file_info *UNUSED(fi))
95 return isofs_real_open(path);
98 static int isofs_read(const char *path, char *buf, size_t size,
99 off_t offset, struct fuse_file_info *UNUSED(fi))
101 return isofs_real_read(path, buf, size, offset);
104 static int isofs_flush(const char *UNUSED(path), struct fuse_file_info *UNUSED(fi)) {
105 return 0;
108 static void* isofs_init() {
109 int rc;
110 run_when_fuse_fs_mounted();
111 return isofs_real_init();
114 static void isofs_destroy(void* param) {
115 return;
118 static int isofs_opendir(const char *path, struct fuse_file_info *UNUSED(fi)) {
119 return isofs_real_opendir(path);
122 static int isofs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t UNUSED(offset),
123 struct fuse_file_info *UNUSED(fi)) {
124 return isofs_real_readdir(path, buf, filler);
127 static int isofs_statfs(const char *UNUSED(path), struct statfs *stbuf)
129 return isofs_real_statfs(stbuf);
132 static struct fuse_operations isofs_oper = {
133 .getattr = isofs_getattr,
134 .readlink = isofs_readlink,
135 .open = isofs_open,
136 .read = isofs_read,
137 .flush = isofs_flush,
138 .init = isofs_init,
139 .destroy = isofs_destroy,
140 .opendir = isofs_opendir,
141 .readdir = isofs_readdir,
142 .statfs = isofs_statfs,
145 int ext2_main(int argc, char *argv[])
147 imagefile = normalize_name(argv[0]);
148 image_fd = open(imagefile, O_RDONLY);
149 if(image_fd == -1) {
150 perror("Can't open image file");
151 fprintf(stderr, "Supplied image file name: \"%s\"\n", imagefile);
152 exit(EXIT_FAILURE);
155 mount_point = normalize_name(argv[1]);
157 if(!iocharset) {
158 iocharset = "UTF-8//IGNORE";
161 int rc;
162 if(maintain_mount_point) {
163 rc = check_mount_point();
164 if(rc != 0) {
165 exit(EXIT_FAILURE);
168 if(maintain_mount_point) {
169 rc = atexit(del_mount_point);
170 if(rc != 0) {
171 fprintf(stderr, "Can't set exit function\n");
172 exit(EXIT_FAILURE);
176 // will exit in case of failure
177 rc = isofs_real_preinit(imagefile, image_fd);
179 return fuse_main(argc, argv, &isofs_oper);