<commit_before>/*
 * NotebookPaths.cpp
 *
 * Copyright (C) 2009-16 by RStudio, Inc.
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

#include "SessionRmdNotebook.hpp"
#include "NotebookCache.hpp"
#include "NotebookPaths.hpp"

#include <ctime>

#include <core/FileSerializer.hpp>

#include <session/SessionModuleContext.hpp>
#include <session/SessionUserSettings.hpp>

using namespace rstudio::core;

namespace rstudio {
namespace session {
namespace modules {
namespace rmarkdown {
namespace notebook {
namespace {

// a mapping of paths to the corresponding IDs
std::map<std::string, std::string> s_idCache;
std::time_t s_cacheWriteTime = 0;

FilePath cachePath(const std::string& nbCtxId)
{
   return notebookCacheRoot().childPath("paths-" + nbCtxId);
}

void cleanNotebookPathMap()
{
   FilePath cache = cachePath(notebookCtxId());
   Error error = core::readStringMapFromFile(cache, &s_idCache);

   for (std::map<std::string, std::string>::iterator it = s_idCache.begin();
        it != s_idCache.end(); 
        it++)
   {
      // clean up cache entries that refer to files that don't exist
      if (!FilePath(it->first).exists())
         s_idCache.erase(it->first);
   }

   // write out updated cache
   error = writeStringMapToFile(cache, s_idCache);
   if (error)
      LOG_ERROR(error);
   s_cacheWriteTime = std::time(NULL);<commit_msg>address possible use of invalidated iterator in notebook cache cleanup<commit_after>/*
 * NotebookPaths.cpp
 *
 * Copyright (C) 2009-16 by RStudio, Inc.
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

#include "SessionRmdNotebook.hpp"
#include "NotebookCache.hpp"
#include "NotebookPaths.hpp"

#include <ctime>

#include <core/FileSerializer.hpp>

#include <session/SessionModuleContext.hpp>
#include <session/SessionUserSettings.hpp>

using namespace rstudio::core;

namespace rstudio {
namespace session {
namespace modules {
namespace rmarkdown {
namespace notebook {
namespace {

// a mapping of paths to the corresponding IDs
std::map<std::string, std::string> s_idCache;
std::time_t s_cacheWriteTime = 0;

FilePath cachePath(const std::string& nbCtxId)
{
   return notebookCacheRoot().childPath("paths-" + nbCtxId);
}

void cleanNotebookPathMap()
{
   FilePath cache = cachePath(notebookCtxId());
   Error error = core::readStringMapFromFile(cache, &s_idCache);

   // loop over entries (conditionalize increment so we don't attempt to
   // move forward over an invalidated iterator)
   for (std::map<std::string, std::string>::iterator it = s_idCache.begin();
        it != s_idCache.end();)
   {
      // clean up cache entries that refer to files that don't exist
      if (!FilePath(it->first).exists())
         s_idCache.erase(it++);
      else
         ++it;
   }

   // write out updated cache
   error = writeStringMapToFile(cache, s_idCache);
   if (error)
      LOG_ERROR(error);
   s_cacheWriteTime = std::time(NULL);
{'fraction_non_alphanumeric': 0.10575916230366492, 'fraction_numerical': 0.005235602094240838, 'mean_word_length': 4.234246575342466, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5006747', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1125, 'n_words': 365}