KDirStat – a graphical disk usage utility
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kdirtree.cpp 32KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. /*
  2. * File name: kdirtree.cpp
  3. * Summary: Support classes for KDirStat
  4. * License: LGPL - See file COPYING.LIB for details.
  5. * Author: Stefan Hundhammer <sh@suse.de>
  6. *
  7. * Updated: 2005-01-07
  8. */
  9. #include "config.h"
  10. #include <string.h>
  11. #include <sys/errno.h>
  12. #include <tqtimer.h>
  13. #include <kapp.h>
  14. #include <tdelocale.h>
  15. #include <tdeconfig.h>
  16. #include "kdirtree.h"
  17. #include "kdirtreeiterators.h"
  18. #include "kdirtreeview.h"
  19. #include "kdirsaver.h"
  20. #include "tdeio/job.h"
  21. #include "tdeio/netaccess.h"
  22. #define HAVE_STUPID_COMPILER 0
  23. using namespace KDirStat;
  24. KFileInfo::KFileInfo( KDirTree * tree,
  25. KDirInfo * parent,
  26. const char * name )
  27. : _parent( parent )
  28. , _next( 0 )
  29. , _tree( tree )
  30. {
  31. _isLocalFile = true;
  32. _isSparseFile = false;
  33. _name = name ? name : "";
  34. _device = 0;
  35. _mode = 0;
  36. _links = 0;
  37. _size = 0;
  38. _blocks = 0;
  39. _mtime = 0;
  40. }
  41. KFileInfo::KFileInfo( const TQString & filenameWithoutPath,
  42. struct stat * statInfo,
  43. KDirTree * tree,
  44. KDirInfo * parent )
  45. : _parent( parent )
  46. , _next( 0 )
  47. , _tree( tree )
  48. {
  49. TQ_CHECK_PTR( statInfo );
  50. _isLocalFile = true;
  51. _name = filenameWithoutPath;
  52. _device = statInfo->st_dev;
  53. _mode = statInfo->st_mode;
  54. _links = statInfo->st_nlink;
  55. _mtime = statInfo->st_mtime;
  56. if ( isSpecial() )
  57. {
  58. _size = 0;
  59. _blocks = 0;
  60. _isSparseFile = false;
  61. }
  62. else
  63. {
  64. _size = statInfo->st_size;
  65. _blocks = statInfo->st_blocks;
  66. _isSparseFile = isFile() && ( allocatedSize() < _size );
  67. if ( _isSparseFile )
  68. {
  69. kdDebug() << "Found sparse file: " << this
  70. << " Byte size: " << formatSize( byteSize() )
  71. << " Allocated: " << formatSize( allocatedSize() )
  72. << endl;
  73. }
  74. #if 0
  75. if ( isFile() && _links > 1 )
  76. {
  77. kdDebug() << _links << " hard links: " << this << endl;
  78. }
  79. #endif
  80. }
  81. #if 0
  82. #warning Debug mode: Huge sizes
  83. _size <<= 10;
  84. #endif
  85. }
  86. KFileInfo::KFileInfo( const KFileItem * fileItem,
  87. KDirTree * tree,
  88. KDirInfo * parent )
  89. : _parent( parent )
  90. , _next( 0 )
  91. , _tree( tree )
  92. {
  93. TQ_CHECK_PTR( fileItem );
  94. _isLocalFile = fileItem->isLocalFile();
  95. _name = parent ? fileItem->name() : fileItem->url().url();
  96. _device = 0;
  97. _mode = fileItem->mode();
  98. _links = 1;
  99. if ( isSpecial() )
  100. {
  101. _size = 0;
  102. _blocks = 0;
  103. _isSparseFile = false;
  104. }
  105. else
  106. {
  107. _size = fileItem->size();
  108. // Since KFileItem does not return any information about allocated disk
  109. // blocks, calculate that information artificially so callers don't
  110. // need to bother with special cases depending on how this object was
  111. // constructed.
  112. _blocks = _size / blockSize();
  113. if ( ( _size % blockSize() ) > 0 )
  114. _blocks++;
  115. // There is no way to find out via KFileInfo if this is a sparse file.
  116. _isSparseFile = false;
  117. }
  118. _mtime = fileItem->time( TDEIO::UDS_MODIFICATION_TIME );
  119. }
  120. KFileInfo::~KFileInfo()
  121. {
  122. // NOP
  123. /**
  124. * The destructor should also take care about unlinking this object from
  125. * its parent's children list, but regrettably that just doesn't work: At
  126. * this point (within the destructor) parts of the object are already
  127. * destroyed, e.g., the virtual table - virtual methods don't work any
  128. * more. Thus, somebody from outside must call deletingChild() just prior
  129. * to the actual "delete".
  130. *
  131. * This sucks, but it's the C++ standard.
  132. **/
  133. }
  134. KFileSize
  135. KFileInfo::allocatedSize() const
  136. {
  137. return blocks() * blockSize();
  138. }
  139. KFileSize
  140. KFileInfo::size() const
  141. {
  142. KFileSize sz = _isSparseFile ? allocatedSize() : _size;
  143. if ( _links > 1 )
  144. sz /= _links;
  145. return sz;
  146. }
  147. TQString
  148. KFileInfo::url() const
  149. {
  150. if ( _parent )
  151. {
  152. TQString parentUrl = _parent->url();
  153. if ( isDotEntry() ) // don't append "/." for dot entries
  154. return parentUrl;
  155. if ( parentUrl == "/" ) // avoid duplicating slashes
  156. return parentUrl + _name;
  157. else
  158. return parentUrl + "/" + _name;
  159. }
  160. else
  161. return _name;
  162. }
  163. TQString
  164. KFileInfo::debugUrl() const
  165. {
  166. return url() + ( isDotEntry() ? "/<Files>" : "" );
  167. }
  168. TQString
  169. KFileInfo::urlPart( int targetLevel ) const
  170. {
  171. int level = treeLevel(); // Cache this - it's expensive!
  172. if ( level < targetLevel )
  173. {
  174. kdError() << k_funcinfo << "URL level " << targetLevel
  175. << " requested, this is level " << level << endl;
  176. return "";
  177. }
  178. const KFileInfo *item = this;
  179. while ( level > targetLevel )
  180. {
  181. level--;
  182. item = item->parent();
  183. }
  184. return item->name();
  185. }
  186. int
  187. KFileInfo::treeLevel() const
  188. {
  189. int level = 0;
  190. KFileInfo * parent = _parent;
  191. while ( parent )
  192. {
  193. level++;
  194. parent = parent->parent();
  195. }
  196. return level;
  197. if ( _parent )
  198. return _parent->treeLevel() + 1;
  199. else
  200. return 0;
  201. }
  202. bool
  203. KFileInfo::hasChildren() const
  204. {
  205. return firstChild() || dotEntry();
  206. }
  207. bool
  208. KFileInfo::isInSubtree( const KFileInfo *subtree ) const
  209. {
  210. const KFileInfo * ancestor = this;
  211. while ( ancestor )
  212. {
  213. if ( ancestor == subtree )
  214. return true;
  215. ancestor = ancestor->parent();
  216. }
  217. return false;
  218. }
  219. KFileInfo *
  220. KFileInfo::locate( TQString url, bool findDotEntries )
  221. {
  222. if ( ! url.startsWith( _name ) )
  223. return 0;
  224. else // URL starts with this node's name
  225. {
  226. url.remove( 0, _name.length() ); // Remove leading name of this node
  227. if ( url.length() == 0 ) // Nothing left?
  228. return this; // Hey! That's us!
  229. if ( url.startsWith( "/" ) ) // If the next thing a path delimiter,
  230. url.remove( 0, 1 ); // remove that leading delimiter.
  231. else // No path delimiter at the beginning
  232. {
  233. if ( _name.right(1) != "/" && // and this is not the root directory
  234. ! isDotEntry() ) // or a dot entry:
  235. return 0; // This can't be any of our children.
  236. }
  237. // Search all children
  238. KFileInfo *child = firstChild();
  239. while ( child )
  240. {
  241. KFileInfo *foundChild = child->locate( url, findDotEntries );
  242. if ( foundChild )
  243. return foundChild;
  244. else
  245. child = child->next();
  246. }
  247. // Special case: The dot entry is requested.
  248. if ( findDotEntries && dotEntry() && url == "<Files>" )
  249. return dotEntry();
  250. // Search the dot entry if there is one - but only if there is no more
  251. // path delimiter left in the URL. The dot entry contains files only,
  252. // and their names may not contain the path delimiter, nor can they
  253. // have children. This check is not strictly necessary, but it may
  254. // speed up things a bit if we don't search the non-directory children
  255. // if the rest of the URL consists of several pathname components.
  256. if ( dotEntry() &&
  257. url.find ( "/" ) < 0 ) // No (more) "/" in this URL
  258. {
  259. return dotEntry()->locate( url, findDotEntries );
  260. }
  261. }
  262. return 0;
  263. }
  264. KDirInfo::KDirInfo( KDirTree * tree,
  265. KDirInfo * parent,
  266. bool asDotEntry )
  267. : KFileInfo( tree, parent )
  268. {
  269. init();
  270. if ( asDotEntry )
  271. {
  272. _isDotEntry = true;
  273. _dotEntry = 0;
  274. _name = ".";
  275. }
  276. else
  277. {
  278. _isDotEntry = false;
  279. _dotEntry = new KDirInfo( tree, this, true );
  280. }
  281. }
  282. KDirInfo::KDirInfo( const TQString & filenameWithoutPath,
  283. struct stat * statInfo,
  284. KDirTree * tree,
  285. KDirInfo * parent )
  286. : KFileInfo( filenameWithoutPath,
  287. statInfo,
  288. tree,
  289. parent )
  290. {
  291. init();
  292. _dotEntry = new KDirInfo( tree, this, true );
  293. }
  294. KDirInfo::KDirInfo( const KFileItem * fileItem,
  295. KDirTree * tree,
  296. KDirInfo * parent )
  297. : KFileInfo( fileItem,
  298. tree,
  299. parent )
  300. {
  301. init();
  302. _dotEntry = new KDirInfo( tree, this, true );
  303. }
  304. void
  305. KDirInfo::init()
  306. {
  307. _isDotEntry = false;
  308. _pendingReadJobs = 0;
  309. _dotEntry = 0;
  310. _firstChild = 0;
  311. _totalSize = _size;
  312. _totalBlocks = _blocks;
  313. _totalItems = 0;
  314. _totalSubDirs = 0;
  315. _totalFiles = 0;
  316. _latestMtime = _mtime;
  317. _isMountPoint = false;
  318. _summaryDirty = false;
  319. _beingDestroyed = false;
  320. _readState = KDirQueued;
  321. }
  322. KDirInfo::~KDirInfo()
  323. {
  324. _beingDestroyed = true;
  325. KFileInfo *child = _firstChild;
  326. // Recursively delete all children.
  327. while ( child )
  328. {
  329. KFileInfo * nextChild = child->next();
  330. delete child;
  331. child = nextChild;
  332. }
  333. // Delete the dot entry.
  334. if ( _dotEntry )
  335. {
  336. delete _dotEntry;
  337. }
  338. }
  339. void
  340. KDirInfo::recalc()
  341. {
  342. // kdDebug() << k_funcinfo << this << endl;
  343. _totalSize = _size;
  344. _totalBlocks = _blocks;
  345. _totalItems = 0;
  346. _totalSubDirs = 0;
  347. _totalFiles = 0;
  348. _latestMtime = _mtime;
  349. KFileInfoIterator it( this, KDotEntryAsSubDir );
  350. while ( *it )
  351. {
  352. _totalSize += (*it)->totalSize();
  353. _totalBlocks += (*it)->totalBlocks();
  354. _totalItems += (*it)->totalItems() + 1;
  355. _totalSubDirs += (*it)->totalSubDirs();
  356. _totalFiles += (*it)->totalFiles();
  357. if ( (*it)->isDir() )
  358. _totalSubDirs++;
  359. if ( (*it)->isFile() )
  360. _totalFiles++;
  361. time_t childLatestMtime = (*it)->latestMtime();
  362. if ( childLatestMtime > _latestMtime )
  363. _latestMtime = childLatestMtime;
  364. ++it;
  365. }
  366. _summaryDirty = false;
  367. }
  368. void
  369. KDirInfo::setMountPoint( bool isMountPoint )
  370. {
  371. _isMountPoint = isMountPoint;
  372. }
  373. KFileSize
  374. KDirInfo::totalSize()
  375. {
  376. if ( _summaryDirty )
  377. recalc();
  378. return _totalSize;
  379. }
  380. KFileSize
  381. KDirInfo::totalBlocks()
  382. {
  383. if ( _summaryDirty )
  384. recalc();
  385. return _totalBlocks;
  386. }
  387. int
  388. KDirInfo::totalItems()
  389. {
  390. if ( _summaryDirty )
  391. recalc();
  392. return _totalItems;
  393. }
  394. int
  395. KDirInfo::totalSubDirs()
  396. {
  397. if ( _summaryDirty )
  398. recalc();
  399. return _totalSubDirs;
  400. }
  401. int
  402. KDirInfo::totalFiles()
  403. {
  404. if ( _summaryDirty )
  405. recalc();
  406. return _totalFiles;
  407. }
  408. time_t
  409. KDirInfo::latestMtime()
  410. {
  411. if ( _summaryDirty )
  412. recalc();
  413. return _latestMtime;
  414. }
  415. bool
  416. KDirInfo::isFinished()
  417. {
  418. return ! isBusy();
  419. }
  420. void KDirInfo::setReadState( KDirReadState newReadState )
  421. {
  422. // "aborted" has higher priority than "finished"
  423. if ( _readState == KDirAborted && newReadState == KDirFinished )
  424. return;
  425. _readState = newReadState;
  426. }
  427. bool
  428. KDirInfo::isBusy()
  429. {
  430. if ( _pendingReadJobs > 0 && _readState != KDirAborted )
  431. return true;
  432. if ( readState() == KDirReading ||
  433. readState() == KDirQueued )
  434. return true;
  435. return false;
  436. }
  437. void
  438. KDirInfo::insertChild( KFileInfo *newChild )
  439. {
  440. TQ_CHECK_PTR( newChild );
  441. if ( newChild->isDir() || _dotEntry == 0 || _isDotEntry )
  442. {
  443. /**
  444. * Only directories are stored directly in pure directory nodes -
  445. * unless something went terribly wrong, e.g. there is no dot entry to use.
  446. * If this is a dot entry, store everything it gets directly within it.
  447. *
  448. * In any of those cases, insert the new child in the children list.
  449. *
  450. * We don't bother with this list's order - it's explicitly declared to
  451. * be unordered, so be warned! We simply insert this new child at the
  452. * list head since this operation can be performed in constant time
  453. * without the need for any additional lastChild etc. pointers or -
  454. * even worse - seeking the correct place for insertion first. This is
  455. * none of our business; the corresponding "view" object for this tree
  456. * will take care of such niceties.
  457. **/
  458. newChild->setNext( _firstChild );
  459. _firstChild = newChild;
  460. newChild->setParent( this ); // make sure the parent pointer is correct
  461. childAdded( newChild ); // update summaries
  462. }
  463. else
  464. {
  465. /*
  466. * If the child is not a directory, don't store it directly here - use
  467. * this entry's dot entry instead.
  468. */
  469. _dotEntry->insertChild( newChild );
  470. }
  471. }
  472. void
  473. KDirInfo::childAdded( KFileInfo *newChild )
  474. {
  475. if ( ! _summaryDirty )
  476. {
  477. _totalSize += newChild->size();
  478. _totalBlocks += newChild->blocks();
  479. _totalItems++;
  480. if ( newChild->isDir() )
  481. _totalSubDirs++;
  482. if ( newChild->isFile() )
  483. _totalFiles++;
  484. if ( newChild->mtime() > _latestMtime )
  485. _latestMtime = newChild->mtime();
  486. }
  487. else
  488. {
  489. // NOP
  490. /*
  491. * Don't bother updating the summary fields if the summary is dirty
  492. * (i.e. outdated) anyway: As soon as anybody wants to know some exact
  493. * value a complete recalculation of the entire subtree will be
  494. * triggered. On the other hand, if nobody wants to know (which is very
  495. * likely) we can save this effort.
  496. */
  497. }
  498. if ( _parent )
  499. _parent->childAdded( newChild );
  500. }
  501. void
  502. KDirInfo::deletingChild( KFileInfo *deletedChild )
  503. {
  504. /**
  505. * When children are deleted, things go downhill: Marking the summary
  506. * fields as dirty (i.e. outdated) is the only thing that can be done here.
  507. *
  508. * The accumulated sizes could be updated (by subtracting this deleted
  509. * child's values from them), but the latest mtime definitely has to be
  510. * recalculated: The child now being deleted might just be the one with the
  511. * latest mtime, and figuring out the second-latest cannot easily be
  512. * done. So we merely mark the summary as dirty and wait until a recalc()
  513. * will be triggered from outside - which might as well never happen when
  514. * nobody wants to know some summary field anyway.
  515. **/
  516. _summaryDirty = true;
  517. if ( _parent )
  518. _parent->deletingChild( deletedChild );
  519. if ( ! _beingDestroyed && deletedChild->parent() == this )
  520. {
  521. /**
  522. * Unlink the child from the children's list - but only if this doesn't
  523. * happen recursively in the destructor of this object: No use
  524. * bothering about the validity of the children's list if this will all
  525. * be history anyway in a moment.
  526. **/
  527. unlinkChild( deletedChild );
  528. }
  529. }
  530. void
  531. KDirInfo::unlinkChild( KFileInfo *deletedChild )
  532. {
  533. if ( deletedChild->parent() != this )
  534. {
  535. kdError() << deletedChild << " is not a child of " << this
  536. << " - cannot unlink from children list!" << endl;
  537. return;
  538. }
  539. if ( deletedChild == _firstChild )
  540. {
  541. // kdDebug() << "Unlinking first child " << deletedChild << endl;
  542. _firstChild = deletedChild->next();
  543. return;
  544. }
  545. KFileInfo *child = firstChild();
  546. while ( child )
  547. {
  548. if ( child->next() == deletedChild )
  549. {
  550. // kdDebug() << "Unlinking " << deletedChild << endl;
  551. child->setNext( deletedChild->next() );
  552. return;
  553. }
  554. child = child->next();
  555. }
  556. kdError() << "Couldn't unlink " << deletedChild << " from "
  557. << this << " children list" << endl;
  558. }
  559. void
  560. KDirInfo::readJobAdded()
  561. {
  562. _pendingReadJobs++;
  563. if ( _parent )
  564. _parent->readJobAdded();
  565. }
  566. void
  567. KDirInfo::readJobFinished()
  568. {
  569. _pendingReadJobs--;
  570. if ( _parent )
  571. _parent->readJobFinished();
  572. }
  573. void
  574. KDirInfo::readJobAborted()
  575. {
  576. _readState = KDirAborted;
  577. if ( _parent )
  578. _parent->readJobAborted();
  579. }
  580. void
  581. KDirInfo::finalizeLocal()
  582. {
  583. cleanupDotEntries();
  584. }
  585. KDirReadState
  586. KDirInfo::readState() const
  587. {
  588. if ( _isDotEntry && _parent )
  589. return _parent->readState();
  590. else
  591. return _readState;
  592. }
  593. void
  594. KDirInfo::cleanupDotEntries()
  595. {
  596. if ( ! _dotEntry || _isDotEntry )
  597. return;
  598. // Reparent dot entry children if there are no subdirectories on this level
  599. if ( ! _firstChild )
  600. {
  601. // kdDebug() << "Removing solo dot entry " << this << " " << endl;
  602. KFileInfo *child = _dotEntry->firstChild();
  603. _firstChild = child; // Move the entire children chain here.
  604. _dotEntry->setFirstChild( 0 ); // _dotEntry will be deleted below.
  605. while ( child )
  606. {
  607. child->setParent( this );
  608. child = child->next();
  609. }
  610. }
  611. // Delete dot entries without any children
  612. if ( ! _dotEntry->firstChild() )
  613. {
  614. // kdDebug() << "Removing empty dot entry " << this << endl;
  615. delete _dotEntry;
  616. _dotEntry = 0;
  617. }
  618. }
  619. KDirReadJob::KDirReadJob( KDirTree * tree,
  620. KDirInfo * dir )
  621. : _tree( tree )
  622. , _dir( dir )
  623. {
  624. _dir->readJobAdded();
  625. }
  626. KDirReadJob::~KDirReadJob()
  627. {
  628. _dir->readJobFinished();
  629. }
  630. void
  631. KDirReadJob::childAdded( KFileInfo *newChild )
  632. {
  633. _tree->childAddedNotify( newChild );
  634. }
  635. void
  636. KDirReadJob::deletingChild( KFileInfo *deletedChild )
  637. {
  638. _tree->deletingChildNotify( deletedChild );
  639. }
  640. KLocalDirReadJob::KLocalDirReadJob( KDirTree * tree,
  641. KDirInfo * dir )
  642. : KDirReadJob( tree, dir )
  643. , _diskDir( 0 )
  644. {
  645. }
  646. KLocalDirReadJob::~KLocalDirReadJob()
  647. {
  648. }
  649. void
  650. KLocalDirReadJob::startReading()
  651. {
  652. struct dirent * entry;
  653. struct stat statInfo;
  654. TQString dirName = _dir->url();
  655. if ( ( _diskDir = opendir( dirName ) ) )
  656. {
  657. _tree->sendProgressInfo( dirName );
  658. _dir->setReadState( KDirReading );
  659. while ( ( entry = readdir( _diskDir ) ) )
  660. {
  661. TQString entryName = entry->d_name;
  662. if ( entryName != "." &&
  663. entryName != ".." )
  664. {
  665. TQString fullName = dirName + "/" + entryName;
  666. if ( lstat( fullName, &statInfo ) == 0 ) // lstat() OK
  667. {
  668. if ( S_ISDIR( statInfo.st_mode ) ) // directory child?
  669. {
  670. KDirInfo *subDir = new KDirInfo( entryName, &statInfo, _tree, _dir );
  671. _dir->insertChild( subDir );
  672. childAdded( subDir );
  673. if ( subDir->dotEntry() )
  674. childAdded( subDir->dotEntry() );
  675. if ( _dir->device() == subDir->device() ) // normal case
  676. {
  677. _tree->addJob( new KLocalDirReadJob( _tree, subDir ) );
  678. }
  679. else // The subdirectory we just found is a mount point.
  680. {
  681. // kdDebug() << "Found mount point " << subDir << endl;
  682. subDir->setMountPoint();
  683. if ( _tree->crossFileSystems() )
  684. {
  685. _tree->addJob( new KLocalDirReadJob( _tree, subDir ) );
  686. }
  687. else
  688. {
  689. subDir->setReadState( KDirOnRequestOnly );
  690. _tree->sendFinalizeLocal( subDir );
  691. subDir->finalizeLocal();
  692. }
  693. }
  694. }
  695. else // non-directory child
  696. {
  697. KFileInfo *child = new KFileInfo( entryName, &statInfo, _tree, _dir );
  698. _dir->insertChild( child );
  699. childAdded( child );
  700. }
  701. }
  702. else // lstat() error
  703. {
  704. // kdWarning() << "lstat(" << fullName << ") failed: " << strerror( errno ) << endl;
  705. /*
  706. * Not much we can do when lstat() didn't work; let's at
  707. * least create an (almost empty) entry as a placeholder.
  708. */
  709. KDirInfo *child = new KDirInfo( _tree, _dir, entry->d_name );
  710. child->setReadState( KDirError );
  711. _dir->insertChild( child );
  712. childAdded( child );
  713. }
  714. }
  715. }
  716. closedir( _diskDir );
  717. // kdDebug() << "Finished reading " << _dir << endl;
  718. _dir->setReadState( KDirFinished );
  719. _tree->sendFinalizeLocal( _dir );
  720. _dir->finalizeLocal();
  721. }
  722. else
  723. {
  724. _dir->setReadState( KDirError );
  725. _tree->sendFinalizeLocal( _dir );
  726. _dir->finalizeLocal();
  727. // kdWarning() << k_funcinfo << "opendir(" << dirName << ") failed" << endl;
  728. // opendir() doesn't set 'errno' according to POSIX :-(
  729. }
  730. _tree->jobFinishedNotify( this );
  731. // Don't add anything after _tree->jobFinishedNotify()
  732. // since this deletes this job!
  733. }
  734. KFileInfo *
  735. KLocalDirReadJob::stat( const KURL & url,
  736. KDirTree * tree,
  737. KDirInfo * parent )
  738. {
  739. struct stat statInfo;
  740. if ( lstat( url.path(), &statInfo ) == 0 ) // lstat() OK
  741. {
  742. TQString name = parent ? url.filename() : url.path();
  743. if ( S_ISDIR( statInfo.st_mode ) ) // directory?
  744. {
  745. KDirInfo * dir = new KDirInfo( name, &statInfo, tree, parent );
  746. if ( dir && parent && dir->device() != parent->device() )
  747. dir->setMountPoint();
  748. return dir;
  749. }
  750. else // no directory
  751. return new KFileInfo( name, &statInfo, tree, parent );
  752. }
  753. else // lstat() failed
  754. return 0;
  755. }
  756. KAnyDirReadJob::KAnyDirReadJob( KDirTree * tree,
  757. KDirInfo * dir )
  758. : TQObject()
  759. , KDirReadJob( tree, dir )
  760. {
  761. _job = 0;
  762. }
  763. KAnyDirReadJob::~KAnyDirReadJob()
  764. {
  765. #if 0
  766. if ( _job )
  767. _job->kill( true ); // quietly
  768. #endif
  769. }
  770. void
  771. KAnyDirReadJob::startReading()
  772. {
  773. KURL url( _dir->url() );
  774. if ( ! url.isValid() )
  775. {
  776. kdWarning() << k_funcinfo << "URL malformed: " << _dir->url() << endl;
  777. }
  778. _job = TDEIO::listDir( url,
  779. false ); // showProgressInfo
  780. connect( _job, TQT_SIGNAL( entries( TDEIO::Job *, const TDEIO::UDSEntryList& ) ),
  781. this, TQT_SLOT ( entries( TDEIO::Job *, const TDEIO::UDSEntryList& ) ) );
  782. connect( _job, TQT_SIGNAL( result ( TDEIO::Job * ) ),
  783. this, TQT_SLOT ( finished( TDEIO::Job * ) ) );
  784. connect( _job, TQT_SIGNAL( canceled( TDEIO::Job * ) ),
  785. this, TQT_SLOT ( finished( TDEIO::Job * ) ) );
  786. }
  787. void
  788. KAnyDirReadJob::entries ( TDEIO::Job * job,
  789. const TDEIO::UDSEntryList & entryList )
  790. {
  791. NOT_USED( job );
  792. KURL url( _dir->url() ); // Cache this - it's expensive!
  793. if ( ! url.isValid() )
  794. {
  795. kdWarning() << k_funcinfo << "URL malformed: " << _dir->url() << endl;
  796. }
  797. TDEIO::UDSEntryListConstIterator it = entryList.begin();
  798. while ( it != entryList.end() )
  799. {
  800. KFileItem entry( *it,
  801. url,
  802. true, // determineMimeTypeOnDemand
  803. true ); // URL is parent directory
  804. if ( entry.name() != "." &&
  805. entry.name() != ".." )
  806. {
  807. // kdDebug() << "Found " << entry.url().url() << endl;
  808. if ( entry.isDir() && // Directory child
  809. ! entry.isLink() ) // and not a symlink?
  810. {
  811. KDirInfo *subDir = new KDirInfo( &entry, _tree, _dir );
  812. _dir->insertChild( subDir );
  813. childAdded( subDir );
  814. if ( subDir->dotEntry() )
  815. childAdded( subDir->dotEntry() );
  816. _tree->addJob( new KAnyDirReadJob( _tree, subDir ) );
  817. }
  818. else // non-directory child
  819. {
  820. KFileInfo *child = new KFileInfo( &entry, _tree, _dir );
  821. _dir->insertChild( child );
  822. childAdded( child );
  823. }
  824. }
  825. ++it;
  826. }
  827. }
  828. void
  829. KAnyDirReadJob::finished( TDEIO::Job * job )
  830. {
  831. if ( job->error() )
  832. _dir->setReadState( KDirError );
  833. else
  834. _dir->setReadState( KDirFinished );
  835. _tree->sendFinalizeLocal( _dir );
  836. _dir->finalizeLocal();
  837. _job = 0; // The job deletes itself after this signal!
  838. _tree->jobFinishedNotify( this );
  839. // Don't add anything after _tree->jobFinishedNotify()
  840. // since this deletes this job!
  841. }
  842. KFileInfo *
  843. KAnyDirReadJob::stat( const KURL & url,
  844. KDirTree * tree,
  845. KDirInfo * parent )
  846. {
  847. TDEIO::UDSEntry uds_entry;
  848. if ( TDEIO::NetAccess::stat( url, uds_entry, tqApp->mainWidget() ) ) // remote stat() OK?
  849. {
  850. KFileItem entry( uds_entry, url,
  851. true, // determine MIME type on demand
  852. false ); // URL specifies parent directory
  853. return entry.isDir() ? new KDirInfo ( &entry, tree, parent ) : new KFileInfo( &entry, tree, parent );
  854. }
  855. else // remote stat() failed
  856. return 0;
  857. #if HAVE_STUPID_COMPILER
  858. /**
  859. * This is stupid, but GCC 2.95.3 claims that "control reaches end of
  860. * non-void function" without this - so let him have this stupid "return".
  861. *
  862. * Sigh.
  863. **/
  864. return 0;
  865. #endif
  866. }
  867. TQString
  868. KAnyDirReadJob::owner( KURL url )
  869. {
  870. TDEIO::UDSEntry uds_entry;
  871. if ( TDEIO::NetAccess::stat( url, uds_entry, tqApp->mainWidget() ) ) // remote stat() OK?
  872. {
  873. KFileItem entry( uds_entry, url,
  874. true, // determine MIME type on demand
  875. false ); // URL specifies parent directory
  876. return entry.user();
  877. }
  878. return TQString();
  879. }
  880. KDirTree::KDirTree()
  881. : TQObject()
  882. {
  883. _root = 0;
  884. _selection = 0;
  885. _isFileProtocol = false;
  886. _isBusy = false;
  887. _readMethod = KDirReadUnknown;
  888. _jobQueue.setAutoDelete( true ); // Delete queued jobs automatically when destroyed
  889. readConfig();
  890. }
  891. KDirTree::~KDirTree()
  892. {
  893. selectItem( 0 );
  894. // Jobs still in the job queue are automatically deleted along with the
  895. // queue since autoDelete is set.
  896. //
  897. // However, the queue needs to be cleared first before the entire tree is
  898. // deleted, otherwise the dir pointers in each read job becomes invalid too
  899. // early.
  900. _jobQueue.clear();
  901. if ( _root )
  902. delete _root;
  903. }
  904. void
  905. KDirTree::readConfig()
  906. {
  907. TDEConfig * config = kapp->config();
  908. config->setGroup( "Directory Reading" );
  909. _crossFileSystems = config->readBoolEntry( "CrossFileSystems", false );
  910. _enableLocalDirReader = config->readBoolEntry( "EnableLocalDirReader", true );
  911. }
  912. void
  913. KDirTree::startReading( const KURL & url )
  914. {
  915. // kdDebug() << k_funcinfo << " " << url.url() << endl;
  916. #if 0
  917. kdDebug() << "url: " << url.url() << endl;
  918. kdDebug() << "path: " << url.path() << endl;
  919. kdDebug() << "filename: " << url.filename() << endl;
  920. kdDebug() << "protocol: " << url.protocol() << endl;
  921. kdDebug() << "isValid: " << url.isValid() << endl;
  922. kdDebug() << "isMalformed: " << url.isMalformed() << endl;
  923. kdDebug() << "isLocalFile: " << url.isLocalFile() << endl;
  924. #endif
  925. _isBusy = true;
  926. emit startingReading();
  927. if ( _root )
  928. {
  929. // Clean up leftover stuff
  930. selectItem( 0 );
  931. emit deletingChild( _root );
  932. // kdDebug() << "Deleting root prior to reading" << endl;
  933. delete _root;
  934. _root = 0;
  935. emit childDeleted();
  936. }
  937. readConfig();
  938. _isFileProtocol = url.isLocalFile();
  939. if ( _isFileProtocol && _enableLocalDirReader )
  940. {
  941. // kdDebug() << "Using local directory reader for " << url.url() << endl;
  942. _readMethod = KDirReadLocal;
  943. _root = KLocalDirReadJob::stat( url, this );
  944. }
  945. else
  946. {
  947. // kdDebug() << "Using TDEIO methods for " << url.url() << endl;
  948. KURL cleanUrl( url );
  949. cleanUrl.cleanPath(); // Resolve relative paths, get rid of multiple '/'
  950. _readMethod = KDirReadKIO;
  951. _root = KAnyDirReadJob::stat( cleanUrl, this );
  952. }
  953. if ( _root )
  954. {
  955. childAddedNotify( _root );
  956. if ( _root->isDir() )
  957. {
  958. KDirInfo *dir = (KDirInfo *) _root;
  959. if ( _readMethod == KDirReadLocal )
  960. addJob( new KLocalDirReadJob( this, dir ) );
  961. else
  962. addJob( new KAnyDirReadJob( this, dir ) );
  963. }
  964. else
  965. {
  966. _isBusy = false;
  967. emit finished();
  968. }
  969. }
  970. else // stat() failed
  971. {
  972. // kdWarning() << "stat(" << url.url() << ") failed" << endl;
  973. _isBusy = false;
  974. emit finished();
  975. emit finalizeLocal( 0 );
  976. }
  977. if ( ! _jobQueue.isEmpty() )
  978. TQTimer::singleShot( 0, this, TQT_SLOT( timeSlicedRead() ) );
  979. }
  980. void
  981. KDirTree::refresh( KFileInfo *subtree )
  982. {
  983. if ( ! _root )
  984. return;
  985. if ( ! subtree || ! subtree->parent() ) // Refresh all (from root)
  986. {
  987. startReading( fixedUrl( _root->url() ) );
  988. }
  989. else // Refresh subtree
  990. {
  991. // Save some values from the old subtree.
  992. KURL url = subtree->url();
  993. KDirInfo * parent = subtree->parent();
  994. // Select nothing if the current selection is to be deleted
  995. if ( _selection && _selection->isInSubtree( subtree ) )
  996. selectItem( 0 );
  997. // Get rid of the old subtree.
  998. emit deletingChild( subtree );
  999. // kdDebug() << "Deleting subtree " << subtree << endl;
  1000. /**
  1001. * This may sound stupid, but the parent must be told to unlink its
  1002. * child from the children list. The child cannot simply do this by
  1003. * itself in its destructor since at this point important parts of the
  1004. * object may already be destroyed, e.g., the virtual table -
  1005. * i.e. virtual methods won't work any more.
  1006. *
  1007. * I just found that out the hard way by several hours of debugging. ;-}
  1008. **/
  1009. parent->deletingChild( subtree );
  1010. delete subtree;
  1011. emit childDeleted();
  1012. // Create new subtree root.
  1013. subtree = ( _readMethod == KDirReadLocal ) ?
  1014. KLocalDirReadJob::stat( url, this, parent ) : KAnyDirReadJob::stat( url, this, parent );
  1015. // kdDebug() << "New subtree: " << subtree << endl;
  1016. if ( subtree )
  1017. {
  1018. // Insert new subtree root into the tree hierarchy.
  1019. parent->insertChild( subtree );
  1020. childAddedNotify( subtree );
  1021. if ( subtree->isDir() )
  1022. {
  1023. // Prepare reading this subtree's contents.
  1024. KDirInfo *dir = (KDirInfo *) subtree;
  1025. if ( _readMethod == KDirReadLocal )
  1026. addJob( new KLocalDirReadJob( this, dir ) );
  1027. else
  1028. addJob( new KAnyDirReadJob( this, dir ) );
  1029. }
  1030. else
  1031. {
  1032. _isBusy = false;
  1033. emit finished();
  1034. }
  1035. // Trigger reading as soon as the event loop continues.
  1036. if ( ! _jobQueue.isEmpty() )
  1037. TQTimer::singleShot( 0, this, TQT_SLOT( timeSlicedRead() ) );
  1038. }
  1039. }
  1040. }
  1041. void
  1042. KDirTree::timeSlicedRead()
  1043. {
  1044. if ( ! _jobQueue.isEmpty() )
  1045. _jobQueue.head()->startReading();
  1046. }
  1047. void
  1048. KDirTree::abortReading()
  1049. {
  1050. if ( _jobQueue.isEmpty() )
  1051. return;
  1052. while ( ! _jobQueue.isEmpty() )
  1053. {
  1054. _jobQueue.head()->dir()->readJobAborted();
  1055. _jobQueue.dequeue();
  1056. }
  1057. _isBusy = false;
  1058. emit aborted();
  1059. }
  1060. void
  1061. KDirTree::jobFinishedNotify( KDirReadJob *job )
  1062. {
  1063. // Get rid of the old (finished) job.
  1064. _jobQueue.dequeue();
  1065. delete job;
  1066. // Look for a new job.
  1067. if ( _jobQueue.isEmpty() ) // No new job available - we're done.
  1068. {
  1069. _isBusy = false;
  1070. emit finished();
  1071. }
  1072. else // There is a new job
  1073. {
  1074. // Set up zero-duration timer for the new job.
  1075. TQTimer::singleShot( 0, this, TQT_SLOT( timeSlicedRead() ) );
  1076. }
  1077. }
  1078. void
  1079. KDirTree::childAddedNotify( KFileInfo *newChild )
  1080. {
  1081. emit childAdded( newChild );
  1082. if ( newChild->dotEntry() )
  1083. emit childAdded( newChild->dotEntry() );
  1084. }
  1085. void
  1086. KDirTree::deletingChildNotify( KFileInfo *deletedChild )
  1087. {
  1088. emit deletingChild( deletedChild );
  1089. // Only now check for selection and root: Give connected objects
  1090. // (i.e. views) a chance to change either while handling the signal.
  1091. if ( _selection && _selection->isInSubtree( deletedChild ) )
  1092. selectItem( 0 );
  1093. if ( deletedChild == _root )
  1094. _root = 0;
  1095. }
  1096. void
  1097. KDirTree::childDeletedNotify()
  1098. {
  1099. emit childDeleted();
  1100. }
  1101. void
  1102. KDirTree::deleteSubtree( KFileInfo *subtree )
  1103. {
  1104. // kdDebug() << "Deleting subtree " << subtree << endl;
  1105. KDirInfo *parent = subtree->parent();
  1106. if ( parent )
  1107. {
  1108. // Give the parent of the child to be deleted a chance to unlink the
  1109. // child from its children list and take care of internal summary
  1110. // fields
  1111. parent->deletingChild( subtree );
  1112. }
  1113. // Send notification to anybody interested (e.g., to attached views)
  1114. deletingChildNotify( subtree );
  1115. if ( parent )
  1116. {
  1117. if ( parent->isDotEntry() && ! parent->hasChildren() )
  1118. // This was the last child of a dot entry
  1119. {
  1120. // Get rid of that now empty and useless dot entry
  1121. if ( parent->parent() )
  1122. {
  1123. if ( parent->parent()->isFinished() )
  1124. {
  1125. // kdDebug() << "Removing empty dot entry " << parent << endl;
  1126. deletingChildNotify( parent );
  1127. parent->parent()->setDotEntry( 0 );
  1128. delete parent;
  1129. }
  1130. }
  1131. else // no parent - this should never happen (?)
  1132. {
  1133. kdError() << "Internal error: Killing dot entry without parent " << parent << endl;
  1134. // Better leave that dot entry alone - we shouldn't have come
  1135. // here in the first place. Who knows what will happen if this
  1136. // thing is deleted now?!
  1137. //
  1138. // Intentionally NOT calling:
  1139. // delete parent;
  1140. }
  1141. }
  1142. }
  1143. delete subtree;
  1144. emit childDeleted();
  1145. }
  1146. void
  1147. KDirTree::addJob( KDirReadJob * job )
  1148. {
  1149. TQ_CHECK_PTR( job );
  1150. _jobQueue.enqueue( job );
  1151. }
  1152. void
  1153. KDirTree::sendProgressInfo( const TQString &infoLine )
  1154. {
  1155. emit progressInfo( infoLine );
  1156. }
  1157. void
  1158. KDirTree::sendFinalizeLocal( KDirInfo *dir )
  1159. {
  1160. emit finalizeLocal( dir );
  1161. }
  1162. void
  1163. KDirTree::selectItem( KFileInfo *newSelection )
  1164. {
  1165. if ( newSelection == _selection )
  1166. return;
  1167. #if 0
  1168. if ( newSelection )
  1169. kdDebug() << k_funcinfo << " selecting " << newSelection << endl;
  1170. else
  1171. kdDebug() << k_funcinfo << " selecting nothing" << endl;
  1172. #endif
  1173. _selection = newSelection;
  1174. emit selectionChanged( _selection );
  1175. }
  1176. KURL
  1177. KDirStat::fixedUrl( const TQString & dirtyUrl )
  1178. {
  1179. KURL url = dirtyUrl;
  1180. if ( ! url.isValid() ) // Maybe it's just a path spec?
  1181. {
  1182. url = KURL(); // Start over with an empty, but valid URL
  1183. url.setPath( dirtyUrl ); // and use just the path part.
  1184. }
  1185. else
  1186. {
  1187. url.cleanPath(); // Resolve relative paths, get rid of multiple slashes.
  1188. }
  1189. // Strip off the rightmost slash - some tdeioslaves (e.g. 'tar') can't handle that.
  1190. TQString path = url.path();
  1191. if ( path.length() > 1 && path.right(1) == "/" )
  1192. {
  1193. path = path.left( path.length()-1 );
  1194. url.setPath( path );
  1195. }
  1196. if ( url.isLocalFile() )
  1197. {
  1198. // Make a relative path an absolute path
  1199. KDirSaver dir( url.path() );
  1200. url.setPath( dir.currentDirPath() );
  1201. }
  1202. return url;
  1203. }
  1204. TQString
  1205. KDirStat::formatSize( KFileSize lSize )
  1206. {
  1207. TQString sizeString;
  1208. double size;
  1209. TQString unit;
  1210. if ( lSize < 1024 )
  1211. {
  1212. sizeString.setNum( (long) lSize );
  1213. unit = i18n( "Bytes" );
  1214. }
  1215. else
  1216. {
  1217. size = lSize / 1024.0; // kB
  1218. if ( size < 1024.0 )
  1219. {
  1220. sizeString.sprintf( "%.1f", size );
  1221. unit = i18n( "kB" );
  1222. }
  1223. else
  1224. {
  1225. size /= 1024.0; // MB
  1226. if ( size < 1024.0 )
  1227. {
  1228. sizeString.sprintf( "%.1f", size );
  1229. unit = i18n ( "MB" );
  1230. }
  1231. else
  1232. {
  1233. size /= 1024.0; // GB - we won't go any further...
  1234. sizeString.sprintf( "%.2f", size );
  1235. unit = i18n ( "GB" );
  1236. }
  1237. }
  1238. }
  1239. if ( ! unit.isEmpty() )
  1240. {
  1241. sizeString += " " + unit;
  1242. }
  1243. return sizeString;
  1244. }
  1245. #include "kdirtree.moc"
  1246. // EOF