architecture.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. Appendix B: History and Architecture
  2. ====================================
  3. .. index:: architecture
  4. ownCloud provides desktop sync clients to synchronize the contents of local
  5. directories from computers, tablets, and handheld devices to the ownCloud
  6. server.
  7. Synchronization is accomplished using csync_, a bidirectional file
  8. synchronizing tool that provides both a command line client as well as a
  9. library. A special module for csync was written to synchronize with the
  10. ownCloud built-in WebDAV server.
  11. The ownCloud Client software is written in C++ using the `Qt Framework`_. As a
  12. result, the ownCloud Client runs on Linux, Windows, and MacOS.
  13. .. _csync: http://www.csync.org
  14. .. _`Qt Framework`: http://www.qt-project.org
  15. The Synchronization Process
  16. ---------------------------
  17. The process of synchronization keeps files in two separate repositories the
  18. same. When synchronized:
  19. - If a file is added to one repository it is copied to the other synchronized repository.
  20. - When a file is changed in one repository, the change is propagated to any other
  21. synchronized repository.
  22. - If a file is deleted in one repository, it is deleted in any other.
  23. It is important to note that the ownCloud synchronization process does not use
  24. a typical client/server system where the server is always master. This is a
  25. major difference between the ownCloud synchronization process and other systems
  26. like a file backup, where only changes to files or folders and the addition of
  27. new files are propagated, but these files and folders are never deleted unless
  28. explicitly deleted in the backup.
  29. During synchronization, the ownCloud Client checks both repositories for
  30. changes frequently. This process is referred to as a *sync run*. In between
  31. sync runs, the local repository is monitored by a file system monitoring
  32. process that starts a sync run immediately if something was edited, added, or
  33. removed.
  34. Synchronization by Time versus ETag
  35. -----------------------------------
  36. .. index:: time stamps, file times, etag, unique id
  37. Until the release of ownCloud 4.5 and ownCloud Client 1.1, the ownCloud
  38. synchronization process employed a single file property -- the file modification
  39. time -- to decide which file was newer and needed to be synchronized to the
  40. other repository.
  41. The *modification timestamp* is part of the files metadata. It is available on
  42. every relevant filesystem and is the typical indicator for a file change.
  43. Modification timestamps do not require special action to create, and have a
  44. general meaning. One design goal of csync is to not require a special server
  45. component. This design goal is why csync was chosen as the backend component.
  46. To compare the modification times of two files from different systems, csync
  47. must operate on the same base. Before ownCloud Client version 1.1.0, csync
  48. required both device repositories to run on the exact same time. This
  49. requirement was achieved through the use of enterprise standard `NTP time
  50. synchronization`_ on all machines.
  51. Because this timing strategy is rather fragile without the use of NTP, ownCloud
  52. 4.5 introduced a unique number (for each file?) that changes whenever the file
  53. changes. Although this number is a unique value, it is not a hash of the file.
  54. Instead, it is a randomly chosen number, that is transmitted in the Etag_
  55. field. Because the file number changes if the file changes, its use is
  56. guaranteed to determine if one of the files has changed and, thereby, launching
  57. a synchronization process.
  58. .. note:: ownCloud Client release 1.1 and later requires file ID capabilities
  59. on the ownCloud server. Servers that run with release earlier than 4.5.0 do
  60. not support using the file ID functionality.
  61. Before the 1.3.0 release of the Desktop Client, the synchronization process
  62. might create false conflict files if time deviates. Original and changed files
  63. conflict only in their timestamp, but not in their content. This behavior was
  64. changed to employ a binary check if files differ.
  65. Like files, directories also hold a unique ID that changes whenever one of the
  66. contained files or directories is modified. Because this is a recursive
  67. process, it significantly reduces the effort required for a synchronization
  68. cycle, because the client only analyzes directories with a modified ID.
  69. The following table outlines the different synchronization methods used,
  70. depending on server/client combination:
  71. .. index:: compatiblity table
  72. +--------------------+-------------------+----------------------------+
  73. | Server Version | Client Version | Sync Methods |
  74. +====================+===================+============================+
  75. | 4.0.x or earlier | 1.0.5 or earlier | Time Stamp |
  76. +--------------------+-------------------+----------------------------+
  77. | 4.0.x or earlier | 1.1 or later | n/a (incompatible) |
  78. +--------------------+-------------------+----------------------------+
  79. | 4.5 or later | 1.0.5 or earlier | Time Stamp |
  80. +--------------------+-------------------+----------------------------+
  81. | 4.5 or later | 1.1 or later | File ID, Time Stamp |
  82. +--------------------+-------------------+----------------------------+
  83. We strongly recommend using ownCloud Server release 4.5 or later when using
  84. ownCloud Client 1.1 or later. Using an incompatible time stamp-based
  85. synchronization mechanism can lead to data loss in rare cases, especially when
  86. multiple clients are involved and one utilizes a non-synchronized NTP time.
  87. .. _`NTP time synchronization`: http://en.wikipedia.org/wiki/Network_Time_Protocol
  88. .. _Etag: http://en.wikipedia.org/wiki/HTTP_ETag
  89. Comparison and Conflict Cases
  90. -----------------------------
  91. As mentioned above, during a *sync run* the client must first detect if one of
  92. the two repositories have changed files. On the local repository, the client
  93. traverses the file tree and compares the modification time of each file with an
  94. expected value stored in its database. If the value is not the same, the client
  95. determines that the file has been modified in the local repository.
  96. .. note:: On the local side, the modification time is a good attribute to use for
  97. detecting changes, because
  98. the value does not depend on time shifts and such.
  99. For the remote (that is, ownCloud server) repository, the client compares the
  100. ETag of each file with its expected value. Again, the expected ETag value is
  101. queried from the client database. If the ETag is the same, the file has not
  102. changed and no synchronization occurs.
  103. In the event a file has changed on both the local and the remote repository
  104. since the last sync run, it can not easily be decided which version of the file
  105. is the one that should be used. However, changes to any side will not be lost. Instead,
  106. a *conflict case* is created. The client resolves this conflict by renaming the
  107. local file, appending a conflict label and timestamp, and saving the remote file
  108. under the original file name.
  109. Example: Assume there is a conflict in message.txt because its contents have
  110. changed both locally and remotely since the last sync run. The local file with
  111. the local changes will be renamed to message_conflict-20160101-153110.txt and
  112. the remote file will be downloaded and saved as message.txt.
  113. Conflict files are always created on the client and never on the server.
  114. Checksum Algorithm Negotiation
  115. ------------------------------
  116. In ownCloud 10.0 we implemented a checksum feature which checks the file integrity on upload and download by computing a checksum after the file transfer finishes.
  117. The client queries the server capabilities after login to decide which checksum algorithm to use.
  118. Currently, SHA1 is hard-coded in the official server release and can't be changed by the end-user.
  119. Note that the server additionally also supports MD5 and Adler-32, but the desktop client will always use the checksum algorithm announced in the capabilities:
  120. ::
  121. GET http://localhost:8000/ocs/v1.php/cloud/capabilities?format=json
  122. ::
  123. json
  124. {
  125. "ocs":{
  126. "meta":{
  127. "status":"ok",
  128. "statuscode":100,
  129. "message":"OK",
  130. "totalitems":"",
  131. "itemsperpage":""
  132. },
  133. "data":{
  134. "version":{
  135. "major":10,
  136. "minor":0,
  137. "micro":0,
  138. "string":"10.0.0 beta",
  139. "edition":"Community"
  140. },
  141. "capabilities":{
  142. "core":{
  143. "pollinterval":60,
  144. "webdav-root":"remote.php/webdav"
  145. },
  146. "dav":{
  147. "chunking":"1.0"
  148. },
  149. "files_sharing":{
  150. "api_enabled":true,
  151. "public":{
  152. "enabled":true,
  153. "password":{
  154. "enforced":false
  155. },
  156. "expire_date":{
  157. "enabled":false
  158. },
  159. "send_mail":false,
  160. "upload":true
  161. },
  162. "user":{
  163. "send_mail":false
  164. },
  165. "resharing":true,
  166. "group_sharing":true,
  167. "federation":{
  168. "outgoing":true,
  169. "incoming":true
  170. }
  171. },
  172. "checksums":{
  173. "supportedTypes":[
  174. "SHA1"
  175. ],
  176. "preferredUploadType":"SHA1"
  177. },
  178. "files":{
  179. "bigfilechunking":true,
  180. "blacklisted_files":[
  181. ".htaccess"
  182. ],
  183. "undelete":true,
  184. "versioning":true
  185. }
  186. }
  187. }
  188. }
  189. }
  190. Upload
  191. ~~~~~~
  192. A checksum is calculated with the previously negotiated algorithm by the client and sent along with the file in an HTTP Header.
  193. ```OC-Checksum: [algorithm]:[checksum]```
  194. .. image:: ./images/checksums/client-activity.png
  195. During file upload, the server computes SHA1, MD5, and Adler-32 checksums and compares one of them to the checksum supplied by the client.
  196. On mismatch, the server returns HTTP Status code 400 (Bad Request) thus signaling the client that the upload failed.
  197. The server then discards the upload, and the client blacklists the file:
  198. .. image:: ./images/checksums/testing-checksums.png
  199. ::
  200. <?xml version='1.0' encoding='utf-8'?>
  201. <d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
  202. <s:exception>Sabre\DAV\Exception\BadRequest</s:exception>
  203. <s:message>The computed checksum does not match the one received from the
  204. client.</s:message>
  205. </d:error>
  206. The client retries the upload using exponential back-off.
  207. On success (matching checksum) the computed checksums are stored by the server in ``oc_filecache`` alongside the file.
  208. Chunked Upload
  209. ~~~~~~~~~~~~~~
  210. Mostly same as above.
  211. The checksum of the full file is sent with every chunk of the file.
  212. But the server only compares the checksum after receiving the checksum sent with the last chunk.
  213. Download
  214. ~~~~~~~~
  215. The server sends the checksum in an HTTP header with the file. (same format as above).
  216. If no checksum is found in ``oc_filecache`` (freshly mounted external storage) it is computed and stored in ``oc_filecache`` on the first download.
  217. The checksum is then provided on all subsequent downloads but not on the first.
  218. .. _ignored-files-label:
  219. Ignored Files
  220. -------------
  221. The ownCloud Client supports the ability to exclude or ignore certain files from the synchronization process.
  222. Some system wide file patterns that are used to exclude or ignore files are included with the client by default and the ownCloud Client provides the ability to add custom patterns.
  223. By default, the ownCloud Client ignores the following files:
  224. * Files matched by one of the patterns defined in the Ignored Files Editor
  225. * Files containing characters that do not work on certain file systems ``(`\, /, :, ?, *, ", >, <, |`)``.
  226. * Files starting with ``._sync_xxxxxxx.db`` and the old format ``.csync_journal.db``, as these files are reserved for journalling.
  227. If a pattern selected using a checkbox in the `ignoredFilesEditor-label` (or if
  228. a line in the exclude file starts with the character ``]`` directly followed by
  229. the file pattern), files matching the pattern are considered *fleeting meta
  230. data*.
  231. These files are ignored and *removed* by the client if found in the
  232. synchronized folder.
  233. This is suitable for meta files created by some applications that have no sustainable meaning.
  234. If a pattern ends with the forward slash (``/``) character, only directories are matched.
  235. The pattern is only applied for directory components of filenames selected using the checkbox.
  236. To match filenames against the exclude patterns, the UNIX standard C library
  237. function ``fnmatch`` is used.
  238. This process checks the filename against the specified pattern using standard shell wildcard pattern matching.
  239. For more information, please refer to `The opengroup website
  240. <http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_13_01>`_.
  241. The path that is checked is the relative path under the sync root directory.
  242. **Pattern and File Match Examples:**
  243. +-----------+------------------------------+
  244. | Pattern | File Matches |
  245. +===========+==============================+
  246. | ``~$*`` | ``~$foo``, ``~$example.doc`` |
  247. +-----------+------------------------------+
  248. | ``fl?p`` | ``flip``, ``flap`` |
  249. +-----------+------------------------------+
  250. | ``moo/`` | ``map/moo/``, ``moo/`` |
  251. +-----------+------------------------------+
  252. The Sync Journal
  253. ----------------
  254. The client stores the ETag number in a per-directory database, called the
  255. *journal*. This database is a hidden file contained in the directory to be
  256. synchronized.
  257. If the journal database is removed, the ownCloud Client CSync backend rebuilds
  258. the database by comparing the files and their modification times. This process
  259. ensures that both server and client are synchronized using the appropriate NTP
  260. time before restarting the client following a database removal.
  261. Custom WebDAV Properties
  262. ------------------------
  263. In the communication between client and server a couple of custom WebDAV properties
  264. were introduced. They are either needed for sync functionality or help have a positive
  265. effect on synchronization performance.
  266. This chapter describes additional XML elements which the server returns in response
  267. to a successful PROPFIND request on a file or directory. The elements are returned in
  268. the namespace ``oc``.
  269. Server Side Permissions
  270. ------------------------
  271. The XML element ``<oc:permissions>`` represents the permission- and sharing state of the
  272. item. It is a list of characters, and each of the chars has a meaning as outlined
  273. in the table below:
  274. +------+----------------+-------------------------------------------+
  275. | Code | Resource | Description |
  276. +------+----------------+-------------------------------------------+
  277. | S | File or Folder | is shared |
  278. +------+----------------+-------------------------------------------+
  279. | R | File or Folder | can share (includes re-share) |
  280. +------+----------------+-------------------------------------------+
  281. | M | File or Folder | is mounted (like on Dropbox, Samba, etc.) |
  282. +------+----------------+-------------------------------------------+
  283. | W | File | can write file |
  284. +------+----------------+-------------------------------------------+
  285. | C | Folder | can create file in folder |
  286. +------+----------------+-------------------------------------------+
  287. | K | Folder | can create folder (mkdir) |
  288. +------+----------------+-------------------------------------------+
  289. | D | File or Folder | can delete file or folder |
  290. +------+----------------+-------------------------------------------+
  291. | N | File or Folder | can rename file or folder |
  292. +------+----------------+-------------------------------------------+
  293. | V | File or Folder | can move file or folder |
  294. +------+----------------+-------------------------------------------+
  295. Example:
  296. <oc:permissions>RDNVCK</oc:permissions>
  297. File- or Directory Size
  298. -----------------------
  299. The XML element ``<oc:size>`` represents the file- or directory size in bytes. For
  300. directories, the size of the whole file tree underneath the directory is accumulated.
  301. Example:
  302. <oc:size>2429176697</oc:size>
  303. FileID
  304. ------
  305. The XML element ``<oc:id>`` represents the so called file ID. It is a non volatile string id
  306. that stays constant as long as the file exists. It is not changed if the file changes or
  307. is renamed or moved.
  308. Example:
  309. <oc:id>00000020oc5cfy6qqizm</oc:id>