trunk 20080912
[gitenigma.git] / include / bootmenue / bmimage.h
bloba3b0982f571a79cd458dbcb55940d159aea640c2
1 /*
2 * $Id: bmimage.h,v 1.10 2005/11/17 14:01:26 sat_turner Exp $
4 * (C) 2005 by digi_casi <digi_casi@tuxbox.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string>
28 #include <vector>
29 #include <dirent.h>
30 #include <iomanip>
31 #include <iostream>
32 #include <fstream>
33 #include <lib/base/estring.h>
34 #ifdef INSTIMAGESUPPORT
35 #include <src/enigma.h>
36 #define TUXBOXDATADIR "/share/tuxbox"
37 #include <src/enigma_dyn_utils.h>
38 #endif
40 using namespace std;
42 class eImage
44 public:
45 eString name;
46 eString location;
49 class bmimages: public Object
51 public:
52 std::vector<eImage> imageList;
53 std::vector<eImage>::iterator it;
55 bmimages() {};
56 ~bmimages()
58 imageList.clear();
61 void setImageName(eString imageDir, eString imageName)
63 if (FILE *f = fopen(eString(imageDir + "/imagename").c_str(), "w"))
65 fprintf(f, "%s", imageName.c_str());
66 fclose(f);
70 eString getImageName(eString imageDir)
72 unsigned int pos = imageDir.find_last_of("/");
73 eString imageName = imageDir.right(imageDir.length() - pos -1);
74 ifstream nameFile(eString(imageDir + "/imagename").c_str());
75 if (nameFile)
77 eString line;
78 getline(nameFile, line, '\n');
79 nameFile.close();
80 if (line)
81 imageName = line;
83 return imageName;
86 int load(eString mpoint, bool clearList)
88 struct stat s;
89 eImage image;
91 eString dir[2] = {mpoint + "/image/", mpoint + "/fwpro/"};
93 if (clearList)
94 imageList.clear();
96 image.name = "Flash-Image";
97 image.location = "";
98 imageList.push_back(image);
100 for (int i = 0; i < 2; i++)
102 DIR *d = opendir(dir[i].c_str());
103 if (d)
105 while (struct dirent *e = readdir(d))
107 if (strcmp(e->d_name, ".") && strcmp(e->d_name, ".."))
109 eString name = dir[i] + eString(e->d_name);
110 stat(name.c_str(), &s);
111 if (S_ISDIR(s.st_mode))
113 image.location = name;
114 image.name = getImageName(name);
115 imageList.push_back(image);
120 closedir(d);
122 return imageList.size();
125 void discard(eString location)
127 system(eString("rm -rf \"" + location + "\"").c_str());
130 void rename(eString from, eString to)
132 eString name = to.right(to.length() - to.find_last_of("/") - 1);
133 setImageName(to, name);
136 #ifdef INSTIMAGESUPPORT
137 int add(eString sourceImage, eString imageName, eString mountDir)
139 int freeSpace = 0;
140 eString imageDir = mountDir + "/fwpro/" + imageName;
142 eDebug("[BOOTMANAGER] unpackImage: installation device = %s, image file = %s, image dir = %s", mountDir.c_str(), sourceImage.c_str(), imageDir.c_str());
144 // check if enough space is available
145 struct statfs s;
146 if (statfs(mountDir.c_str(), &s) >= 0)
147 freeSpace = (s.f_bavail * (s.f_bsize / 1024));
148 eDebug("[BOOTMANAGER] unpackImage: free space on device = %d", freeSpace);
149 if (freeSpace < 20000)
150 return -1;
152 if (access(mountDir.c_str(), W_OK) != 0)
153 return -2;
155 // check if directory is available, delete it and recreate it
156 if (access(imageDir.c_str(), W_OK) == 0)
157 system(eString("rm -rf " + imageDir).c_str());
158 system(eString("mkdir " + imageDir + " --mode=777 --parents").c_str());
159 if (access(imageDir.c_str(), W_OK) != 0)
160 return -3;
162 // split image file
163 eString squashfsPart = mountDir + "/squashfs.img";
164 remove(squashfsPart.c_str());
165 if (system(eString("dd if=" + sourceImage + " of=" + squashfsPart + " bs=1024 skip=1152 count=4992").c_str()) >> 8)
166 return -4;
167 eString cramfsPart = mountDir + "/cramfs.img";
168 remove(cramfsPart.c_str());
169 if (system(eString("dd if=" + sourceImage + " of=" + cramfsPart + " bs=1024 skip=0 count=1152").c_str()) >> 8)
170 return -5;
171 remove(sourceImage.c_str());
173 system(eString("cp " + eString(TEMPLATE_DIR) + "instimg.tmp /tmp/instimg.sh").c_str());
174 system("chmod +x /tmp/instimg.sh");
175 system("touch /var/etc/.dont_restart_enigma");
176 system(eString("/tmp/instimg.sh " + imageDir + " " + cramfsPart + " " + squashfsPart + " &").c_str());
177 eZap::getInstance()->quit(2);
179 return 0;
181 #endif