16 #include <mach-o/dyld.h>
39 const char FILE_SEPARATOR =
'/';
41 size_t separatorIndex = path.rfind(FILE_SEPARATOR);
42 string fileAndExtension;
43 if (separatorIndex != string::npos)
45 dir = path.substr(0, separatorIndex + 1);
46 if (separatorIndex < path.length())
47 fileAndExtension = path.substr(separatorIndex + 1, path.length());
51 fileAndExtension = path;
54 size_t dotIndex = fileAndExtension.rfind(
".");
55 if (dotIndex != string::npos)
57 file = fileAndExtension.substr(0, dotIndex);
58 extension = fileAndExtension.substr(dotIndex + 1, fileAndExtension.length());
62 file = fileAndExtension;
73 for (
int i = 0; i < path.length(); ++i)
75 while (i+1 < path.length() && path[i+1] ==
'/')
80 path.erase(path.length()-1);
93 return path1 == path2;
104 char *cwd = getcwd(NULL, 0);
107 VUserLog(
"Couldn't get current working directory: %s", strerror(errno));
111 string absolutePath = string(cwd) +
"/" + path;
124 if (directory.empty())
126 if (directory.at(directory.length()-1) !=
'/')
128 string suffix = (extension.empty() ?
"" : (
"." + extension));
129 string pathTemplate = directory + file +
"-XXXXXX" + suffix;
130 char *path = (
char *)calloc(pathTemplate.length() + 1,
sizeof(char));
131 strncpy(path, pathTemplate.c_str(), pathTemplate.length());
132 int fd = mkstemps(path, suffix.length());
134 string pathStr(path);
149 string pathTemplate =
getTmpDir() +
"/" + prefix +
".XXXXXX";
150 char *path = (
char *)calloc(pathTemplate.length() + 1,
sizeof(char));
151 strncpy(path, pathTemplate.c_str(), pathTemplate.length());
153 string pathStr(path);
184 char *cwd = getcwd(NULL, 0);
185 if (strstr(cwd,
"/Library/Containers/"))
197 char userTempDir[PATH_MAX];
198 if (confstr(_CS_DARWIN_USER_TEMP_DIR, userTempDir, PATH_MAX) > 0)
200 string userTempDirS(userTempDir);
215 throw VuoException(
"Couldn't create directory with empty path");
220 const char FILE_SEPARATOR =
'/';
221 size_t lastNonSeparator = path.find_last_not_of(FILE_SEPARATOR);
222 if (lastNonSeparator != string::npos)
223 path.resize(lastNonSeparator + 1);
225 string parentDir, file, ext;
229 int ret = mkdir(path.c_str(), 0700);
231 throw VuoException((
string(
"Couldn't create directory \"" + path +
"\": ") + strerror(errno)).c_str());
242 static string frameworkPath;
243 static dispatch_once_t once = 0;
244 dispatch_once(&once, ^{
247 const char *frameworkPathFragment =
"/Vuo.framework/Versions/";
248 uint32_t imageCount = _dyld_image_count();
249 for (uint32_t i = 0; i < imageCount; ++i)
251 const char *dylibPath = _dyld_get_image_name(i);
252 const char *found = strstr(dylibPath, frameworkPathFragment);
255 char *pathC = strndup(dylibPath, found - dylibPath);
256 string path = string(pathC) +
"/Vuo.framework";
260 frameworkPath = path;
267 char executablePath[PATH_MAX + 1];
268 uint32_t size =
sizeof(executablePath);
270 if (! _NSGetExecutablePath(executablePath, &size))
272 char cleanedExecutablePath[PATH_MAX + 1];
274 realpath(executablePath, cleanedExecutablePath);
275 string path = cleanedExecutablePath;
276 string dir, file, ext;
278 path = dir.substr(0, dir.length() - 1);
280 path = dir.substr(0, dir.length() - 1);
281 path +=
"/Frameworks/Vuo.framework";
285 frameworkPath = path;
293 return frameworkPath;
302 static string runnerFrameworkPath;
303 static dispatch_once_t once = 0;
304 dispatch_once(&once, ^{
309 runnerFrameworkPath = possibleFrameworkPath;
314 const char *frameworkPathFragment =
"/VuoRunner.framework/Versions/";
315 uint32_t imageCount = _dyld_image_count();
316 for (uint32_t i = 0; i < imageCount; ++i)
318 const char *dylibPath = _dyld_get_image_name(i);
319 const char *found = strstr(dylibPath, frameworkPathFragment);
322 char *pathC = strndup(dylibPath, found - dylibPath);
323 string path = string(pathC) +
"/VuoRunner.framework";
327 runnerFrameworkPath = path;
334 char executablePath[PATH_MAX + 1];
335 uint32_t size =
sizeof(executablePath);
337 if (! _NSGetExecutablePath(executablePath, &size))
339 char cleanedExecutablePath[PATH_MAX + 1];
341 realpath(executablePath, cleanedExecutablePath);
342 string path = cleanedExecutablePath;
343 string dir, file, ext;
345 path = dir.substr(0, dir.length() - 1);
347 path = dir.substr(0, dir.length() - 1);
348 path +=
"/Frameworks/VuoRunner.framework";
352 runnerFrameworkPath = path;
359 return runnerFrameworkPath;
372 string compositionDir, compositionFile, ext;
376 string parentDir, compositionDirName;
377 splitPath(compositionDir, parentDir, compositionDirName, ext);
380 string compositionBaseDir = (compositionDirName ==
"Modules" ? parentDir : compositionDir);
382 string compositionModulesDir = compositionBaseDir +
"/Modules";
385 return compositionModulesDir;
393 return string(getenv(
"HOME")) +
"/Library/Application Support/Vuo/Modules";
401 return "/Library/Application Support/Vuo/Modules";
409 return string(getenv(
"HOME")) +
"/Library/Caches/org.vuo/" + VUO_VERSION_AND_BUILD_STRING;
422 string dir, file, ext;
447 string bomUtf8 =
"\xEF\xBB\xBF";
448 string bomUtf16Be =
"\xFE\xFF";
449 string bomUtf16Le =
"\xFF\xFE";
450 string boms[] = { bomUtf8, bomUtf16Be, bomUtf16Le };
451 for (
int i = 0; i < 3; ++i)
454 for (
int j = 0; j < boms[i].length(); ++j)
455 if (boms[i][j] != s[j])
459 return boms[i].length();
473 while (getline(cin, line))
488 ifstream in(path.c_str(), ios::in | ios::binary);
490 throw VuoException(
string(
"Couldn't read file: ") + strerror(errno) +
" — " + path);
493 in.seekg(0, ios::end);
494 contents.resize(in.tellg());
495 in.seekg(0, ios::beg);
496 in.read(&contents[0], contents.size());
510 FILE *f = fopen(file.c_str(),
"wb");
512 throw VuoException(
string(
"Couldn't open file for writing: ") + strerror(errno) +
" — " + file);
514 size_t numBytesWritten = fwrite(data,
sizeof(
char), numBytes, f);
515 if (numBytesWritten != numBytes)
518 throw VuoException(
string(
"Couldn't write all data: ") + strerror(errno) +
" — " + file);
546 string dir, file, ext;
548 string tmpPath =
makeTmpFile(
"." + file, ext, dir);
566 return access(path.c_str(), 0) == 0;
581 int status = stat(path.c_str(), &st_buf);
582 return (! status && S_ISDIR(st_buf.st_mode));
591 int status = lstat(path.c_str(), &st_buf);
592 return (! status && S_ISLNK(st_buf.st_mode));
606 return access(path.c_str(), R_OK) == 0;
620 FILE *f = fopen(path.c_str(),
"rb");
624 fseek(f, 0, SEEK_END);
633 fseek(f, 0, SEEK_SET);
635 size_t ret = fread(&z, 1, 1, f);
645 FILE *f = fopen(path.c_str(),
"a");
654 int ret = remove(path.c_str());
656 VUserLog(
"Couldn't delete file: %s — %s", strerror(errno), path.c_str());
667 DIR *d = opendir(path.c_str());
670 VUserLog(
"Couldn't read directory: %s — %s", strerror(errno), path.c_str());
675 while( (de=readdir(d)) )
677 string fileName = de->d_name;
678 string filePath = path +
"/" + fileName;
680 if (fileName ==
"." || fileName ==
"..")
701 int ret = rename(fromPath.c_str(), toPath.c_str());
703 throw VuoException(
string(
"Couldn't move file: ") + strerror(errno) +
" — " + toPath);
726 int i = open(fromPath.c_str(), O_RDONLY);
728 throw VuoException(
string(
"Couldn't open copy source: ") + strerror(errno) +
" — " + fromPath);
730 int o = open(toPath.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
734 throw VuoException(
string(
"Couldn't open copy destination: ") + strerror(errno) +
" — " + toPath);
737 int ret = fcopyfile(i, o, NULL, COPYFILE_DATA | (preserveMetadata ? COPYFILE_STAT : 0));
738 char *e = strerror(errno);
743 throw VuoException(
string(
"Couldn't copy ") + fromPath +
" to " + toPath +
": " + e);
758 char linkDestination[PATH_MAX + 1];
759 ssize_t len = readlink(fromPath.c_str(), linkDestination, PATH_MAX);
760 linkDestination[len] = 0;
761 if (symlink(linkDestination, toPath.c_str()) == -1)
762 throw VuoException(
string(
"Couldn't copy symlink \"") + fromPath +
"\" to \"" + toPath +
"\": " + strerror(errno));
772 for (
auto file : files)
774 string sourceFile = fromPath +
"/" + file->getRelativePath();
775 string targetFile = toPath +
"/" + file->getRelativePath();
788 lstat(path.c_str(), &s);
789 return s.st_mtimespec.tv_sec;
798 lstat(path.c_str(), &s);
801 gettimeofday(&t, NULL);
803 return t.tv_sec - s.st_atimespec.tv_sec;
820 bool shouldSearchRecursively)
828 DIR *d = opendir(dirPath.c_str());
831 if (access(dirPath.c_str(), F_OK) != -1)
832 throw VuoException(
string(
"Couldn't read directory: ") + strerror(errno) +
" — " + dirPath);
837 while( (de=readdir(d)) )
839 string fileName = de->d_name;
840 string relativeFilePath = dirPath +
"/" + fileName;
842 if (fileName ==
"." || fileName ==
"..")
845 bool isArchive =
false;
846 for (set<string>::iterator archiveExtension = archiveExtensions.begin(); archiveExtension != archiveExtensions.end(); ++archiveExtension)
856 files.insert(fs.begin(), fs.end());
861 bool shouldSearchDir = shouldSearchRecursively
867 for (set<File *>::iterator i = filesInDir.begin(); i != filesInDir.end(); ++i)
870 f->dirPath = dirPath;
871 f->filePath = fileName +
"/" + f->filePath;
873 files.insert(filesInDir.begin(), filesInDir.end());
877 File *f =
new File(dirPath, fileName);
900 set<File *> matchingFiles;
902 for (set<File *>::iterator i = allFiles.begin(); i != allFiles.end(); ++i)
904 bool endsWithExtension =
false;
905 for (set<string>::iterator extension = extensions.begin(); extension != extensions.end(); ++extension)
907 endsWithExtension =
true;
910 matchingFiles.insert(*i);
915 return matchingFiles;
935 mz_uint numFiles = mz_zip_reader_get_num_files(archive->
zipArchive);
936 for (mz_uint i = 0; i < numFiles; ++i)
938 mz_zip_archive_file_stat file_stat;
939 bool success = mz_zip_reader_file_stat(archive->
zipArchive, i, &file_stat);
942 VUserLog(
"Error: Couldn't read file '%u' in zip archive '%s'.", i, archive->
path.c_str());
946 File *f =
new File(archive, file_stat.m_filename);
968 set<File *> matchingFiles;
970 for (set<File *>::iterator i = allFiles.begin(); i != allFiles.end(); ++i)
974 string dir, file, ext;
978 bool endsWithExtension =
false;
979 for (set<string>::iterator extension = extensions.begin(); extension != extensions.end(); ++extension)
981 endsWithExtension =
true;
983 if (beginsWithDir && endsWithExtension)
984 matchingFiles.insert(f);
989 return matchingFiles;
1005 for (set<File *>::iterator i = archiveFiles.begin(); i != archiveFiles.end(); ++i)
1039 this->
zipArchive = (mz_zip_archive *)calloc(1,
sizeof(mz_zip_archive));
1041 bool success = mz_zip_reader_init_file(
zipArchive,
path.c_str(), 0);
1056 mz_zip_reader_end(zipArchive);
1077 this->fileDescriptor = -1;
1078 this->archive = NULL;
1090 this->fileDescriptor = -1;
1091 this->archive = archive;
1101 string newFilePath = basename() +
"." + extension;
1104 return File(archive, newFilePath);
1106 return File(dirPath, newFilePath);
1115 if (archive && --archive->referenceCount == 0)
1124 return (archive != NULL);
1134 return (archive ? archive->path :
"");
1153 throw VuoException(
"Can't return a simple absolute path for a file in an archive.");
1155 return dirPath +
"/" + filePath;
1166 throw VuoException(
"Can't return a simple absolute path for a file in an archive.");
1176 return filePath.substr(0, filePath.find_last_of(
'.'));
1184 return filePath.substr(filePath.find_last_of(
'.') + 1);
1193 return mz_zip_reader_locate_file(archive->zipArchive, filePath.c_str(),
nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE) != -1;
1215 string fullPath = (dirPath.empty() ?
"" : (dirPath +
"/")) + filePath;
1217 FILE *pFile = fopen ( fullPath.c_str() ,
"rb" );
1219 throw VuoException(
string(
"Couldn't open file: ") + strerror(errno) +
" — " + fullPath);
1222 fseek (pFile , 0 , SEEK_END);
1223 size_t lSize = ftell (pFile);
1227 buffer = (
char*) malloc (
sizeof(
char)*lSize);
1230 numBytes = fread (buffer,1,lSize,pFile);
1232 if (numBytes != lSize)
1235 throw VuoException(
string(
"Couldn't read file: ") + strerror(errno) +
" — " + fullPath);
1240 buffer = (
char *)mz_zip_reader_extract_file_to_heap(archive->zipArchive, filePath.c_str(), &numBytes, 0);
1254 char *buffer = getContentsAsRawData(numBytes);
1255 string s(buffer, numBytes);
1271 if (fileDescriptor < 0)
1272 fileDescriptor = open((dirPath +
"/" + filePath).c_str(), O_RDONLY);
1273 int ret = flock(fileDescriptor, LOCK_SH | (nonBlocking ? LOCK_NB : 0));
1288 if (fileDescriptor < 0)
1289 fileDescriptor = open((dirPath +
"/" + filePath).c_str(), O_RDONLY);
1290 int ret = flock(fileDescriptor, LOCK_EX | (nonBlocking ? LOCK_NB : 0));
1300 flock(fileDescriptor, LOCK_UN);
1301 close(fileDescriptor);
1302 fileDescriptor = -1;
1323 string binaryDir, binaryFile, binaryExt;
1324 splitPath(processAndArgs[0], binaryDir, binaryFile, binaryExt);
1326 string errorPrefix =
"Couldn't execute " + binaryFile +
": ";
1330 int ret = pipe(outputPipe);
1332 throw VuoException(errorPrefix +
"couldn't open pipe: " + strerror(ret));
1333 int pipeRead = outputPipe[0];
1334 int pipeWrite = outputPipe[1];
1336 posix_spawn_file_actions_t fileActions;
1337 posix_spawn_file_actions_init(&fileActions);
1338 posix_spawn_file_actions_adddup2(&fileActions, pipeWrite, STDOUT_FILENO);
1339 posix_spawn_file_actions_adddup2(&fileActions, pipeWrite, STDERR_FILENO);
1340 posix_spawn_file_actions_addclose(&fileActions, pipeRead);
1344 char **processAndArgsZ = (
char **)malloc(
sizeof(
char *) * (processAndArgs.size() + 1));
1346 for (
auto arg : processAndArgs)
1348 processAndArgsZ[argCount++] = strdup(binaryFile.c_str());
1350 processAndArgsZ[argCount++] = strdup(arg.c_str());
1351 processAndArgsZ[argCount] =
nullptr;
1355 ret = posix_spawn(&pid, processAndArgs[0].c_str(), &fileActions, NULL, (
char *
const *)processAndArgsZ, NULL);
1357 posix_spawn_file_actions_destroy(&fileActions);
1358 for (
int i = 0; i < argCount; ++i)
1359 free(processAndArgsZ[i]);
1366 if (waitpid(pid, &status, 0) == -1)
1367 throw VuoException(errorPrefix +
"waitpid failed: " + strerror(errno));
1368 else if (status != 0)
1373 while (read(pipeRead, &buf, 255) > 0)
1389 return extension ==
"vuo";
1415 return extensions.find(extension) != extensions.end();
1426 e.insert(
"vuoshader");
1427 e.insert(
"vuoobjectfilter");
1428 return e.find(extension) != e.end();