From: Arvin Schnell Date: Fri, 17 Aug 2012 10:04:49 +0000 (+0200) Subject: - thread safety X-Git-Tag: v0.1.3~147 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=73bd6adc1ce4784e898a2781af2b4e699a4b30f6;p=thirdparty%2Fsnapper.git - thread safety --- diff --git a/snapper/AppUtil.cc b/snapper/AppUtil.cc index bad25254..5c1d276f 100644 --- a/snapper/AppUtil.cc +++ b/snapper/AppUtil.cc @@ -117,7 +117,7 @@ namespace snapper int r1 = ioctl(dest_fd, BTRFS_IOC_CLONE, src_fd); if (r1 != 0) { - y2err("ioctl failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("ioctl failed errno:" << errno << " (" << stringerror(errno) << ")"); } return r1 == 0; @@ -131,7 +131,7 @@ namespace snapper int r1 = fstat(src_fd, &src_stat); if (r1 != 0) { - y2err("fstat failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("fstat failed errno:" << errno << " (" << stringerror(errno) << ")"); return false; } @@ -151,14 +151,14 @@ namespace snapper int r2 = read(src_fd, block, t); if (r2 != t) { - y2err("read failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("read failed errno:" << errno << " (" << stringerror(errno) << ")"); return false; } int r3 = write(dest_fd, block, t); if (r3 != t) { - y2err("write failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("write failed errno:" << errno << " (" << stringerror(errno) << ")"); return false; } @@ -199,6 +199,22 @@ namespace snapper } + string + stringerror(int errnum) + { +#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE + char buf1[100]; + if (strerror_r(errno, buf1, sizeof(buf1)-1) == 0) + return string(buf1); + return string("strerror failed"); +#else + char buf1[100]; + const char* buf2 = strerror_r(errno, buf1, sizeof(buf1)-1); + return string(buf2); +#endif + } + + string sformat(const string& format, ...) { diff --git a/snapper/AppUtil.h b/snapper/AppUtil.h index 7e4755ed..4bca1e7c 100644 --- a/snapper/AppUtil.h +++ b/snapper/AppUtil.h @@ -55,6 +55,8 @@ namespace snapper string realpath(const string& path); + string stringerror(int errnum); + template void classic(StreamType& stream) { diff --git a/snapper/Compare.cc b/snapper/Compare.cc index 937eb07a..d049b64f 100644 --- a/snapper/Compare.cc +++ b/snapper/Compare.cc @@ -49,7 +49,7 @@ namespace snapper dirfd = ::open(base_path.c_str(), O_RDONLY | O_NOATIME); if (dirfd < 0) { - y2err("open failed path:" << base_path << " error:" << strerror(errno)); + y2err("open failed path:" << base_path << " error:" << stringerror(errno)); throw IOErrorException(); } } @@ -61,7 +61,7 @@ namespace snapper dirfd = ::openat(dir.dirfd, name.c_str(), O_RDONLY | O_NOFOLLOW | O_NOATIME); if (dirfd < 0) { - y2err("open failed path:" << dir.fullname(name) << " (" << strerror(errno) << ")"); + y2err("open failed path:" << dir.fullname(name) << " (" << stringerror(errno) << ")"); throw IOErrorException(); } } @@ -93,14 +93,14 @@ namespace snapper int fd = dup(dirfd); if (fd == -1) { - y2err("dup failed" << " error:" << strerror(errno)); + y2err("dup failed" << " error:" << stringerror(errno)); throw IOErrorException(); } DIR* dp = fdopendir(fd); if (dp == NULL) { - y2err("fdopendir failed path:" << fullname() << " error:" << strerror(errno)); + y2err("fdopendir failed path:" << fullname() << " error:" << stringerror(errno)); ::close(fd); throw IOErrorException(); } diff --git a/snapper/Comparison.cc b/snapper/Comparison.cc index 938605b4..8c76dd64 100644 --- a/snapper/Comparison.cc +++ b/snapper/Comparison.cc @@ -190,7 +190,7 @@ namespace snapper FILE* file = mkstemp(tmp_name); if (!file) { - y2err("mkstemp failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("mkstemp failed errno:" << errno << " (" << stringerror(errno) << ")"); throw IOErrorException(); } diff --git a/snapper/File.cc b/snapper/File.cc index b0f3097c..8af4027d 100644 --- a/snapper/File.cc +++ b/snapper/File.cc @@ -326,7 +326,7 @@ namespace snapper int src_fd = open(getAbsolutePath(LOC_PRE).c_str(), O_RDONLY | O_LARGEFILE); if (src_fd < 0) { - y2err("open failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("open failed errno:" << errno << " (" << stringerror(errno) << ")"); return false; } @@ -334,21 +334,21 @@ namespace snapper O_CREAT | O_TRUNC, mode); if (dest_fd < 0) { - y2err("open failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("open failed errno:" << errno << " (" << stringerror(errno) << ")"); return false; } int r1 = fchmod(dest_fd, mode); if (r1 != 0) { - y2err("fchmod failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("fchmod failed errno:" << errno << " (" << stringerror(errno) << ")"); return false; } int r2 = fchown(dest_fd, owner, group); if (r2 != 0) { - y2err("fchown failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("fchown failed errno:" << errno << " (" << stringerror(errno) << ")"); return false; } diff --git a/snapper/Filesystem.cc b/snapper/Filesystem.cc index abd10a3a..944b7e18 100644 --- a/snapper/Filesystem.cc +++ b/snapper/Filesystem.cc @@ -166,7 +166,7 @@ namespace snapper } else if (errno != EEXIST) { - y2err("mkdir failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("mkdir failed errno:" << errno << " (" << stringerror(errno) << ")"); throw CreateConfigFailedException("mkdir failed"); } @@ -179,7 +179,7 @@ namespace snapper } else if (errno != EEXIST) { - y2err("mkdir failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("mkdir failed errno:" << errno << " (" << stringerror(errno) << ")"); throw CreateConfigFailedException("mkdir failed"); } } @@ -191,14 +191,14 @@ namespace snapper int r1 = rmdir((subvolume + "/.snapshots/.info").c_str()); if (r1 != 0) { - y2err("rmdir failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("rmdir failed errno:" << errno << " (" << stringerror(errno) << ")"); throw DeleteConfigFailedException("rmdir failed"); } int r2 = rmdir((subvolume + "/.snapshots").c_str()); if (r2 != 0) { - y2err("rmdir failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("rmdir failed errno:" << errno << " (" << stringerror(errno) << ")"); throw DeleteConfigFailedException("rmdir failed"); } } @@ -291,7 +291,7 @@ namespace snapper int r1 = mkdir(snapshotDir(num).c_str(), 0755); if (r1 != 0 && errno != EEXIST) { - y2err("mkdir failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("mkdir failed errno:" << errno << " (" << stringerror(errno) << ")"); throw MountSnapshotFailedException(); } diff --git a/snapper/Snapshot.cc b/snapper/Snapshot.cc index 00857efd..8066541f 100644 --- a/snapper/Snapshot.cc +++ b/snapper/Snapshot.cc @@ -359,7 +359,7 @@ namespace snapper if (errno == EEXIST) continue; - y2err("mkdir failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("mkdir failed errno:" << errno << " (" << stringerror(errno) << ")"); throw IOErrorException(); } @@ -416,14 +416,14 @@ namespace snapper catch (const IOErrorException& e) { y2err("saving info.xml failed infoDir: " << infoDir() << " errno: << " << errno << - " (" << strerror(errno) << ")"); + " (" << stringerror(errno) << ")"); throw; } if (rename(string(infoDir() + "/info.xml.tmp").c_str(), string(infoDir() + "/info.xml").c_str()) != 0) { y2err("rename info.xml failed infoDir: " << infoDir() << " errno: << " << errno << - " (" << strerror(errno) << ")"); + " (" << stringerror(errno) << ")"); throw IOErrorException(); } } diff --git a/snapper/SystemCmd.cc b/snapper/SystemCmd.cc index 7ad99e83..ff9d5118 100644 --- a/snapper/SystemCmd.cc +++ b/snapper/SystemCmd.cc @@ -189,12 +189,12 @@ SystemCmd::doExecute( const string& Cmd ) bool ok_bi = true; if( !testmode && pipe(sout)<0 ) { - y2err("pipe stdout creation failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("pipe stdout creation failed errno:" << errno << " (" << stringerror(errno) << ")"); ok_bi = false; } if( !testmode && !Combine_b && pipe(serr)<0 ) { - y2err("pipe stderr creation failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("pipe stderr creation failed errno:" << errno << " (" << stringerror(errno) << ")"); ok_bi = false; } if( !testmode && ok_bi ) @@ -202,14 +202,14 @@ SystemCmd::doExecute( const string& Cmd ) pfds[0].fd = sout[0]; if( fcntl( pfds[0].fd, F_SETFL, O_NONBLOCK )<0 ) { - y2err("fcntl O_NONBLOCK failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("fcntl O_NONBLOCK failed errno:" << errno << " (" << stringerror(errno) << ")"); } if( !Combine_b ) { pfds[1].fd = serr[0]; if( fcntl( pfds[1].fd, F_SETFL, O_NONBLOCK )<0 ) { - y2err("fcntl O_NONBLOCK failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("fcntl O_NONBLOCK failed errno:" << errno << " (" << stringerror(errno) << ")"); } } y2deb("sout:" << pfds[0].fd << " serr:" << (Combine_b?-1:pfds[1].fd)); @@ -220,23 +220,23 @@ SystemCmd::doExecute( const string& Cmd ) setenv( "LANGUAGE", "C", 1 ); if( dup2( sout[1], STDOUT_FILENO )<0 ) { - y2err("dup2 stdout child failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("dup2 stdout child failed errno:" << errno << " (" << stringerror(errno) << ")"); } if( !Combine_b && dup2( serr[1], STDERR_FILENO )<0 ) { - y2err("dup2 stderr child failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("dup2 stderr child failed errno:" << errno << " (" << stringerror(errno) << ")"); } if( Combine_b && dup2( STDOUT_FILENO, STDERR_FILENO )<0 ) { - y2err("dup2 stderr child failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("dup2 stderr child failed errno:" << errno << " (" << stringerror(errno) << ")"); } if( close( sout[0] )<0 ) { - y2err("close child failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("close child failed errno:" << errno << " (" << stringerror(errno) << ")"); } if( !Combine_b && close( serr[0] )<0 ) { - y2err("close child failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("close child failed errno:" << errno << " (" << stringerror(errno) << ")"); } closeOpenFds(); Ret_i = execl( Shell_Ci.c_str(), Shell_Ci.c_str(), "-c", @@ -249,24 +249,24 @@ SystemCmd::doExecute( const string& Cmd ) default: if( close( sout[1] )<0 ) { - y2err("close parent failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("close parent failed errno:" << errno << " (" << stringerror(errno) << ")"); } if( !Combine_b && close( serr[1] )<0 ) { - y2err("close parent failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("close parent failed errno:" << errno << " (" << stringerror(errno) << ")"); } Ret_i = 0; File_aC[IDX_STDOUT] = fdopen( sout[0], "r" ); if( File_aC[IDX_STDOUT] == NULL ) { - y2err("fdopen stdout failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("fdopen stdout failed errno:" << errno << " (" << stringerror(errno) << ")"); } if( !Combine_b ) { File_aC[IDX_STDERR] = fdopen( serr[0], "r" ); if( File_aC[IDX_STDERR] == NULL ) { - y2err("fdopen stderr failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("fdopen stderr failed errno:" << errno << " (" << stringerror(errno) << ")"); } } if( !Background_b ) @@ -312,7 +312,7 @@ SystemCmd::doWait( bool Hang_bv, int& Ret_ir ) int sel = poll( pfds, Combine_b?1:2, 1000 ); if (sel < 0) { - y2err("poll failed errno:" << errno << " (" << strerror(errno) << ")"); + y2err("poll failed errno:" << errno << " (" << stringerror(errno) << ")"); } y2deb("poll ret:" << sel); if( sel>0 ) @@ -482,8 +482,8 @@ SystemCmd::checkOutput() #define BUF_LEN 256 void -SystemCmd::getUntilEOF( FILE* File_Cr, vector& Lines_Cr, - bool& NewLine_br, bool Stderr_bv ) +SystemCmd::getUntilEOF(FILE* File_Cr, vector& Lines_Cr, bool& NewLine_br, + bool Stderr_bv) { size_t old_size = Lines_Cr.size(); char Buf_ti[BUF_LEN]; diff --git a/snapper/SystemCmd.h b/snapper/SystemCmd.h index 863ee01e..93e6854e 100644 --- a/snapper/SystemCmd.h +++ b/snapper/SystemCmd.h @@ -85,9 +85,9 @@ namespace snapper void closeOpenFds() const; int doExecute(const string& Cmd_Cv); bool doWait(bool Hang_bv, int& Ret_ir); - void checkOutput(); - void getUntilEOF(FILE* File_Cr, std::vector& Lines_Cr, - bool& NewLineSeen_br, bool Stderr_bv); + void checkOutput(); + void getUntilEOF(FILE* File_Cr, std::vector& Lines_Cr, bool& NewLineSeen_br, + bool Stderr_bv); void extractNewline(const string& Buf_ti, int Cnt_ii, bool& NewLineSeen_br, string& Text_Cr, std::vector& Lines_Cr); void addLine(const string& Text_Cv, std::vector& Lines_Cr);