Posts

Post not yet marked as solved
3 Replies
1.8k Views
/bin/cp and mkdir() in Xcode C++ in $HOME/Desktop both hang - suspect this points to a bug in Mojave or LibrariesHi Team,This is really wierd. The following code compiles on Xcode. Its behaviour is driving me nuts - so would appreciate anyone's help inunderstanding what is going on as I would very much like to be able to create folders in my home directory with a c++ program.The code is designed to create a path (if it does not already exist) and and a final file in that path.Environment = 2012 MBPro running Mojave 10.14.4, and the latest version of XCode (10.2.1)My home directory is "pwapwa".Running XCode in debug mode, it causes Xcode to hang (using lots of cpu) at the point it is executing mkdir pwa5in the directory /Volumes/MacBookII/Users/pwapwa/DesktopIn order to recover I have to kill Xcode.XCode also hangs if I set sSubPathName[4] = "Desktop1";at the point it tries to execute mkdir pwa5after successfully creating "Desktop1".It does not fail if I set sSubPathName[4] = "Green1";or if I then set sSubPathName[7] = "pwapwa"; sSubPathName[8] = "Desktop"; sSubPathName[9] = "pwa1";Conclusion:mkdir causes Xcode to hang if it is used to create a subdirectory under $HOME/Desktop*where Desktop* is any combination of characters after the word Desktop or any subdirectory under $HOME/Desktop/Similar weird behavior from /bin/cp------------------------------------------I note that /bin/mkdir works under all circumstances (although a version of /usr/local/bin/mkdir that used to work before upgrading to Mojave exhibits very similar behavior.)/bin/cp -rf abc def - where 'abc' is an empty folder and 'def' does not exist- works if abc is in $HOME/abc- does not work if abc is in $HOME/Desktop* or any subdirectory of $HOME/DesktopI have reinstalled Xcode and Mojave (over existing filesystem) and get the same behavior.I have changed reset permissions of my $HOME and subdirectories. Still mkdir and /bin/cp hang.I suspect that somewhere in the libraries there is a test for "$HOME/Desktop" in the path of a file that is being created and a bug that results in the code hanging.Please advise if this is peculirar to my installation (I have not yet checked it on other installations of Mojave or if it standard.If anyne can explain why this happens please let me know.It is crippling to my application if it cannot create folders anywhere under $HOME/Desktop directory.------------------------------------------//// main.cpp// TestMkDir//// Created by petera on 5/10/19.// Copyright © 2019 petera. All rights reserved.//#include "sys/stat.h"#include <iostream>#include <string>#include <fstream>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdlib.h>using namespace std;void vBuildPathAndFile( string sSubPaths[], int iSubPaths, string sFileName ) { char cCWD[2000]; int i; string sCurrentPath; struct stat sb; bool bIsDirectory; fstream sFptr; string sFullPath; sCurrentPath = ""; for (i = 0; i < iSubPaths; i++) { getcwd(cCWD, 2000); cout << "Current Working Directory Directory at start of loop: " << cCWD << "\n"; if (sSubPaths[i][0] != '/') { sCurrentPath.append("/"); } cout << "Adding [" << sSubPaths[i] << "]\n";; sCurrentPath.append(sSubPaths[i]); cout << "Directory to test: " << sCurrentPath << "\n"; if (stat(sCurrentPath.c_str(), &sb) == -1) { // File does not exist cout << "Path [" << sCurrentPath << "] does not exist, will try and create it.\n"; if (mkdir(sSubPaths[i].c_str(), S_IRWXU) == 0) { cout << "Path [" << sCurrentPath << "] was successfully created.\n"; } else { cout << "Failed to create Path [" << sCurrentPath << "] - exit\n"; exit (-1); } } else { // It exists - now Check that is a directory bIsDirectory = false; switch (sb.st_mode & S_IFMT) { case S_IFBLK: cout << "[" << sCurrentPath << "] is a block device\n"; break; case S_IFCHR: cout << "[" << sCurrentPath << "] is a character device\n"; break; case S_IFDIR: cout << "[" << sCurrentPath << "] is a directory\n"; bIsDirectory = true; break; case S_IFIFO: cout << "[" << sCurrentPath << "] is a FIFO/pipe\n"; break; case S_IFLNK: cout << "[" << sCurrentPath << "] is a symlink\n"; break; case S_IFREG: cout << "[" << sCurrentPath << "] is a regular file\n"; break; case S_IFSOCK: cout << "[" << sCurrentPath << "] is a socket\n"; break; default: cout << "[" << sCurrentPath << "] is a unknown?\n"; break; } if (!bIsDirectory) { cout << "[" << sCurrentPath << "] is not a directory - exit\n"; exit (-1); } } chdir(sCurrentPath.c_str()); } // If we got here - it is time to add the file // Change to this path chdir(sCurrentPath.c_str()); sFullPath = sCurrentPath + "/" + sFileName; sFptr.open(sFileName.c_str(), fstream::out); if (sFptr.is_open()) { cout << "[" << sFullPath << "] was successfully created\n"; sFptr.close(); } cout << "\n\nCheck if we can open another file with full path using open\n"; sFullPath = sCurrentPath + "/test2"; sFptr.open(sFullPath.c_str(), fstream::out); if (sFptr.is_open()) { cout << "[" << sFullPath << "] as a full path was successfully created\n"; sFptr.close(); } else { cout << "[" << sFullPath << "] as a full path was NOT successfully created\n"; } cout << "Check if we can open another file with full path that also has an non-existant folder in the path\n"; sFullPath = sCurrentPath + "/pwapwa0/test2"; sFptr.open(sFullPath.c_str(), fstream::out); if (sFptr.is_open()) { cout << "[" << sFullPath << "] as a full path was successfully created\n"; sFptr.close(); } else { cout << "[" << sFullPath << "] as a full path was NOT successfully created\n"; } cout << "We have used mkdir many times thus far to make each successive folder one at at time in the path\n"; cout << " Now will try and use mkdir to create one extra folder using the full path rather than the next directory\n"; sFullPath = sCurrentPath + "\nextFolder"; if (mkdir(sFullPath.c_str(), S_IRWXU) == 0) { cout << "Path [" << sFullPath << "] was successfully created.\n"; } else { cout << "Failed to create Path [" << sFullPath << "] - exit\n"; exit (-1); }}int main(int argc, const char * argv[]) { string sSubPathName[10]; string sFileName; int iSubPaths; sSubPathName[0] = "/Volumes"; sSubPathName[1] = "MacBookII"; sSubPathName[2] = "Users"; sSubPathName[3] = "pwapwa"; sSubPathName[4] = "Desktop"; sSubPathName[5] = "pwa5"; sSubPathName[6] = "pwa4"; sSubPathName[7] = "pwa3"; sSubPathName[8] = "pwa2"; sSubPathName[9] = "pwa1"; iSubPaths = 10; sFileName = "pwaFile.txt"; vBuildPathAndFile( sSubPathName, iSubPaths, sFileName ); return 0;}
Posted
by pwapub1.
Last updated
.