int retro_vfs_file_rename_impl(const char *old_path, const char *new_path) { char *old_path_local = NULL; char *new_path_local = NULL; wchar_t *old_path_wide = NULL; wchar_t *new_path_wide = NULL; if (!old_path || !*old_path || !new_path || !*new_path) return -1; (void)old_path_local; (void)new_path_local; (void)old_path_wide; (void)new_path_wide; #if defined(_WIN32) && !defined(_XBOX) #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 old_path_local = utf8_to_local_string_alloc(old_path); new_path_local = utf8_to_local_string_alloc(new_path); if (old_path_local) { if (new_path_local) { int ret = rename(old_path_local, new_path_local); free(old_path_local); free(new_path_local); return ret==0 ? 0 : -1; } free(old_path_local); } if (new_path_local) free(new_path_local); #else old_path_wide = utf8_to_utf16_string_alloc(old_path); new_path_wide = utf8_to_utf16_string_alloc(new_path); if (old_path_wide) { if (new_path_wide) { int ret = _wrename(old_path_wide, new_path_wide); free(old_path_wide); free(new_path_wide); return ret==0 ? 0 : -1; } free(old_path_wide); } if (new_path_wide) free(new_path_wide); #endif return -1; #else return rename(old_path, new_path)==0 ? 0 : -1; #endif }
__inline void CFileBase::CloseFile() { if (NULL == m_pFileHandle) return; fflush(m_pFileHandle); fclose(m_pFileHandle); m_pFileHandle = NULL; CString strFileName = GetFileName(); int iIndex = strFileName.Find(_T('-')); if (-1 == iIndex || 0 == strlen(m_oParameter.m_cSaveTime)) return; #pragma warning(push) #pragma warning(disable: 4996) char Buffer[MAX_PATH] = { 0 }; m_oCreateTime.FormatString(Buffer, m_sLastTime); strcat(Buffer, m_oParameter.m_cExeName); CString strNewName = strFileName.Left(iIndex + 1) + CString(Buffer); #pragma warning(pop) _wrename(strFileName, strNewName); }
void FileAccessWindows::close() { if (!f) return; fclose(f); f = NULL; if (save_path!="") { //unlink(save_path.utf8().get_data()); //print_line("renaming.."); //_wunlink(save_path.c_str()); //unlink if exists //int rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str()); bool rename_error; if (!PathFileExistsW(save_path.c_str())) { //creating new file rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str())!=0; } else { //atomic replace for existing file rename_error = !ReplaceFileW(save_path.c_str(), (save_path+".tmp").c_str(), NULL, 2|4, NULL, NULL); } save_path=""; ERR_FAIL_COND( rename_error ); } }
int VSIWin32FilesystemHandler::Rename( const char *oldpath, const char *newpath ) { #if (defined(WIN32) && _MSC_VER >= 1310) || __MSVCRT_VERSION__ >= 0x0601 if( CSLTestBoolean( CPLGetConfigOption( "GDAL_FILENAME_IS_UTF8", "YES" ) ) ) { int nResult; wchar_t *pwszOldPath = CPLRecodeToWChar( oldpath, CPL_ENC_UTF8, CPL_ENC_UCS2 ); wchar_t *pwszNewPath = CPLRecodeToWChar( newpath, CPL_ENC_UTF8, CPL_ENC_UCS2 ); nResult = _wrename( pwszOldPath, pwszNewPath ); CPLFree( pwszOldPath ); CPLFree( pwszNewPath ); return nResult; } else #endif { return rename( oldpath, newpath ); } }
/** * The rename function updates the filename. */ int pcsl_file_rename(const pcsl_string * oldName, const pcsl_string * newName) { int res; int status; const jchar * pszOldFilename = pcsl_string_get_utf16_data(oldName); if(pszOldFilename == NULL) { return -1; } else { const jchar * pszNewFilename = pcsl_string_get_utf16_data(newName); if(pszNewFilename == NULL) { pcsl_string_release_utf16_data(pszOldFilename, oldName); return -1; } res = _wrename(pszOldFilename, pszNewFilename); if(res < 0) { status = -1; } else { status = 0; } return status; } }
int win_rename(const char *oldpath, const char *newpath) { wchar_t *oldwinpath, *newwinpath; int ret; if (!strcmp("/", oldpath) && !strcmp("/", newpath)) { /* Emulate root */ errno = EROFS; return -1; } oldwinpath = intpath2winpath(oldpath); if (!oldwinpath) { errno = EINVAL; return -1; } newwinpath = intpath2winpath(newpath); if (!newwinpath) { free(oldwinpath); errno = EINVAL; return -1; } ret = _wrename(oldwinpath, newwinpath); free(oldwinpath); free(newwinpath); return ret; }
int wmain(int argc, wchar_t* argv[]) { if(argc < 3) { std::wcerr << L"Usage: " << argv[0] << L" infile delay [forced samplerate]" << std::endl; std::wcerr << L"Delay is in milliseconds and may be fractional." << std::endl; return 1; } delay = _wtof(argv[2]); if(delay <= 0) { std::wcerr << L"Error: Delay must be positive millisecond value, may be fractional." << std::endl; return 1; } if(argc >= 4) { sampleRate = _wtoi(argv[3]); } FILE *f = _wfopen(argv[1], L"rb"); if(f == nullptr) { std::wcerr << L"Cannot open: " << argv[1] << L" for reading!" << std::endl; return 1; } std::wstring newFile = argv[1]; newFile += L".tmp"; FILE *of = _wfopen(newFile.c_str(), L"wb"); if(of == nullptr) { std::wcerr << L"Cannot open output file: " << newFile.c_str() << L" for writing!" << std::endl; return 1; } if(!DecodeWAV(f, of) && !DecodeFLAC(f, of)) { std::wcerr << L"Error: Unknown file type" << std::endl; fclose(of); _wremove(newFile.c_str()); return 1; } fclose(f); fclose(of); _stat stat; _wstat(argv[1], &stat); _utimbuf timbuf; timbuf.actime = stat.st_atime; timbuf.modtime = stat.st_mtime; _wutime(newFile.c_str(), &timbuf); _wremove(argv[1]); _wrename(newFile.c_str(), argv[1]); return 0; }
/*----------------------------------------------------------------------* rtp_wfile_rename *----------------------------------------------------------------------*/ int rtp_wfile_rename (unsigned short * name, unsigned short * newname) { #if (_WIN32_WINNT) >= 0x0400 #ifdef RTP_DEBUG int result; /* ----------------------------------- */ /* Clear the error state by setting */ /* to 0. */ /* ----------------------------------- */ SetLastError (0); #endif name = _rtp_unicode_name_to_winname (name); newname = _rtp_unicode_name_to_winname (newname); if (_wrename ((const unsigned short *)name, (const unsigned short *)newname) != 0) { #ifdef RTP_DEBUG result = GetLastError(); RTP_DEBUG_OUTPUT_STR("rtp_wfile_rename: error returned "); RTP_DEBUG_OUTPUT_INT(result); RTP_DEBUG_OUTPUT_STR(".\n"); #endif return (-1); } return (0); #endif return (-1); }
int my_wrename(const wchar_t * oldName, const wchar_t * newName) { #ifdef _WIN32 return _wrename(oldName, newName); #else return rename(wideCharToUtf8(oldName).c_str(), wideCharToUtf8(newName).c_str()); #endif }
WXDLLIMPEXP_BASE int wxMSLU__wrename(const wchar_t *oldname, const wchar_t *newname) { if ( wxUsingUnicowsDll() ) return rename(wxConvFile.cWX2MB(oldname), wxConvFile.cWX2MB(newname)); else return _wrename(oldname, newname); }
int S_windows_rename(const char *oldpathname, const char *newpathname) { wchar_t woldpathname[PATH_MAX], wnewpathname[PATH_MAX]; if (MultiByteToWideChar(CP_UTF8,0,oldpathname,-1,woldpathname,PATH_MAX) == 0 || MultiByteToWideChar(CP_UTF8,0,newpathname,-1,wnewpathname,PATH_MAX) == 0) return rename(oldpathname, newpathname); else return _wrename(woldpathname, wnewpathname); }
static int rt_rename(EIF_FILENAME from, EIF_FILENAME to) { #ifdef EIF_WINDOWS return _wrename(from, to); #else return rename(from, to); #endif }
int SharedUtil::File::Rename(const char* szOldFilename, const char* szNewFilename) { #ifdef WIN32 return _wrename(FromUTF8(szOldFilename), FromUTF8(szNewFilename)); #else return rename(szOldFilename, szNewFilename); #endif }
//PRIVATE: Delete file with index 0 et rename all file i to i-1 until m_filenr-1 //After this function, the file with index m_filenr-1 is free (it doesn't exist) void CLog::renameFiles(const wchar_t *root_filename) { //We remove the file 0 std::wstring src; src=root_filename; src+=L"0.log"; #ifdef WIN32 _wremove(src.c_str()); #else remove(utilStringNarrow(src).c_str()); #endif std::wstring dest; wchar_t isrc[5]; wchar_t idest[5]; #ifdef WIN32 struct _stat results; #else struct stat results; #endif //For all file until m_filenr-1 // 1 become 0 // 2 become 1 //i+1 become i //m_filenr-1 become m_filenr-2 for(int i=0;i<m_filenr;i++) { swprintf_s(isrc,5,L"%d",i+1); swprintf_s(idest,5,L"%d",i); //if the source does not exist, we stop src=root_filename; src+=isrc; src+=L".log"; #ifdef WIN32 if (_wstat(src.c_str(), &results) != 0) break; #else if (stat(utilStringNarrow(src).c_str(), &results) != 0) break; #endif dest=root_filename; dest+=idest; dest+=L".log"; //Rename of the file #ifdef WIN32 _wrename(src.c_str(),dest.c_str()); #else rename(utilStringNarrow(src).c_str(),utilStringNarrow(dest).c_str()); #endif } }
int __cdecl rename( const char *oldname, const char *newname ) { /* We want to use MoveFileExW but not MoveFileExA (or MoveFile). So convert the strings to Unicode representation and call _wrename. Note that minwin does not support OEM CP for conversion via the FileAPIs APIs. So we unconditionally use the ACP for conversion */ int oldnamelen; int newnamelen; wchar_t *widenamesbuffer = NULL; wchar_t *oldnamew = NULL; wchar_t *newnamew = NULL; int retval; UINT codePage = CP_ACP; #if !(defined (_CORESYS) || defined (_CRT_APP) || defined (_KERNELX)) if (!__crtIsPackagedApp() && !AreFileApisANSI()) codePage = CP_OEMCP; #endif /* !(defined (_CORESYS) || defined (_CRT_APP) || defined (_KERNELX)) */ if ( ( (oldnamelen = MultiByteToWideChar(codePage, 0, oldname, -1, 0, 0) ) == 0 ) || ( (newnamelen = MultiByteToWideChar(codePage, 0, newname, -1, 0, 0) ) == 0 ) ) { _dosmaperr(GetLastError()); return -1; } /* allocate enough space for both */ if ( (widenamesbuffer = (wchar_t*)_malloc_crt( (oldnamelen+newnamelen) * sizeof(wchar_t) ) ) == NULL ) { /* errno is set by _malloc */ return -1; } /* now do the conversion */ oldnamew = widenamesbuffer; newnamew = widenamesbuffer + oldnamelen; if ( ( MultiByteToWideChar(codePage, 0, oldname, -1, oldnamew, oldnamelen) == 0 ) || ( MultiByteToWideChar(codePage, 0, newname, -1, newnamew, newnamelen) == 0 ) ) { _free_crt(widenamesbuffer); _dosmaperr(GetLastError()); return -1; } /* and event call the wide-character variant */ retval = _wrename(oldnamew,newnamew); _free_crt(widenamesbuffer); /* _free_crt leaves errno alone if everything completes as expected */ return retval; }
void FileAccessWindows::close() { if (!f) return; fclose(f); f = NULL; if (save_path!="") { //unlink(save_path.utf8().get_data()); //print_line("renaming.."); //_wunlink(save_path.c_str()); //unlink if exists //int rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str()); bool rename_error; #ifdef WINRT_ENABLED // WinRT has no PathFileExists, so we check attributes instead DWORD fileAttr; fileAttr = GetFileAttributesW(save_path.c_str()); if (INVALID_FILE_ATTRIBUTES == fileAttr) { #else if (!PathFileExistsW(save_path.c_str())) { #endif //creating new file rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str())!=0; } else { //atomic replace for existing file rename_error = !ReplaceFileW(save_path.c_str(), (save_path+".tmp").c_str(), NULL, 2|4, NULL, NULL); } if (rename_error && close_fail_notify) { close_fail_notify(save_path); } save_path=""; ERR_FAIL_COND( rename_error ); } } bool FileAccessWindows::is_open() const { return (f!=NULL); } void FileAccessWindows::seek(size_t p_position) { ERR_FAIL_COND(!f); last_error=OK; if ( fseek(f,p_position,SEEK_SET) ) check_errors(); }
void CjocLog::Initialize (std::wstring &strFile, unsigned long lMaxFileSize, bool bAddLogWhenCreateFile) { bool bCreate = false; if (strFile.empty () == true){throw JOC_Error_(enjocErrors::err_bad_arguments);} FILE *pFile = NULL; pFile = _wfopen (strFile.c_str (),L"r"); if (pFile != NULL) { //Exists fclose (pFile); //Set file m_strFile = strFile; struct _stat64i32 buf; _wstat(m_strFile.c_str (),&buf); if (buf.st_size > lMaxFileSize) { std::wstring strNewFileName; strNewFileName = m_strFile + L".old"; //Delete .old if exists _wremove(strNewFileName.c_str ()); int nRc = _wrename (m_strFile.c_str (),strNewFileName.c_str ()); if (nRc != 0){throw JOC_Error_arg_(enjocErrors::err_renaming_file,m_strFile.c_str ());} bCreate = true; } } else { bCreate = true; } if (bCreate == true) { //Create pFile = _wfopen (strFile.c_str (),L"w"); if (pFile == NULL){throw JOC_Error_arg_(enjocErrors::err_creating_file_for_write,strFile.c_str ());} fclose (pFile); //Set file m_strFile = strFile; if (bAddLogWhenCreateFile == true) Append((std::wstring)L"Log file created"); } }
int x264_rename( const char *oldname, const char *newname ) { wchar_t oldname_utf16[MAX_PATH]; wchar_t newname_utf16[MAX_PATH]; if( utf8_to_utf16( oldname, oldname_utf16 ) && utf8_to_utf16( newname, newname_utf16 ) ) { /* POSIX says that rename() removes the destination, but Win32 doesn't. */ _wunlink( newname_utf16 ); return _wrename( oldname_utf16, newname_utf16 ); } return -1; }
int urename(const char *oldname, const char *newname ) { int r = -1; UTF16_ENCODE(oldname); UTF16_ENCODE (newname); if(oldname_16 && newname_16) r = _wrename(oldname_16, newname_16); UTF16_UN_ENCODE(newname); UTF16_UN_ENCODE(oldname); return r; }
bool FileSystem::MoveFile(const char* srcFilename, const char* dstFilename) { #ifdef WIN32 return _wrename(UtfPathToWidePath(srcFilename), UtfPathToWidePath(dstFilename)) == 0; #else bool ok = rename(srcFilename, dstFilename) == 0; if (!ok && errno == EXDEV) { ok = CopyFile(srcFilename, dstFilename) && DeleteFile(srcFilename); } return ok; #endif }
LogFile::~LogFile() { fs_.close(); fs_.clear(); std::size_t found = fileName_.find(L".log"); if (found == wstr_t::npos) { return; } wstr_t closeFileName = fileName_.substr(0, found); closeFileName += CLOCK.nowTime(L"_%Y%m%d-%H%M%S.log"); _wrename(fileName_.c_str(), closeFileName.c_str()); }
__inline void CFileBase::AdjustFileName(const SYSTEMTIME &Time, BOOL bAllFile /*= TRUE*/) { if (-1 != m_iBkCount) { ATLASSERT(m_strFileName.GetLength() != 0); CString strValue; strValue.Format(_T("%ld_"), m_iBkCount++); CString strFileName = m_strFileName; int Index = strFileName.ReverseFind(_T('\\')); strFileName.Insert(Index + 1, strValue); _wrename(m_strFileName, strFileName); return; } static CFileSearcher Searcher; CAtlArray<BACKUPFILE> arrFileNames; Searcher.Search(this, Time, arrFileNames); CString strNewName, strValue, strPath; m_iBkCount = (LONG)arrFileNames.GetCount(); for (long i = 0; i < m_iBkCount; i++) { strValue = arrFileNames[i].strFileName; int iIndex = strValue.Find(_T('_')); if (-1 != iIndex) strValue.Delete(0, iIndex + 1); else if (!bAllFile) continue; strPath = arrFileNames[i].strFolder; strNewName.Format(_T("%d_%s"), i, strValue); if (strNewName != arrFileNames[i].strFileName) _wrename(strPath + arrFileNames[i].strFileName, strPath + strNewName); } }
// renames file srcFilename to destFilename, returns true on success bool Rename(const std::string& srcFilename, const std::string& destFilename) { NGLOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); #ifdef _WIN32 if (_wrename(Common::UTF8ToUTF16W(srcFilename).c_str(), Common::UTF8ToUTF16W(destFilename).c_str()) == 0) return true; #else if (rename(srcFilename.c_str(), destFilename.c_str()) == 0) return true; #endif NGLOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename, GetLastErrorMsg()); return false; }
int _urename(const char* oldname, const char* newname){ wchar_t* woldname, *wnewname; int status; if(!(woldname = utf8_to_ucs2(oldname))) return 1; if(!(wnewname = utf8_to_ucs2(newname))){ free(woldname); return 1; } status = _wrename(woldname, wnewname); free(woldname); free(wnewname); return status; }
static int os_rename (lua_State *L) { const char *fromname = luaL_checkstring(L, 1); const char *toname = luaL_checkstring(L, 2); #ifdef WIN32 wchar_t wfromname[MAX_PATH+1]; wchar_t wtoname[MAX_PATH+1]; MultiByteToWideChar(CP_UTF8, 0, fromname, -1, wfromname, MAX_PATH+1); MultiByteToWideChar(CP_UTF8, 0, toname, -1, wtoname, MAX_PATH+1); return os_pushresult(L, _wrename(wfromname, wtoname) == 0, fromname); #else // FIXME: non-win32 conversion to local filesystem encoding? return os_pushresult(L, rename(fromname, toname) == 0, fromname); #endif }
static int tcommit_one(struct tfile_t* tf, size_t off) { #ifdef __WINNT__ wchar_t* wtf = 0; wchar_t* wf = 0; #endif int old_errno; if (fchmod(tf->fd, tf->mode)) goto err; #if 0 /* TODO: make this optional */ if (fsync(tf->fd)) goto err; #endif if (close(tf->fd)) goto err; /* TODO: rename is broken on windows. it doesn't allow to replace * exising files atomically. see google for existing implementations * and replace rename() for windows targets with a suitable one. */ #ifdef __WINNT__ wtf = utf8_to_ucs2(tf->tmpfilename); wf = utf8_to_ucs2(tf->filename); if (!wtf || !wf) goto err; if (_wrename(wtf, wf)) goto err; free(wtf); free(wf); #else if (rename(tf->tmpfilename, tf->filename)) goto err; #endif free(tf->filename); free(tf->tmpfilename); pool_cut(&mappool, off, sizeof(struct tfile_t)); return 0; err: #ifdef __WINNT__ if (wtf) free(wtf); if (wf) free(wf); #endif old_errno = errno; trollback_one(tf, off); errno = old_errno; return -1; }
bool System_RenameFile(char * oldName, char * newName) { #if defined(__WIN32__) bool result; uint16 * _woldName = __ecereNameSpace__ecere__sys__UTF8toUTF16(oldName, null); uint16 * _wnewName = __ecereNameSpace__ecere__sys__UTF8toUTF16(newName, null); result = _wrename(_woldName, _wnewName) == 0; __ecereNameSpace__ecere__com__eSystem_Delete(_woldName); __ecereNameSpace__ecere__com__eSystem_Delete(_wnewName); return result; #else return rename(oldName, newName) == 0; #endif }
/* bellow we provide a set of wrappers for some I/O functions to use wchar_t. * on win32 this solves the issue that we need paths to be utf-16 encoded. */ int subsurface_rename(const char *path, const char *newpath) { int ret = -1; if (!path || !newpath) return ret; wchar_t *wpath = utf8_to_utf16(path); wchar_t *wnewpath = utf8_to_utf16(newpath); if (wpath && wnewpath) ret = _wrename(wpath, wnewpath); free((void *)wpath); free((void *)wnewpath); return ret; }
/** \fn ADM_copyFile */ uint8_t ADM_renameFile(const char *source, const char *target) { int sourceFileNameLength = utf8StringToWideChar(source, -1, NULL); int targetFileNameLength = utf8StringToWideChar(target, -1, NULL); wchar_t *wcFileSource=(wchar_t*)_alloca(sourceFileNameLength*sizeof(wchar_t)); wchar_t *wcFileTarget= (wchar_t*)_alloca(targetFileNameLength * sizeof(wchar_t)); utf8StringToWideChar(source, -1, wcFileSource); utf8StringToWideChar(target, -1, wcFileTarget); if(!_wrename(wcFileSource,wcFileTarget)) return true; ADM_error("Failed to rename %s to %s\n",source,target); return false; }
int rename_utf8(const char *oldname, const char *newname) { wchar_t *wold = NULL; wchar_t *wnew = NULL; int ret = -1; while (1) { if (!(wold = wchar_from_utf8(oldname))) break; if (!(wnew = wchar_from_utf8(newname))) break; ret = _wrename(wold, wnew); break; } if (wold) free(wold); if (wnew) free(wnew); return ret; }