other-licenses/7zstub/src/7zip/Common/FilePathAutoRename.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 // FilePathAutoRename.cpp
michael@0 2
michael@0 3 #include "StdAfx.h"
michael@0 4 #include "FilePathAutoRename.h"
michael@0 5
michael@0 6 #include "Common/Defs.h"
michael@0 7 #include "Common/IntToString.h"
michael@0 8
michael@0 9 #include "Windows/FileName.h"
michael@0 10 #include "Windows/FileFind.h"
michael@0 11
michael@0 12 using namespace NWindows;
michael@0 13
michael@0 14 static bool MakeAutoName(const UString &name,
michael@0 15 const UString &extension, int value, UString &path)
michael@0 16 {
michael@0 17 wchar_t number[32];
michael@0 18 ConvertUInt64ToString(value, number);
michael@0 19 path = name;
michael@0 20 path += number;
michael@0 21 path += extension;
michael@0 22 return NFile::NFind::DoesFileExist(path);
michael@0 23 }
michael@0 24
michael@0 25 bool AutoRenamePath(UString &fullProcessedPath)
michael@0 26 {
michael@0 27 UString path;
michael@0 28 int dotPos = fullProcessedPath.ReverseFind(L'.');
michael@0 29
michael@0 30 int slashPos = fullProcessedPath.ReverseFind(L'/');
michael@0 31 #ifdef _WIN32
michael@0 32 int slash1Pos = fullProcessedPath.ReverseFind(L'\\');
michael@0 33 slashPos = MyMax(slashPos, slash1Pos);
michael@0 34 #endif
michael@0 35
michael@0 36 UString name, extension;
michael@0 37 if (dotPos > slashPos && dotPos > 0)
michael@0 38 {
michael@0 39 name = fullProcessedPath.Left(dotPos);
michael@0 40 extension = fullProcessedPath.Mid(dotPos);
michael@0 41 }
michael@0 42 else
michael@0 43 name = fullProcessedPath;
michael@0 44 name += L'_';
michael@0 45 int indexLeft = 1, indexRight = (1 << 30);
michael@0 46 while (indexLeft != indexRight)
michael@0 47 {
michael@0 48 int indexMid = (indexLeft + indexRight) / 2;
michael@0 49 if (MakeAutoName(name, extension, indexMid, path))
michael@0 50 indexLeft = indexMid + 1;
michael@0 51 else
michael@0 52 indexRight = indexMid;
michael@0 53 }
michael@0 54 if (MakeAutoName(name, extension, indexRight, fullProcessedPath))
michael@0 55 return false;
michael@0 56 return true;
michael@0 57 }

mercurial