Vidalia  0.3.1
file.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file file.cpp
13 ** \brief Functions and definitions for common file I/O operations
14 */
15 
16 #include "file.h"
17 #include "stringutil.h"
18 
19 #if defined(Q_OS_WIN32)
20 #include "win32.h"
21 #endif
22 
23 #include <QDir>
24 #include <QFile>
25 
26 
27 /** Create an empty file named <b>filename</b>. if <b>createdir</b> is true,
28  * then the full path to <b>filename</b> will be created. Returns true on
29  * success, or false on error and <b>errmsg</b> will be set. */
30 bool
31 touch_file(const QString &filename, bool createdir, QString *errmsg)
32 {
33  /* Expand the file's path if it starts with a shortcut, like "~/" or
34  * "%APPDATA%" */
35  QString expanded = expand_filename(filename);
36 
37  /* If the file's path doesn't exist and we're supposed to create it, do that
38  * now. */
39  if (createdir && !create_path(QFileInfo(expanded).absolutePath())) {
40  return false;
41  }
42 
43  /* Touch the file */
44  QFile file(expanded);
45  if (!QFileInfo(expanded).exists()) {
46  if (!file.open(QIODevice::WriteOnly)) {
47  return err(errmsg, file.errorString());
48  }
49  }
50  return true;
51 }
52 
53 /** Creates all directories in <b>path</b>, if they do not exist. */
54 bool
55 create_path(const QString &path)
56 {
57  QDir dir(path);
58  if (!dir.exists()) {
59  if (!dir.mkpath(dir.absolutePath())) {
60  return false;
61  }
62  }
63  return true;
64 }
65 
66 /** Recursively copy the contents of one directory to another. The
67  * destination must already exist. Returns true on success, and false
68  * otherwise. */
69 bool
70 copy_dir(const QString &source, const QString &dest)
71 {
72  /* Source and destination as QDir's */
73  QDir src(source);
74  QDir dst(dest);
75 
76  /* Get contents of the directory */
77  QFileInfoList contents = src.entryInfoList(QDir::Files | QDir::Dirs
78  | QDir::NoDotAndDotDot);
79 
80  /* Copy each entry in src to dst */
81  foreach (QFileInfo fileInfo, contents) {
82  /* Get absolute path of source and destination */
83  QString fileName = fileInfo.fileName();
84  QString srcFilePath = src.absoluteFilePath(fileName);
85  QString dstFilePath = dst.absoluteFilePath(fileName);
86 
87  if (fileInfo.isDir()) {
88  /* This is a directory, make it and recurse */
89  if (!dst.mkdir(fileName))
90  return false;
91  if (!copy_dir(srcFilePath, dstFilePath))
92  return false;
93  } else if (fileInfo.isFile()) {
94  /* This is a file, copy it */
95  if (!QFile::copy(srcFilePath, dstFilePath))
96  return false;
97  }
98  /* Ignore special files (e.g. symlinks, devices) */
99 
100  }
101  return true;
102 }
103 
104 /** Expands <b>filename</b> if it starts with "~/". On Windows, this will
105  * expand "%APPDATA%" and "%PROGRAMFILES%". If <b>filename</b> does not
106  * start with a shortcut, <b>filename</b> will be returned unmodified. */
107 QString
108 expand_filename(const QString &filename)
109 {
110  QString fname = filename;
111 #if defined(Q_OS_WIN32)
112  if (fname.startsWith("%APPDATA%\\") ||
113  fname.startsWith("%APPDATA%/"))
114  return fname.replace(0, 9, win32_app_data_folder());
115 
116  if (fname.startsWith("%PROGRAMFILES%\\") ||
117  fname.startsWith("%PROGRAMFILES%/"))
118  return fname.replace(0, 14, win32_program_files_folder());
119 #else
120  if (fname.startsWith("~/"))
121  return fname.replace(0, 1, QDir::homePath());
122 #endif
123  return fname;
124 }
125 
err
bool err(QString *str, const QString &errmsg)
Definition: stringutil.cpp:37
win32.h
win32_program_files_folder
QString win32_program_files_folder()
Definition: win32.cpp:78
win32_app_data_folder
QString win32_app_data_folder()
Definition: win32.cpp:86
stringutil.h
create_path
bool create_path(const QString &path)
Definition: file.cpp:55
touch_file
bool touch_file(const QString &filename, bool createdir, QString *errmsg)
Definition: file.cpp:31
expand_filename
QString expand_filename(const QString &filename)
Definition: file.cpp:108
copy_dir
bool copy_dir(const QString &source, const QString &dest)
Definition: file.cpp:70
file.h