12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #include "dated_copy.h"
- #include "dirname.h"
- #include "basename.h"
- #include <ctime>
- #include <fstream>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <iostream>
- #if !defined(_WIN32)
- #include <unistd.h>
- IGL_INLINE bool igl::dated_copy(const std::string & src_path, const std::string & dir)
- {
- using namespace std;
-
- char buffer[80];
- time_t rawtime;
- struct tm * timeinfo;
- time (&rawtime);
- timeinfo = localtime (&rawtime);
-
- strftime (buffer,80,"%Y-%m-%dT%H-%M-%S",timeinfo);
- string src_basename = basename(src_path);
- string dst_basename = src_basename+"-"+buffer;
- string dst_path = dir+"/"+dst_basename;
- cerr<<"Saving binary to "<<dst_path;
- {
-
- ifstream src(src_path,ios::binary);
- if (!src.is_open())
- {
- cerr<<" failed."<<endl;
- return false;
- }
- ofstream dst(dst_path,ios::binary);
- if(!dst.is_open())
- {
- cerr<<" failed."<<endl;
- return false;
- }
- dst << src.rdbuf();
- }
- cerr<<" succeeded."<<endl;
- cerr<<"Setting permissions of "<<dst_path;
- {
- int src_posix = fileno(fopen(src_path.c_str(),"r"));
- if(src_posix == -1)
- {
- cerr<<" failed."<<endl;
- return false;
- }
- struct stat fst;
- fstat(src_posix,&fst);
- int dst_posix = fileno(fopen(dst_path.c_str(),"r"));
- if(dst_posix == -1)
- {
- cerr<<" failed."<<endl;
- return false;
- }
-
- if(fchown(dst_posix,fst.st_uid,fst.st_gid))
- {
- cerr<<" failed."<<endl;
- return false;
- }
-
- if(fchmod(dst_posix,fst.st_mode) == -1)
- {
- cerr<<" failed."<<endl;
- return false;
- }
- cerr<<" succeeded."<<endl;
- }
- return true;
- }
- IGL_INLINE bool igl::dated_copy(const std::string & src_path)
- {
- return dated_copy(src_path,dirname(src_path));
- }
- #endif
|