TInt CTestWideApi::wfdopen_ivalm1()
	{
	int wfd;
	int ret_wmkdir = wmkdir(L"C:\\ggg" , S_IWUSR);
	if(ret_wmkdir)
		{
	  	_LIT(Kerr , "Failed to make dir") ;
		INFO_PRINTF1(Kerr) ;
		return KErrGeneral ;		
		}				
	if((wfd=wopen(L"C:\\ggg\\lll.txt", O_WRONLY | O_CREAT | O_TRUNC, 0777))<0)
		{
	   	_LIT(Kopen,"Failed to open file");
		INFO_PRINTF1(Kopen);
		wrmdir(L"C:\\ggg");        		
		return KErrGeneral;
	    }
    FILE *fp =wfdopen(wfd ,NULL);
    if(NULL != fp)    	   
	    {
	 	wunlink(L"C:\\ggg\\lll.txt");
	   	wrmdir(L"C:\\ggg");
	   	return KErrGeneral;    	
	    }
	close(wfd);
    wunlink(L"C:\\ggg\\lll.txt");
	wrmdir(L"C:\\ggg");
    return KErrNone;  
	}
TInt CTestWideApi::wremove_val1()
	{
	wchar_t *dirName = L"C:\\wremoveDir"; 
	wchar_t *fileName1 = L"C:\\wremoveDir\\file1.txt"; 
	wchar_t *fileName2 = L"C:\\wremoveDir\\file2.txt"; 	
    TInt ret = -1;    
    errno = 0;
    if(wmkdir(dirName ,S_IWUSR)  < 0) 
	    {
    	INFO_PRINTF1(_L("Failed to create the parent directory"));
    	return KErrGeneral ;
	    }    
    FILE *fp = wfopen(fileName1, (wchar_t *)"w");    
    if(fp == NULL) 
	    {
    	INFO_PRINTF1(_L("Failed to create the child directory"));
    	return KErrGeneral ;
	    }
    fclose(fp);        
    
    fp = wfopen(fileName2, (wchar_t *)"w");    
    if(fp == NULL) 
	    {
    	INFO_PRINTF1(_L("Failed to create the child directory"));
    	return KErrGeneral ;
	    }
    fclose(fp);    
    ret = wremove(fileName1);
    if(ret != 0) 
	    {
    	INFO_PRINTF1(_L("wremove failed"));
    	wunlink(fileName1);
    	return KErrGeneral;
	    }    
    ret = wremove(fileName2);
    if(ret != 0) 
	    {
    	INFO_PRINTF1(_L("wremove failed"));
    	wunlink(fileName2);    	
    	return KErrGeneral;
	    }    
    ret = wremove(dirName);
    if(ret != 0)
		{
		INFO_PRINTF1(_L("wremove failed"));
		return KErrGeneral;
		}    
    return KErrNone ;        
	}
Exemple #3
0
Status DeleteDirectory(const OsPath& path)
{
	// note: we have to recursively empty the directory before it can
	// be deleted (required by Windows and POSIX rmdir()).

	CFileInfos files; DirectoryNames subdirectoryNames;
	RETURN_STATUS_IF_ERR(GetDirectoryEntries(path, &files, &subdirectoryNames));

	// delete files
	for(size_t i = 0; i < files.size(); i++)
	{
		const OsPath pathname = path / files[i].Name();
		errno = 0;
		if(wunlink(pathname) != 0)
			WARN_RETURN(StatusFromErrno());
	}

	// recurse over subdirectoryNames
	for(size_t i = 0; i < subdirectoryNames.size(); i++)
		RETURN_STATUS_IF_ERR(DeleteDirectory(path / subdirectoryNames[i]));

	errno = 0;
	if(wrmdir(path) != 0)
		WARN_RETURN(StatusFromErrno());

	return INFO::OK;
}
Exemple #4
0
void history_t::clear(void) {
    scoped_lock locker(lock);
    new_items.clear();
    deleted_items.clear();
    unsaved_item_count = 0;
    old_item_offsets.clear();
    wcstring filename = history_filename(name, L"");
    if (! filename.empty())
        wunlink(filename);
    this->clear_file_state();
    
}
TInt CTestWideApi::wfreopen_valinv()
	{
	TPtrC nameRead;	
	_LIT( KString, "Parameter1" );	    	
	TBool res = GetStringFromConfig(ConfigSection(), KString, nameRead);
	if(!res)
		{
		_LIT(Kerr , "Failed to read string") ;
		INFO_PRINTF1(Kerr) ;
		return KErrGeneral ;
		}	
	int ret_wmkdir = wmkdir(L"C:\\ggg" , S_IWUSR);
	if((errno == EEXIST) || (!ret_wmkdir))
		{
		TBuf8<100> buf;	   				
		buf.Copy(nameRead);
		char* filename = (char*) buf.Ptr();
		filename[buf.Length()]='\0';
		wchar_t *pathName = (wchar_t *)malloc(30*sizeof(wchar_t));
		if(pathName==NULL)
			{
			wrmdir(L"C:\\ggg");        								
			return KErrNoMemory;							
			}						
		size_t siz = mbstowcs(pathName, filename, 30);
		wunlink(pathName) ;
        FILE *fp = wfreopen(pathName ,L"w",stdout);
        if(fp == NULL) 
			{
			_LIT(Kropen , " Failed to reopen file " ) ;
			INFO_PRINTF1(Kropen) ;
			wrmdir(L"C:\\ggg");        		
			free(pathName);
			return KErrNone;
			}
        else
        	{
    		wrmdir(L"C:\\ggg");  
    		free(pathName);
    		return KErrGeneral;
        	}
		}
	else
		{
	  	_LIT(Kerr , "Failed to make dir") ;
		INFO_PRINTF1(Kerr) ;
		return KErrGeneral ;
		}
	}
Exemple #6
0
void CSimulation2Impl::ReportSerializationFailure(
	SerializationTestState* primaryStateBefore, SerializationTestState* primaryStateAfter,
	SerializationTestState* secondaryStateBefore, SerializationTestState* secondaryStateAfter)
{
	OsPath path = psLogDir() / "oos_log";
	CreateDirectories(path, 0700);

	// Clean up obsolete files from previous runs
	wunlink(path / "hash.before.a");
	wunlink(path / "hash.before.b");
	wunlink(path / "debug.before.a");
	wunlink(path / "debug.before.b");
	wunlink(path / "state.before.a");
	wunlink(path / "state.before.b");
	wunlink(path / "hash.after.a");
	wunlink(path / "hash.after.b");
	wunlink(path / "debug.after.a");
	wunlink(path / "debug.after.b");
	wunlink(path / "state.after.a");
	wunlink(path / "state.after.b");

	if (primaryStateBefore)
		DumpSerializationTestState(*primaryStateBefore, path, L"before.a");
	if (primaryStateAfter)
		DumpSerializationTestState(*primaryStateAfter, path, L"after.a");
	if (secondaryStateBefore)
		DumpSerializationTestState(*secondaryStateBefore, path, L"before.b");
	if (secondaryStateAfter)
		DumpSerializationTestState(*secondaryStateAfter, path, L"after.b");

	debug_warn(L"Serialization test failure");
}
TInt CTestWideApi::wfdopen_ivalm()
	{
	TPtrC nameRead;
	_LIT( KString, "Parameter1" );	    	
	TBool res = GetStringFromConfig(ConfigSection(), KString, nameRead);
	if(!res)
		{
		_LIT(Kerr , "Failed to read string") ;
		INFO_PRINTF1(Kerr) ;
		return KErrGeneral ;
		}	
	int ret_wmkdir = wmkdir(L"C:\\ggg" , S_IWUSR);
	if((errno == EEXIST) || (!ret_wmkdir))
		{
		TBuf8<100> buf;
	
		int wfd;
		buf.Copy(nameRead);
		char* filemode = (char*) buf.Ptr();
		filemode[buf.Length()]='\0';
		wchar_t *dmode = (wchar_t *)malloc(30*sizeof(wchar_t));
		if(dmode==NULL)
			{
			wrmdir(L"C:\\ggg");        								
			return KErrNoMemory;							
			}						
		size_t siz = mbstowcs(dmode, filemode, 30);
		wunlink(L"C:\\ggg\\lll.txt");
		if((wfd=wopen(L"C:\\ggg\\lll.txt", O_WRONLY | O_CREAT | O_TRUNC, 0777))<0)
	        {
        	_LIT(Kopen,"Failed to open file");
			INFO_PRINTF1(Kopen);
			wrmdir(L"C:\\ggg");      
			free(dmode) ;
			return KErrGeneral;
	        }
        else
	        {
	         FILE *fp =wfdopen(wfd ,dmode);
        	 if(NULL == fp)    	
        	 	{
	    	 	_LIT(Kopen,"wfdopen failed");
				INFO_PRINTF1(Kopen);
				close(wfd);
				wunlink(L"C:\\ggg\\lll.txt");
				wrmdir(L"C:\\ggg"); 
				free(dmode) ;     		
				return KErrNone;
        	 	}
        	 else
        	    { 
    	    	wunlink(L"C:\\ggg\\lll.txt");
   				wrmdir(L"C:\\ggg");
   				free(dmode) ;
    	    	return KErrGeneral;
        	    }        	 	
    	    }      
      	}
	else
		{
	  	_LIT(Kerr , "Failed to make dir") ;
		INFO_PRINTF1(Kerr) ;
		wunlink(L"C:\\ggg\\lll.txt");
   		wrmdir(L"C:\\ggg");
		return KErrGeneral ;
		}
	}
TInt CTestWideApi::wfreopen_val()
	{
	TPtrC nameRead;
	_LIT( KString, "Parameter1" );
	TBool res = GetStringFromConfig(ConfigSection(), KString, nameRead);
	if(!res)
		{
		_LIT(Kerr , "Failed to read string") ;
		INFO_PRINTF1(Kerr) ;
		return KErrGeneral ;
		}
    int ret_wmkdir = wmkdir(L"C:\\ggg" , S_IWUSR);
	if((errno == EEXIST) || (!ret_wmkdir))
		{
		TBuf8<100> buf;	
		int wfd;
		FILE *fp;
		buf.Copy(nameRead);
		char c = 'z';
		char* filename = (char*) buf.Ptr();
		filename[buf.Length()]='\0';
		wchar_t *pathName = (wchar_t *)malloc(30*sizeof(wchar_t));
		if(pathName==NULL)
			{
			wrmdir(L"C:\\ggg");        								
			return KErrNoMemory;							
			}							
		size_t siz = mbstowcs(pathName, filename, 30);
		wunlink(pathName) ;
        if((wfd=wopen(pathName, O_WRONLY | O_CREAT | O_TRUNC, 0666))<0)
        	{
        	_LIT(Kopen,"Failed to open file");
			INFO_PRINTF1(Kopen);
			wrmdir(L"C:\\ggg");  
			free(pathName);      		
			return KErrGeneral;
        	}
        else
        	{
             write(wfd,&c,1); 	
        	}
        close(wfd);
        if((fp=wfreopen(pathName ,L"r",stdin))==NULL) 
			{
			_LIT(Kropen , " Failed to reopen file " ) ;
			INFO_PRINTF1(Kropen) ;
			wrmdir(L"C:\\ggg");  
			free(pathName);       		
			return KErrGeneral;
			}
     	char * filen="C:\\tem.txt";
     	unlink(filen);
     	int fd=open(filen, O_WRONLY | O_CREAT | O_TRUNC, 0666);
		if(fd<0)
			{
			_LIT(Kopen,"Failed to open file");
			INFO_PRINTF1(Kopen);
			wrmdir(L"C:\\ggg");        		
			free(pathName); 
			return KErrGeneral  ;
			}
		else
			{
		   	write(fd,&c,1);
			}
		close(fd); 
	    fclose(fp); 
	    unlink(filen);
		wunlink(pathName);	
		wrmdir(L"C:\\ggg");  
		free(pathName);       		
        return KErrNone ;
	    }
	else
		{
	  	_LIT(Kerr , "Failed to make dir") ;
		INFO_PRINTF1(Kerr) ;
		return KErrGeneral ;
		}
	}
// -----------------------------------------------------------------------------
//Function Name : wfdopen_val
//API Tested : wfdopen
//TestCase Description: testing the behaviour of wfdopen for valid arguments
// initialized to NULL.
// -----------------------------------------------------------------------------  
TInt CTestWideApi::wfdopen_val()
	{
	TPtrC nameRead;
	wchar_t *pathName = NULL;
	_LIT( KString, "Parameter1" );	    	
	TBool res = GetStringFromConfig(ConfigSection(), KString, nameRead);
	if(!res)
		{
		_LIT(Kerr , "Failed to read string") ;
		INFO_PRINTF1(Kerr) ;
		return KErrGeneral ;
		}		
	int ret_wmkdir = wmkdir(L"C:\\ggg" , S_IWUSR);
	if((errno == EEXIST) || (!ret_wmkdir))
		{
		TBuf8<100> buf;	
		int wfd;
		FILE *fp;
		buf.Copy(nameRead);
		char* filename = (char*) buf.Ptr();
		filename[buf.Length()]='\0';
		pathName = (wchar_t *)malloc(30*sizeof(wchar_t));
		if(pathName==NULL)
			{
			wrmdir(L"C:\\ggg");        								
			return KErrNoMemory;							
			}						
		size_t siz = mbstowcs(pathName, filename, 30);
		wunlink(pathName) ;
        if((wfd=wopen(pathName, O_WRONLY | O_CREAT | O_TRUNC, 0666))<0)
	        {
        	_LIT(Kopen,"Failed to open file");
			INFO_PRINTF1(Kopen);
			wrmdir(L"C:\\ggg"); 
			free(pathName);       		
			return KErrGeneral;
	        }
        else
	        {
        	 if(NULL == (fp =wfdopen(wfd ,L"w")))    	
        	 	{
        	 	_LIT(Kopen,"wfdopen failed");
				INFO_PRINTF1(Kopen);
				wrmdir(L"C:\\ggg"); 
				free(pathName);       		
				return KErrGeneral;
        	 	}
        	 char *buffer="Writing to the file";
        	 size_t size=strlen(buffer);        	 
        	 if(size!=(fwrite(buffer,1,size,fp)))
        	 	{
        	 	_LIT(Kopen,"fwrite failed");
				INFO_PRINTF1(Kopen);
				wrmdir(L"C:\\ggg");    
				free(pathName);    		
				return KErrGeneral;
        	 	}        	 	
    	    }
       fclose(fp);
       wunlink(pathName);
       wrmdir(L"C:\\ggg");
       free(pathName);        		
	   return KErrNone;
	    }
	else
		{
	  	_LIT(Kerr , "Failed to make dir") ;
		INFO_PRINTF1(Kerr) ;
		wrmdir(L"C:\\ggg");
		return KErrGeneral ;
		}
	}
/* Returns true if modified variables were written, false if not. (There may still be variable changes due to other processes on a false return). */
bool env_universal_t::sync(callback_data_list_t *callbacks)
{
    UNIVERSAL_LOG("sync");
    scoped_lock locker(lock);
    /* Our saving strategy:
    
    1. Open the file, producing an fd.
    2. Lock the file (may be combined with step 1 on systems with O_EXLOCK)
    3. After taking the lock, check if the file at the given path is different from what we opened. If so, start over.
    4. Read from the file. This can be elided if its dev/inode is unchanged since the last read
    5. Open an adjacent temporary file
    6. Write our changes to an adjacent file
    7. Move the adjacent file into place via rename. This is assumed to be atomic.
    8. Release the lock and close the file
    
    Consider what happens if Process 1 and 2 both do this simultaneously. Can there be data loss? Process 1 opens the file and then attempts to take the lock. Now, either process 1 will see the original file, or process 2's new file. If it sees the new file, we're OK: it's going to read from the new file, and so there's no data loss. If it sees the old file, then process 2 must have locked it (if process 1 locks it, switch their roles). The lock will block until process 2 reaches step 7; at that point process 1 will reach step 2, notice that the file has changed, and then start over.
    
    It's possible that the underlying filesystem does not support locks (lockless NFS). In this case, we risk data loss if two shells try to write their universal variables simultaneously. In practice this is unlikely, since uvars are usually written interactively.
    
    Prior versions of fish used a hard link scheme to support file locking on lockless NFS. The risk here is that if the process crashes or is killed while holding the lock, future instances of fish will not be able to obtain it. This seems to be a greater risk than that of data loss on lockless NFS. Users who put their home directory on lockless NFS are playing with fire anyways.
    */
    const wcstring vars_path = explicit_vars_path.empty() ? default_vars_path() : explicit_vars_path;
    
    /* If we have no changes, just load */
    if (modified.empty())
    {
        this->load_from_path(vars_path, callbacks);
        return false;
    }
    
    const wcstring directory = wdirname(vars_path);
    bool success = true;
    int vars_fd = -1;
    int private_fd = -1;
    wcstring private_file_path;
    
    UNIVERSAL_LOG("Performing full sync");
    
    /* Open the file */
    if (success)
    {
        success = this->open_and_acquire_lock(vars_path, &vars_fd);
    }

    /* Read from it */
    if (success)
    {
        assert(vars_fd >= 0);
        this->load_from_fd(vars_fd, callbacks);
    }

    /* Open adjacent temporary file */
    if (success)
    {
        success = this->open_temporary_file(directory, &private_file_path, &private_fd);
    }
    
    /* Write to it */
    if (success)
    {
        assert(private_fd >= 0);
        success = this->write_to_fd(private_fd, private_file_path);
    }
    
    if (success)
    {
        /* Apply new file */
        success = this->move_new_vars_file_into_place(private_file_path, vars_path);
    }
    
    if (success)
    {
        /* Since we moved the new file into place, clear the path so we don't try to unlink it */
         private_file_path.clear();
    }
    
    /* Clean up */
    if (vars_fd >= 0)
    {
        close(vars_fd);
    }
    if (private_fd >= 0)
    {
        close(private_fd);
    }
    if (! private_file_path.empty())
    {
        wunlink(private_file_path);
    }
    
    if (success)
    {
        /* All of our modified variables have now been written out. */
        modified.clear();
    }
    
    return success;
}