From 7f2dd5a8fd39ca0147f2b43e607b2a3ca2a75110 Mon Sep 17 00:00:00 2001 From: Frederic Back Date: Mon, 25 Sep 2006 09:58:07 +0000 Subject: * use unsigned long for size git-svn-id: file:///home/lennart/svn/public/fring/trunk@25 d0d2c35f-0a1e-0410-abeb-dabff30a67ee --- src/extension/extensiontest.py | 4 ++++ src/extension/fringtools.cpp | 35 +++++++++++++++-------------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/extension/extensiontest.py b/src/extension/extensiontest.py index e95c7a3..cd4b6aa 100644 --- a/src/extension/extensiontest.py +++ b/src/extension/extensiontest.py @@ -13,6 +13,9 @@ else: tree_path = os.path.expanduser("~") def read( t, tab=0 ): fn, data, size = t print " "*tab,fn,size + + if tab>0: return + if not data: return for e in data: read(e,tab+1) @@ -20,4 +23,5 @@ def read( t, tab=0 ): print 'getting "%s":'%tree_path i = fringtools.build_tree(tree_path, True) print "done." + read(i) diff --git a/src/extension/fringtools.cpp b/src/extension/fringtools.cpp index 93643be..9205816 100644 --- a/src/extension/fringtools.cpp +++ b/src/extension/fringtools.cpp @@ -16,9 +16,11 @@ def _build_tree(self,path): try: p = os.path.join(path, fn) except: continue s = os.lstat(p) - if stat.S_ISDIR(s.st_mode): l.append( (fn, self._build_tree(p)) ) - elif stat.S_ISREG(s.st_mode): l.append( (fn, s.st_size) ) - return sum_list(l, os.path.split(path)[-1]) + if stat.S_ISDIR(s.st_mode): + l.append( (fn, self._build_tree(p), s.st_size) ) + elif stat.S_ISREG(s.st_mode): + l.append( (fn, None, s.st_size) ) + return sum_list(os.path.split(path)[-1], l, 0) ============================================================================== @@ -28,14 +30,12 @@ Instead of using SumList, the new build_tree will use a tuple /// recursive function to build a list system with directory information PyObject* build_tree( std::string path, const bool& skipHidden) { - PyObject* l = PyList_New(0); - DIR* d = opendir(path.c_str()); - struct dirent *e; - - int total = 0; + if(d == 0) Py_RETURN_NONE; + unsigned long total = 0; + struct dirent *e; while((e = readdir(d)) != NULL) { std::string fn = e->d_name; @@ -46,21 +46,22 @@ PyObject* build_tree( std::string path, const bool& skipHidden) { struct stat s; lstat(fn.c_str(), &s); + if (s.st_size < 0) + std::cout << "alert2 " << path << ", " << total << std::endl << std::flush; + if(S_ISDIR(s.st_mode)) { PyObject* sub = build_tree(fn,skipHidden); PyTuple_SetItem(sub,0,PyString_FromString(e->d_name)); PyList_Append(l, sub ); - total += PyInt_AsLong( PyTuple_GET_ITEM(sub,2) ); + total += PyLong_AsUnsignedLong( PyTuple_GET_ITEM(sub,2) ); } else if (S_ISREG(s.st_mode)) { - PyList_Append(l, Py_BuildValue("(sOi)",e->d_name,Py_None,s.st_size) ); + PyList_Append(l, Py_BuildValue("(sOk)",e->d_name,Py_None,s.st_size) ); total += s.st_size; } - } closedir(d); - - return Py_BuildValue("(sOi)", path.c_str(), l, total ); + return Py_BuildValue("(sOk)", path.c_str(), l, total ); } /// initial function call. @@ -68,13 +69,7 @@ static PyObject* fringtools_build_tree(PyObject *self, PyObject *args) { const char *path; int skip_hidden = 1; if (!PyArg_ParseTuple(args, "si", &path, &skip_hidden)) return NULL; - - PyObject* o; - //Py_BEGIN_ALLOW_THREADS - o = build_tree(std::string(path),(skip_hidden==1)); - //Py_END_ALLOW_THREADS - - return o; + return build_tree(std::string(path),(skip_hidden==1)); } //============================================================================== -- cgit