Exemplo n.º 1
0
static void _getDescriptorName(
	SG_context *pCtx,
	const _request_headers *pRequestHeaders,
	SG_string **ppRepoName)
{
	SG_pathname *pPathCwd = NULL;

	if (_startsWith("repos/", pRequestHeaders->pUri) && (strlen(pRequestHeaders->pUri) > 6))
	{
		const char *nameStart = pRequestHeaders->pUri + 6;
		const char *nameEnd = strchr(nameStart, '/');

		SG_ERR_CHECK(  SG_STRING__ALLOC(pCtx, ppRepoName)  );

		if (nameEnd == NULL)
			SG_ERR_CHECK(  SG_string__set__sz(pCtx, *ppRepoName, nameStart)  );
		else
			SG_ERR_CHECK(  SG_string__set__buf_len(pCtx, *ppRepoName, (const SG_byte *)nameStart,
												   (nameEnd - nameStart) * sizeof(char))  );
	}
	else
	{
		SG_ERR_CHECK(  SG_PATHNAME__ALLOC(pCtx, &pPathCwd)  );
		SG_ERR_CHECK(  SG_pathname__set__from_cwd(pCtx, pPathCwd)  );
		SG_ERR_CHECK(  SG_workingdir__find_mapping(pCtx, pPathCwd, NULL, ppRepoName, NULL)  );
	}

fail:
	SG_PATHNAME_NULLFREE(pCtx, pPathCwd);
}
bool UpdateIndexData::mightBeIndexed(const FieldRef& path) const {
    if (_allPathsIndexed) {
        return true;
    }

    FieldRef canonicalPath = getCanonicalIndexField(path);

    for (const auto& idx : _canonicalPaths) {
        if (_startsWith(canonicalPath, idx) || _startsWith(idx, canonicalPath))
            return true;
    }

    for (const auto& pathComponent : _pathComponents) {
        for (size_t partIdx = 0; partIdx < path.numParts(); ++partIdx) {
            if (pathComponent == path.getPart(partIdx)) {
                return true;
            }
        }
    }

    return false;
}
void events::run()
{
	m_running = true ;
	m_mtoto   = this ;

	connect( m_mtoto,SIGNAL( finished() ),m_main,SLOT( threadStopped() ) ) ;
	connect( m_mtoto,SIGNAL( finished() ),m_mtoto,SLOT( deleteLater() ) ) ;

	connect( this,SIGNAL( volumeMiniProperties( volumeEntryProperties * ) ),
		 m_babu,SLOT( autoMountVolume( volumeEntryProperties * ) ) ) ;
	connect( this,SIGNAL( volumeRemoved( QString ) ),
		 m_babu,SLOT( volumeRemoved( QString ) ) ) ;

	utility::fileHandle f( inotify_init() ) ;

	int fd = f.handle() ;

	if( fd == -1 ){

		return this->failedToStart() ;
	}

	int dev = inotify_add_watch( fd,"/dev",IN_CREATE|IN_DELETE ) ;
	int dm  = inotify_add_watch( fd,"/dev/mapper",IN_CREATE|IN_DELETE ) ;
	int md  = -1 ;

	if( utility::pathExists( "/dev/dm" ) ){

		md = inotify_add_watch( fd,"/dev/md",IN_DELETE ) ;
	}

	auto _allowed_device = []( const char * device ){

		/*
		 * dont care about these devices.
		 * /dev/sgX seem to be created when a usb device is plugged in
		 * /dev/dm-X are dm devices we dont care about since we will be dealing with them differently
		 */
		bool s = _startsWith( device,"sg" )     ||
			 _startsWith( device,"dm-" )    ||
			 _contains( device,"dev/tmp" )  ||
			 _contains( device,"dev-tmp" )  ||
			 _contains( device,".tmp.md." ) ||
			 _contains( device,"md/md-device-map" ) ;

		return s == false ;
	} ;

	auto _device_action = [&]( const struct inotify_event * event ){

		/*
		 * /dev/md/ folder seem to be deleted when the last entry in it is removed and
		 * created before the first entry is added.To account for this,monitor for the
		 * folder creation to start monitoring its contents.
		 */

		if( event->wd == dev && event->mask & IN_CREATE ){

			if( _stringsAreEqual( "md",event->name ) ){

				md = inotify_add_watch( fd,"/dev/md",IN_DELETE ) ;

				return false ;
			}
		}

		if( event->wd == dev && event->mask & IN_DELETE ){

			if( _stringsAreEqual( "md",event->name ) ){

				inotify_rm_watch( md,dev ) ;

				return false ;
			}
		}

		return true ;
	} ;

	const char * currentEvent ;
	const char * end ;

	constexpr int BUFF_SIZE = 4096 ;
	char buffer[ BUFF_SIZE ] ;

	fd_set rfds ;

	FD_ZERO( &rfds ) ;

	int select_fd = fd + 1 ;

	auto _eventsReceived = [ & ](){

		/*
		 * we are blocking on select() and not on read() because QThread->terminate() does not seem to
		 * be able to get out of a blocked read() on certain Qt versions.
		 */

		auto _gotEvents = [ & ](){

			FD_SET( fd,&rfds ) ;

			return select( select_fd,&rfds,nullptr,nullptr,nullptr ) > 0 ;
		} ;

		if( _gotEvents() ){

			auto s = read( fd,buffer,BUFF_SIZE ) ;

			if( s > 0 ){

				end          = buffer + s ;
				currentEvent = buffer ;

				return true ;
			}
		}

		return false ;
	} ;

	auto _processEvent = [ & ]( const struct inotify_event * event ){

		if( _device_action( event ) && _allowed_device( event->name ) ){

			auto _device = [ & ](){

				if( event->wd == dm ){

					return zuluMountTask::devices::dm_device ;

				}else if( event->wd == md ){

					return zuluMountTask::devices::md_device ;

				}else{
					return zuluMountTask::devices::device ;
				}
			} ;

			zuluMountTask::event e{ _device(),event->mask == IN_CREATE,event->name } ;

			Task::exec( [ this,e ](){

				auto r = zuluMountTask::deviceProperties( e ) ;

				if( r.volumeRemoved ){

					emit volumeRemoved( r.volumeName ) ;
				}else{
					emit volumeMiniProperties( r.entry ) ;
				}
			} ) ;
		}
	} ;

	#define _cast( x ) reinterpret_cast< const struct inotify_event * >( currentEvent )
	#define _eventSize sizeof( struct inotify_event )

	while( true ){

		if( _eventsReceived() ){

			while( currentEvent < end ){

				auto event = _cast( currentEvent ) ;

				if( event ){

					_processEvent( event ) ;
					currentEvent += _eventSize + event->len ;
				}else{
					currentEvent += _eventSize ;
				}
			}
		}
	}
}