Beispiel #1
0
int Rename( struct File *s, const char *path, const char *nname )
{
	DEBUG("Rename %s to %s\n", path, nname );
	
	if( s != NULL )
	{
		SpecialData *sd = (SpecialData *)s->f_SpecialData;
	
		char command[ 1024 ];	// maybe we should count that...
		
		sprintf( command, "php \"modules/system/module.php\" \"module=files&command=dosaction&action=rename&sessionid=%s&path=%s&newname=%s\";",
			s->f_SessionID, path, nname
		);
		
		int answerLength = 0;
		ListString *result = PHPCall( command, &answerLength );
		
		// TODO: we should parse result to get information about success
		if( result ) ListStringDelete( result );
		
		// Success
		return 1;
	}
	return -1;
}
Beispiel #2
0
int UnMount( struct FHandler *s, void *f )
{
	if( f != NULL )
	{
		DEBUG("[fsysphp] Unmount filesystem\n");
		File *lf = (File*)f;
		
		if( lf->f_SpecialData )
		{
			SpecialData *sd = (SpecialData *)lf->f_SpecialData;
		
			char command[ 2048 ];	// maybe we should count that...

			sprintf( command, "php \"modules/system/module.php\" \"command=unmount&devname=%s&module=%s&sessionid=%s\";",
				lf->f_Name,  sd->module, lf->f_SessionID
			);
		
			int answerLength;
			ListString *result = PHPCall( command, &answerLength );
			if( result && result->ls_Size >= 0 )
			{
				if( strncmp( result->ls_Data, "fail", 4 ) == 0 )
				{
					DEBUG( "[fsysphp] Failed to unmount device %s..\n", lf->f_Name );
				}
			}
			else
			{
				DEBUG( "[fsysphp] Unknown error unmounting device %s..\n", lf->f_Name );
			}
		
			// we should parse result to get information about success
		
			if( result ) ListStringDelete( result );
			
			// Free up active device information
			if( sd->module ) FFree( sd->module );
			if( lf->f_SessionID ) FFree( lf->f_SessionID );
			if( sd->type ) FFree( sd->type );
			FFree( lf->f_SpecialData );
		}
		
		// Free up active device information
		if( lf->f_Name ){ FFree( lf->f_Name ); }
		if( lf->f_Path ){ FFree( lf->f_Path ); }

	}
	return 0;
}
Beispiel #3
0
BufString *Dir( File *s, const char *path )
{
	char *comm = NULL;
	
	if( ( comm = calloc( strlen( path ) +512, sizeof(char) ) ) != NULL )
	{
		strcpy( comm, s->f_Name );
		strcat( comm, ":" );
		
		if( path != NULL )
		{
			strcat( comm, path ); 
		}
		
		if( s != NULL )
		{
			SpecialData *sd = (SpecialData *)s->f_SpecialData;
	
			char command[ 1024 ];	// maybe we should count that...
		
			sprintf( command, "php \"modules/system/module.php\" \"type=%s&module=files&args=false&command=directory&authkey=false&sessionid=%s&path=%s&subPath=\";",
				sd->type, s->f_SessionID, comm//path
			);
		
			int answerLength;
			BufString *bs  = NULL;
			ListString *result = PHPCall( command, &answerLength );
			if( result != NULL )
			{
				bs =BufStringNewSize( result->ls_Size );
				if( bs != NULL )
				{
					BufStringAddSize( bs, result->ls_Data, result->ls_Size );
				}
				ListStringDelete( result );
			}
		
			// we should parse result to get information about success
	
			free( comm );
			return bs;
		}
		
		free( comm );
	}
	return NULL;
}
Beispiel #4
0
int Rename( struct File *s, const char *path, const char *nname )
{
	DEBUG("[fsysphp] Rename %s to %s\n", path, nname );
	
	char *comm = NULL;
	
	if( ( comm = calloc( strlen( path ) +512, sizeof(char) ) ) != NULL )
	{
		strcpy( comm, s->f_Name );
		strcat( comm, ":" );
		
		if( path != NULL )
		{
			strcat( comm, path ); 
		}
		
		if( s != NULL )
		{
			SpecialData *sd = (SpecialData *)s->f_SpecialData;
	
			char command[ 1024 ];	// maybe we should count that...
		
			sprintf( command, "php \"modules/system/module.php\" \"module=files&command=dosaction&action=rename&sessionid=%s&path=%s&newname=%s\";",
				s->f_SessionID, comm, nname
			);
		
			int answerLength = 0;
			ListString *result = PHPCall( command, &answerLength );
		
			// TODO: we should parse result to get information about success
			if( result ) ListStringDelete( result );
		
			free( comm );
		}
		free( comm );
	}
	else
	{
		return -1;
	}
	return 1;
}
Beispiel #5
0
int MakeDir( struct File *f, const char *path )
{
	DEBUG("makedir filesystem\n");
	if( f != NULL && f->f_SpecialData != NULL )
	{
		SpecialData *sd = (SpecialData *)f->f_SpecialData;
	
		char command[ 2048 ];	// maybe we should count that...
		
		///system.library/module?module=files&command=dosaction&action=makedir&sessionid=2ffg3987t093843948&path=some:directory/newdir
		
		sprintf( command, "php \"modules/system/module.php\" \"module=files&command=dosaction&action=makedir&sessionid=%s&path=%s\";",
			f->f_SessionID, path
		);
		
		DEBUG("MAKEDIR %s\n", command );
	
		int answerLength = 0;
		
		ListString *result = PHPCall( command, &answerLength );
		
		if( result && result->ls_Size >= 0 )
		{
			if( strncmp( result->ls_Data, "fail", 4 ) == 0 )
			{
				ERROR( "phpfs: Failed to execute makedir on device %s..\n", f->f_Name );
			}
		}
		else
		{
			ERROR( "phpfs: Unknown error unmounting device %s..\n", f->f_Name );
		}
	
		// TODO: we should parse result to get information about success
		if( result ) ListStringDelete( result );
	}

	return -1;
}
Beispiel #6
0
BufString *Dir( File *s, const char *path )
{
	if( s != NULL )
	{
		SpecialData *sd = (SpecialData *)s->f_SpecialData;
	
		char command[ 1024 ];	// maybe we should count that...
		
		sprintf( command, "php \"modules/system/module.php\" \"type=%s&module=files&args=false&command=directory&authkey=false&sessionid=%s&path=%s&subPath=\";",
			sd->type, s->f_SessionID, path
		);
		
		int answerLength;
		ListString *result = PHPCall( command, &answerLength );
		BufString *bs =BufStringNewSize( result->ls_Size );
		BufStringAddSize( bs, result->ls_Data, result->ls_Size );
		ListStringDelete( result );
		
		// we should parse result to get information about success
	
		return bs;
	}
	return NULL;
}
Beispiel #7
0
extern inline Http *ProtocolHttp( Socket* sock, char* data, unsigned int length )
{
	Http *response = NULL;
	DEBUG("HTTP Callback called\n");
	
	if( length <= 0 )
	{
		DEBUG("RESULT<0 http400\n");
		
		struct TagItem tags[] = {
			{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicateN( "close", 5 ) },
			{TAG_DONE, TAG_DONE}
		};
		
		response = HttpNewSimple( HTTP_400_BAD_REQUEST,  tags );
	
		//HttpWriteAndFree( response );
	}
	
	// Get the current request we're working on, or start a new one
	Http* request = (Http*)sock->data;
	if( !request )
	{
		request = HttpNew( sock );
		request->timestamp = time( NULL );
		sock->data = (void*)request;
	}
	
	//DEBUG("Checking timeout, data %s\n", data );

	//DEBUG("time %ld\nreqtimestamp %ld\nreqtimestamp %ld\n",
	//	  time( NULL ), request->timestamp, HTTP_REQUEST_TIMEOUT );
	// Timeout
	if( time( NULL ) > request->timestamp + HTTP_REQUEST_TIMEOUT )
	{
		struct TagItem tags[] = {
			{ HTTP_HEADER_CONTENT_TYPE, (ULONG)  StringDuplicate( "text/plain" ) },
			{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
			{TAG_DONE, TAG_DONE}
		};
		
		response = HttpNewSimple( HTTP_408_REQUEST_TIME_OUT,  tags );
		
		HttpAddTextContent( response, "408 Request Timeout\n" );
		//HttpWriteAndFree( response );
		HttpFreeRequest( request );
		sock->data = NULL;
		DEBUG("HTTP TIMER\n");
		return response;
	}
	

	// Continue parsing the request
	int result = HttpParsePartialRequest( request, data, length );
	
	// Protocol error
	if( result < 0 )
	{
		DEBUG("RESULT<0 http400\n");
		struct TagItem tags[] = {
			{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
			{TAG_DONE, TAG_DONE}
		};
		
		response = HttpNewSimple( HTTP_400_BAD_REQUEST,  tags );
	
		//HttpWriteAndFree( response );
	}
	// Request not fully parsed yet. Return and wait for more data
	else if( result == -1 )
	{
		DEBUG( " <- (%d): Waiting for more data\n", sock->fd );
		HttpFreeRequest( request );
		return response;
	}
	// Request parsed without errors!
	else if( result == 1 )
	{
		Uri* uri = request->uri;
		
		// Disallow proxy requests
		if( uri && ( uri->scheme || uri->authority ) )
		{
			struct TagItem tags[] = {
				{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
				{TAG_DONE, TAG_DONE}
			};
		
			response = HttpNewSimple( HTTP_403_FORBIDDEN,  tags );
	
			result = 403;
		}

		// Cross-domain requests uses a pre-flight OPTIONS call
		if( !request->errorCode && request->method && strcmp( request->method, "OPTIONS" ) == 0 )
		{
			struct TagItem tags[] = {
				{ HTTP_HEADER_CONTROL_ALLOW_ORIGIN, (ULONG)StringDuplicateN( "*", 1 ) },
				{ HTTP_HEADER_CONTROL_ALLOW_HEADERS, (ULONG)StringDuplicateN( "Origin, X-Requested-With, Content-Type, Accept, Method", 54 ) },
				{ HTTP_HEADER_CONTROL_ALLOW_METHODS,  (ULONG)StringDuplicateN( "GET, POST, OPTIONS", 18 ) },
				{ HTTP_HEADER_CONNECTION, (ULONG)StringDuplicateN( "close", 5 ) },
				{TAG_DONE, TAG_DONE}
			};
		
			if( response != NULL ) ERROR("Response != NULL\n");
			response = HttpNewSimple( HTTP_200_OK,  tags );

			result = 200;
		}		
		// Check for connection upgrade
		else if( !request->errorCode && HttpHeaderContains( request, "connection", "Upgrade", false ) )
		{
			struct TagItem tags[] = {
				{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
				{TAG_DONE, TAG_DONE}
			};
		
			response = HttpNewSimple(  HTTP_400_BAD_REQUEST, tags );
	
		}
		else
		{
			Path* path = NULL;
			if( uri->path->raw )
			{
				int nlen = 0;
				for( ; ; nlen++ )
				{
					if( !uri->path->raw[nlen] )
						break;
				}
				DEBUG("Want to parse path: %s (%d)\n", uri->path->raw, nlen );
				path = PathNew( uri->path->raw );
				if( path ) PathResolve( path );  // Resolve checks for "../"'s, and removes as many as it can.
			}
			if( !path || !path->resolved ) // If it cannot remove all, path->resolved == false.
			{
				DEBUG( "We have no path..\n" );
				struct TagItem tags[] = {
					{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
					{TAG_DONE, TAG_DONE}
				};
		
				response = HttpNewSimple( HTTP_403_FORBIDDEN,  tags );
	
				result = 403;
			}
			else
			{
				DEBUG( "We got through. %s\n", path->parts[ 0 ] );
				
				if( path->size >= 2 && StringCheckExtension( path->parts[0], "library" ) == 0 )
				{
					// system.library is main library and should be use for most things
					// we open it and close in main
					//DEBUG("systemlib found\n");
					DEBUG("Calling systemlib\n");
					
					if( strcmp( path->parts[ 0 ], "system.library" ) == 0 )
					{
						DEBUG( "%s\n", path->parts[1] );
						response = SLIB->SysWebRequest( SLIB, &(path->parts[1]), request );
						
						if( response == NULL )
						{
							struct TagItem tags[] = {
								{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
								{TAG_DONE, TAG_DONE}
							};	
		
							response = HttpNewSimple(  HTTP_500_INTERNAL_SERVER_ERROR,  tags );
	
							result = 500;
						}
					}
					else
					{
						FriendCoreInstance_t *fci = (FriendCoreInstance_t *) sock->s_Data;
						Library* lib = FriendCoreGetLibrary( fci, path->parts[0], 1 );
						if( lib && lib->WebRequest )
						{
							response =(Http *) lib->WebRequest( lib, path->parts[1], request );
							if( response == NULL )
							{
								struct TagItem tags[] = {
									{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
									{TAG_DONE, TAG_DONE}
								};	
		
								response = HttpNewSimple( HTTP_500_INTERNAL_SERVER_ERROR,  tags );
	
								result = 500;
							}
						}
						else
						{
							struct TagItem tags[] = {
								{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
								{TAG_DONE, TAG_DONE}
							};	
		
							response = HttpNewSimple( HTTP_404_NOT_FOUND,  tags );
	
							result = 404;
						}
					}
				}
				// We're calling on a static file.
				else
				{
					//DEBUG("Getting resources\n");
					// Read the file
					Path *base = PathNew( "resources" );
					Path* complete = PathJoin( base, path );
					BOOL freeFile = FALSE;
					
					LocFile* file = CacheManagerFileGet( SLIB->cm, complete->raw );
					if( file == NULL )
					{
						file = LocFileNew( complete->raw, FILE_READ_NOW | FILE_CACHEABLE );
						if( file != NULL )
						{
							if( CacheManagerFilePut( SLIB->cm, file ) != 0 )
							{
								freeFile = TRUE;
							}
						}
					}

					// Send reply
					if( file != NULL )
					{
						char* mime = NULL;
						
						if(  file->buffer == NULL )
						{
							ERROR("File is empty %s\n", complete->raw );
						}

						if( complete->extension )
						{
							mime = StringDuplicate( MimeFromExtension( complete->extension ) );
						}
						else
						{
							mime = StringDuplicate( "text/plain" );
						}
						
						struct TagItem tags[] = {
							{ HTTP_HEADER_CONTENT_TYPE, (ULONG)  mime },
							{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
							{TAG_DONE, TAG_DONE}
						};
		
						response = HttpNewSimple( HTTP_200_OK, tags );

						
						//DEBUG("Before returning data\n");
						
						HttpSetContent( response, file->buffer, file->bufferSize );
						
						// write here and set data to NULL!!!!!
						// retusn response
						HttpWrite( response, sock );
						result = 200;
						
						INFO("--------------------------------------------------------------%d\n", freeFile );
						if( freeFile == TRUE )
						{
							//ERROR("\n\n\n\nFREEEEEEFILE\n");
							LocFileFree( file );
						}
						response->content = NULL;
						response->sizeOfContent = 0;
						
						response->h_WriteType = FREE_ONLY;
					}
					else
					{
						DEBUG( "[ProtocolHttp] Going ahead with %s.\n", path->parts ? path->parts[0] : "No path part.." );
						
						// Try to fall back on module
						// TODO: Make this behaviour configurable
						char command[255];
						sprintf( command, "php \"php/catch_all.php\" \"%s\";", uri->path->raw ); 
						DEBUG( "[ProtocolHttp] Executing %s\n", command );
						ListString *bs = RunPHPScript( command );
						int phpRun = FALSE;
						if( bs )
						{
							if( bs->ls_Size > 0 )
							{
								struct TagItem tags[] = {
									{ HTTP_HEADER_CONTENT_TYPE, (ULONG)  StringDuplicate( "text/html" ) },
									{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
									{TAG_DONE, TAG_DONE}
								};
		
								response = HttpNewSimple(  HTTP_200_OK,  tags );
						
								HttpSetContent( response, bs->ls_Data, bs->ls_Size );
								
								result = 200;
								phpRun = TRUE;
								bs->ls_Data = NULL;
							}
							ListStringDelete( bs );
						}
						
						if( !phpRun )
						{
							DEBUG("File do not exist\n");
							
							struct TagItem tags[] = {
								{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
								{TAG_DONE, TAG_DONE}
							};	
		
							response = HttpNewSimple( HTTP_404_NOT_FOUND,  tags );
	
							result = 404;
						}
					}
					
					PathFree( base );
					PathFree( complete );
				}
			}
			PathFree( path );
		}

		// SPRING CLEANING!!! TIME TO CLEAN THE CASTLE!!! :) :) :)
		HttpFreeRequest( request );
		
		if( result != 101 )
		{
			sock->data = NULL;
		}
		return response;
	}
	// Winter cleaning
	HttpFreeRequest( request );
	return response;
}
Beispiel #8
0
int MakeDir( struct File *f, const char *path )
{
	DEBUG("[fsysphp] makedir filesystem\n");
	if( f != NULL && f->f_SpecialData != NULL )
	{
		char *comm = NULL;
	
		// If we don't have a path or we have a path without colon, allocate
		// for a base path 
		if( strstr( path, ":" ) == NULL || !path )
		{
			comm = calloc( ( path ? strlen( path ) : 0 ) + 512, sizeof(char) );
			if( comm == NULL ) return -1;
		}
		// If we have comm or a path
		if( comm || path )
		{
			// If we have a comm
			if( comm )
			{
				strcpy( comm, f->f_Name );
				strcat( comm, ":" );
			}
			// A path and a comm, concat
			if( path != NULL && comm )
				strcat( comm, path ); 
				
			// No comm, just use path!
			if( !comm ) comm = StringDuplicate( path );
		
			SpecialData *sd = (SpecialData *)f->f_SpecialData;
	
			char command[ 2048 ];	// maybe we should count that...
		
			sprintf( command, "php \"modules/system/module.php\" \"module=files&command=dosaction&action=makedir&sessionid=%s&path=%s\";",
				f->f_SessionID, comm
			);
		
			DEBUG("[fsysphp] MAKEDIR %s\n", command );
	
			int answerLength = 0;
		
			ListString *result = PHPCall( command, &answerLength );
		
			if( result && result->ls_Size >= 0 )
			{
				if( strncmp( result->ls_Data, "fail", 4 ) == 0 )
				{
					ERROR( "[fsysphp] phpfs: Failed to execute makedir on device %s..\n", f->f_Name );
				}
			}
			else
			{
				ERROR( "[fsysphp] Unknown error unmounting device %s..\n", f->f_Name );
			}
	
			// TODO: we should parse result to get information about success
			if( result ) ListStringDelete( result );
		}
	}
	else
	{
		return -1;
	}
	return 1;
}
Beispiel #9
0
int FileClose( struct File *s, void *fp )
{
	if( fp != NULL )
	{
		int close = 0;
		
		File *lfp = ( File *)fp;
		
		if( lfp->f_SpecialData )
		{
			SpecialData *sd = ( SpecialData *)lfp->f_SpecialData;
			
			if( sd->fp )
			{
				close = fclose( ( FILE *)sd->fp );
				sd->fp = NULL;
			}
			
			DEBUG("[fsysphp] CLOSE, file path %s\n", sd->fname );
			
			if( sd->mode == MODE_READ )
			{
				remove( sd->fname );
			}
			else if( sd->mode == MODE_WRITE )
			{
				
				//Write

				//system.library/module?module=files&command=write&sessionid=87b6y897vg6897&path=some:directory/file.txt&data=data
				SpecialData *globalsd = (SpecialData *)s->f_SpecialData;
				
				char command[ 2048 ];
				
				if( PathHasColon( sd->path ) )
				{
					sprintf( command, "php \"modules/system/module.php\" \"module=files&command=write&sessionid=%s&path=%s&tmpfile=%s\";",
						lfp->f_SessionID, sd->path, sd->fname );
				}
				else
				{
					sprintf( command, "php \"modules/system/module.php\" \"module=files&command=write&sessionid=%s&path=%s:%s&tmpfile=%s\";",
						lfp->f_SessionID, s->f_Name, sd->path, sd->fname
					);
				}
				//INFO("Call write command %s\n", command );
				//INFO("\nSDPATH %s\nlf main name %s\n\n", sd->path, s->f_Name ); 
	
				int answerLength = 0;
		
				ListString *result = PHPCall( command, &answerLength );
				if( result != NULL )
				{
					if( result->ls_Data && result->ls_Size > 0 )
					{
						DEBUG("[fsysphp] CHECK PHPCLOSE %.*s\n", result->ls_Size, result->ls_Data );
					}
					ListStringDelete( result );
				}
				
				remove( sd->fname );
			}
			
			FFree( sd->fname );
			FFree( sd->path );
			
			FFree( lfp->f_SpecialData );
		}
		
		if( lfp->f_Path ) FFree( lfp->f_Path );
		if( lfp->f_SessionID ) FFree( lfp->f_SessionID );
		if( lfp->f_Buffer ) FFree( lfp->f_Buffer );
		
		// And the structure
		FFree( lfp );
		
		DEBUG( "[fsysphp] FileClose: Closing file pointer.\n" );
		
		return close;
	}
	
	return 0;
}
Beispiel #10
0
void *FileOpen( struct File *s, const char *path, char *mode )
{
	
	//Read

	//system.library/module?module=files&command=read&sessionid=78346h89379376&path=some:directory/file.txt
	
	SpecialData *sd = (SpecialData *)s->f_SpecialData;
	
	char *comm = NULL;
	
	if( ( comm = calloc( strlen( path ) +512, sizeof(char) ) ) != NULL )
	{
		strcpy( comm, s->f_Name );
		strcat( comm, ":" );
		
		if( path != NULL )
		{
			strcat( comm, path ); 
		}
		
		if( sd != NULL )
		{
			if( mode[0] == 'r' || strcmp( mode, "rb" ) == 0 )
			{
				char tmpfilename[ 712 ];
				int lockf;
			
				// Make sure we can make the tmp file unique with lock!
				int retries = 100;
				do
				{
					sprintf( tmpfilename, "/tmp/%s_read_%d%d%d%d", s->f_SessionID, rand()%9999, rand()%9999, rand()%9999, rand()%9999 );
					DEBUG( "[fsysphp] Trying to lock %s\n", tmpfilename );
					if( ( lockf = open( tmpfilename, O_CREAT|O_EXCL|O_RDWR ) ) >= 0 )
					{
						break;
					}
					unlink( tmpfilename );
					// Failed.. bailing
					if( retries-- <= 0 )
					{
						ERROR( "[fsysphp] [FileOpen] Failed to get exclusive lock on lockfile.\n" );
						return NULL;
					}
				}
				while( TRUE );
			
				DEBUG( "[fsysphp] Success in locking %s\n", tmpfilename );
			
				// Open the tmp file and get a file lock!
			
				// Get the data
				char command[ 1024 ];	// maybe we should count that...

				sprintf( command, 
					"php \"modules/system/module.php\" \"type=%s&module=files&args=false&command=read&authkey=false&sessionid=%s&path=%s&mode=%s\";",
					sd->type, s->f_SessionID, comm, mode
				);
		
				DEBUG( "[fsysphp] Getting data for tempfile, seen below as command:\n" );
				DEBUG( "[fsysphp] %s\n", command );
			
				int answerLength = 0;			
				ListString *result = PHPCall( command, &answerLength );
			
				// Open a file pointer
				if( result && result->ls_Data )
				{
					// Write the buffer to the file
					int written = write( lockf, ( void *)result->ls_Data, result->ls_Size );
				
					// Clean out result
					ListStringDelete( result ); result = NULL;
		
					// Remove lock!
					FILE *locfp = NULL;
					fcntl( lockf, F_SETLKW, F_UNLCK );
					fchmod( lockf, 0755 );
					close( lockf ); lockf = -1;
				
					if( ( locfp = fopen( tmpfilename, mode ) ) != NULL )
					{
						// Flick the lock off!
						fseek ( locfp, 0, SEEK_SET );
					
						// Ready the file structure
						File *locfil = NULL;
						if( ( locfil = FCalloc( 1, sizeof( File ) ) ) != NULL )
						{
							locfil->f_Path = StringDup( path );
						
							if( (locfil->f_SpecialData = FCalloc( 1, sizeof( SpecialData ) ) ) != NULL )
							{
								sd->fp = locfp;
								SpecialData *locsd = (SpecialData *)locfil->f_SpecialData;
								locsd->fp = locfp;
								locsd->mode = MODE_READ;
								locsd->fname = StringDup( tmpfilename );
								locsd->path = StringDup( path );
								locfil->f_SessionID = StringDup( s->f_SessionID );
					
								DEBUG("[fsysphp] FileOpened, memory allocated for reading.\n" );
								return locfil;
							}
				
							// Free this one
							FFree( locfil->f_Path );
							FFree( locfil );
						}
						// Close the dangling fp
						fclose( locfp );
					}
					else
					{
						ERROR("[fsysphp] Cannot open temporary file %s\n", tmpfilename );
					}
				}
				else
				{
					ERROR("[fsysphp] Cannot create temporary file %s\n", tmpfilename );
				}
				// Close lock
				if( lockf >= 0 ) 
				{
					DEBUG( "[fsysphp] Closing lock..\n" );
					close( lockf );
				}
			}
			else if( mode[0] == 'w' )
			{
				char tmpfilename[ 712 ];
			
				// Make sure we can make the tmp file unique
				do
				{
					sprintf( tmpfilename, "/tmp/%s_write_%d%d%d%d", s->f_SessionID, rand()%9999, rand()%9999, rand()%9999, rand()%9999 );
				}
				while( access( tmpfilename, F_OK ) != -1 );
			
				DEBUG("[fsysphp] WRITE FILE %s\n", tmpfilename );
			
				FILE *locfp;
				if( ( locfp = fopen( tmpfilename, "w+" ) ) != NULL )
				{
					File *locfil = NULL;
	
					if( ( locfil = FCalloc( sizeof( File ), 1 ) ) != NULL )
					{
						locfil->f_Path = StringDup( path );
				
						if( ( locfil->f_SpecialData = FCalloc( 1, sizeof( SpecialData ) ) ) != NULL )
						{
							SpecialData *locsd = (SpecialData *)locfil->f_SpecialData;
							locsd->fp = locfp;
							locsd->mode = MODE_WRITE;
							locsd->fname = StringDup( tmpfilename );
							locsd->path = StringDup( path );
							locfil->f_SessionID = StringDup( s->f_SessionID );

							DEBUG("[fsysphp] FileOpened, memory allocated\n");
				
							return locfil;
						}
					
						// Free it
						FFree( locfil->f_Path );
						FFree( locfil );
					}
					// Close the dangling fp
					fclose( locfp );
				}
				else
				{
					ERROR("Cannot create temporary file %s\n", tmpfilename );
				}
			}
			else
			{
				ERROR("Mode not supported\n");
			}
		}
		free( comm );
	}	// comm
	
	return NULL;
}
Beispiel #11
0
void *Mount( struct FHandler *s, struct TagItem *ti )
{
	File *dev = NULL;
	char *path = NULL;
	char *name = NULL;
	User *usr = NULL;
	char *module = NULL;
	char *type = NULL;
	char *authid = NULL;
	
	if( s == NULL )
	{
		return NULL;
	}
	
	DEBUG("[fsysphp] Mounting PHPFS filesystem!\n");
	
	if( ( dev = FCalloc( 1, sizeof( File ) ) ) != NULL )
	{
		struct TagItem *lptr = ti;
		
		// typical mount call
		//   'php "modules/system/module.php" "type=corvo&devname=HomeSql&path=&module=system&unmount=%5Bobject%20Object%5D&sessionid=0eff3a525c8e0495301f7418bd6b6ce358a995e6";'
		
		//
		// checking passed arguments
		//
		
		while( lptr->ti_Tag != TAG_DONE )
		{
			switch( lptr->ti_Tag )
			{
				//printf("TAG %x\n", lptr->ti_Tag);
				case FSys_Mount_Path:
					path = (char *)lptr->ti_Data;
					//DEBUG("Mount FS path set '%s'  len %d\n", path, strlen( path ) );
					break;
				case FSys_Mount_Host:
					break;
				case FSys_Mount_Port:
					break;
				case FSys_Mount_Name:
					name = (char *)lptr->ti_Data;
					break;
				case FSys_Mount_User:
					usr = (User *)lptr->ti_Data;
					break;
				case FSys_Mount_Type:
					type = (char *)lptr->ti_Data;
					//INFO("TYPE PASSED %s size %d\n", type, strlen( type ) );
					break;
				case FSys_Mount_Module:
					module = (char *)lptr->ti_Data;
					break;
			}
			lptr++;
		}
		
		//
		/*
		if( path == NULL )// || strlen( path ) < 1 )
		{
			DEBUG("[ERROR]: Path option not found!\n");
			free( dev );
			return NULL;
		}
		*/
		init( s );
		
		// we are trying to open folder/connection
		
		if( path != NULL )
		{
			if( strlen( path ) < 1 )
			{
				dev->f_Path = calloc( 2, sizeof(char) );
			}
			else
			{
				dev->f_Path = StringDup( path );
			}
			DEBUG("[fsysphp] phpfs path is ok '%s' (ignore this message, unimplemented!)\n", dev->f_Path );
		}
		
		dev->f_FSys = s;
		dev->f_Position = 0;
		dev->f_User = usr;
		dev->f_Name = StringDup( name );
		
		dev->f_Type = FType_Directory;
		dev->f_Size = 0;
		
		SpecialData *sd = calloc( 1, sizeof( SpecialData ) );
		if( sd != NULL )
		{
			sd->module = StringDup( module );
			dev->f_SessionID = StringDup( usr->u_SessionID );
			sd->type = StringDup( type );
			dev->f_SpecialData = sd;
			
			char command[ 2048 ];	// maybe we should count that...
		
			sprintf( command, "php \"modules/system/module.php\" \"command=mount&type=%s&devname=%s&path=%s&module=%s&sessionid=%s\";",
				 type, name, path, module, usr->u_SessionID
			);
		
			int answerLength;
			ListString *result = PHPCall( command, &answerLength );
		
			if( result && result->ls_Size >= 0 )
			{

				DEBUG( "[fsysphp] Return was \"%s\"\n", result->ls_Data );
				if( strncmp( result->ls_Data, "ok", 2 ) != 0 )
				{
					DEBUG( "[fsysphp] Failed to mount device %s..\n", name );
					DEBUG( "[fsysphp] Output was: %s\n", result->ls_Data );
					if( sd->module ) FFree( sd->module );
					if( dev->f_SessionID ) FFree( dev->f_SessionID );
					if( sd->type ) FFree( sd->type );
					if( dev->f_Name ) FFree( dev->f_Name );
					if( dev->f_Path ) FFree( dev->f_Path );
					FFree( sd );
					FFree( dev );
					
					// Free up buffer
					ListStringDelete( result );
					
					return NULL;
				}
			}
			else
			{
				DEBUG( "[fsysphp] Error mounting device %s..\n", name );
				if( sd->module ) FFree( sd->module );
				if( dev->f_SessionID ) FFree( dev->f_SessionID );
				if( sd->type ) FFree( sd->type );
				if( dev->f_Name ) FFree( dev->f_Name );
				if( dev->f_Path ) FFree( dev->f_Path );
				FFree( sd );
				FFree( dev );
				
				// Free up buffer
				if( result ) ListStringDelete( result );
				return NULL;
			}
		
			if( result ) ListStringDelete( result );
		}
		DEBUG("[fsysphp] IS DIRECTORY data filled\n");
	}
	
	DEBUG("[fsysphp] mount ok\n");
	
	return dev;
}