示例#1
0
文件: fs.c 项目: liwei606/OS_proj
int fs_insert(FileSystem *fs, const char *f, int pos, int l, const char *data) {
    Inode *inode = NULL;
    char *buffer = NULL;
    File *file = NULL;
    int i = 0;
    
    inode = folder_lookup(fs, fs->cur, f);
    if (!inode) return ERROR;
    buffer = (char *) malloc(inode->filesize + l);
    if (!buffer) return ERROR;
    file = file_new(fs, inode);
    file_get_contents(file, buffer);
    file_free(&file);
    if (pos > inode->filesize) {
        pos = inode->filesize;
    }
    for (i = inode->filesize - 1; i >= pos; --i) {
        buffer[i + l] = buffer[i];
    }
    memcpy(buffer + pos, data, l);
    file = file_new(fs, inode);
    file_put_contents(file, buffer, inode->filesize + l);
    file_free(&file);
    free(buffer);
    return OK;
}
示例#2
0
文件: fs.c 项目: liwei606/OS_proj
int fs_delete(FileSystem *fs, const char *f, int pos, int l) {
    Inode *inode = NULL;
    char *data = NULL;
    File *file = NULL;
    int i = 0;
    
    inode = folder_lookup(fs, fs->cur, f);
    if (!inode) return ERROR;
    data = (char *) malloc(inode->filesize);
    if (!data) return ERROR;
    file = file_new(fs, inode);
    file_get_contents(file, data);
    file_free(&file);
    if (pos + l > inode->filesize) {
        l = inode->filesize - pos;
    }
    for (i = pos + l; i < inode->filesize; ++i) {
        data[i - l] = data[i];
    }
    file = file_new(fs, inode);
    file_put_contents(file, data, inode->filesize - l);
    file_free(&file);
    free(data);
    return OK;
}
示例#3
0
BOOL file_put_contents(LPCTSTR lpszFilename, LPCTSTR lpszBuffer)
{
	CStringA strA;
	USES_CONVERSION;
	strA = CT2CA( lpszBuffer );
	return file_put_contents(lpszFilename, (BYTE*)strA.GetString(), strA.GetLength());
}
示例#4
0
文件: file.cpp 项目: adan830/poseidon
int file_put_contents_nothrow(const char *path, StreamBuffer contents, bool append){
	try {
		file_put_contents(path, STD_MOVE(contents), append);
		return 0;
	} catch(SystemException &e){
		return e.code();
	}
}
示例#5
0
文件: fs.c 项目: liwei606/OS_proj
int fs_write(FileSystem *fs, const char *f, int l, const char *data) {
    Inode *inode = NULL;
    File *file = NULL;
    
    inode = folder_lookup(fs, fs->cur, f);
    if (!inode) return ERROR;
    file = file_new(fs, inode);
    file_put_contents(file, data, l);
    file_free(&file);
    return OK;
}
示例#6
0
/* 对模板文件进行编译.*/
int _lua_tpl_compile(lua_State * L)
{
    int cache = 0,lbuf = 0;
    unsigned int _var_size = 0;
    size_t lfile;

    const char *cfile = NULL,
                * file= luaL_checklstring(L, 1,&lfile),
                  * buf = NULL;
#if defined(_WIN32) || defined(_WIN64)
    automem_t mem= {NULL};
#else
    automem_t mem= {
        .size = 0,
        .pdata = NULL
    };
#endif

    if(lua_isboolean(L, 2))
        cache =lua_toboolean(L, 2);

    if(0 != cache) {
        struct stat st1,st2;
        cfile=(char *)malloc(lfile+5);
        memcpy((char *)cfile,file,lfile);
        strcpy((char *)cfile+lfile,".tpl");
        if((0 == stat(file,&st1)) && (0 == stat(cfile, &st2)))
        {
            if(st1.st_mtime <= st2.st_mtime) {
                read_from_cached(L, &mem, cfile);
                free((void*)cfile);
                lua_pushlstring(L, mem.pdata, mem.size);
                goto lua_tpl_compile_final;
            }
        }
    }

    if(NULL != (buf = file_get_contents(file, &lbuf)))
    {
        lua_tpl_expand_variables(L, &mem, lbuf);
        _var_size = mem.size;
        lua_tpl_compile_local(L, &mem, buf, lbuf);
        free((void*)buf);
        lua_pushlstring(L,(char *)mem.pdata,mem.size);
    }
    if(0 != cache && mem.pdata != NULL)
        file_put_contents(cfile, mem.pdata + _var_size, mem.size - _var_size);

    if(NULL != cfile)
        free((void *)cfile);
lua_tpl_compile_final:
    automem_uninit(&mem);
    return 1;
}
示例#7
0
文件: app.cpp 项目: 2php/ssdb
void Application::write_pid(){
	if(app_args.pidfile.empty()){
		return;
	}
	int pid = (int)getpid();
	std::string s = str(pid);
	int ret = file_put_contents(app_args.pidfile, s);
	if(ret == -1){
		log_error("Failed to write pidfile '%s'(%s)", app_args.pidfile.c_str(), strerror(errno));
		exit(1);
	}
}
示例#8
0
int main() {
  struct compressor *cc;
  struct background *cc_bgd;
  unsigned char *rnd1,*rnd2;

  log_set_level("",LOG_DEBUG);
  logging_fd(2);
  cc = compressor_create();
  cc_bgd = compressor_background(cc);

  rnd1 = malloc(SIZE);
  rnd2 = malloc(SIZE);

  make_random(rnd1,SIZE);
  make_random(rnd2,SIZE);

  if(unlink("ct-a.gz")) { end(); }
  if(unlink("ct-b.gz")) { end(); }
  if(file_put_contents("ct-a",rnd1,SIZE)) { end(); }
  if(file_put_contents("ct-b",rnd2,SIZE)) { end(); }

  compressor_add(cc,"ct-a");
  background_start(cc_bgd);
  compressor_add(cc,"ct-b");

  background_finish(cc_bgd);
  compressor_release(cc);

  if(unlink("ct-a")) { end(); }
  if(unlink("ct-b")) { end(); }
  free(rnd1);
  free(rnd2);

  logging_done();
  return 0;
}
示例#9
0
文件: fs.c 项目: liwei606/OS_proj
void folder_close(Folder **folder) {
    if (folder && *folder) {
        int len = 0;
        int i = 0;
        char *buffer = NULL;
        int offset = 0;
        
        len = 4;
        for (i = 0; i < (*folder)->nitem; ++i) {
            len += 4;
            len += strlen((*folder)->items[i].cname);
            len += 4;
        }
        buffer = (char *) malloc(len);
        util_writeint(buffer, 0, (*folder)->nitem);
#ifdef DEBUG
        fprintf(stderr, "folder_close, (*folder)->nitem=%d\n", (*folder)->nitem);
#endif
        offset = 4;
        for (i = 0; i < (*folder)->nitem; ++i) {
            int cname_len = 0;
            
            cname_len = (int) strlen((*folder)->items[i].cname);
            util_writeint(buffer, offset, cname_len);
            offset += 4;
            memcpy(buffer + offset, (*folder)->items[i].cname, cname_len);
            offset += cname_len;
            util_writeint(buffer, offset, (*folder)->items[i].page_num);
            offset += 4;
        }
#ifdef DEBUG
        fprintf(stderr, "offset=%d, len=%d\n", offset, len);
        fprintf(stderr, "folder_close buffer=");
        for (i = 0; i < len; ++i) fprintf(stderr, " %x", buffer[i]);
        fprintf(stderr, "\n");
#endif
        file_put_contents(AS_FILE(*folder), buffer, len);
        free(buffer);
        free((*folder)->items);
        free(*folder);
        *folder = NULL;
    }
}
示例#10
0
INT CFileDownloader::_DownloadFileFrom( INT64 &lFileSize, INT64 &lContentSize, BOOL bUseProxyConfig )
{	
	INT64 lTotalReaded = 0;
	DWORD dwTickCount = 0;
	LPCTSTR lpszRangeHeadr = NULL;
	CString strHeaderRange;
	
	// 断点续传的必须有文件大小
	if( lFileSize>0 && lContentSize==0 )
		return 0;
	
	if( lFileSize>0 )
	{
		strHeaderRange.Format(_T("Range: bytes=%d-\r\n"), lFileSize);
		lpszRangeHeadr = strHeaderRange;
	}
	if( !_IssueRequest(m_strHostname, m_strHostPath, lpszRangeHeadr, bUseProxyConfig) )
		return 0;
	
	DWORD dwStatusRequired = lFileSize==0 ? HTTP_STATUS_OK:HTTP_STATUS_PARTIAL_CONTENT;
	DWORD dwStatus = 0;
	DWORD dwSize = sizeof(DWORD);
	BOOL bStatusOK = HttpQueryInfo(m_hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwSize, NULL);
	if( !bStatusOK || dwStatus!=dwStatusRequired)
		return 0;
	
	lTotalReaded = lFileSize;											// 已下载文件大小
	INT lContentSizeNew = _GetHttpInfoFileSize(m_hRequest) + lFileSize;	// 文件大小 
	if(lFileSize==0)
		lContentSize = lContentSizeNew;
	else
	{
		if(lContentSize!=lContentSizeNew)
			return -1;
	}
	
	// 对新文件保存下载的信息文件 nfo 文件 
	CString strLastModified;
	if( lFileSize==0 && lContentSize>0 && _GetHttpInfo(m_hRequest, HTTP_QUERY_LAST_MODIFIED, strLastModified) )
	{
		strLastModified.AppendFormat(_T("; length=%I64d"), lContentSize);
		file_put_contents(m_strDownloadFileInfo, strLastModified);
	}
	
#ifdef _DEBUG
	if( lFileSize!=0 )
	{
		CString strContentRange;
		ATLVERIFY( _GetHttpInfo(m_hRequest, HTTP_QUERY_CONTENT_RANGE, strContentRange) );
		{
			LPCTSTR psz = strContentRange;
			while( *psz && !isdigit(*psz) )
				++psz;
			DEBUG_TRACE(_T("ContentRange(%I64d) %s, %s\n"), lFileSize, strContentRange, psz);
		}
	}
#endif
	
	int err = 0;
	while(!m_Stopped)
	{
		DWORD dwReaded = 0;
		if( !::InternetReadFile(m_hRequest, (LPVOID)m_pBuffer, HTTP_BUFFER_SIZE-1, &dwReaded) )
		{
			DEBUG_TRACE(_T("InternetReadFile Failed %d \n"), GetLastError());
			// 如果有读取到内容的话, 直接从默认的最大RETRY 开始倒数 
			// err = 0;
			break;
		}
		else
		{
			DWORD dwLen = 0;
			if( !WriteFile(m_hFile, m_pBuffer, dwReaded, &dwLen, NULL) || dwReaded!=dwLen )	// Write File error  
			{
				err = -2;
				break;
			}

			lTotalReaded += dwReaded;
			DWORD dwNow = GetTickCount();
			if( (dwNow-dwTickCount)>200 )
			{
				// Send file download progress in 0.2s 
				dwTickCount = dwNow;
				_FireFileDownloaderEvent(IFileDownloaderObserver::DOWNLOAD_STATUS, lTotalReaded, lContentSize);
			}
		}
		err = 1;
		if(dwReaded==0)
			break;
	}
	DEBUG_TRACE(_T("InternetReadFile Done:%d (LastError :%d) TotalReaded/FileSize/ContentLength: %I64d/%I64d/%I64d \n"), err, GetLastError(), lTotalReaded, lFileSize, lContentSize);
	lFileSize = lTotalReaded;
	return err;
}
示例#11
0
int CUserPatcher::_Patch( LPCTSTR szfile, const T_UserPatchInfo &unpkinfo, const char *, CString &strLogfile )
{
	USES_CONVERSION;
	TCHAR szTempPath[MAX_PATH];
	
#if 1 
	TCHAR szTempName[MAX_PATH];
	GetTempPath(MAX_PATH, szTempName);
	GetTempFileName(szTempName, _T("BK.UPK"), 0, szTempPath);
	DeleteFile( szTempPath );
#else
	strcpy(szTempPath, "c:\\office.unpack\\");
#endif
	
	INT err = KPATCHER_ERR_GENERAL;
	do
	{
		strings files;
		if(!m_objUpk->Extract(szfile, szTempPath, files))
		{
			err = KPATCHER_ERR_EXTRACT;
			break;
		}

		typedef std::pair<std::string, std::string> stringPair;
		typedef std::vector< stringPair >  stringPairs;
		stringPairs ps;
		for(int i=0; i<unpkinfo.files.GetSize(); ++i)
		{
			// 
			const T_UserPatchInfo::PatchFileInfo &pi = unpkinfo.files[i];
			std::string strfromfilename;

			_GetFilename( CT2CA(pi.strFilenameFrom), strfromfilename);
			for(strings::iterator it2=files.begin(); it2!=files.end(); ++it2)
			{
				std::string strfilename2;
				_GetFilename( it2->c_str(), strfilename2 );

				if(stricmp(strfromfilename.c_str(), strfilename2.c_str())==0)
				{
					std::string sto = CT2CA(pi.strFilenameTo);
					ps.push_back( stringPair(*it2, sto) );
					break;
				}
			}
		}
		if( ps.size()< unpkinfo.files.GetSize() )
		{
			err = KPATCHER_ERR_FILENOTMATCH;
			break;
		}
		
		// 2. check file is not used 
		BOOL bFileIsUsing = FALSE;
		for(unsigned int i=0; i<ps.size(); ++i)
		{
			CString strFilename = CA2CT(ps[i].second.c_str());
			if( PathFileExists(strFilename) && IsFileUsing( strFilename ) )
			{
				bFileIsUsing = TRUE;
				break;
			}
		}
		if(bFileIsUsing)
		{
			err = KPATCHER_ERR_FILEBUSY;
			break;
		}
				
		// 3. replace files
		struct T_ReplaceInfo
		{
			std::string strTo, strTmpBackup, strTmp;
			LARGE_INTEGER llfrom, llto;
			bool skipReplace;
		};
		bool hasError = false;
		std::vector<T_ReplaceInfo> tmpfiles;
		// copy all files into target path 
		for( unsigned int i=0; i<ps.size() && !hasError; ++i)
		{
			// BAKCUP in local DIR 
			std::string	strfrom, strto, strtmp, strbackup;
			strfrom = ps[i].first;
			strto = ps[i].second;
			strtmp = strto + "_TMP";
			strbackup = ps[i].second + "_TMPBK";

			T_ReplaceInfo r;
			r.strTo = strto;
			r.strTmpBackup = strbackup;
			r.strTmp = strtmp;
			if( GetFileVersion( CA2CT(strfrom.c_str()), r.llfrom) && GetFileVersion( CA2CT(strto.c_str()), r.llto) && r.llfrom.QuadPart==r.llto.QuadPart )
			{
				r.skipReplace = true;
			}
			else
			{
				CreateDirs( strto.c_str() );				

				BOOL b1 = true, b2 = true;
				if( IsFileExist( CA2CT(strto.c_str()) ) )
					b1 = MyMoveFileA( strto.c_str(), strbackup.c_str());
				b2 = MyMoveFileA(strfrom.c_str(), strtmp.c_str());
				if( !b1	|| !b2 )
				{
#ifdef _DEBUG
					DWORD dwErr = GetLastError();
					CStringA strA;
					strA.Format("MOVE FILE ERROR %d\r\n%d %s -> %s\r\n%d %s -> %s \r\n", dwErr, b1, strto.c_str(), strbackup.c_str(), b2, strfrom.c_str(), strtmp.c_str() );
					MessageBoxA(NULL, strA, NULL, MB_OK);
#endif 
					hasError = true;
				}
				r.skipReplace = false;
			}						
			tmpfiles.push_back( r );			
		}
		
		// 4. rollback or commit replace operation  		
		if( hasError )
		{
			for( unsigned int i=0; i<tmpfiles.size(); ++i)
			{
				T_ReplaceInfo &r = tmpfiles[i];
				if( r.skipReplace )
					continue;				
				MyMoveFileA( r.strTmpBackup.c_str(), r.strTo.c_str() );
			}
			err = KPATCHER_ERR_REPLACE;
		}
		else
		{
			// Assume all move operation success 
			CStringA slog;
			CStringA strProductKey = CT2CA(unpkinfo.strProductKey), strPatchKey = CT2CA(unpkinfo.strPatchKey);
			slog.AppendFormat("REG:%s\t%s\r\n", strProductKey, strPatchKey);
			for( unsigned int i=0; i<tmpfiles.size(); ++i)
			{
				T_ReplaceInfo &r = tmpfiles[i];
				std::string strbackup = r.strTo + "_bk";
				CString strVfrom, strVto;
				CStringA strVfromA, strVtoA;
				GenVersionStr( r.llfrom, strVfrom);
				GenVersionStr( r.llto, strVto);
				strVfromA = CT2CA( strVfrom );
				strVtoA = CT2CA( strVto );
				slog.AppendFormat("VER:%s\t%s\r\n", strVfromA, strVtoA);
				slog.AppendFormat("FILE:%d\t%s\t%s\r\n", r.skipReplace, strbackup.c_str(), r.strTo.c_str() );

				if( r.skipReplace )
					continue;				
				// 
				if( IsFileExist( CA2CT(r.strTmpBackup.c_str()) ) )
					MyMoveFileA(r.strTmpBackup.c_str(), strbackup.c_str());
				MyMoveFileA(r.strTmp.c_str(), r.strTo.c_str());
			}

			std::string dir;
			_GetFileDir( ps[0].second.c_str(), dir );
			char logfilename[MAX_PATH];
			sprintf(logfilename, "$NTUninstKB%d$.log", unpkinfo.nKBID);
			dir += '\\';
			dir += logfilename;

			strLogfile = CA2CT(dir.c_str());
			file_put_contents( strLogfile, (BYTE*)(LPCSTR)slog, slog.GetLength());
			err = KPATCHER_OK;
		}
	}while(FALSE);
	

#ifndef _DEBUG
	{
		TCHAR szPath[MAX_PATH] = {0};
		_tcscpy(szPath, szTempPath);
		SHFILEOPSTRUCT shfileopstruct = {0};
		shfileopstruct.wFunc = FO_DELETE;
		shfileopstruct.pFrom = szPath;   
		shfileopstruct.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;   
		SHFileOperation(&shfileopstruct); 
	}
#endif
	return err;
}