bool ScImageCacheWriteAction::start()
{
	Q_ASSERT(!m_locked);
	if (m_locked)
	{
		scDebug() << "BUG: attempt to start action twice";
		return false;
	}
	if (!ScImageCacheManager::instance().acquireWriteLock())
	{
		scDebug() << "failed to acquire cache write lock";
		return false;
	}
	m_locked = true;
	return true;
}
bool ScImageCacheWriteAction::update(const QString & dir, ScLockedFile *p, AccessCounter & from, AccessCounter & to)
{
	AccessCounter counter = 0;
	if (p->exists() && !ScImageCacheDir::lastAccess(dir, counter))
	{
		scDebug() << "failed to read" << p->name() << "dir =" << dir;
		return false;
	}
	from = counter;
	counter++;
	if (!p->open())
	{
		scDebug() << "failed to open" << p->name() << "for writing";
		return false;
	}
	QTextStream out(p->io());
	out << counter;
	to = counter;
	return true;
}
bool ScImageCacheWriteAction::commit()
{
	bool rv = true;
	Q_ASSERT(m_locked);
	if (!m_locked)
	{
		scDebug() << "BUG: attempt to release non-locked cache write lock";
		return false;
	}
	ScImageCacheManager & scm = ScImageCacheManager::instance();
	foreach (QString file, m_files)
		scm.updateFile(file);
	for (FileMap::iterator i = m_access.begin(); i != m_access.end(); i++)
	{
		ScLockedFile *p = *i;
		AccessCounter from, to;
		if (update(i.key(), p, from, to))
		{
			if (p->commit())
			{
				scDebug() << "updated" << p->name() << "from" << from << "to" << to;
				scm.updateAccess(i.key(), from, to);
			}
			else
			{
				scDebug() << "failed to commit changes to" << p->name();
				rv = false;
			}
		}
		else
			rv = false;
	}
	if (!unlock())
		rv = false;
	clear();
	if (!m_haveMasterLock)
		scm.tryCleanup();
	return rv;
}
bool ScImageCacheWriteAction::unlock()
{
	bool rv = true;
	if (!m_haveMasterLock)
	{
		for (FileMap::iterator i = m_access.begin(); i != m_access.end(); i++)
		{
			ScLockedFile *p = *i;
			if (!p->unlock())
				rv = false;
		}
	}
	if (!ScImageCacheManager::instance().releaseWriteLock())
	{
		scDebug() << "failed to release cache write lock";
		rv = false;
	}
	m_locked = false;
	return rv;
}
bool ScImageCacheWriteAction::add(const QString & file)
{
	Q_ASSERT(m_locked);
	if (!m_locked)
	{
		scDebug() << "BUG: attempt to add" << file << "to action without start";
		return false;
	}

	QStringList dirs = file.split('/');
	QStringList dl;

	while (dirs.count() > 1)
	{
		dirs.removeLast();
		QString d = dirs.join("/");
		if (!m_access.contains(d))
			dl << d;
	}

	while (!dl.isEmpty())
	{
		QString d = dl.takeLast();
		ScLockedFile *p = new ScLockedFileRW(ScImageCacheManager::absolutePath(d) + "/" + ScImageCacheDir::accessFileName);
		Q_CHECK_PTR(p);
		if (!p)
			return false;
		if (!m_haveMasterLock && !p->lock())
			return false;
		m_access[d] = p;
	}

	m_files << file;

	return true;
}
Beispiel #6
0
sc_status
scParseCommandLineOptions(
	sc_session session, sc_uint argc, const char **argv, sc_uint expected, sc_option* options)
{
    int i, o;
    cl_bool *found = scAllocate(session, sizeof(sc_bool) * expected);
    if(!found)
    	return SC_OUT_OF_HOST_MEMORY;

	memset(found, SC_FALSE, sizeof(sc_bool) * expected);
	
    for (i = 0; i < argc && argv && argv[i]; i++) 
    {
		for(o = 0; o < expected; o++)
    	{
			if(found[o] == SC_TRUE)
    			continue;
    			
    		const char* start = argv[i];
    		if(start[0] != '-')
    			continue;

			const char* name = scGetOptionName(options[o], NULL);

			scDebug(session, "ceParseCommandLineOptions: Parsing '%s'\n", name);

    		const char* str = strstr(argv[i], name);
    		if(str && str - start <= 2)
    		{
				const char* assign = strstr(str, "=");
				if(assign)
				{
					scDebug(session, "ceParseCommandLineOptions: Parsed '%s' as '%s'\n", name, &assign[1]);
	        	    
	        	    ParseOption(&assign[1], options[o]);
	        	    
	        	    sc_value value = scGetOptionValue(options[o], NULL);
	        	    
					scDebug(session, "ceParseCommandLineOptions: Parsed '%s' as '%s' -> '%s' (%s)\n", name, argv[i], 
						scGetSymbolName(scCreateSymbolFromValue(session, value, NULL) ),
						scGetTypeString(scGetValueType(value, NULL), NULL));
					
					found[o] = SC_TRUE;
				}
				else if(i + 1 < argc && argv[i + 1])
				{
					scDebug(session, "ceParseCommandLineOptions: Parsed '%s' as '%s'\n", name, argv[i]);
	        	    
	        	    ParseOption(argv[i], options[o]);
					
					sc_value value = scGetOptionValue(options[o], NULL);
	        	    
					scDebug(session, "ceParseCommandLineOptions: Parsed '%s' as '%s' -> '%s' (%s)\n", name, argv[i], 
						scGetSymbolName(scCreateSymbolFromValue(session, value, NULL) ),
						scGetTypeString(scGetValueType(value, NULL), NULL));
					
					found[o] = SC_TRUE;
				}
				
				if(found[o] == SC_TRUE)
	        	    break;
    		}
    	}

		if(found[o] == SC_TRUE)
			continue;

    }

	scDeallocate(session, found);
    return SC_SUCCESS;
}