@@ -115,59 +115,76 @@ FS EXT2::getFilesystemSupport()
115115
116116void EXT2::setUsedSectors (Partition &partition)
117117{
118- QString output, error, strmatch, strcmd;
119- m_blocksSize = m_numOfFreeOrUsedBlocks = m_totalNumOfBlock = -1 ;
118+ QString output, error, strcmd;
120119 strcmd = QString (" dumpe2fs -h %1" ).arg (partition.getPath ());
120+
121121 if (!Utils::executCmd (strcmd, output, error)) {
122- strmatch = (" Block count:" );
122+ long long blockCount = -1 ;
123+ long long blockSize = -1 ;
124+ long long freeBlocks = -1 ;
125+ long long reservedBlocks = 0 ;
126+
127+ // 获取总块数
128+ QString strmatch = " Block count:" ;
123129 int index = output.indexOf (strmatch);
124130 if (index >= 0 && index < output.length ()) {
125- m_totalNumOfBlock = Utils::regexpLabel (output, QString (" (?<=Block count:).*(?=\n )" )).trimmed ().toLong ();
131+ blockCount = Utils::regexpLabel (output, QString (" (?<=Block count:).*(?=\n )" )).trimmed ().toLong ();
126132 }
127- // qDebug() << output;
128- // qDebug() << output.mid(index, strmatch.length()).toLatin1() << m_totalNumOfBlock;
129- strmatch = ( " Block size:" ) ;
133+
134+ // 获取块大小
135+ strmatch = " Block size:" ;
130136 index = output.indexOf (strmatch);
131137 if (index >= 0 && index < output.length ()) {
132- m_blocksSize = Utils::regexpLabel (output, QString (" (?<=Block size:).*(?=\n )" )).trimmed ().toLong ();
138+ blockSize = Utils::regexpLabel (output, QString (" (?<=Block size:).*(?=\n )" )).trimmed ().toLong ();
133139 }
134- // qDebug() << output << output.mid(index, strmatch.length()).toLatin1() << m_blocksSize;
135-
136- if (partition.m_busy ) {
137- Byte_Value fs_all;
138- Byte_Value fs_free;
139- if (Utils::getMountedFileSystemUsage (partition.getMountPoint (), fs_all, fs_free) == 0 ) {
140- partition.setSectorUsage (qRound64 (fs_all / double (partition.m_sectorSize )), qRound64 (fs_free / double (partition.m_sectorSize )));
141- partition.m_fsBlockSize = m_blocksSize;
142- }
143- } else {
144- // Resize2fs won't shrink a file system smaller than it's own
145- // estimated minimum size, so use that to derive the free space.
146- m_numOfFreeOrUsedBlocks = -1 ;
147- if (!Utils::executCmd (QString (" resize2fs -P %1" ).arg (partition.getPath ()), output, error)) {
148- if (sscanf (output.toLatin1 (), " Estimated minimum size of the filesystem: %lld" , &m_numOfFreeOrUsedBlocks) == 1
149- || sscanf (output.toStdString ().c_str (), " 预计文件系统的最小尺寸:%lld" , &m_numOfFreeOrUsedBlocks) == 1 )
150- m_numOfFreeOrUsedBlocks = m_totalNumOfBlock - m_numOfFreeOrUsedBlocks;
151- }
152- // Resize2fs can fail reporting please run fsck first. Fall back
153- // to reading dumpe2fs output for free space.
154- if (m_numOfFreeOrUsedBlocks == -1 ) {
155- strmatch = " Free blocks:" ;
156- index = output.indexOf (strmatch);
157- if (index < output.length ())
158- sscanf (output.mid (index, strmatch.length ()).toLatin1 (), " Free blocks: %lld" , &m_numOfFreeOrUsedBlocks);
159- }
160-
161- if (m_totalNumOfBlock > -1 && m_numOfFreeOrUsedBlocks > -1 && m_blocksSize > -1 ) {
162- m_totalNumOfBlock = qRound64 (m_totalNumOfBlock * (m_blocksSize / double (partition.m_sectorSize )));
163- m_numOfFreeOrUsedBlocks = qRound64 (m_numOfFreeOrUsedBlocks * (m_blocksSize / double (partition.m_sectorSize )));
164- qDebug () << " 111111111111111111111111" << m_totalNumOfBlock << m_numOfFreeOrUsedBlocks << m_blocksSize;
165- partition.setSectorUsage (m_totalNumOfBlock, m_numOfFreeOrUsedBlocks);
166- partition.m_fsBlockSize = m_blocksSize;
140+
141+ // 获取空闲块数
142+ strmatch = " Free blocks:" ;
143+ index = output.indexOf (strmatch);
144+ if (index >= 0 && index < output.length ()) {
145+ freeBlocks = Utils::regexpLabel (output, QString (" (?<=Free blocks:).*(?=\n )" )).trimmed ().toLong ();
146+ }
147+
148+ // 获取保留块数
149+ strmatch = " Reserved block count:" ;
150+ index = output.indexOf (strmatch);
151+ if (index >= 0 && index < output.length ()) {
152+ reservedBlocks = Utils::regexpLabel (output, QString (" (?<=Reserved block count:).*(?=\n )" )).trimmed ().toLong ();
153+ }
154+
155+ // 验证数据有效性并计算扇区使用情况
156+ if (blockCount > 0 && blockSize > 0 && freeBlocks >= 0 && reservedBlocks >= 0 ) {
157+ // 计算用户实际可用的空闲块数:可用 = (freeBlocks - reservedBlocks)
158+ long long userAvailableBlocks = (freeBlocks > reservedBlocks) ? (freeBlocks - reservedBlocks) : 0 ;
159+
160+ // 转换为扇区数
161+ Sector totalSectors = qRound64 (blockCount * (blockSize / double (partition.m_sectorSize )));
162+ Sector availableSectors = qRound64 (userAvailableBlocks * (blockSize / double (partition.m_sectorSize )));
163+
164+ qDebug () << " EXT* usage calculation: blockCount=" << blockCount
165+ << " blockSize=" << blockSize
166+ << " freeBlocks=" << freeBlocks
167+ << " reservedBlocks=" << reservedBlocks
168+ << " userAvailableBlocks=" << userAvailableBlocks
169+ << " totalSectors=" << totalSectors
170+ << " availableSectors=" << availableSectors;
171+
172+ // 如果分区已挂载,使用 stat 获取更准确的可用块数
173+ if (partition.m_busy && !partition.getMountPoints ().empty ()) {
174+ Byte_Value statTotalSize, statAvailableSize;
175+ if (Utils::getMountedFileSystemUsage (partition.getMountPoint (), statTotalSize, statAvailableSize) == 0 ) {
176+ // 使用 stat 获取的可用空间(已经排除了保留空间)
177+ Sector statAvailableSectors = qRound64 (statAvailableSize / double (partition.m_sectorSize ));
178+ qDebug () << " Using stat data for mounted partition: statAvailableSectors=" << statAvailableSectors;
179+ partition.setSectorUsage (totalSectors, statAvailableSectors);
180+ } else {
181+ partition.setSectorUsage (totalSectors, availableSectors);
182+ }
183+ } else {
184+ partition.setSectorUsage (totalSectors, availableSectors);
167185 }
186+ partition.m_fsBlockSize = blockSize;
168187 }
169-
170-
171188 } else {
172189 qDebug () << __FUNCTION__ << " dumpe2fs -h failed :" << output << error;
173190 }
0 commit comments