Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix item deleted between browse and stat #70

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/sequenceParser/ItemStat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ItemStat::ItemStat( const Item& item, const bool approximative )
, maxSize(0)
, realSize(0)
, sizeOnDisk(0)
, accessTime(0)
, accessTime(-1)
, modificationTime(-1)
, lastChangeTime(-1)
, ownerCanRead(false)
Expand Down Expand Up @@ -62,7 +62,7 @@ ItemStat::ItemStat( const Item& item, const bool approximative )
statSequence( item, approximative );
break;
}
case eTypeUndefined:
default:
break;
}
}
Expand Down Expand Up @@ -101,7 +101,11 @@ void ItemStat::setGroupName()
void ItemStat::statLink( const boost::filesystem::path& path )
{
boost::system::error_code errorCode;
modificationTime = bfs::last_write_time(path, errorCode);
const long long last_write_time = bfs::last_write_time(path, errorCode);
if(errorCode == boost::system::errc::success)
{
modificationTime = last_write_time;
}

#ifdef __UNIX__
struct stat statInfos;
Expand Down Expand Up @@ -132,11 +136,13 @@ void ItemStat::statLink( const boost::filesystem::path& path )

void ItemStat::statFolder( const boost::filesystem::path& path )
{
using namespace boost::filesystem;
boost::system::error_code errorCode;

fullNbHardLinks = nbHardLinks = bfs::hard_link_count( path, errorCode );
modificationTime = bfs::last_write_time( path, errorCode );
const size_t hard_link_count = bfs::hard_link_count(path, errorCode);
if(errorCode == boost::system::errc::success)
{
fullNbHardLinks = nbHardLinks = hard_link_count;
modificationTime = bfs::last_write_time(path, errorCode);
}

#ifdef __UNIX__
struct stat statInfos;
Expand Down Expand Up @@ -165,13 +171,16 @@ void ItemStat::statFolder( const boost::filesystem::path& path )

void ItemStat::statFile( const boost::filesystem::path& path )
{
using namespace boost::filesystem;
boost::system::error_code errorCode;
fullNbHardLinks = nbHardLinks = bfs::hard_link_count( path, errorCode );
size = bfs::file_size( path, errorCode );
minSize = size;
maxSize = size;
modificationTime = bfs::last_write_time( path, errorCode );
const size_t hard_link_count = bfs::hard_link_count(path, errorCode);
if(errorCode == boost::system::errc::success)
{
fullNbHardLinks = nbHardLinks = hard_link_count;
size = bfs::file_size(path, errorCode);
minSize = size;
maxSize = size;
modificationTime = bfs::last_write_time(path, errorCode);
}

#ifdef __UNIX__
struct stat statInfos;
Expand All @@ -198,9 +207,6 @@ void ItemStat::statFile( const boost::filesystem::path& path )

void ItemStat::statSequence( const Item& item, const bool approximative )
{
using namespace boost::filesystem;
using namespace sequenceParser;

#ifdef __UNIX__
struct stat statInfos;
const int statStatus = lstat(item.getAbsoluteFirstFilename().c_str(), &statInfos);
Expand Down
152 changes: 121 additions & 31 deletions test/pyTest/testStat.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,43 @@ def checkCommonParameters(itemStat):
assert_equals(itemStat.otherCanExecute, False)


def checkUnsetItemStat(itemStat):
"""
Check attributes of the given itemStat that should not be set.
"""
# user and group
assert_equals(itemStat.userName, "unknown")
assert_equals(itemStat.groupName, "unknown")
# check id
assert_equals(itemStat.deviceId, 0)
assert_equals(itemStat.inodeId, 0)
assert_equals(itemStat.userId, 0)
assert_equals(itemStat.groupId, 0)
# nb hard links
assert_equals(itemStat.nbHardLinks, 0)
assert_equals(itemStat.fullNbHardLinks, 0)
# check size
assert_equals(itemStat.size, 0)
assert_equals(itemStat.minSize, 0)
assert_equals(itemStat.maxSize, 0)
assert_equals(itemStat.realSize, 0)
assert_equals(itemStat.sizeOnDisk, 0)
# time
assert_equals(itemStat.accessTime, -1)
assert_equals(itemStat.modificationTime, -1)
assert_equals(itemStat.lastChangeTime, -1)
# permissions
assert_equals(itemStat.ownerCanRead, False)
assert_equals(itemStat.ownerCanWrite, False)
assert_equals(itemStat.ownerCanExecute, False)
assert_equals(itemStat.groupCanRead, False)
assert_equals(itemStat.groupCanWrite, False)
assert_equals(itemStat.groupCanExecute, False)
assert_equals(itemStat.otherCanRead, False)
assert_equals(itemStat.otherCanWrite, False)
assert_equals(itemStat.otherCanExecute, False)


def testFileStat():
"""
Check stats of a single file.
Expand All @@ -85,6 +122,25 @@ def testFileStat():
assert_less_equal(itemStat.lastChangeTime, currentTime)


def testFileDeleted():
"""
Check stats of a file which is deleted between the browse and the stat.
"""
# create new elements
fileToDelete = "fileToDelete.txt"
createFile(root_path, fileToDelete)
# browse the all directory
items = seq.browse(root_path)
# remove the last file created and get the stats of this file
itemStat = None
for item in items:
if item.getFilename() == fileToDelete:
os.remove(os.path.join(root_path, fileToDelete))
itemStat = seq.ItemStat(item)
break
checkUnsetItemStat(itemStat)


def testSymLinkStat():
"""
Check stats of a folder.
Expand Down Expand Up @@ -119,6 +175,25 @@ def testSymLinkStat():
assert_equals(itemStat.otherCanExecute, True)


def testSymLinkDeleted():
"""
Check stats of a symbolic link which is deleted between the browse and the stat.
"""
# create new symbolic link
linkToDelete = "linkToDelete.txt"
createSymLink(root_path, "plop.txt", linkToDelete)
# browse the all directory
items = seq.browse(root_path)
# remove the last file created and get the stats of this symbolic link
itemStat = None
for item in items:
if item.getFilename() == linkToDelete:
os.remove(os.path.join(root_path, linkToDelete))
itemStat = seq.ItemStat(item)
break
checkUnsetItemStat(itemStat)


def testFolderStat():
"""
Check stats of a folder.
Expand All @@ -138,6 +213,25 @@ def testFolderStat():
assert_greater_equal(itemStat.sizeOnDisk, itemStat.size)


def testFolderDeleted():
"""
Check stats of a folder which is deleted between the browse and the stat.
"""
# create new folder
folderToDelete = "folderToDelete"
createFolder(root_path, folderToDelete)
# browse the all directory
items = seq.browse(root_path)
# remove the last file created and get the stats of this folder
itemStat = None
for item in items:
if item.getFilename() == folderToDelete:
shutil.rmtree(os.path.join(root_path, folderToDelete))
itemStat = seq.ItemStat(item)
break
checkUnsetItemStat(itemStat)


def testSequenceStat():
"""
Check stats of a sequence.
Expand All @@ -158,40 +252,36 @@ def testSequenceStat():
assert_greater_equal(itemStat.sizeOnDisk, itemStat.size)


def testSequenceDeleted():
"""
Check stats of a sequence which is deleted between the browse and the stat.
"""
# create new sequence
files_to_create = [
"bar.001.jpg",
"bar.002.jpg",
"bar.003.jpg",
]
for f in files_to_create:
createFile(root_path, f)
# browse the all directory
items = seq.browse(root_path)
# remove the last file created and get the stats of this symbolic link
itemStat = None
for item in items:
if item.getFilename() == "bar.###.jpg":
os.remove(os.path.join(root_path, "bar.001.jpg"))
os.remove(os.path.join(root_path, "bar.002.jpg"))
os.remove(os.path.join(root_path, "bar.003.jpg"))
itemStat = seq.ItemStat(item)
break
checkUnsetItemStat(itemStat)


def testUndefinedStat():
"""
Check stats of a file which is tagged as undefined.
"""
itemFile = seq.Item(seq.eTypeUndefined, os.path.join(root_path, "plop.txt"))
itemStat = seq.ItemStat(itemFile)
# user and group
assert_equals(itemStat.userName, "unknown")
assert_equals(itemStat.groupName, "unknown")
# check id
assert_equals(itemStat.deviceId, 0)
assert_equals(itemStat.inodeId, 0)
assert_equals(itemStat.userId, 0)
assert_equals(itemStat.groupId, 0)
# nb hard links
assert_equals(itemStat.nbHardLinks, 0)
assert_equals(itemStat.fullNbHardLinks, 0)
# check size
assert_equals(itemStat.size, 0)
assert_equals(itemStat.minSize, 0)
assert_equals(itemStat.maxSize, 0)
assert_equals(itemStat.realSize, 0)
assert_equals(itemStat.sizeOnDisk, 0)
# time
assert_equals(itemStat.accessTime, 0)
assert_equals(itemStat.modificationTime, -1)
assert_equals(itemStat.lastChangeTime, -1)
# permissions
assert_equals(itemStat.ownerCanRead, False)
assert_equals(itemStat.ownerCanWrite, False)
assert_equals(itemStat.ownerCanExecute, False)
assert_equals(itemStat.groupCanRead, False)
assert_equals(itemStat.groupCanWrite, False)
assert_equals(itemStat.groupCanExecute, False)
assert_equals(itemStat.otherCanRead, False)
assert_equals(itemStat.otherCanWrite, False)
assert_equals(itemStat.otherCanExecute, False)
checkUnsetItemStat(itemStat)