void FileStream::SetLength(ulong Length) { if (m_fileAccess == FileAccess::Read) throw CryptoProcessingException("FileStream:SetLength", "The file was opened as read only!"); if (Length < m_fileSize) { #if defined(CEX_OS_WINDOWS) int handle; if (_sopen_s(&handle, m_fileName.c_str(), _O_RDWR | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE) == 0) _chsize(handle, Length); #elif defined(CEX_OS_POSIX) truncate(m_fileName.c_str(), Length); #endif } else if (Length > m_fileSize) { m_fileStream.seekg(Length - 1, std::ios::beg); m_fileStream.write("", 1); m_fileStream.seekg(0, std::ios::beg); } }
bson_reader_t * bson_reader_new_from_file (const char *path, /* IN */ bson_error_t *error) /* OUT */ { char errmsg_buf[BSON_ERROR_BUFFER_SIZE]; char *errmsg; int fd; bson_return_val_if_fail (path, NULL); #ifdef BSON_OS_WIN32 if (_sopen_s (&fd, path, (_O_RDONLY | _O_BINARY), _SH_DENYNO, 0) != 0) { fd = -1; } #else fd = open (path, O_RDONLY); #endif if (fd == -1) { errmsg = bson_strerror_r (errno, errmsg_buf, sizeof errmsg_buf); bson_set_error (error, BSON_ERROR_READER, BSON_ERROR_READER_BADFD, "%s", errmsg); return NULL; } return bson_reader_new_from_fd (fd, true); }
int NXLFSMessageCache::store(const NXPart* data, int oid) { insert(data, oid); if(!m_cache || !m_maxdisk) return 0; NXMutexLocker ml(m_mutex, "store", data->msgid); char p[MAX_PATH]; if(sprintf_s(p, "%s\\%s\\%X\\%s", m_cache, "msg", slot(data->msgid), data->msgid) == -1) return R(-1, "buffer error"); int fd = -1; if(_sopen_s(&fd, p, _O_BINARY | _O_WRONLY | _O_CREAT, _SH_DENYWR, _S_IWRITE)) return R(-11, "open error"); if(fd == -1) return R(-12, "no file descriptor"); size_t len = sizeof(NXPart) + data->len - 1; if(len != _write(fd, data, len)) { _close(fd); return R(-13, "write error"); } _close(fd); // ERR return 0; }
int STORAGE::DynamicMemoryMappedFile::getFileDescriptor(const char *fname, bool create) { int fd; int err; if (create) { err = _sopen_s(&fd, fname, _O_RDWR | _O_BINARY | _O_RANDOM | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE); } else { err = _sopen_s(&fd, fname, _O_RDWR | _O_BINARY | _O_RANDOM, _SH_DENYNO, _S_IREAD | _S_IWRITE); } if (err != 0) { std::ostringstream os; os << err; logEvent(ERROR, "Unable to open backing file, aborted with error " + os.str()); shutdown(FAILURE); } return fd; }
int EbrOpenWithPermission(const char* file, int mode, int share, int pmode) { if (strcmp(file, "/dev/urandom") == 0) { EbrFileDevRandom* ret = new EbrFileDevRandom(); EbrFile* addedFile = EbrAllocFile(ret); return addedFile->idx; } bool stop = false; if (stop) { return -1; } int ret = -1; _sopen_s(&ret, CPathMapper(file), mode, share, pmode); if (ret == -1) { return -1; } EbrIOFile* newFile = new EbrIOFile(); newFile->filefd = ret; EbrFile* addedFile = EbrAllocFile(newFile); return addedFile->idx; }
// // M_ReadFile // int M_ReadFile (char const* name, byte** buffer) { int handle, count, length; struct stat fileinfo; byte *buf; _sopen_s(&handle, name, O_RDONLY | O_BINARY, SH_DENYRW, _S_IREAD); if (handle == -1) I_Error("Couldn't read file %s", name); if (fstat(handle, &fileinfo) == -1) I_Error("Couldn't read file %s", name); length = fileinfo.st_size; buf = reinterpret_cast<byte*>(Z_Malloc(length, PU_STATIC, NULL)); count = _read(handle, buf, length); _close(handle); if (count < length) I_Error("Couldn't read file %s", name); *buffer = buf; return length; }
//-------------------------------------------------------------------------- int HTTPDServer::page (struct MHD_Connection *connection, const char * page) { int ret = 0; // char * root = getenv("FAUSTDocumentRoot"); string file = "."; file += page; const char* type = getMIMEType (file); int fd; #if defined(_WIN32) && !defined(__MINGW32__) int fhd; fd = _sopen_s(&fhd, file.c_str(), _O_RDONLY, _SH_DENYNO, _S_IREAD); #else fd = open (file.c_str(), O_RDONLY); #endif if (fd != -1) { int length = lseek(fd, (long)0, SEEK_END); lseek(fd, 0, SEEK_SET); struct MHD_Response *response = MHD_create_response_from_fd (length, fd); if (!response ) { cerr << "MHD_create_response_from_fd error: null response\n"; return MHD_NO; } MHD_add_response_header (response, "Content-Type", type ? type : "text/plain"); MHD_add_response_header (response, "Access-Control-Allow-Origin", "*"); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); } else { ret = send (connection, "", 0, MHD_HTTP_NOT_FOUND); } return ret; }
Log::LogFile::LogFile(const string& filename) { #ifdef _WIN32 _sopen_s(&file_descriptor, filename.c_str(), _O_APPEND | _O_RDWR | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE); #else file_descriptor = open(filename.c_str(), O_APPEND | O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); #endif file_log = Destination(new rlog::StdioNode(file_descriptor, DEFAULT_FLAGS)); }
/* Function to wrap open() */ int libewf_common_open( const char *filename, uint8_t flags ) { #if defined( HAVE_WINDOWS_API ) int file_descriptor = 0; #endif int open_flags = 0; if( filename == NULL ) { LIBEWF_WARNING_PRINT( "libewf_common_open: invalid filename.\n" ); return( -1 ); } if( flags == LIBEWF_OPEN_READ ) { #if defined( HAVE_WINDOWS_API ) open_flags = _O_RDONLY | _O_BINARY; #else open_flags = O_RDONLY; #endif } else if( flags == LIBEWF_OPEN_WRITE ) { #if defined( HAVE_WINDOWS_API ) open_flags = _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY; #else open_flags = O_WRONLY | O_CREAT | O_TRUNC; #endif } else if( flags == LIBEWF_OPEN_READ_WRITE ) { #if defined( HAVE_WINDOWS_API ) open_flags = _O_RDWR | _O_BINARY; #else open_flags = O_RDWR ; #endif } else { LIBEWF_WARNING_PRINT( "libewf_common_open: flags not supported.\n" ); return( -1 ); } #if defined( HAVE_WINDOWS_API ) if( _sopen_s( &file_descriptor, filename, open_flags, _SH_DENYRW, ( _S_IREAD | _S_IWRITE ) ) != 0 ) { LIBEWF_WARNING_PRINT( "libewf_common_open: error opening file.\n" ); return( -1 ); } return( file_descriptor ); #else return( open( filename, open_flags, 0644 ) ); #endif }
const NXPart* NXLFSMessageCache::fetch(const char* msgid) { if(!m_cache || !m_maxdisk) return NULL; int r; char p[MAX_PATH]; if(sprintf_s(p, "%s\\%s\\%X\\%s", m_cache, "msg", slot(msgid), msgid) == -1) { r = R(-1, "buffer error"); return NULL; } int fd = -1; if(_sopen_s(&fd, p, _O_BINARY | _O_RDONLY, _SH_DENYWR, _S_IWRITE)) return NULL; if(fd == -1) return NULL; auto_close acfd(fd); struct stat st; if(fstat(fd, &st)) { r = R(-2, "stat error"); return NULL; } size_t size = st.st_size; if(size <= sizeof(NXPart)) { r = R(-3, "file too small"); return NULL; } auto_free<char> data0(size); if(size != _read(fd, data0, size)) { r = R(-4, "read error"); return NULL; } acfd.close(); NXPart* data = (NXPart*)((char*)data0);// todo: review if(data->version != NXPARTVERSION) { _unlink(p); r = R(-5, "wrong part version"); return NULL; } if(strcmp(data->msgid, msgid)) { _unlink(p); r = R(-6, "msgid mismatch"); return NULL; } if(data->len != size - (sizeof(NXPart)-1)) { _unlink(p); r = R(-7, "length mismatch"); return NULL; } //data->len = size - (sizeof(NXPart) - 1); cfuse(msgid, size); data0.release(); return insert(data, oid()); }
int create_truncated_file(const char *filename, off_t size) { int file, result = 0; if (!_sopen_s(&file, filename, _O_RDWR | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE)) { result = _chsize(file, size); _close(file); } return result; }
BOOL Stat(PCHAR Filename, struct _stat &Dest) { int client = 0; errno_t err = _sopen_s(&client, Filename, _O_RDONLY,_SH_DENYNO, _S_IREAD | _S_IWRITE); if (err) { return FALSE; } _fstat(client,&Dest); _close(client); return TRUE; }
static errno_t open_internal(LPCSTR fn, int& handle) { return ( _sopen_s( &handle, fn, _O_RDONLY | _O_BINARY, _SH_DENYNO, _S_IREAD ) ); }
CVersus::CVersus() { // Find the versus data. First check in the current directory // then in the path provided by the registry. If both fail, // disable versus. _sopen_s(&_versus_fh, "versus.bin", _O_RDONLY | _O_BINARY, _SH_DENYWR, NULL); if (_versus_fh == k_undefined) { _sopen_s(&_versus_fh, prefs.versus_path(), _O_RDONLY | _O_BINARY, _SH_DENYWR, NULL); } if (_versus_fh == k_undefined) { // We do no longer warn directly, // but only when versus symbols get used without the file. versus_bin_not_loaded = true; } else { versus_bin_not_loaded = false; } }
int hostpc_tx_sizeB(char *to_pcie_file) { int filesize; if( _sopen_s( &fhB, to_pcie_file, _O_RDONLY | _O_BINARY, _SH_DENYNO, 0 ) ) { perror( "open failed on input file" ); exit( 1 ); } filesize = _filelength(fhB); return filesize; }
// _____________________________________________________________________ // // // MAIN // _____________________________________________________________________ // int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) { // One instance is enough HANDLE hGlobalLock = CreateMutex(NULL, TRUE, L"RestoreWindowsMutex"); if(hGlobalLock == INVALID_HANDLE_VALUE || GetLastError() == ERROR_ALREADY_EXISTS) return 0; const char* tok = 0; char* context = NULL; const char* delims = " =\t"; tok = strtok_s(lpCmdLine, delims, &context); while(tok) { if(!_stricmp(tok, "--debuglog")) { _sopen_s(&logfile, "RestoreWindows.log", _O_WRONLY | _O_BINARY | _O_TRUNC | _O_CREAT, _SH_DENYNO, _S_IWRITE); } else if(!_stricmp(tok, "--delay")) { tok = strtok_s(0, delims, &context); if(tok) ResumeTimerValue = clamp(0, 1000*60*10, atoi(tok)); } tok = strtok_s(0, delims, &context); } DesiredMonitorPlacements = GetAllMonitors(); Log("Delay: %d\n\n", ResumeTimerValue); for(auto it = DesiredMonitorPlacements.begin(); it != DesiredMonitorPlacements.end(); ++it) Log("Monitor: %d, %d\n", it->right-it->left, it->bottom-it->top); Log("\n"); LoadAllWindowPlacements(); HWINEVENTHOOK hWinEventHook = SetWinEventHook(EVENT_MIN, EVENT_MAX, NULL, WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT); DetectWindow = CreateDetectionWnd(); MSG msg; while(GetMessage(&msg, NULL, 0, 0) > 0) { DispatchMessage(&msg); } // Usually never gets here... UnhookWinEvent(hWinEventHook); _close(logfile); CloseHandle(hGlobalLock); return 0; }
int main(int argc, char* argv[]) { int h_bmp1, h_bmp2, h_bmp3; errno_t err_file1 = _sopen_s(&h_bmp1,"man.bmp", _O_RDONLY, _SH_DENYNO,_S_IREAD | _S_IWRITE); errno_t err_file2 = _sopen_s(&h_bmp2,"background.bmp", _O_RDONLY, _SH_DENYNO,_S_IREAD | _S_IWRITE); h_bmp3 = _creat( "res.bmp", _S_IREAD | _S_IWRITE ); if( err_file1||err_file2||(h_bmp3==-1)) exit( 1 ); unsigned char * buf_bmp1, * buf_bmp2, * man, * background; unsigned long len_bmp1, len_bmp2; len_bmp1 = _filelength(h_bmp1); len_bmp2 = _filelength(h_bmp2); man = buf_bmp1 = new unsigned char[len_bmp1]; background = buf_bmp2 = new unsigned char[len_bmp2]; buf_bmp1=background;// _read(h_bmp1, man, len_bmp1); _read(h_bmp2, background, len_bmp2); BITMAPFILEHEADER bmp_fh1, bmp_fh2; BITMAPINFO bmp_inf1, bmp_inf2; bmp_fh1 = *((BITMAPFILEHEADER*)man); bmp_fh2 = *((BITMAPFILEHEADER*)background); man = man + sizeof(BITMAPFILEHEADER); background = background + sizeof(BITMAPFILEHEADER); bmp_inf1 = *((BITMAPINFO*)man); bmp_inf2 = *((BITMAPINFO*)background); man = man + sizeof(BITMAPINFO); background = background + sizeof(BITMAPINFO); mmx(man,background, bmp_inf1.bmiHeader.biSizeImage); _write(h_bmp3,buf_bmp1/*man*/, len_bmp1); _close(h_bmp1); _close(h_bmp2); _close(h_bmp3); return 0; }
/** * truncates a file to the given length, if the file exists and can be opened * for writing. */ inline void truncate(const std::string& filename, size_t length) { #if defined(_WIN32) int fd = -1; _sopen_s(&fd, filename.c_str(), _O_WRONLY | _O_BINARY, _SH_DENYNO, _S_IWRITE); if (fd != -1) { _chsize_s(fd, static_cast<__int64>(length)); _close(fd); } #else ::truncate(filename.c_str(), static_cast<off_t>(length)); #endif }
int websPageOpen(webs_t wp, char_t *lpath, char_t *path, int mode, int perm) { #if defined(WIN32) errno_t error; #endif a_assert(websValid(wp)); #ifdef WEBS_PAGE_ROM return websRomPageOpen(wp, path, mode, perm); #elif defined(WIN32) error = _sopen_s(&(wp->docfd), lpath, mode, _SH_DENYNO, _S_IREAD); return (wp->docfd = gopen(lpath, mode, _S_IREAD)); #else return (wp->docfd = gopen(lpath, mode, perm)); #endif /* WEBS_PAGE_ROM */ }
/****************************************************************************** Open a new GIF file for write, specified by name. If TestExistance then if the file exists this routines fails (returns NULL). Returns a dynamically allocated GifFileType pointer which serves as the GIF info record. The Error member is cleared if successful. ******************************************************************************/ GifFileType * EGifOpenFileName(const char *FileName, const bool TestExistence, int *Error) { int FileHandle; GifFileType *GifFile; errno_t openErr; if (TestExistence) openErr = _sopen_s(&FileHandle, FileName, _O_WRONLY | _O_CREAT | _O_EXCL, _SH_DENYRW, _S_IREAD | _S_IWRITE); else openErr = _sopen_s(&FileHandle, FileName, _O_WRONLY | _O_CREAT | _O_TRUNC, _SH_DENYRW, _S_IREAD | _S_IWRITE); if (openErr != 0) { if (Error != NULL) *Error = E_GIF_ERR_OPEN_FAILED; return NULL; } GifFile = EGifOpenFileHandle(FileHandle, Error); if (GifFile == (GifFileType *) NULL) (void)_close(FileHandle); return GifFile; }
/****************************************************************************** Open a new GIF file for read, given by its name. Returns dynamically allocated GifFileType pointer which serves as the GIF info record. ******************************************************************************/ GifFileType * DGifOpenFileName(const char *FileName, int *Error) { int FileHandle; GifFileType *GifFile; errno_t theError = _sopen_s(&FileHandle, FileName, _O_RDONLY, _SH_DENYWR, 0); if (FileHandle == -1) { if (Error != NULL) *Error = D_GIF_ERR_OPEN_FAILED; return NULL; } GifFile = DGifOpenFileHandle(FileHandle, Error); return GifFile; }
void RunnerDB::saveRunners(const char *file) { int f=-1; _sopen_s(&f, file, _O_BINARY|_O_CREAT|_O_TRUNC|_O_WRONLY, _SH_DENYWR, _S_IREAD|_S_IWRITE); if (f!=-1) { int version = 5460002; _write(f, &version, 4); _write(f, &dataDate, 4); _write(f, &dataTime, 4); if (!rdb.empty()) _write(f, &rdb[0], rdb.size()*sizeof(RunnerDBEntry)); _close(f); } else throw std::exception("Could not save runner database."); }
/** * Fallback version of open_cb NOT providing racefree CLOEXEC, * but setting CLOEXEC after file open (if FD_CLOEXEC is defined). */ int rd_kafka_open_cb_generic (const char *pathname, int flags, mode_t mode, void *opaque) { #ifndef _MSC_VER int fd; int on = 1; fd = open(pathname, flags, mode); if (fd == -1) return -1; #ifdef FD_CLOEXEC fcntl(fd, F_SETFD, FD_CLOEXEC, &on); #endif return fd; #else int fd; if (_sopen_s(&fd, pathname, flags, _SH_DENYNO, mode) != 0) return -1; return fd; #endif }
/** * @brief A secure version of Windows open. * * @param[in] filename - File name. * @param[in] oflag - Type of operations allowed. * @param[in] pmode - Permission mode. * * @return int * @retval a file descriptor for the opened file. success * @retval -1 indicates an error. * * @ see open() on MSDN. */ int win_open(const char *filename, int oflag, ...) { int fd = -1; errno_t err = 0; int pmode = -1; va_list vl = NULL; va_start(vl, oflag); pmode = va_arg(vl, int) & (S_IREAD | S_IWRITE); err = _sopen_s(&fd, filename, oflag, _SH_DENYNO, pmode); va_end(vl); if ((err) || (fd == -1)) { errno = err; return (-1); } return fd; }
qboolean M_WriteFile (char const* name, void* source, int length) { int handle; int count; _sopen_s(&handle, name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, SH_DENYRW, _S_IWRITE); if (handle == -1) return false; count = _write(handle, source, length); _close(handle); if (count < length) return false; return true; }
//////////////////////////////////////////////////////////////////////////////// /// Ouverture du fichier /// /// Parametres : /// \param filename nom du fichier /// \param oflag cf parametre de _sopen_s /// \param shflag cf parametre de _sopen_s /// \param pmode cf parametre de _sopen_s /// /// \return true si le fichier est ouvert, false sinon //////////////////////////////////////////////////////////////////////////////// bool CAdeFile::Open(const std::string& fileName, int oflag, int shflag, int pmode) { if (m_Fd != -1) { // un fichier est deja ouvert return false; } //ouverture du fichier if(_sopen_s( &m_Fd , fileName.c_str() , oflag , shflag , pmode ) != 0) { // fichier pas ouvert m_Fd = -1; return false; } return true; }
inline int mkstemp(std::string& filename) { #if defined(_WIN32) std::string path, name; detail::splitpath(filename, path, name); char temppath[MAX_PATH]; // note: GetTempFileName only uses the first 3 chars of the prefix (name) // specified. if (GetTempFileName(path.c_str(), name.c_str(), 0, temppath) == 0) { // failed! return invalid handle. return -1; } int handle = -1; // _sopen_s sets handle to -1 on error. _sopen_s(&handle, temppath, _O_WRONLY | _O_CREAT | _O_BINARY, _SH_DENYNO, _S_IWRITE); if (handle != -1) filename = temppath; return handle; #else // defined(_WIN32) std::unique_ptr<char[]> s_template(new char[filename.size() + 1]); std::copy(filename.begin(), filename.end(), s_template.get()); s_template[filename.size()] = 0; int handle = -1; #if defined(__MACH__) // TODO: figure out how to open with O_SYNC handle = ::mkstemp(s_template.get()); #else handle = mkostemp(s_template.get(), O_WRONLY | O_SYNC); #endif if (handle != -1) filename = s_template.get(); return handle; #endif // defined(_WIN32) }
/** * \brief Change the file of a size. Smaller sizes will truncate the file, larger sizes will pad * the file with zeroes. */ static void change_size(const std::string & path, boost::int64_t length) { #if defined(__GNUC__) if (truncate(path.c_str(), length) != 0) throw std::runtime_error("could not change size: " + get_error_string(errno)); #elif defined(_MSC_VER) int fh; if( _sopen_s( &fh, path.c_str(), _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE ) == 0 ) { int result = _chsize_s( fh, length ); if (result != 0) { _close( fh ); throw std::runtime_error( std::string("could not change size: ") + get_error_string(errno) ); } _close( fh ); } else throw std::runtime_error("could not open file: " + get_error_string(errno)); #endif }
mongoc_stream_t * mongoc_stream_file_new_for_path (const char *path, /* IN */ int flags, /* IN */ int mode) /* IN */ { int fd = -1; BSON_ASSERT (path); #ifdef _WIN32 if (_sopen_s (&fd, path, (flags | _O_BINARY), _SH_DENYNO, mode) != 0) { fd = -1; } #else fd = open (path, flags, mode); #endif if (fd == -1) { return NULL; } return mongoc_stream_file_new (fd); }
static int ms_open(const char *filename, int oflag, int pmode) { int result = -1; _sopen_s(&result, filename, oflag | O_BINARY, _SH_DENYNO, pmode); return result; }