YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
yarpcontextutils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "yarpcontextutils.h"
7
8#include <yarp/conf/dirs.h>
9#include <yarp/os/Os.h>
10#include <yarp/os/Log.h>
14
15#include <set>
16#include <string>
17#include <fstream>
18#include <iostream>
19#include <cstdlib>
20#include <cstring>
21#include <cerrno>
22
23
24#if defined(_WIN32)
25#pragma warning (disable : 4018)
26#pragma warning (disable : 4267)
27
28#ifdef DELETE
29#undef DELETE
30#endif
31
32#ifdef max
33#undef max
34#endif
35
36#ifdef min
37#undef min
38#endif
39
40#endif
41
42#include <diff_match_patch.h>
43
44#if defined(_WIN32)
45#pragma warning(default:4018)
46#pragma warning(default:4267)
47#endif
48
49
50using namespace yarp::os;
51
53{
54 switch (ftype)
55 {
56 case 0:
57 return std::string("contexts");
58 case 1:
59 return std::string("robots");
60 default:
61 return {};
62 }
63}
64
66{
67 switch (ftype)
68 {
69 case 0:
70 return std::string(".contexts");
71 case 1:
72 return std::string(".robots");
73 default:
74 return {};
75 }
76}
77
78bool isHidden(std::string fileName)
79{
80#if defined(_WIN32)
81 DWORD attributes = GetFileAttributes(fileName.c_str());
82 if (attributes & FILE_ATTRIBUTE_HIDDEN)
83 return true;
84#else
85 if (fileName[0] == '.') {
86 return true;
87 }
88#endif
89 return false;
90}
91
92bool fileCopy(std::string srcFileName, std::string destFileName, bool force, bool verbose)
93{
94 char buf[BUFSIZ];
95 size_t size;
96 FILE* source = fopen(srcFileName.c_str(), "rb");
97 if (source == nullptr)
98 {
99 if (verbose) {
100 printf("Could not open source file %s\n", srcFileName.c_str());
101 }
102 return false;
103 }
104 if (!yarp::os::stat(destFileName.c_str()))
105 {
106 if (force)
107 {
109 }
110 else
111 {
112 if (verbose) {
113 printf("File already exists : %s\n", destFileName.c_str());
114 }
115 fclose(source);
116 return false;
117 }
118 }
119 FILE* dest = fopen(destFileName.c_str(), "wb");
120 if (dest == nullptr)
121 {
122 if (verbose) {
123 printf("Could not open target file %s\n", destFileName.c_str());
124 }
125 fclose(source);
126 return false;
127 }
128 // clean and more secure
129 // feof(FILE* stream) returns non-zero if the end of file indicator for stream is set
130
131 while ((size = fread(buf, 1, BUFSIZ, source)))
132 {
133 fwrite(buf, 1, size, dest);
134 }
135
136 fclose(source);
137 fclose(dest);
138 return true;
139}
140
141bool fileRemove(std::string fileName)
142{
143 return !yarp::os::impl::unlink(fileName.c_str());
144}
145
146int recursiveCopy(std::string srcDirName, std::string destDirName, bool force, bool verbose)
147{
148 bool ok = true;
149
151 if (yarp::os::impl::stat(srcDirName.c_str(), &statbuf) == -1)
152 {
153 if (verbose) {
154 printf("Error in checking properties for %s\n", srcDirName.c_str());
155 }
156 return -1;
157 }
158 if ((statbuf.st_mode & S_IFMT) == S_IFREG) {
159 ok = fileCopy(srcDirName, destDirName, force, verbose) && ok;
160 } else if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
161 yarp::os::impl::dirent **namelist;
162 int n = yarp::os::impl::scandir(srcDirName.c_str(), &namelist, nullptr, yarp::os::impl::alphasort);
163 if (n<0)
164 {
165 if (verbose) {
166 std::cerr << "Could not read from directory " << srcDirName << '\n';
167 }
168 return -1;
169 }
170 if (yarp::os::mkdir((destDirName).c_str()) < 0)
171 {
172 if (errno == EEXIST && force)
173 {
174 bool ok = !recursiveRemove(destDirName, verbose);
175 ok = ok && !yarp::os::mkdir((destDirName).c_str());
176 if (!ok)
177 {
178 if (verbose) {
179 printf("Could not create directory %s\n", destDirName.c_str());
180 }
181 return -1;
182 }
183 }
184 else
185 {
186 if (verbose) {
187 if (errno == EEXIST)
188 {
189 printf("Directory %s already exist; remove it first, or use the diff/merge commands\n", destDirName.c_str());
190 }
191 else
192 {
193 printf("Could not create directory %s\n", destDirName.c_str());
194 }
195 }
196 return -1;
197 }
198 }
199 for (int i = 0; i<n; i++)
200 {
201
202 std::string name = namelist[i]->d_name;
203
204 if (name != "." && name != "..")
205 {
206 std::string srcPath = std::string(srcDirName).append(PATH_SEPARATOR).append(name);
207 std::string destPath = std::string(destDirName).append(PATH_SEPARATOR).append(name);
209 }
210 free(namelist[i]);
211 }
212 free(namelist);
213 }
214 return (ok == true ? 0 : -1);
215}
216
217int recursiveRemove(std::string dirName, bool verbose)
218{
220 if (yarp::os::impl::stat(dirName.c_str(), &statbuf) == -1)
221 {
222 if (verbose) {
223 printf("Error in checking properties for %s\n", dirName.c_str());
224 }
225 return -1;
226 }
227 if ((statbuf.st_mode & S_IFMT) == S_IFREG) {
228 return fileRemove(dirName) ? 0 : -1;
229 } else if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
230 yarp::os::impl::dirent **namelist;
231 int n = yarp::os::impl::scandir(dirName.c_str(), &namelist, nullptr, yarp::os::impl::alphasort);
232 if (n<0)
233 {
234 if (verbose) {
235 printf("Could not read from directory %s\n", dirName.c_str());
236 }
237 return yarp::os::rmdir(dirName.c_str()); // TODO check if this is useful...
238 }
239
240 for (int i = 0; i<n; i++)
241 {
242 std::string name = namelist[i]->d_name;
243 std::string path = std::string(dirName).append(PATH_SEPARATOR).append(name);
244 if (name != "." && name != "..")
245 {
246 recursiveRemove(path, verbose);
247 }
248 free(namelist[i]);
249 }
250 free(namelist);
251
252 return yarp::os::rmdir(dirName.c_str());
253 } else {
254 return -1;
255 }
256}
257
258std::vector<std::string> listContentDirs(const std::string &curPath)
259{
260 std::vector<std::string> dirsList;
261 yarp::os::impl::dirent **namelist;
262 int n = yarp::os::impl::scandir(curPath.c_str(), &namelist, nullptr, yarp::os::impl::alphasort);
263 if (n<0) {
264 return dirsList;
265 }
266 for (int i = 0; i<n; i++) {
267
268 std::string name = namelist[i]->d_name;
269 if (name != "." && name != "..")
270 {
272 std::string path = std::string(curPath).append(PATH_SEPARATOR).append(name);
273 if (yarp::os::impl::stat(path.c_str(), &statbuf) == -1) {
274 printf("Error in checking properties for %s\n", path.c_str());
275 }
276
277 if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
278 dirsList.push_back(name);
279 }
280 }
281 free(namelist[i]);
282 }
283 free(namelist);
284 return dirsList;
285}
286
287std::vector<std::string> listContentFiles(const std::string &curPath)
288{
289 std::vector<std::string> fileList;
290 yarp::os::impl::dirent **namelist;
291 int n = yarp::os::impl::scandir(curPath.c_str(), &namelist, nullptr, yarp::os::impl::alphasort);
292 if (n<0) {
293 return fileList;
294 }
295 std::list<std::string> fileStack;
296 for (int i = 0; i<n; i++)
297 {
298 fileStack.emplace_back(namelist[i]->d_name);
299 free(namelist[i]);
300 }
301 free(namelist);
302
303 while (!fileStack.empty())
304 {
305 std::string name = fileStack.front();
306
307 if (name != "." && name != "..")
308 {
310 std::string path = std::string(curPath).append(PATH_SEPARATOR).append(name);
311 if (yarp::os::impl::stat(path.c_str(), &statbuf) == -1) {
312 printf("Error in checking properties for %s\n", path.c_str());
313 }
314 if ((statbuf.st_mode & S_IFMT) == S_IFREG)
315 {
316 fileList.push_back(path);
317 }
318
319 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
320 {
321 std::vector<std::string> nestedFiles = listContentFiles(path);
322 for (auto& nestedFile : nestedFiles) {
323 fileStack.push_back(std::string {path}.append(PATH_SEPARATOR).append(nestedFile));
324 }
325 }
326 }
327 fileStack.pop_front();
328 }
329 return fileList;
330}
331
332void printContentDirs(const std::string &curPath)
333{
334 std::vector<std::string> dirsList = listContentDirs(curPath);
335 for (auto& dirsIt : dirsList) {
336 printf("%s\n", dirsIt.c_str());
337 }
338}
339
340
342{
344 opts.searchLocations = ResourceFinderOptions::User;
346 printf("**LOCAL USER DATA:\n");
347 for (size_t curPathId = 0; curPathId<contextPaths.size(); ++curPathId)
348 {
349 printf("* Directory : %s\n", contextPaths.get(curPathId).asString().c_str());
350 printContentDirs(contextPaths.get(curPathId).asString());
351 }
352}
353
355{
357 opts.searchLocations = ResourceFinderOptions::Sysadmin;
359 printf("**SYSADMIN DATA:\n");
360 for (size_t curPathId = 0; curPathId<contextPaths.size(); ++curPathId)
361 {
362 printf("* Directory : %s\n", contextPaths.get(curPathId).asString().c_str());
363 printContentDirs(contextPaths.get(curPathId).asString());
364 }
365}
366
368{
370 opts.searchLocations = ResourceFinderOptions::Installed;
372 printf("**INSTALLED DATA:\n");
373 for (size_t curPathId = 0; curPathId<contextPaths.size(); ++curPathId)
374 {
375 printf("* Directory : %s\n", contextPaths.get(curPathId).asString().c_str());
376 printContentDirs(contextPaths.get(curPathId).asString());
377 }
378}
379
381{
382 std::string yarpdatahome = yarp::conf::dirs::yarpdatahome();
383 yarp::os::impl::DIR* dir = yarp::os::impl::opendir((yarpdatahome).c_str());
384 if (dir != nullptr) {
385 yarp::os::impl::closedir(dir);
386 } else {
387 yarp::os::mkdir((yarpdatahome).c_str());
388 }
389
390 dir = yarp::os::impl::opendir((yarpdatahome.append(PATH_SEPARATOR).append(getFolderStringName(ftype))).c_str());
391 if (dir != nullptr) {
392 yarp::os::impl::closedir(dir);
393 } else {
394 yarp::os::mkdir((yarpdatahome.append(PATH_SEPARATOR).append(getFolderStringName(ftype))).c_str());
395 }
396 dir = yarp::os::impl::opendir((yarpdatahome.append(PATH_SEPARATOR).append(getFolderStringNameHidden(ftype))).c_str());
397 if (dir != nullptr) {
398 yarp::os::impl::closedir(dir);
399 } else {
400 std::string hiddenPath = (yarpdatahome.append(PATH_SEPARATOR).append(getFolderStringNameHidden(ftype)));
402#if defined(_WIN32)
404#endif
405 }
406}
407
408bool prepareSubFolders(const std::string& startDir, const std::string& fileName)
409{
410 std::string fname(fileName);
411 if (fname.find('/') == std::string::npos) {
412 return true;
413 } else {
414 size_t curPos, startPos = 0;
415 while ((curPos = fname.find('/', startPos)) != std::string::npos)
416 {
417 yarp::os::mkdir((startDir + PATH_SEPARATOR + fname.substr(0, curPos)).c_str());
418 startPos = curPos + std::string(PATH_SEPARATOR).length();
419 }
420 }
421 return true;
422}
423
424bool recursiveFileList(const char* basePath, const char* suffix, std::set<std::string>& filenames)
425{
426 std::string strPath = std::string(basePath);
427 if ((strPath.rfind('/') == std::string::npos) || (strPath.rfind('/') != strPath.size() - 1)) {
428 strPath = strPath + std::string(PATH_SEPARATOR);
429 }
430
431 std::string mySuffix = std::string(suffix);
432
433 if (((mySuffix.rfind('/') == std::string::npos) || (mySuffix.rfind('/') != mySuffix.size() - 1)) && mySuffix != "") {
434 mySuffix = mySuffix + std::string(PATH_SEPARATOR);
435 }
436
437 strPath += mySuffix;
438
439 yarp::os::impl::dirent **namelist;
440 int n = yarp::os::impl::scandir(strPath.c_str(), &namelist, nullptr, yarp::os::impl::alphasort);
441 if (n<0)
442 {
443 std::cerr << "Could not read from directory " << strPath << '\n';
444 return false;
445 }
446 bool ok = true;
447 for (int i = 0; i<n; i++)
448 {
449
450 std::string name = namelist[i]->d_name;
452 if (name != "." && name != "..")
453 {
454 yarp::os::impl::stat(std::string(strPath).append(PATH_SEPARATOR).append(name).c_str(), &statbuf);
455 if ((statbuf.st_mode & S_IFMT) == S_IFREG)
456 {
457 filenames.insert(mySuffix + name);
458 }
459 else
460 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
461 {
462 ok = ok && recursiveFileList(basePath, (mySuffix + name).c_str(), filenames);
463 }
464 }
465 free(namelist[i]);
466 }
467 free(namelist);
468 return ok;
469}
470
471
472int recursiveDiff(std::string srcDirName, std::string destDirName, std::ostream &output)
473{
474 std::set<std::string> srcFileList;
475 bool ok = recursiveFileList(srcDirName.c_str(), "", srcFileList);
476 std::set<std::string> destFileList;
477 ok = ok && recursiveFileList(destDirName.c_str(), "", destFileList);
478
479 if (!ok) {
480 return -1;
481 }
482 size_t nModifiedFiles = 0;
483 for (const auto& srcIt : srcFileList)
484 {
485 auto destPos = destFileList.find(srcIt);
486 if (destPos != destFileList.end())
487 {
489 std::string srcFileName = std::string{srcDirName}.append(PATH_SEPARATOR).append(srcIt);
490 if (isHidden(srcFileName)) {
491 continue;
492 }
493
494 std::ifstream in(srcFileName.c_str());
495 std::string srcStr((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
496 in.close();
497 in.open((destDirName + PATH_SEPARATOR + (*destPos)).c_str());
498 std::string destStr((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
499 std::string patchString = dmp.patch_toText(dmp.patch_make(srcStr, destStr));
500 if (patchString != "")
501 {
502 output << "- " << srcDirName << PATH_SEPARATOR << srcIt << '\n';
503 output << "+ " << destDirName << PATH_SEPARATOR << (*destPos) << '\n';
504 output << dmp.patch_toText(dmp.patch_make(srcStr, destStr)) << '\n';
506 }
507 destFileList.erase(destPos);
508 }
509 // else
510 // {
511 // output << "Added file " << srcDirName+PATH_SEPARATOR +(*srcIt) <<'\n';
512 // nModifiedFiles++;
513 // }
514 }
515
516 // for(std::set<std::string>::iterator destIt=destFileList.begin(); destIt !=destFileList.end(); ++destIt)
517 // {
518 // std::string destFileName=destDirName+ PATH_SEPARATOR + (*destIt);
519 // if(isHidden(destFileName))
520 // continue;
521 //
522 // output << "Removed file " << destFileName <<'\n';
523 // nModifiedFiles++;
524 //
525 // }
526
527 return nModifiedFiles;//tbm
528}
529
530int fileMerge(std::string srcFileName, std::string destFileName, std::string commonParentName)
531{
533 std::ifstream in(srcFileName.c_str());
534 std::string srcStr((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
535 in.close();
536 in.open(destFileName.c_str());
537 std::string destStr((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
538 in.close();
539 in.open(commonParentName.c_str());
540 std::string hiddenDestStr((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
541 in.close();
543 std::pair<std::string, std::vector<bool> > mergeResults = dmp.patch_apply(patches, srcStr);
544 std::ofstream out(destFileName.c_str());
545 out << mergeResults.first;
546 out.close();
547 //update hidden file from installed one
548 out.open(commonParentName.c_str());
549 out << srcStr;
550 out.close();
551 return 0;
552}
553
554int recursiveMerge(std::string srcDirName, std::string destDirName, std::string commonParentName, std::ostream &output)
555{
556 std::set<std::string> srcFileList;
557 bool ok = recursiveFileList(srcDirName.c_str(), "", srcFileList);
558 std::set<std::string> destFileList;
559 ok = ok && recursiveFileList(destDirName.c_str(), "", destFileList);
560 std::set<std::string> hiddenFilesList;
561 ok = ok && recursiveFileList(commonParentName.c_str(), "", hiddenFilesList);
562
563 if (!ok) {
564 return -1;
565 }
566
567 for (const auto& srcIt : srcFileList)
568 {
569 std::string srcFileName = std::string{srcDirName}.append(PATH_SEPARATOR).append(srcIt);
570 if (isHidden(srcFileName)) {
571 continue;
572 }
573
574 auto destPos = destFileList.find(srcIt);
575 if (destPos != destFileList.end())
576 {
577 std::string destFileName = destDirName + PATH_SEPARATOR + (*destPos);
579 if (hiddenDestPos != hiddenFilesList.end())
580 {
581 std::string hiddenFileName = commonParentName + PATH_SEPARATOR + (*hiddenDestPos);
583 }
584 else
585 {
586 printf("Could not merge automatically, use mergetool\n");
587 }
588 destFileList.erase(destPos);
589 }
590 // else
591 // {
592 // std::set<std::string>::iterator hiddenDestPos=hiddenFilesList.find(*srcIt);
593 // if (hiddenDestPos==hiddenFilesList.end())
594 // {
595 // output << "File " << srcDirName+PATH_SEPARATOR +(*srcIt) << " has been added to the original context" << '\n';
596 // }
597 // }
598 }
599
600 // for(std::set<std::string>::iterator destIt=destFileList.begin(); destIt !=destFileList.end(); ++destIt)
601 // {
602 // std::set<std::string>::iterator hiddenDestPos=hiddenFilesList.find(*destIt);
603 // if (hiddenDestPos==hiddenFilesList.end())
604 // output << "File " << destDirName+PATH_SEPARATOR +(*destIt) << " does not belong to the original context" << '\n';
605 // }
606
607 return (ok ? 0 : 1);//tbm
608}
609
610#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
611int import(yarp::os::Bottle& importArg, folderType fType, bool verbose)
612#else
614#endif
615{
616 std::string contextName;
617 if (importArg.size() > 1) {
618 contextName = importArg.get(1).asString();
619 }
620 if (contextName == "")
621 {
622 printf("No %s name provided\n", fType == CONTEXTS ? "context" : "robot");
623 return 0;
624 }
626#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
629 rf.setVerbose(verbose);
631#endif
633 opts.searchLocations = ResourceFinderOptions::Installed;
634 std::string yarpdatahome = yarp::conf::dirs::yarpdatahome();
636 std::string destDirname = yarpdatahome.append(PATH_SEPARATOR).append(getFolderStringName(fType)).append(PATH_SEPARATOR).append(contextName);
637 //tmp:
638 std::string hiddenDirname = yarpdatahome.append(PATH_SEPARATOR).append(getFolderStringNameHidden(fType)).append(PATH_SEPARATOR).append(contextName);
640 if (fType== CONTEXTS && importArg.size() >= 3)
641 {
644 bool ok = true;
645 for (size_t i = 2; i<importArg.size(); ++i)
646 {
647 std::string fileName = importArg.get(i).asString();
648 if (fileName != "")
649 {
650 ok = prepareSubFolders(destDirname, fileName);
651 ok = (recursiveCopy(std::string(originalpath).append(PATH_SEPARATOR).append(fileName), std::string(destDirname).append(PATH_SEPARATOR).append(fileName)) >= 0) && ok;
652 if (ok)
653 {
655 recursiveCopy(std::string(originalpath).append(PATH_SEPARATOR).append(fileName), std::string(hiddenDirname).append(PATH_SEPARATOR).append(fileName), true, false);
656 }
657 }
658 }
659 if (ok)
660 {
661 printf("Copied selected files to %s\n", destDirname.c_str());
662 return 0;
663 }
664 else
665 {
666 printf("ERRORS OCCURRED WHILE IMPORTING FILES FOR %s %s\n", fType == CONTEXTS ? "CONTEXT" : "ROBOT", contextName.c_str());
667 return 1;
668 }
669 }
670 else if ((fType == ROBOTS && importArg.size() == 2) ||
671 (fType == CONTEXTS && importArg.size() == 2))
672 {
675
676 if (result < 0) {
677 printf("ERRORS OCCURRED WHILE IMPORTING %s %s\n", fType == CONTEXTS ? "CONTEXT" : "ROBOT", contextName.c_str());
678 } else {
679 printf("Copied %s %s from %s to %s .\n", fType == CONTEXTS ? "context" : "robot", contextName.c_str(), originalpath.c_str(), destDirname.c_str());
680 printf("Current locations for this %s:\n", fType == CONTEXTS ? "context" : "robot");
682 for (size_t curCont = 0; curCont < paths.size(); ++curCont) {
683 printf("%s\n", paths.get(curCont).asString().c_str());
684 }
685 }
686 return result;
687 }
688 else
689 {
690 yError("Invalid number of parameters");
691 return 1;
692 }
693}
694
695#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
696int importAll(folderType fType, bool verbose)
697#else
699#endif
700{
702#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
705 rf.setVerbose(verbose);
707#endif
708
711 opts.searchLocations = ResourceFinderOptions::Installed;
712 std::string yarpdatahome = yarp::conf::dirs::yarpdatahome();
714 for (size_t curPathId = 0; curPathId<contextPaths.size(); ++curPathId)
715 {
716
717 std::string curPath = contextPaths.get(curPathId).toString();
718
719 yarp::os::impl::dirent **namelist;
720 int n = yarp::os::impl::scandir(curPath.c_str(), &namelist, nullptr, yarp::os::impl::alphasort);
721 if (n<0) {
722 continue;
723 }
724 for (int i = 0; i<n; i++) {
725
726 std::string name = namelist[i]->d_name;
727
728 if (name != "." && name != "..")
729 {
731 std::string originalpath = std::string(curPath).append(PATH_SEPARATOR).append(name);
732 yarp::os::impl::stat(originalpath.c_str(), &statbuf);
733 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
734 {
735 std::string destDirname = yarpdatahome.append(PATH_SEPARATOR).append(getFolderStringName(fType)).append(PATH_SEPARATOR).append(name);
737 std::string hiddenDirname = yarpdatahome.append(PATH_SEPARATOR).append(getFolderStringNameHidden(fType)).append(PATH_SEPARATOR).append(name);
738 recursiveCopy(originalpath, hiddenDirname, true, false);// TODO: check result!
739 }
740 }
741 free(namelist[i]);
742 }
743 free(namelist);
744 }
745
746 printf("New user %s:\n", fType == CONTEXTS ? "contexts" : "robots");
747 printContentDirs(yarpdatahome.append(PATH_SEPARATOR).append(getFolderStringName(fType)));
748 return 0;
749}
750
751#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
753#else
755#endif
756{
757 std::string contextName;
758 if (removeArg.size() > 1) {
759 contextName = removeArg.get(1).asString();
760 }
761 if (contextName == "")
762 {
763 printf("No %s name provided\n", fType == CONTEXTS ? "context" : "robot");
764 return 0;
765 }
767#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
770 rf.setVerbose(verbose);
772#endif
774 opts.searchLocations = ResourceFinderOptions::User;
775 std::string targetPath = rf.findPath(getFolderStringName(fType).append(PATH_SEPARATOR).append(contextName), opts);
776 if (targetPath == "")
777 {
778 printf("Could not find %s %s !\n", fType == CONTEXTS ? "context" : "robot", contextName.c_str());
779 return 0;
780 }
781 else
782 {
783 if ((fType == ROBOTS && removeArg.size() == 2) ||
784 (fType == CONTEXTS && removeArg.size() == 2))
785 {
786 char choice = 'n';
787 printf("Are you sure you want to remove this folder: %s ? (y/n): ", targetPath.c_str());
788 int got = scanf("%c", &choice);
790 if (choice == 'y')
791 {
792 int result = recursiveRemove(targetPath);
793 if (result < 0) {
794 printf("ERRORS OCCURRED WHILE REMOVING %s\n", targetPath.c_str());
795 } else {
796 printf("Removed folder %s\n", targetPath.c_str());
797 }
798 //remove hidden folder:
800 if (hiddenPath != "") {
802 }
803 return result;
804 }
805 else
806 {
807 printf("Skipped\n");
808 return 0;
809 }
810 }
811 else if (fType == CONTEXTS && removeArg.size() >= 3)
812 {
813 char choice = 'n';
814 printf("Are you sure you want to remove files from this folder: %s ? (y/n): ", targetPath.c_str());
815 int got = scanf("%c", &choice);
817 if (choice == 'y')
818 {
819 bool ok = true;
820 bool removeHidden = true;
822 if (hiddenPath != "") {
823 removeHidden = false;
824 }
825
826 for (size_t i = 2; i<removeArg.size(); ++i)
827 {
828 std::string fileName = removeArg.get(i).asString();
829 if (fileName != "")
830 {
831 ok = (recursiveRemove(std::string(targetPath).append(PATH_SEPARATOR).append(fileName)) >= 0) && ok;
832 if (removeHidden) {
833 recursiveRemove(std::string(hiddenPath).append(PATH_SEPARATOR).append(fileName), false);
834 }
835 }
836 }
837 if (ok)
838 {
839 printf("Removed selected files from %s\n", targetPath.c_str());
840 return 0;
841 }
842 else
843 {
844 printf("ERRORS OCCURRED WHILE IMPORTING FILES FOR %s %s\n", fType == CONTEXTS ? "CONTEXT" : "ROBOT", contextName.c_str());
845 return 1;
846 }
847 }
848 else
849 {
850 printf("Skipped\n");
851 return 0;
852 }
853 }
854 else
855 {
856 yError ("Invalid number of parameters");
857 return 1;
858 }
859 }
860}
861
862#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
863int diff(std::string contextName, folderType fType, bool verbose)
864#else
865int diff(std::string contextName, folderType fType)
866#endif
867{
869#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
872 rf.setVerbose(verbose);
874#endif
875
877 opts.searchLocations = ResourceFinderOptions::User;
878 std::string userPath = rf.findPath(getFolderStringName(fType).append(PATH_SEPARATOR).append(contextName), opts);
879
880 opts.searchLocations = ResourceFinderOptions::Installed;
882
883#ifdef DO_TEXT_DIFF
884 //use this for an internal diff implementation
886 return 0;
887#else
888 //use this for an external diff program
889 char command[500];
890#if defined(_WIN32)
891 strcpy(command, "winmerge ");
892#else
893 strcpy(command, "meld ");
894#endif
895 strcat(command, installedPath.c_str());
896 strcat(command, " ");
897 strcat(command, userPath.c_str());
898 return std::system(command);
899#endif
900}
901
902#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
903int diffList(folderType fType, bool verbose)
904#else
906#endif
907{
910 opts.searchLocations = ResourceFinderOptions::Installed;
912 for (size_t n = 0; n <installedPaths.size(); ++n)
913 {
914 std::string installedPath = installedPaths.get(n).asString();
915 std::vector<std::string> subDirs = listContentDirs(installedPath);
916 for (auto& subDir : subDirs)
917 {
918 std::ostream tmp(nullptr);
919 opts.searchLocations = ResourceFinderOptions::User;
920#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
923 rf.setQuiet();
925#endif
926 std::string userPath = rf.findPath(getFolderStringName(fType).append(PATH_SEPARATOR).append(subDir), opts);
927 if (userPath == "") {
928 continue;
929 }
930 try
931 {
932 if (recursiveDiff(installedPath.append(PATH_SEPARATOR).append(subDir), userPath, tmp) > 0) {
933 std::cout << subDir << '\n';
934 }
935 }
936 catch (...)
937 {
938 printf("Exception occurred during call to diffList() on path \"%s\"\n", userPath.c_str());
939 }
940 }
941 }
942
943 return 0;
944}
945
946#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
948#else
950#endif
951{
952 std::string contextName;
953 if (mergeArg.size() > 1) {
954 contextName = mergeArg.get(1).asString();
955 }
956 if (contextName == "")
957 {
958 printf("No %s name provided\n", fType == CONTEXTS ? "context" : "robot");
959 return 0;
960 }
962#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
965 rf.setVerbose(verbose);
967#endif
968
969 if (mergeArg.size() >2)
970 {
971 for (size_t i = 2; i<mergeArg.size(); ++i)
972 {
973 std::string fileName = mergeArg.get(i).asString();
974 if (fileName != "")
975 {
977 opts.searchLocations = ResourceFinderOptions::User;
978 std::string userFileName = rf.findPath(getFolderStringName(fType).append(PATH_SEPARATOR).append(contextName).append(PATH_SEPARATOR).append(fileName), opts);
979
980 std::string hiddenFileName = rf.findPath(getFolderStringNameHidden(fType).append(PATH_SEPARATOR).append(contextName).append(PATH_SEPARATOR).append(fileName), opts);
981
982 opts.searchLocations = ResourceFinderOptions::Installed;
983 std::string installedFileName = rf.findPath(getFolderStringName(fType).append(PATH_SEPARATOR).append(contextName).append(PATH_SEPARATOR).append(fileName), opts);
984
985 if (userFileName != "" && hiddenFileName != "" && installedFileName != "") {
987 } else if (userFileName != "" && installedFileName != "") {
988 printf("Need to use mergetool\n");
989 } else {
990 printf("Could not merge file %s\n", fileName.c_str());
991 }
992 }
993 }
994 }
995 else
996 {
998 opts.searchLocations = ResourceFinderOptions::User;
999 std::string userPath = rf.findPath(getFolderStringName(fType).append(PATH_SEPARATOR).append(contextName), opts);
1000
1002
1003 opts.searchLocations = ResourceFinderOptions::Installed;
1005
1007 }
1008 return 0;
1009}
#define yError(...)
Definition Log.h:361
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
These options are loosely based on http://wiki.icub.org/wiki/YARP_ResourceFinder.
Helper class for finding config files and other external resources.
std::string findPath(const std::string &name)
Expand a partial path to a full path.
yarp::os::Bottle findPaths(const std::string &name)
Expand a partial path to a list of paths.
bool setQuiet(bool quiet=true)
Request that information be suppressed from the console.
bool setVerbose(bool verbose=true)
Request that information be printed to the console on how resources are being found.
std::string yarpdatahome()
Returns the directory where user-specific YARP data files should be written.
Definition dirs.h:254
struct ::stat YARP_stat
An interface to the operating system, including Port based communication.
int mkdir(const char *p)
Portable wrapper for the mkdir() function.
Definition Os.cpp:30
int stat(const char *path)
Portable wrapper for the stat() function.
Definition Os.cpp:78
int rmdir(const char *p)
Portable wrapper for the rmdir() function.
Definition Os.cpp:68
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition system.h:334
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition system.h:333
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition system.h:335
#define YARP_UNUSED(var)
Definition api.h:162
int recursiveMerge(std::string srcDirName, std::string destDirName, std::string commonParentName, std::ostream &output)
bool isHidden(std::string fileName)
int fileMerge(std::string srcFileName, std::string destFileName, std::string commonParentName)
void printSysadmFolders(yarp::os::ResourceFinder &rf, folderType ftype)
int recursiveDiff(std::string srcDirName, std::string destDirName, std::ostream &output)
void prepareHomeFolder(yarp::os::ResourceFinder &rf, folderType ftype)
void printInstalledFolders(yarp::os::ResourceFinder &rf, folderType ftype)
bool recursiveFileList(const char *basePath, const char *suffix, std::set< std::string > &filenames)
void printUserFolders(yarp::os::ResourceFinder &rf, folderType ftype)
void printContentDirs(const std::string &curPath)
std::vector< std::string > listContentDirs(const std::string &curPath)
int remove(yarp::os::Bottle &removeArg, folderType fType, bool verbose)
int diffList(folderType fType, bool verbose)
std::vector< std::string > listContentFiles(const std::string &curPath)
bool fileCopy(std::string srcFileName, std::string destFileName, bool force, bool verbose)
std::string getFolderStringName(folderType ftype)
int diff(std::string contextName, folderType fType, bool verbose)
std::string getFolderStringNameHidden(folderType ftype)
int importAll(folderType fType, bool verbose)
int recursiveCopy(std::string srcDirName, std::string destDirName, bool force, bool verbose)
int recursiveRemove(std::string dirName, bool verbose)
bool prepareSubFolders(const std::string &startDir, const std::string &fileName)
bool fileRemove(std::string fileName)
int merge(yarp::os::Bottle &mergeArg, folderType fType, bool verbose)
#define PATH_SEPARATOR
folderType
@ CONTEXTS
@ ROBOTS