~ubuntu-branches/ubuntu/vivid/unrar-nonfree/vivid

« back to all changes in this revision

Viewing changes to filcreat.cpp

  • Committer: Package Import Robot
  • Author(s): Martin Meredith
  • Date: 2015-02-03 12:58:01 UTC
  • mfrom: (1.1.18) (5.1.18 sid)
  • Revision ID: package-import@ubuntu.com-20150203125801-od6ev8cqy1er51vz
Tags: 1:5.2.5-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
// It is useful we we need to overwrite an existing folder or file,
5
5
// but need user confirmation for that.
6
6
bool FileCreate(RAROptions *Cmd,File *NewFile,wchar *Name,size_t MaxNameSize,
7
 
                OVERWRITE_MODE Mode,bool *UserReject,int64 FileSize,
8
 
                RarTime *FileTime,bool WriteOnly)
 
7
                bool *UserReject,int64 FileSize,RarTime *FileTime,bool WriteOnly)
9
8
{
10
9
  if (UserReject!=NULL)
11
10
    *UserReject=false;
30
29
    // autorename below can change the name, so we need to check it again.
31
30
    ShortNameChanged=false;
32
31
#endif
33
 
    if (Mode==OVERWRITE_NONE)
 
32
    UIASKREP_RESULT Choice=uiAskReplaceEx(Cmd,Name,MaxNameSize,FileSize,FileTime,(NewFile==NULL ? UIASKREP_F_NORENAME:0));
 
33
 
 
34
    if (Choice==UIASKREP_R_REPLACE)
 
35
      break;
 
36
    if (Choice==UIASKREP_R_SKIP)
34
37
    {
35
38
      if (UserReject!=NULL)
36
39
        *UserReject=true;
37
40
      return false;
38
41
    }
39
 
 
40
 
    // Must be before Cmd->AllYes check or -y switch would override -or.
41
 
    if (Mode==OVERWRITE_AUTORENAME)
42
 
    {
43
 
      if (!GetAutoRenamedName(Name,MaxNameSize))
44
 
        Mode=OVERWRITE_DEFAULT;
45
 
      continue;
46
 
    }
47
 
 
48
 
#ifdef SILENT
49
 
    Mode=OVERWRITE_ALL;
50
 
#endif
51
 
 
52
 
    // This check must be after OVERWRITE_AUTORENAME processing or -y switch
53
 
    // would override -or.
54
 
    if (Cmd->AllYes || Mode==OVERWRITE_ALL)
55
 
      break;
56
 
 
57
 
    if (Mode==OVERWRITE_DEFAULT || Mode==OVERWRITE_FORCE_ASK)
58
 
    {
59
 
      wchar NewName[NM];
60
 
      *NewName=0;
61
 
      eprintf(St(MFileExists),Name);
62
 
      int Choice=Ask(St(MYesNoAllRenQ));
63
 
      if (Choice==1)
64
 
        break;
65
 
      if (Choice==2)
66
 
      {
67
 
        if (UserReject!=NULL)
68
 
          *UserReject=true;
69
 
        return false;
70
 
      }
71
 
      if (Choice==3)
72
 
      {
73
 
        Cmd->Overwrite=OVERWRITE_ALL;
74
 
        break;
75
 
      }
76
 
      if (Choice==4)
77
 
      {
78
 
        if (UserReject!=NULL)
79
 
          *UserReject=true;
80
 
        Cmd->Overwrite=OVERWRITE_NONE;
81
 
        return false;
82
 
      }
83
 
      if (Choice==5)
84
 
      {
85
 
#ifndef GUI
86
 
        mprintf(St(MAskNewName));
87
 
        if (!getwstr(NewName,ASIZE(NewName)))
88
 
        {
89
 
          // Process fwgets failure as if user answered 'No'.
90
 
          if (UserReject!=NULL)
91
 
            *UserReject=true;
92
 
          return false;
93
 
        }
94
 
#endif
95
 
        if (PointToName(NewName)==NewName)
96
 
          SetName(Name,NewName,MaxNameSize);
97
 
        else
98
 
          wcsncpyz(Name,NewName,MaxNameSize);
99
 
        continue;
100
 
      }
101
 
      if (Choice==6)
102
 
        ErrHandler.Exit(RARX_USERBREAK);
103
 
    }
 
42
    if (Choice==UIASKREP_R_CANCEL)
 
43
      ErrHandler.Exit(RARX_USERBREAK);
104
44
  }
105
45
  uint FileMode=WriteOnly ? FMF_WRITE|FMF_SHAREREAD:FMF_UPDATE|FMF_SHAREREAD;
106
46
  if (NewFile!=NULL && NewFile->Create(Name,FileMode))
107
47
    return true;
108
 
  PrepareToDelete(Name);
109
48
  CreatePath(Name,true);
110
49
  return NewFile!=NULL ? NewFile->Create(Name,FileMode):DelFile(Name);
111
50
}
114
53
bool GetAutoRenamedName(wchar *Name,size_t MaxNameSize)
115
54
{
116
55
  wchar NewName[NM];
117
 
  if (wcslen(Name)>ASIZE(NewName)-10)
 
56
  size_t NameLength=wcslen(Name);
 
57
#ifdef _ANDROID
 
58
  if (NameLength>ASIZE(NewName)-10)
118
59
    return false;
 
60
#endif
119
61
  wchar *Ext=GetExt(Name);
120
62
  if (Ext==NULL)
121
 
    Ext=Name+wcslen(Name);
122
 
  *NewName=0;
 
63
    Ext=Name+NameLength;
123
64
  for (uint FileVer=1;;FileVer++)
124
65
  {
 
66
#ifdef _ANDROID // No swprintf in Android NDK r9.
 
67
    uint NamePrefixLength=Ext-Name;
 
68
    wcsncpy(NewName,Name,NamePrefixLength);
 
69
    wcscpy(NewName+NamePrefixLength,L"(");
 
70
    itoa(FileVer,NewName+NamePrefixLength+1);
 
71
    wcscat(NewName,L")");
 
72
    wcscat(NewName,Ext);
 
73
#else
125
74
    swprintf(NewName,ASIZE(NewName),L"%.*ls(%u)%ls",uint(Ext-Name),Name,FileVer,Ext);
 
75
#endif
126
76
    if (!FileExist(NewName))
127
77
    {
128
78
      wcsncpyz(Name,NewName,MaxNameSize);
201
151
  File KeepShortFile;
202
152
  bool Created=false;
203
153
  if (!FileExist(Name))
204
 
    Created=KeepShortFile.Create(Name);
 
154
    Created=KeepShortFile.Create(Name,FMF_WRITE|FMF_SHAREREAD);
205
155
 
206
156
  // Now we rename the existing file from temporary name to original long name.
207
157
  // Since its previous short name is occupied by another file, it should