|
1 /* |
|
2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <algorithm> |
|
19 #include <exception> |
|
20 #include <e32const.h> |
|
21 #include <f32file.h> |
|
22 #include <driveinfo.h> |
|
23 #include <sysutil.h> |
|
24 #include <sysutildomaincrkeys.h> |
|
25 |
|
26 #include "cxesettings.h" |
|
27 #include "cxutils.h" |
|
28 #include "cxesysutil.h" |
|
29 |
|
30 |
|
31 namespace |
|
32 { |
|
33 const uint KDiskSafetyLimit = 400000; // Amount of free disk space to leave unused |
|
34 } |
|
35 |
|
36 /*! |
|
37 * Checks if free disk drive storage space is or will fall below critical |
|
38 * level. Static configuration values stored in Central Repository are |
|
39 * used to determine a critical level for each drive. |
|
40 * |
|
41 * Usage example: |
|
42 * @code |
|
43 * TInt dataSize = 500; |
|
44 * if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFsSession, dataSize, EDriveC ) ) |
|
45 * { |
|
46 * // Can not write the data, there's not enough free space on disk. |
|
47 * ... |
|
48 * } |
|
49 * else |
|
50 * { |
|
51 * // It's ok to actually write the data. |
|
52 * ... |
|
53 * } |
|
54 * @endcode |
|
55 * |
|
56 * @since S60 v2.0 |
|
57 * |
|
58 * @param aFs File server session. Must be given if available, e.g. from |
|
59 * EIKON environment. If NULL, this method will create a |
|
60 * temporary session, which causes the method to consume more |
|
61 * time and system resources. |
|
62 * @param aBytesToWrite Number of bytes the caller is about to write to |
|
63 * disk. If value 0 is given, this method checks |
|
64 * if the current disk space is already below critical |
|
65 * level. |
|
66 * @param aDrive Identifies the disk drive to be checked. Numeric values |
|
67 * for identifying disk drives are defined in TDriveNumber |
|
68 * enumeration. |
|
69 * |
|
70 * @see TDriveNumber in f32file.h. |
|
71 * |
|
72 * @return ETrue if disk space would go below critical level after writing |
|
73 * aBytesToWrite more data, EFalse otherwise. |
|
74 * |
|
75 * @leave Leaves with one of the Symbian error codes if checking the disk |
|
76 * space fails, for instance if the drive contains no media or there |
|
77 * is not enough free memory to create a temporary connection to |
|
78 * file server. |
|
79 */ |
|
80 bool CxeSysUtil::DiskSpaceBelowCriticalLevel( |
|
81 RFs* aFs, |
|
82 TInt aBytesToWrite, |
|
83 TInt aDrive ) |
|
84 { |
|
85 bool fullDisk = false; |
|
86 TRAPD( utilErr, |
|
87 fullDisk = SysUtil::DiskSpaceBelowCriticalLevelL( |
|
88 aFs, aBytesToWrite, aDrive ) ); |
|
89 if(utilErr) { |
|
90 fullDisk = false; |
|
91 } |
|
92 return fullDisk; |
|
93 } |
|
94 |
|
95 /*! |
|
96 * Get the drive index to use for camera. |
|
97 * Normally it is mass memory, but if that is not available in the device at all, |
|
98 * using phone memory. It should be noted, that if mass memory is temporarily |
|
99 * unavailable, storage will not be changed to phone memory, but space available |
|
100 * will report zero. |
|
101 */ |
|
102 int CxeSysUtil::getCameraDrive(RFs &fs) |
|
103 { |
|
104 CX_DEBUG_ENTER_FUNCTION(); |
|
105 int index(-1); |
|
106 try { |
|
107 // Get drive index for mass storage |
|
108 int status(DriveInfo::GetDefaultDrive(DriveInfo::EDefaultMassStorage, index)); |
|
109 CX_DEBUG(("status getting mass memory drive index: %d", status)); |
|
110 qt_symbian_throwIfError(status); |
|
111 |
|
112 // Get the drive info |
|
113 TDriveInfo driveInfo; |
|
114 status = fs.Drive(driveInfo, index); |
|
115 CX_DEBUG(("status getting mass memory drive info: %d", status)); |
|
116 qt_symbian_throwIfError(status); |
|
117 |
|
118 // If and only if mass memory drive does not exist at all, |
|
119 // we resort to phone internal memory (c-drive). |
|
120 // E.g. dismounted mass memory because of USB connection is intentionally |
|
121 // *not* causing us to switch to phone memory. |
|
122 if (driveInfo.iDriveAtt == KDriveAbsent) { |
|
123 CX_DEBUG(("[WARNING]: Mass memory not found at all, resorting to phone memory!")); |
|
124 status = DriveInfo::GetDefaultDrive(DriveInfo::EDefaultPhoneMemory, index); |
|
125 CX_DEBUG(("status getting phone memory drive index: %d", status)); |
|
126 } |
|
127 qt_symbian_throwIfError(status); |
|
128 } catch(const std::exception& e) { |
|
129 Q_UNUSED(e); // avoid warning if trace is not used. |
|
130 CX_DEBUG(("[WARNING]: exception [%s]", e.what())); |
|
131 } |
|
132 |
|
133 CX_DEBUG_EXIT_FUNCTION(); |
|
134 return index; |
|
135 } |
|
136 |
|
137 /*! |
|
138 * Return space available for Camera to use. |
|
139 * @param fs File Server session |
|
140 * @param index Index for the drive to be checked. |
|
141 * @param settings Camera settings instance |
|
142 * @return Amount of bytes free for Camera usage. |
|
143 */ |
|
144 qint64 CxeSysUtil::spaceAvailable(RFs &fs, int index, CxeSettings &settings) |
|
145 { |
|
146 CX_DEBUG_ENTER_FUNCTION(); |
|
147 |
|
148 qint64 freeSpace(0); |
|
149 |
|
150 try { |
|
151 // Get volume info |
|
152 TVolumeInfo volumeInfo; |
|
153 int status(fs.Volume(volumeInfo, index)); |
|
154 CX_DEBUG(("status getting volume info: %d", status)); |
|
155 qt_symbian_throwIfError(status); |
|
156 |
|
157 // Get critical treshold for free space on the drive. |
|
158 // We must leave the free space above this. |
|
159 QVariant criticalThreshold; |
|
160 settings.get(KCRUidDiskLevel.iUid, KDiskCriticalThreshold, Cxe::Repository, criticalThreshold); |
|
161 |
|
162 // Calculate space that we can still use. A small safety buffer is used above critical value. |
|
163 freeSpace = std::max(qint64(0), volumeInfo.iFree - criticalThreshold.toInt() - KDiskSafetyLimit); |
|
164 |
|
165 } catch(const std::exception& e) { |
|
166 Q_UNUSED(e); |
|
167 CX_DEBUG(("[WARNING]: exception [%s]", e.what())); |
|
168 freeSpace = 0; |
|
169 } |
|
170 |
|
171 CX_DEBUG(("CxeSysUtil - free space: %d", freeSpace)); |
|
172 CX_DEBUG_EXIT_FUNCTION(); |
|
173 return freeSpace; |
|
174 } |
|
175 |
|
176 // end of file |