mkdir() returns -1 when launched through Finder - c++

I have a simple program that makes a directory when it is executed:
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(){
if(int a = mkdir("abc",0700)){
std::cout << "Failed to create: " << a << std::endl;
}
else{
std::cout << "Created." << std::endl;
}
}
It behaves differently for two different use cases:
Running the compiled binary through Terminal
Output: Created.
Launching this program via Finder with double click.
Output: Failed to create: -1
How do I make this so that launching this program via Finder creates the folder abc without using Cocoa framework (compiles with g++ only)?

Thanks to Wooble for pointing it out in the comment section that the problem is due to the working directory. When I launched it through Finder, the current working directory was my home directory.
Below is how I address the problem:
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libproc.h>
int main(int argc, char** argv){
// Gets and prints out the current directory
char cwd[1024];
getcwd(cwd, sizeof(cwd));
std::cout << "Current Working Directory: " << cwd << std::endl;
// Above is not necessary
// Changes working directory to directory that contains executable
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
if(proc_pidpath (getpid(), pathbuf, sizeof(pathbuf)) > 0){ // Gets the executable path
std::string s(pathbuf);
s = s.substr(0,s.find_last_of('/')); // Removes executable name from path
std::cout << "Executable Path: " << s << std::endl;
chdir(s.c_str()); // Changes working directory
}
if(int a = mkdir("abc",0700)){
std::cout << "Failed to create: " << a << std::endl;
}
else{
std::cout << "Created." << std::endl;
}
}

Related

Problem with boost::process with long (>260) start_dir (windows)

I am on Windows 10, and want to use boost process to start a child. When the child's working directory is too long, I get an exception:
CreateProcess failed: The directory name is invalid.
I wrote a test program to debug this:
#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <boost/asio/high_resolution_timer.hpp>
#include <iostream>
#include <iomanip>
#include <thread>
#include <string>
#include <filesystem>
namespace bp = boost::process;
void test(const std::filesystem::path& wdir)
{
boost::asio::io_context io;
bp::child child;
try
{
std::filesystem::create_directories(wdir);
std::cout << "exists " << std::filesystem::exists(wdir) << " len " << wdir.generic_wstring().size() << '\n';
std::cout << "start proc\n";
child = bp::child(
"C:/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe",
std::vector<std::string> { "ls" },
io,
boost::process::start_dir = LR"(\\?\)" + wdir.generic_wstring());
std::this_thread::sleep_for(std::chrono::seconds{ 3 });
child.wait();
}
catch (const std::exception& e)
{
std::cout << "EXCEPTION " << e.what() << "\n";
}
std::cout << "done\n";
}
int main()
{
std::cout << "short path\n";
test("D:/tmp/10378020400asdfasdfqw4retf");
std::cout << "\nlong path\n";
test("D:/tmp/10378020400826168668/unicode/qwe/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiertzertz/aaaqqqwwwiiiiertzertz/eertz/"
"iiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiii"
"iiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiii"
"iiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"
"i/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqw"
"wwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiii"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iii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaq"
"qqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiii"
"iiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/aaaqqqwwwiiiiiiiiiiiii"
"iiiiiiiiiiiiiiiiiii/");
return 0;
}
I get this output:
short path
exists 1 len 33
start proc
done
long path
exists 1 len 2177
start proc
EXCEPTION CreateProcess failed: The directory name is invalid.
done
It seems the problem comes from the internally called CreateProcessW.
The documentation does not mention any limit for lpCurrentDirectory, and also allows UNC paths (hence I tried adding the prefix \\?\), but it does not make any difference whether I use the UNC syntax or not.
My questions are:
Is this an inherent limitation of Windows?
Is there any way to circumvent this limitation using boost?
Is there any way to circumvent this limitation using some other Win32 API function?
What I tired: using normal and UNC paths.

fstream fails to write/open files on raspberry pi

I am trying to run a cpp program on raspberry pi 3 b+ (from 'pi' user) but when I try to open a file with 'fstream' library it doesn't work.
I am using the following code (from main):
std::ios::sync_with_stdio(false);
std::string path = "/NbData";
std::ofstream nbData(path);
if (!nbData) {
std::cout << "Error during process...";
return 0;
}
nbData.seekp(std::ios::beg);
The program always fails there and stops because no file is created (I don't get a fatal error but the test fails and it outputs 'Error during process' which means no file was created).
I am compiling with the following command (there are no issues when I compile):
g++ -std=c++0x nbFinder.cpp -o nbFinder
I have already tried my program on Xcode and everything worked perfectly...
The problem is your path. You must put the file, you are using just the path and if the path do not exist will throw an error. In your case you just using std::string path = "/NbData";, that is you path not your file.
To be able to open your file you need make sure your path exist. Try use the code bellow, he will check if the path exist case not will create and then try to open your file.
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
int main() {
std::ios::sync_with_stdio(false);
std::string path = "./test_dir/";
std::string file = "test.txt";
// Will check if thie file exist, if not will creat
struct stat info;
if (stat(path.c_str(), &info) != 0) {
std::cout << "cannot access " << path << std::endl;
system(("mkdir " + path).c_str());
} else if(info.st_mode & S_IFDIR) {
std::cout << "is a directory" << path << std::endl;
} else {
std::cout << "is no directory" << path << std::endl;
system(("mkdir " + path).c_str());
}
std::ofstream nbData(path + file);
if (!nbData) {
std::cout << "Error during process...";
return 0;
}
nbData.seekp(std::ios::beg);
return 0;
}

Incomplete mkdir and copyFile if power lost

I have an application that creates a directory and copy a file to it. This is working fine, normally. But if the window shuts down correctly and the power is lost, the files will not always be available or complete.
My OS is Windows Embedded POSReady7 (Version 6.1 Build 7601: Service Pack 1) and on the hard disk is "write caching" option disabled. I have a C++ Application. I used also _flushall but it doesn't help.
I have written a test application where I can see the problem. I start the application, wait ten seconds after the output is completed and then unplug the power. The files are not available after restart.
Here is my code:
#include "stdafx.h"
#include <iostream>
#include <direct.h>
#include <string>
#include <Windows.h>
#include <atlstr.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string csPathName("D:\\temp\\FileCopyTest\\");
int mkRetValue = _mkdir( csPathName.c_str() );
cout << "Create directory " << csPathName.c_str() << " with return value " << mkRetValue << endl;
BOOL copyRetValue = CopyFileEx( _T("D:\\temp\\test.txt"), _T("D:\\temp\\FileCopyTest\\test.txt"), nullptr, nullptr, FALSE, COPY_FILE_NO_BUFFERING );
cout << "Copy file, return value " << copyRetValue << endl;
int flushRetValue = _flushall();
cout << "flush files: " << flushRetValue << endl;
char c;
cin >> c;
return 0;
}
Have you tried putting copy within the scope?
{
CopyFileEx( _T("D:\\temp\\test.txt"), _T("D:\\temp\\FileCopyTest\\test.txt"), nullptr, nullptr, FALSE, COPY_FILE_NO_BUFFERING );
}

R6010- Abort gets hit when rename or copy_file method of boost filesystem gets hit

Description:
I am trying to move all the files in a directory to a certain(user choosen directory)based on their extension to a certain directory via boost file system.
Problem:
When the rename/copy_file method of boost filesystem gets hit,I am receiving the R6010-Abort method called error.
Example:
SourceDirectory:C:\Source\a.txt
DestinationDirectory:C:\Destination
After execution:
DestinationDirectory:C:\Destination\a.txt
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/progress.hpp"
#include "boost/algorithm/string/regex.hpp"
#include "boost/regex.hpp"
namespace fs = boost::filesystem;
using namespace std;
void categorizeFolder()
{
//Source Folder
std::string folderToCategorize;
cout<<"Choose the folder you want to categorize:";
cin>>folderToCategorize;
cout << "The directory you have choosen is: " << folderToCategorize << endl;
//Destination folder
std::string newfolder;
cout<<"Choose the folder you want to store your files:";
cin>>newfolder;
cout << "The directory you have choosen is: " << newfolder << endl;
std::vector< std::string > all_matching_files;
boost::filesystem::directory_iterator end_itr;
for( boost::filesystem::directory_iterator i( folderToCategorize ); i != end_itr; ++i )
{
if( !boost::filesystem::is_regular_file( i->status() ) ) continue;
if( i->path().extension() == ".txt" )
{
cout<<i->path().extension();//Printing File extension
cout<<i->path();//Printing file path
cout<<i->path().filename()<<endl; //Printing filename
fs::rename(i->path(), newfolder);//This would move the file//Even tried fs::copy_file(i->path(), newfolder)
}
}
}
Kindly let me know if i am missing something in the above code.Thanks in advance.
Regards,
Ravi
The linux error looks like this:
terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
what(): boost::filesystem::rename: Is a directory: "/tmp/first/test.txt", "/tmp/second"
The fact that the API call is named rename and not, e.g. moveToFolder, should have given you an idea that you need to supply a full pathname in the target.
fs::rename(
it->path(),
fs::path(newfolder) / it->path().filename());
to fix it.
Here's a version with some better organization and error handling. It will even create the target directory if it doesn't already exist!
#include <boost/filesystem.hpp>
#include <iostream>
#include <string>
namespace fs = boost::filesystem;
using namespace std;
void categorizeFolder(fs::path folderToCategorize, fs::path newfolder)
{
if (!fs::exists(newfolder))
fs::create_directories(newfolder);
if (!fs::is_directory(newfolder))
{
std::cerr << "Destination folder does not exist and could not be created: " << fs::absolute(newfolder) << "\n";
return;
}
for(fs::directory_iterator it(folderToCategorize), end_itr; it != end_itr; ++it)
{
if(!fs::is_regular_file(it->status()))
continue;
if(it->path().extension() == ".txt")
{
// std::cout << it->path().extension() << "\n";
// std::cout << it->path() << "\n";
// std::cout << it->path().filename() << "\n";
fs::rename(it->path(), fs::path(newfolder) / it->path().filename()); // move the file
}
}
}
int main(int argc, const char *argv[])
{
if (argc<3)
{
std::cout << "Usage: " << argv[0] << " folderToCategorize newfolder\n";
return 255;
}
std::string const folderToCategorize = argv[1];
std::string const newfolder = argv[2];
std::cout << "The directory you have choosen is: " << folderToCategorize << endl;
std::cout << "The directory you have choosen is: " << newfolder << endl;
categorizeFolder(folderToCategorize, newfolder);
}

C++ - stat(), access() not working under gnu gcc

I've got a pretty basic console program here, to determine if a folder or file exists or not using stat:
#include <iostream>
#include <sys/stat.h>
using namespace std;
int main() {
char path[] = "myfolder/";
struct stat status;
if(stat(path,&status)==0) { cout << "Folder found." << endl; }
else { cout << "Can't find folder." << endl; } //Doesn't exist
cin.get();
return 0;
}
I have also tried the access version:
#include <iostream>
#include <io.h>
using namespace std;
int main() {
char path[] = "myfolder/";
if(access(path,0)==0) { cout << "Folder found." << endl; }
else { cout << "Can't find folder." << endl; } //Doesn't exist
cin.get();
return 0;
}
Neither of them find my folder (which is right there in the same directory as the program). These worked on my last compiler (the default one with DevCpp). I switched to CodeBlocks and am compiling with Gnu GCC now, if that helps. I'm sure it's a quick fix - can someone help out?
(Obviously I'm a noob at this so if you need any other information I've left out please let me know).
UPDATE
The problem was with the base directory. The updated, working program is as follows:
#include <iostream>
#include <sys/stat.h>
using namespace std;
int main() {
cout << "Current directory: " << system("cd") << endl;
char path[] = "./bin/Release/myfolder";
struct stat status;
if(stat(path,&status)==0) { cout << "Directory found." << endl; }
else { cout << "Can't find directory." << endl; } //Doesn't exist
cin.get();
return 0;
}
ANOTHER UPDATE
Turns out that a trailing backslash on the path is big trouble.
Right before your stat call, insert the code:
system("pwd"); // for UNIXy systems
system("cd"); // for Windowsy systems
(or equivalent) to check your current directory. I think you'll find it's not what you think.
Alternatively, run the executable from the command line where you know what directory you're in. IDEs will frequently run your executable from a directory you may not expect.
Or, use the full path name so that it doesn't matter which directory you're in.
For what it's worth, your first code segment works perfectly (gcc under Ubuntu 10):
pax$ ls my*
ls: cannot access my*: No such file or directory
pax$ ./qq
Cannot find folder.
pax$ mkdir myfolder
pax$ ll -d my*
drwxr-xr-x 2 pax pax 4096 2010-12-14 09:33 myfolder/
pax$ ./qq
Folder found.
Are you sure that the current directory of your running program is what you expect it to be? Try changing path to an absolute pathname to see if that helps.
Check your PWD when you running your program. This problem is not caused by compiler. You DevCpp may set a working directory for your program automatically.
You can find out why stat() failed (which is a C function, not C++, by the way), by checking errno:
#include <cerrno>
...
if (stat(path,&status) != 0)
{
std::cout << "stat() failed:" << std::strerror(errno) << endl;
}