Merge lp:~carlos-mazieri/ubuntu-filemanager-app/trash-operations-1 into lp:ubuntu-filemanager-app

Proposed by Carlos Jose Mazieri
Status: Merged
Approved by: Arto Jalkanen
Approved revision: 187
Merged at revision: 186
Proposed branch: lp:~carlos-mazieri/ubuntu-filemanager-app/trash-operations-1
Merge into: lp:ubuntu-filemanager-app
Diff against target: 88 lines (+80/-0)
1 file modified
src/plugin/folderlistmodel/diriteminfo.h (+80/-0)
To merge this branch: bzr merge lp:~carlos-mazieri/ubuntu-filemanager-app/trash-operations-1
Reviewer Review Type Date Requested Status
Arto Jalkanen Approve
Ubuntu Phone Apps Jenkins Bot continuous-integration Approve
Review via email: mp+219939@code.launchpad.net

Commit message

created ActionPaths structure

Description of the change

ActionPaths will be used for handling Actions,
it provides common helper functions for source and target paths or urls.

Currently Actions are supposed to have just one source parent folder and one target parent path as copy/cut are made from just one place and paste puts items on just one place.
These items are stored on FileSystemAction::Action::origPath and FileSystemAction::Action::targetPath.

Restoring items from Trash requires items to be moved to different places, so an ActionPaths will be used in the FileSystemAction::ActionEntry which makes items to be independent from the main FileSystemAction::Action once they have their own source/target paths information.

It also provides ActionPaths::toggle() which is intended for Undo Actions.

To post a comment you must log in.
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Arto Jalkanen (ajalkane) wrote :

1)

Why this, pathLen is local variable and never used after this:

32 + else
33 + { //avoid bad index
34 + pathLen = 0;
35 + }

2)

This is a problematic function: inline void setSource(const QString& source)

Consider if it's been called like this:

setSource("/path/to/file");
...
setSource("aFile")

Then we have QStringRef's that will not be valid and probably causes undefined behaviour if I've understood QStringRef's documentation correctly.

review: Needs Fixing
Revision history for this message
Carlos Jose Mazieri (carlos-mazieri) wrote :

> 1)
>
> Why this, pathLen is local variable and never used after this:
>
> 32 + else
> 33 + { //avoid bad index
> 34 + pathLen = 0;
> 35 + }

This is a mistake, from some previous versions not presented here pathLen was not local.
I will remove this.

> 2)
>
> This is a problematic function: inline void setSource(const QString& source)
>
> Consider if it's been called like this:
>
> setSource("/path/to/file");
> ...
> setSource("aFile")
>
> Then we have QStringRef's that will not be valid and probably causes undefined
> behaviour if I've understood QStringRef's documentation correctly.

I understand that you are asking about relative paths, for absolute paths like setSource("/path/to/file") it is supposed to work.

If so, regarding relative paths it would not work, but it states in the beginning of the file:
        "Paths stored here are supposed to NOT be relative."
In the current implementation it is only used with absolute paths.
Let me know if it is acceptable.

Revision history for this message
Arto Jalkanen (ajalkane) wrote :

> > 1)
> >
> > Why this, pathLen is local variable and never used after this:
> >
> > 32 + else
> > 33 + { //avoid bad index
> > 34 + pathLen = 0;
> > 35 + }
>
> This is a mistake, from some previous versions not presented here pathLen was
> not local.
> I will remove this.
>
>
>
> > 2)
> >
> > This is a problematic function: inline void setSource(const QString& source)
> >
> > Consider if it's been called like this:
> >
> > setSource("/path/to/file");
> > ...
> > setSource("aFile")
> >
> > Then we have QStringRef's that will not be valid and probably causes
> undefined
> > behaviour if I've understood QStringRef's documentation correctly.
>
> I understand that you are asking about relative paths, for absolute paths like
> setSource("/path/to/file") it is supposed to work.
>
> If so, regarding relative paths it would not work, but it states in the
> beginning of the file:
> "Paths stored here are supposed to NOT be relative."
> In the current implementation it is only used with absolute paths.
> Let me know if it is acceptable.

I'd ask that the function setSource() will set or reset _sFile and _origPath variables whenever _source variable changes. While it may not be problem in how it's currently used, it is a potentially hard to find error in future if the implementation changes.

So in the current merge request it could be something like:

if (pathLen != -1)
{
    _sFile = QStringRef(&_source, pathLen + 1, _source.size() - pathLen - 1);
    _origPath = QStringRef(&_source, 0, pathLen);
}
else
{ //avoid bad index
    pathLen = 0;
    _sFile = QStringRef();
    _origPath = QStringRef();
}

That ways there's no possibility using _sFile or _origPath results in memory corruption however setSource() function is called.

186. By Carlos Jose Mazieri

improved ActionPaths::setSource(), prevent possible memory corruption if using relative paths even relative paths are not supported.

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Carlos Jose Mazieri (carlos-mazieri) wrote :

Revision 186 addresses requests 1) and 2)

Revision history for this message
Carlos Jose Mazieri (carlos-mazieri) wrote :

> > > 1)
> > >
> > > Why this, pathLen is local variable and never used after this:
> > >
> > > 32 + else
> > > 33 + { //avoid bad index
> > > 34 + pathLen = 0;
> > > 35 + }
> >
> > This is a mistake, from some previous versions not presented here pathLen
> was
> > not local.
> > I will remove this.
> >
> >
> >
> > > 2)
> > >
> > > This is a problematic function: inline void setSource(const QString&
> source)
> > >
> > > Consider if it's been called like this:
> > >
> > > setSource("/path/to/file");
> > > ...
> > > setSource("aFile")
> > >
> > > Then we have QStringRef's that will not be valid and probably causes
> > undefined
> > > behaviour if I've understood QStringRef's documentation correctly.
> >
> > I understand that you are asking about relative paths, for absolute paths
> like
> > setSource("/path/to/file") it is supposed to work.
> >
> > If so, regarding relative paths it would not work, but it states in the
> > beginning of the file:
> > "Paths stored here are supposed to NOT be relative."
> > In the current implementation it is only used with absolute paths.
> > Let me know if it is acceptable.
>
> I'd ask that the function setSource() will set or reset _sFile and _origPath
> variables whenever _source variable changes. While it may not be problem in
> how it's currently used, it is a potentially hard to find error in future if
> the implementation changes.
>
> So in the current merge request it could be something like:
>
> if (pathLen != -1)
> {
> _sFile = QStringRef(&_source, pathLen + 1, _source.size() - pathLen -
> 1);
> _origPath = QStringRef(&_source, 0, pathLen);
> }
> else
> { //avoid bad index
> pathLen = 0;
> _sFile = QStringRef();
> _origPath = QStringRef();

Thinking a little more about it, it looks like the two lines above do not have any effect.
Both "_sFile" and "_origPath" were already initialized with QStringRef default constructor.

I will change for that:

 inline void setSource(const QString& source)
 {
        int pathLen = source.lastIndexOf(QDir::separator());
        if (pathLen != -1)
        {
           _source = source;
           _sFile = QStringRef(&_source, pathLen + 1, _source.size() - pathLen - 1);
           _origPath = QStringRef(&_source, 0, pathLen);
        }
        else
        { // using ./source
            setSource(QString(".") + QDir::separator() + source);
        }
    }

> }
>
> That ways there's no possibility using _sFile or _origPath results in memory
> corruption however setSource() function is called.

187. By Carlos Jose Mazieri

ActionPaths::setSource(), forcing current path if using relative paths which is not supported.

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Arto Jalkanen (ajalkane) wrote :
Download full text (3.3 KiB)

> > > > 1)
> > > >
> > > > Why this, pathLen is local variable and never used after this:
> > > >
> > > > 32 + else
> > > > 33 + { //avoid bad index
> > > > 34 + pathLen = 0;
> > > > 35 + }
> > >
> > > This is a mistake, from some previous versions not presented here pathLen
> > was
> > > not local.
> > > I will remove this.
> > >
> > >
> > >
> > > > 2)
> > > >
> > > > This is a problematic function: inline void setSource(const QString&
> > source)
> > > >
> > > > Consider if it's been called like this:
> > > >
> > > > setSource("/path/to/file");
> > > > ...
> > > > setSource("aFile")
> > > >
> > > > Then we have QStringRef's that will not be valid and probably causes
> > > undefined
> > > > behaviour if I've understood QStringRef's documentation correctly.
> > >
> > > I understand that you are asking about relative paths, for absolute paths
> > like
> > > setSource("/path/to/file") it is supposed to work.
> > >
> > > If so, regarding relative paths it would not work, but it states in the
> > > beginning of the file:
> > > "Paths stored here are supposed to NOT be relative."
> > > In the current implementation it is only used with absolute paths.
> > > Let me know if it is acceptable.
> >
> > I'd ask that the function setSource() will set or reset _sFile and _origPath
> > variables whenever _source variable changes. While it may not be problem in
> > how it's currently used, it is a potentially hard to find error in future if
> > the implementation changes.
> >
> > So in the current merge request it could be something like:
> >
> > if (pathLen != -1)
> > {
> > _sFile = QStringRef(&_source, pathLen + 1, _source.size() - pathLen -
> > 1);
> > _origPath = QStringRef(&_source, 0, pathLen);
> > }
> > else
> > { //avoid bad index
> > pathLen = 0;
> > _sFile = QStringRef();
> > _origPath = QStringRef();
>
> Thinking a little more about it, it looks like the two lines above do not have
> any effect.
> Both "_sFile" and "_origPath" were already initialized with QStringRef default
> constructor.

There is no problem if setSource() function is only used from constructor. Then all is fine. But as it stands now, setSource is a public function and can be called after the object has been created. If you want to enforce it being called only in constructor, make it private and then no such issues that I brought up can happen.

But I see no problems with the changes you've done now, the QStringRef variables seem to be quaranteed to be always set again when _source changes. So I'm approving, and if you want to refine it further it can be another commit. Thanks!

>
> I will change for that:
>
> inline void setSource(const QString& source)
> {
> int pathLen = source.lastIndexOf(QDir::separator());
> if (pathLen != -1)
> {
> _source = source;
> _sFile = QStringRef(&_source, pathLen + 1, _source.size() -
> pathLen - 1);
> _origPath = QStringRef(&_source, 0, pathLen);
> }
> else
> { // using ./source
> setSource(QString(".") + QDir::separator() + source);
> }
> ...

Read more...

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/plugin/folderlistmodel/diriteminfo.h'
2--- src/plugin/folderlistmodel/diriteminfo.h 2014-05-01 17:38:23 +0000
3+++ src/plugin/folderlistmodel/diriteminfo.h 2014-05-18 18:09:25 +0000
4@@ -163,4 +163,84 @@
5
6
7
8+/*!
9+ * \brief The ActionPaths struct contains helper functions to do simple path handling
10+ *
11+ * Paths stored here are supposed to NOT be relative.
12+ *
13+ * It does not use any QFileInfo method, so it may work for any URL type
14+ */
15+struct ActionPaths
16+{
17+ public:
18+ ActionPaths() {}
19+ ActionPaths(const QString& source)
20+ {
21+ setSource(source);
22+ }
23+ inline void setSource(const QString& source)
24+ {
25+ int pathLen = source.lastIndexOf(QDir::separator());
26+ if (pathLen != -1)
27+ {
28+ _source = source;
29+ _sFile = QStringRef(&_source, pathLen + 1, _source.size() - pathLen - 1);
30+ _origPath = QStringRef(&_source, 0, pathLen);
31+ }
32+ else
33+ {
34+ //avoids possible memory corruption using relative paths, QStringRef would be empty/null
35+ //relative paths currently are not supported
36+ setSource(QString(".") + QDir::separator() + source);
37+ }
38+ }
39+ inline void setTargetPathOnly(const QString& path)
40+ {
41+ _targetPath = path;
42+ _target = path + QDir::separator();
43+ _target += _sFile;
44+ }
45+ inline void setTargetFullName(const QString& fullPathname)
46+ {
47+ _target = fullPathname;
48+ int lastSeparator = _target.lastIndexOf(QDir::separator());
49+ if (lastSeparator > 0)
50+ {
51+ _targetPath = _target.mid(0, lastSeparator);
52+ }
53+ }
54+ inline ActionPaths& operator=(const ActionPaths& other)
55+ {
56+ setSource(other._source);
57+ setTargetFullName(other._target);
58+ return *this;
59+ }
60+ inline bool areEquals() const {return _source == _target;}
61+ inline bool arePathsEquals() const
62+ {
63+ return QStringRef::compare(_origPath, _targetPath) == 0;
64+ }
65+ inline const QString& source() const {return _source;}
66+ inline const QString& target() const {return _target;}
67+ inline const QString& targetPath()const {return _targetPath;}
68+ inline const QStringRef& file() const {return _sFile;}
69+ inline int baseOrigSize() const {return _origPath.size();}
70+ inline void toggle()
71+ {
72+ QString savedSource(_source);
73+ setSource(_target);
74+ setTargetFullName(savedSource);
75+ }
76+ private:
77+ QString _source; //!< source full pathname
78+ QString _target; //!< target full pathname
79+ QString _targetPath; //!< target path only
80+ QStringRef _sFile; //!< source file name only
81+ QStringRef _origPath; //!< source path only
82+};
83+
84+Q_DECLARE_METATYPE(ActionPaths)
85+
86+typedef QList<ActionPaths> ActionPathList;
87+
88 #endif // DIRITEMINFO_H

Subscribers

People subscribed via source and target branches