Merge lp:~humpolec-team/humpolec/UbuntuInstaller-check-storage-size into lp:humpolec

Proposed by Yuan-Chen Cheng
Status: Merged
Merged at revision: 15
Proposed branch: lp:~humpolec-team/humpolec/UbuntuInstaller-check-storage-size
Merge into: lp:humpolec
Diff against target: 172 lines (+78/-14)
2 files modified
src/com/canonical/ubuntu/installer/UbuntuInstallService.java (+66/-14)
src/com/canonical/ubuntu/installer/Utils.java (+12/-0)
To merge this branch: bzr merge lp:~humpolec-team/humpolec/UbuntuInstaller-check-storage-size
Reviewer Review Type Date Requested Status
Rex Tsai Approve
Yuan-Chen Cheng (community) Needs Resubmitting
Review via email: mp+199079@code.launchpad.net

Description of the change

check storage size before download

To post a comment you must log in.
16. By Yuan-Chen Cheng

revert format change by ecipse

Revision history for this message
Rex Tsai (chihchun) wrote :

* UbuntuInstallService.java line 11: s/save/safe/
* line 119: s/eought/enough/
* long getAvailableBlocksLong is available at API 18, our minimum support is API 17. If we are move to API 18, maybe we should use getAvailableBytes ()[1].

[1] http://developer.android.com/reference/android/os/StatFs.html#getAvailableBytes%28%29

review: Needs Fixing
17. By Yuan-Chen Cheng

typo fix and use api 17

Revision history for this message
Yuan-Chen Cheng (ycheng-twn) wrote :

fix accordingly

review: Needs Resubmitting
Revision history for this message
Rex Tsai (chihchun) wrote :

Looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/com/canonical/ubuntu/installer/UbuntuInstallService.java'
2--- src/com/canonical/ubuntu/installer/UbuntuInstallService.java 2013-12-13 16:57:41 +0000
3+++ src/com/canonical/ubuntu/installer/UbuntuInstallService.java 2013-12-16 09:10:46 +0000
4@@ -108,6 +108,12 @@
5 private static final String URL_IMAGE_MASTER = "gpg/image-master.tar.xz";
6 private static final String URL_IMAGE_SIGNING = "gpg/image-signing.tar.xz";
7 private static final String ASC_SUFFIX = ".asc";
8+
9+ // 2G for file system, 512M for swap.
10+ private static long INSTALL_SIZE_REQUIRED = (2048L + 512L) * 1024L * 1024L;
11+ // 15M extra space to keep it safe.
12+ private static long EXTRA_SIZE_REQUIRED = 15 * 1024 * 1024;
13+
14
15 // =================================================================================================
16 // Packed assets
17@@ -144,6 +150,7 @@
18 private static final int PROGRESS_MKSWAP_ADJUSTMENT = 17; // equivalent of time tar --checkpoint=200
19 private PowerManager mPowerManager;
20 private PowerManager.WakeLock mWakeLock;
21+ private boolean workPathInCache = false;
22 private String mRootOfWorkPath;
23 private boolean mIsCanceled;
24
25@@ -172,8 +179,10 @@
26 if (testDir.mkdir()) {
27 testDir.delete();
28 mRootOfWorkPath = "/cache";
29+ workPathInCache = true;
30 } else {
31 mRootOfWorkPath = getFilesDir().toString(); // "/data/data/com.canonical.ubuntuinstaller/files";
32+ workPathInCache = false;
33 }
34 }
35
36@@ -591,23 +600,23 @@
37 // get right version, otherwise first since that is most recent one
38 Image choosenRelease = null;
39 if (version != -1) {
40- // look for given release
41- for (Image i : releases) {
42- if (i.version == version) {
43- choosenRelease = i;
44- break;
45- }
46- }
47- if (choosenRelease == null) {
48+ // look for given release
49+ for (Image i : releases) {
50+ if (i.version == version) {
51+ choosenRelease = i;
52+ break;
53+ }
54+ }
55+ if (choosenRelease == null) {
56 Log.e(TAG, "wrong release vwersion");
57 result.putExtra(DOWNLOAD_RESULT_EXTRA_INT, -1);
58 result.putExtra(DOWNLOAD_RESULT_EXTRA_STR, "wrong release vwersion");
59- return result;
60- }
61+ return result;
62+ }
63 } else {
64- choosenRelease = releases.get(0);
65+ choosenRelease = releases.get(0);
66 }
67- JsonChannelParser.File updateFiles[] = choosenRelease.files;
68+ JsonChannelParser.File updateFiles[] = choosenRelease.files;
69 // sort update files
70 List<JsonChannelParser.File> filesArray = new LinkedList<JsonChannelParser.File>();
71 for(JsonChannelParser.File f: updateFiles) {
72@@ -619,7 +628,7 @@
73 // get list of keyrings to download
74 String keyrings[] = {
75 String.format("%s/%s",BASE_URL, URL_IMAGE_MASTER),
76- String.format("%s/%s",BASE_URL, URL_IMAGE_SIGNING),
77+ String.format("%s/%s",BASE_URL, URL_IMAGE_SIGNING),
78 };
79 String keyringsFilenames[] = new String[keyrings.length * 2];
80
81@@ -640,6 +649,22 @@
82 mProgress = 0;
83 broadcastProgress(mLastSignalledProgress, null);
84 mTotalSize = Utils.calculateDownloadSize(filesArray);
85+
86+ boolean isStorageEnough = isStorageSpaceEnoughtBFDownload(mTotalSize);
87+ if (! isStorageEnough) {
88+ String msg = "Need more storage: ";
89+ if (workPathInCache) {
90+ msg += "/cache need " + String.valueOf(mTotalSize) + " bytes for download and /data need 2.5G for system";
91+ } else {
92+ msg += "/data need 2.5G for system plus " + String.valueOf(mTotalSize) + " bytes for download";
93+ }
94+ Log.i(TAG, msg);
95+
96+ result.putExtra(DOWNLOAD_RESULT_EXTRA_INT, -1);
97+ result.putExtra(DOWNLOAD_RESULT_EXTRA_STR, msg);
98+ return result;
99+ }
100+
101 // mProgressSteps = mTotalDownloadSize / 100; // we want 1% steps
102 try {
103 int i = 0;
104@@ -648,7 +673,7 @@
105 // download signature
106 keyringsFilenames[i++] = doDownloadUrl(new URL(url+ASC_SUFFIX),release);
107 }
108-
109+
110 // download all update images
111 i = 0;
112 for (JsonChannelParser.File file : filesArray){
113@@ -837,4 +862,31 @@
114 }
115 sendBroadcast(i);
116 }
117+
118+ /**
119+ * Check whether storage free space is enough.
120+ * @param downloadSize: download size from json. 0 means file already downloaded.
121+ * @return true if stoarge size is ok to go.
122+ */
123+ private boolean isStorageSpaceEnoughtBFDownload(long downloadSize) {
124+ long dataSizeRequired = INSTALL_SIZE_REQUIRED;
125+
126+ if (workPathInCache) {
127+ if (downloadSize > 0) {
128+ long cacheFreeSpace = Utils.getFreeSpaceInBytes("/cache");
129+ if (cacheFreeSpace < EXTRA_SIZE_REQUIRED + downloadSize) {
130+ return false;
131+ }
132+ }
133+ } else {
134+ dataSizeRequired += downloadSize;
135+ }
136+
137+ long dataFreeSpace = Utils.getFreeSpaceInBytes("/data");
138+ if (dataFreeSpace < EXTRA_SIZE_REQUIRED + dataSizeRequired) {
139+ return false;
140+ }
141+ return true;
142+ }
143+
144 }
145
146=== modified file 'src/com/canonical/ubuntu/installer/Utils.java'
147--- src/com/canonical/ubuntu/installer/Utils.java 2013-12-13 01:06:14 +0000
148+++ src/com/canonical/ubuntu/installer/Utils.java 2013-12-16 09:10:46 +0000
149@@ -21,6 +21,7 @@
150
151 import android.content.Context;
152 import android.content.res.AssetManager;
153+import android.os.StatFs;
154 import android.util.Log;
155 import android.widget.Toast;
156
157@@ -177,4 +178,15 @@
158 }
159 return size;
160 }
161+
162+ @SuppressWarnings("deprecation")
163+ public static long getFreeSpaceInBytes(String fsPath) {
164+ StatFs stats = new StatFs(fsPath);
165+ // not using getAvailableBytes() for it's not available in android 4.2
166+ int availableBlocks = stats.getAvailableBlocks();
167+ int blockSizeInBytes = stats.getBlockSize();
168+ long freeSpaceInBytes = ((long)availableBlocks) * ((long)blockSizeInBytes);
169+ return freeSpaceInBytes;
170+ }
171+
172 }

Subscribers

People subscribed via source and target branches

to all changes: