CubitString CubitUtil::get_temporary_filename() { CubitString ret_str; #ifdef WIN32 //get a place to put the temporary file CubitString temp_path = get_temp_directory(); // make an empty temporary and return the name for it wchar_t temp_file_name[MAX_PATH]; if( GetTempFileNameW(CubitString::toUtf16(temp_path.c_str()).c_str(), L"CBT", 0, temp_file_name) != 0 ) ret_str = CubitString::toUtf8(temp_file_name); #else CubitString tmpdir = get_temp_directory(); const char* filepattern = "CBT.XXXXXX"; //needs to be two longer because of the "/"? char *temp_file_name = new char[tmpdir.length() + strlen(filepattern) + 2]; sprintf(temp_file_name, "%s/%s", tmpdir.c_str(), filepattern); // make an empty file and return the name for it int fd = mkstemp(temp_file_name); if( fd != -1 ) { ret_str = temp_file_name; // release the open done by mkstemp, // temporary file still exists close(fd); } delete [] temp_file_name; #endif return ret_str; }
/** * Create a path name with the following format: * "/tmp/@prefix-$PID-XXXXXX". The returned C string is either * auto-freed if @context is NULL. This function returns NULL if an * error occurred. */ char *create_temp_name(TALLOC_CTX *context, const char *prefix) { const char *temp_directory = get_temp_directory(); char *name; if (context == NULL) context = talloc_autofree_context(); name = talloc_asprintf(context, "%s/%s-%d-XXXXXX", temp_directory, prefix, getpid()); if (name == NULL) { note(NULL, ERROR, INTERNAL, "can't allocate memory"); return NULL; } return name; }
/** * Remove recursively the content of the current working directory. * This latter has to lie in temp_directory (ie. "/tmp" on most * systems). This function returns -1 if a fatal error occured * (ie. the recursion must be stopped), the number of non-fatal errors * otherwise. * * WARNING: this function changes the current working directory for * the calling process. */ static int clean_temp_cwd() { const char *temp_directory = get_temp_directory(); const size_t length_temp_directory = strlen(temp_directory); char *prefix = NULL; int nb_errors = 0; DIR *dir = NULL; int status; prefix = talloc_size(NULL, length_temp_directory + 1); if (prefix == NULL) { note(NULL, WARNING, INTERNAL, "can't allocate memory"); nb_errors++; goto end; } /* Sanity check: ensure the current directory lies in * "/tmp". */ status = readlink("/proc/self/cwd", prefix, length_temp_directory); if (status < 0) { note(NULL, WARNING, SYSTEM, "can't readlink '/proc/self/cwd'"); nb_errors++; goto end; } prefix[status] = '\0'; if (strncmp(prefix, temp_directory, length_temp_directory) != 0) { note(NULL, ERROR, INTERNAL, "trying to remove a directory outside of '%s', " "please report this error.\n", temp_directory); nb_errors++; goto end; } dir = opendir("."); if (dir == NULL) { note(NULL, WARNING, SYSTEM, "can't open '.'"); nb_errors++; goto end; } while (1) { struct dirent *entry; errno = 0; entry = readdir(dir); if (entry == NULL) break; if ( strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; status = chmod(entry->d_name, 0700); if (status < 0) { note(NULL, WARNING, SYSTEM, "cant chmod '%s'", entry->d_name); nb_errors++; continue; } if (entry->d_type == DT_DIR) { status = chdir(entry->d_name); if (status < 0) { note(NULL, WARNING, SYSTEM, "can't chdir '%s'", entry->d_name); nb_errors++; continue; } /* Recurse. */ status = clean_temp_cwd(); if (status < 0) { nb_errors = -1; goto end; } nb_errors += status; status = chdir(".."); if (status < 0) { note(NULL, ERROR, SYSTEM, "can't chdir to '..'"); nb_errors = -1; goto end; } status = rmdir(entry->d_name); } else { status = unlink(entry->d_name); } if (status < 0) { note(NULL, WARNING, SYSTEM, "can't remove '%s'", entry->d_name); nb_errors++; continue; } } if (errno != 0) { note(NULL, WARNING, SYSTEM, "can't readdir '.'"); nb_errors++; } end: TALLOC_FREE(prefix); if (dir != NULL) (void) closedir(dir); return nb_errors; }
//----------------------------------------------------------------------------- int main( int argc, char* argv[] ) { std::string path; std::string temp_path, root_path; // names of files and directories std::string test_file = "test.txt"; std::string subdir1_name = "subdir1"; std::string subdir2_name = "subdir2"; std::string subsubdir1_name = "subsubdir1"; std::string subdir_test_file = "subtest.txt"; // get the current working path from the system std::string working_path = get_current_path(); // attempt to create a temporary directory if( !get_temp_directory( temp_path ) ) return 5; // temp path just reported to be successfully created. test existence method if( !path_exists( temp_path ) ) return 1; // temp path is a path to a directory, so test is_directory if( !is_directory( temp_path ) ) return 2; // create a file in the temp path if( !create_test_file( temp_path, test_file ) ) return 8; // already know temp path is not a file, so test is_file for negative case if( is_file( temp_path ) ) return 3; // test whether the is file method reports the test file correctly path = combine_path( temp_path, test_file ); if( !is_file( path ) ) return 3; // attempt to remove the temporary directory if( !remove_directory( temp_path ) ) return 6; //-- // get another temporary directory this time call it the root path if( !get_temp_directory( root_path ) ) return 7; // create a file in the root path if( !create_test_file( root_path, test_file ) ) return 8; // create a directory in the root path std::string subdir1_path = combine_path( root_path, subdir1_name ); if( !get_directory( subdir1_path ) ) return 9; // create another directory in the root path std::string subdir2_path = combine_path( root_path, subdir2_name ); if( !get_directory( subdir2_path ) ) return 9; // create a directory in the first child directory of the root path std::string subsubdir1_path = combine_path( subdir1_path, subsubdir1_name ); if( !get_directory( subsubdir1_path ) ) return 9; // create a file in the child of the child directory if( !create_test_file( subsubdir1_path, subdir_test_file ) ) return 8; /* // get the subdirectories of the root std::vector<std::string> dirs = get_subdirectories( root_path ); for( std::vector<std::string>::iterator it = dirs.begin(); it != dirs.end(); it++ ) { printf( "dir: %s\n", it->c_str() ); } */ // test find file without recursion. search for the test file in the root if( !find_file( path, test_file, root_path ) ) return 1; // validate the path returned from find file temp_path = combine_path( root_path, test_file ); if( temp_path != path ) return 1; // test find file with recursion. search for the subtest file in the dirs if( !find_file( path, subdir_test_file, root_path, true ) ) return 1; temp_path = combine_path( subsubdir1_path, subdir_test_file ); if( temp_path != path ) return 1; // clean up the root path if( !remove_directory( root_path ) ) return 6; if( find_file( path, "gzserver", "/usr/local", true ) ) { printf( "path: %s\n", path.c_str() ); } return 0; }