Introduction to the Subversion API function and development ideas

zhaozj2021-02-16  78

Now with some friends consider writing a development cooperation with online communication, resource sharing. When the information was found, I found the Subversion. Fans it. In the first instructions, I haven't translated into a Chinese component (it is sticked to the following things) translation, now studying its source code, telling the truth, really good! Here is the body of translation:

Subversion development information

Subversion is an open source project, its financial support from Collabnet Inc, which is a software company in California. This project welcomes programmers to join the development, encourage you to do something for it, whether it is a point idea or a correction of bugs, or refining code.

This chapter is a person who helps this software developed directly with the source code that intends to use this software. We will publish the internal details of this software here, which can continue to develop Subversion for you, or you can write new tools with Subversion's libraries.

Library

Subversion uses modular design and is a collection of Group C libraries. Each library has a well-defined interface and Purpose, and most modules are in one of the three main layers. These three layers are: Repositiry Layer, Storage Access Layer and Client Layer. Before learning these layers, learn the detailed directory of the libraries listed in the table below. To maintain consistency, use their UNIX library name (such as: libsvc_fs, libsvn_wc, mod_dav_svn).

Table: Subversion library directory

Library

Introduction

Libsvn_client

The main interface of the client program

Libsvn_delta

Tree and text Differencing Routines

Libsvn_fs

Subversion file system library.

Libsvn_RA

Repository Access Commons and Module Loader

Libsvn_ra_dav

WebDAV storage access module

Libsvn_ra_local

Local storage access module

Libsvn_ra_svn

A private protocol storage access module

Libsvn_repos

Storage (warehouse) interface

Libsvn_subr

Various useful subroutines

Libsvn_wc

Work Version (Copy) Manager

MOD_DAV_SVN

Map the WebDAV operation to the Apache module of the Subversion

In the table above, the word "miscellaneous" only has a good thing. Subversion's development team is very rigorous to let various functions in the fitting layer and library. The biggest advantage of modular design is that it is not complicated from the developer's perspective. As a developer, you can quickly find the location you need. The following chart indicates how the Subversion library is assembled.

Chart: Structure of Subversion

Another advantage of using modular design is: We can replace a specified module with a new library, and implement the same API without affecting other code. In fact, this situation has happened. Libsvn_ra_dav, libsvn_ra_local, and implemented are the same interface. The three libraries communicate with the storage layer, where libsvn_ra_dav and libsvn_ra_svn communicate through network, and libsvn_ra_local is directly connected.

Subversion's client design is also highly modular. The client that can already be used is only a command-based program, but there are many third parties already developing the GUI program. These clients use the same API function, and for most client programming, libsvn_client a library is sufficient. Storage layer

There are two libraries associated with the storage layer, one is the Repository library, one is a FileSystem library. These libraries provide a revision report for storage and versions of control data. This layer is connected to the Client layer by storing the access layer, and from the user's point of view, the material is from the other end of the network.

The Subversion file system can be accessed through the libsvn_fs API function. This file system is actually a virtual file system, not the real file system used in the operating system. Here, so-called storage, not really created a file or directory, in contrast, it is using a database system as a background. The currently used database system is BDB. But with future development, it will be compatible with other database systems, perhaps accessible via ODBC.

The API function of this file system provides features that can be used in most file systems: you can create, mobile files, and directory, copy, and move them, edit the contents of the file, and more. Some features also need to continue to be improved, such as: add, delete, and moving the original data (attributes) of files or folders. In addition, this file system has version control functions, that is, after you changed the directory tree, it can remember before, or previously previous directory trees - until the initial look.

Any fracture of your directory tree is done in a Subversion transaction. Here is a simplified example for describing how to edit your file system:

1, Begin: Start a Subversion transaction;

2, modify (add, delete, attribute editing, etc.);

3. CommT: Submit a transaction.

Once you submit a transaction, modifications to the file system will be stored in Historical Artifacts. Every time this operation will form a single new directory tree amendment version, and each version has a Snapshot that is always existing so that you can always access them.

Explanation of transactions

Using transactions in libsvn_fs libraries close to database code, it is especially easy to confuse it with the transaction used by the database system itself. Both transactions support atomic operations and transaction isolation. In other words, use a transaction, you can make your set of operations either success, or all failure, if it fails, just like something happened - doing, do not affect the operation of the data.

Database transactions typically only handle some small operations for the database itself. The Subversion's transaction is large, which handles some high-level operations, like to modify a collection of directory and files, and generate new versions of file system libraries. If you haven't appreciated it, you should consider it. Before using the database transaction, you need to create a Subversion transaction first (this, if the SunVersion transaction fails, the database will not create any transactions at all).

Fortunately, in the API function of the file system, the database system has almost completely hidden. It is only meaningful when you deepen the specific implementation of the data system.

Similar to the general file system, you want to use the Subversion file system, access or describe a fixed version of the file or directory, like a path string, like / foo / bar, just like you are often used The operation in the shell program is operated. By passing the name of the file or directory by giving the appropriate API function, you can create a new file or directory. The same is true when querying information. Unlike most file systems, only one path does not provide enough information to describe a file or folder. The ordinary folder structure is the following (as an example of a two-layer structure):

Chart: Directory Structure of 2 Structure

Subversion's file system has a property that belongs to its own, that is, this is "time". In this file system interface, almost every function of the PATH parameter, there is a root parameter at the same time. This SVN_FS_ROOT_T parameter describes a revision also describes a transaction and provides different / foo / bar with version 98 / foo / bar on version 98. The chart below shows this feature:

Chart: Revision Time, Subversion Features

As mentioned earlier, lib_svn's API function is very similar to other file systems, but it has a very interesting version control capability. Using these API functions have been able to meet the requirements of the basic pairs of files and directory, despite this, Subversion still requires more features, which introduces the libsvn_repos library.

Libsvn_repos is basically the package of file system function libraries. This library is responsible for creating a warehouse layer to ensure that the file system is initialized, and so on. This library also provides some hook scripts that are executed when some behavior occurs when it occurs. These scripts can be used for notification, authorization, and other things that the warehouse administrator wants to do. These functions are not directly related to the version control system, so they are placed in separate libraries.

Developers who use libsvn_repos libraries will find that it does not have all interfaces of the full package file system. Only some of the active activities of some major file systems will be encapsulated by the storage interface. These include creation and submitting the Subversion transaction, and modifications to the revision. After that, the remaining events will also be encapsulated. Didn't package or call the libsvn_fs API function directly.

Below is a code snippet, which uses storage and file system interfaces, and creates a new file system revision by adding a directory. Note that in this example (including all examples later), the SVN_ERR macro simply detects unsuccessful errors from a function, and returns it if there is an error.

Example: Using a storage layer

/ * Create a new directory new_directory in the repos_path directory (Subversion Warehouse Directory). All memory allocations are made in Pool. This function creates an additional version of New_Directory. * /

Static SVN_ERROR_T * MAKE_NEW_DIRECTORY (Const Char * Repos_path,

Const char * new_directory,

APR_POOL_T * POOL)

{

SVN_ERROR_T * ERR;

SVN_REPOS_T * REPOS

SVN_FS_T * FS;

SVN_REVNUM_TYOGEST_REV;

SVN_FS_TXN_T * TXN;

SVN_FS_ROOT_T * TXN_ROOT;

Const char * conflict_str;

/ * Open the repos_path directory in the warehouse * /

SVN_ERR (SVN_REPOS_Open (& Repos, Repos_Path, Pool); / * Get pointers for file system objects stored in REPOS * /

FS = SVN_REPOS_FS (REPOS);

/ * Query the file system to get the latest revision of the current existing * /

SVN_ERR (SVN_FS_YOUNGEST_REV (& youngest_rev, fs, pool);

/ * Based on Youngest_Rev to start a transaction. Because we are based on the latest Snapshot of the file system, there is a lot of possibilities of conflicts when submitting transactions later. * /

SVN_ERR (SVN_FS_BEGIN_TXN (& TXN, FS, YOUNGEST_REV, POOL);

/ * Now, we have launched a new Subversion transaction, first get a root object that represents this transaction * /

SVN_ERR (SVN_FS_TXN_ROOT (& TXN_ROOT, TXN, POOL);

/ * Create a new directory new_directory * / under the ROOT of the transaction

SVN_ERR (SVN_FS_MAKE_DIR (TXN_ROOT, New_DIRECTORY, POOL);

/ * Submit this transaction, create a new revision version containing our newly added directory * /

Err = SVN_REPOS_FS_COMMIT_TXN (& Conflict_Str, REPOS,

& youngest_rev, txn);

IF (! ERR)

{

/ * Is there no error? Very good, output a brief report * /

Printf ("Directory '% S' Was SuccessFully Added As New Revision"

"'%" SVN_REVNUM_T_FMT "' ./n", new_directory, youngest_rev);

}

ELSE IF (Err-> APR_ERR == SVN_ERR_FS_CONFLICT))

{

/ * UH-OH. Because of a conflict, our submission failed (we are changing the area, there may be other people change first). Output error message. * /

Printf ("A Conflict Occurred At Path '% S' While Attempting"

"To Add Directory '% s' to the repository at '% s' ./ n",

Conflict_str, new_directory, repos_path;

}

Else

{

/ * Other errors appear, output error message * /

Printf ("An Error Occurred While Attempting to Add Directory '% S'"

"To the repository at '% s' ./ n",

New_directory, repos_path;

}

/ * Return the result * /

Return ERR;

}

In the previous code snippet, we call the interface of Repository and FileSystem. We can submit a transaction simply using SVN_FS_COMMIT_TXN, but the API of the file system does not know the HOOK mechanism of the Repository library. If you want to automate some tasks unrelated to the Subversion (for example, send a message to others, tell them what you do), then you must use the libsvn_repos package submission function - SVN_REPOS_FS_COMMIT_TXN. This function first executes a "pre-commit" hook script (if any), then submit a transaction, and finally execute a "post-commit" hook script. This hook provides a report mechanism for file system libraries that don't really belong to the core. (See the Hook Script chapter) The Hook mechanism is one of the reasons why the Repository library is separated from the FileSystem code. The libsvn_repos API function also provides several other important features, including:

1. In Subversion Repository and Filesysytems included in Repository, create, open, destroy and execute Recovery Steps.

2. Describe the difference in the two file system trees.

3. Query all information submitted logs that exist file edited.

4. Generate "DUMP" of a readable file system, which is a complete description of the revision in the file system.

5. Analyze the format of DUMP and import a revision version from "Dump" into a different Subversion Repositiry.

Repository Access Layer

If the Subversion Repository layer is in "Line's bottom", the repository access layer is Line itself. It is responsible for carrying data between Client libraries and repository. This layer contains libsvn_ra module import library, the RA module itself (currently including libsvn_dav, libsvn_ra_local, and libsvn_ra_svn libraries), and other libraries required by these RA modules, like mod_dav_svn, or libsvn_ra_svn service SVNserve.

Subversion uses the URL to indicate storage resources, so the URL architecture portion of the protocol (usually file:, http:, https:, or svn :) is used to determine which RA module handling communication. Each module registers a list of protocols it knows "Talk", so Ra Loader can determine which module handling task is used at runtime. You can perform SVN-VERSION, determine which RA module is available in the Subversion command line client, and what protocol they all support (the example below):

$ SVN --Version

SVN, Version 0.25.0 (Dev Build)

Compiled Jul 18 2003, 16:25:59

COLLABNET, 2000-2003.

Subversion is open source software, see http://subversion.tigris.org/

The Following Repository Access (RA) Modules Are Available:

* RA_DAV: Module for Accessing A Repository Via WebDAV (DELTAV) Protocol.

- Handles 'http' Schema * Ra_local: Module for Accessing a repository on local disk.

- Handles 'File' Schema

* RA_SVN: Module for Accessing a repository use the svn network protocol.

- Handles 'SVN' Schema

RA-DAV (RRPOSITORY Access using HTTP / DAV)

This library can be used when the client machine runs on a machine different from the server. To understand how this module works, we must first explain some other key parts configured in the Repository Access layer - the powerful Apache HTTP server and Neon HTTP / Webdav Client library.

The main network service of the Subversion is Apache HTTP service. Apache is a proven, scalable source code open server. It can withstand high network loads and can run on multiple platforms. Apache servers support a lot of standard validation protocols and can be extended by loading other modules. It also supports network PIPELING and cache optimization. By using the Apache server, Subversion gains all of these properties. In addition, since most firewalls are allowed to allow HTTP protocols, the system administrator does not have to change the firewall configuration in order to make the Subversion work.

Subversion uses HTTP and WebDAV (with Delta) to communicate with the Apache server. In this chapter, the WebDAV section of this chapter is related to its detailed explanation. Apache2.0 itself has a Mod_DAV module that can understand the DAV extension of HTTP. Subversion itself supports the MOD_DAV_SVN module, which is another Apache module that works with MOD_DAV as a background to support special operations of WebDAV and DeltaV.

When accessing repository via HTTP, the RA LOADER library selects libsvn_ra_dav as an access module. The Subversion client calls the universal RA interface, and these calls will be missed into a group of HTTP / WebDAV Request. By using the Neon library, libsvn_ra_dav transmits these request to the Apache server. Apache has been received later, discovering the URLs of these Request Location to configure to DAV LOCATION (using the location in http.conf), these Request is stripped to strip its own mod_dav module. If the configuration is correct, mod_dav uses Subversion's own mod_dav_svn handling the relevant file system requirements, not the universal mod_dav_fs from Apache. The final result is: The client directly communicates with mod_dav_svn, and the latter is binding directly on the Subversion Repository layer.

This is a brief introduction to the actual thing. For example: Subversion Repository may not be protected by Apache's permission system. In this way, when the client is connected, it may be refused by the Apache's permission system. At this time, libsvn_ra_dav will get information inadequate from Apache, and return to the client to get updated authorization data. If you can provide data correctly, the user has the permissions of the query Apache, and the command that automatically attempts to execute will be received, everything is OK next. If the permission information is insufficient, the request will eventually fail, and the client will report an error to the user. By using Non and Apache, Subversion also has sufficient processing capabilities in some complex areas. For example, if Neon discovers the OpenSSL library, it will allow the client to use the SSL encryption protocol to communicate with Apache server (Apache server own MOD_SSL "know this language"). In addition, both neon itself and Apache's mod_deflate can understand the "deflate" algorithm, so it can compress the request to make the transmitted data smaller. Here will also support redirection services.

RA-SVN (dedicated Respository Access protocol)

As a standard HTTP / WebDAV protocol, Subveness also provides a dedicated protocol. The libsvn_ra_svn module uses its own socket and communicates with a separate server (SVNServer program), this server is on a host. The client uses the svn: // structure to access the repository.

This mechanism lacks most of the advantages of using the Apache server in the above section, but it may have certain attractions to certain system administrators. It is easy to configure and run, install a SVNServer process is very fast. Compared to Apache, it is also a lot, it is easy to check. In addition, some system administrators already have their own SSH underlying structure, and I hope that Subversion uses it. The client using RA_SVN is easy to adjust to the SSH protocol.

RA-LOCAL (directly accessing Respository)

Not all communication to Repository requires a server process and a network layer. For users who just want to access the repository on the local disk, just use the file: URL format, and the features provided by the libsvn_ra_local library. This module binds the Repository and FileSystem libraries directly, so there is no need to communicate online.

Subversion requires the server name or empty in File: or empty, or Localhost, and does not require communication ports. Your URL should look like this: File: // localhost / path / to / repos or file: /// path / to / repos.

It should be noted that File: This URL cannot be used in a regular web browser, otherwise the browser will query this file directly in the local file system, but the Subversion's file system is actually a virtual file system. Your browser can't understand this system.

Your own RA library

If you still want to use another agreement, that is just right. That's why you want to design a repository access layer! Developers can redesign a network layer, implement the RA interface at one end, and the other end implements communication with repository. The new library can use the ready-made protocol, you can also invent one yourself. You can use IPC or Let's Get Crazy, Shall WE? Settings you can design an Email-based protocol. Subversion provides an API function, and you are responsible for providing creativity. Client layer

At the client, most of the client library is used to manage job versions, including directory that accommodate files, and subdirects that are existing as local services, you can edit the local "image" of Repository, and reflect from / send to Repository Access layer Change.

Subversion's work repository - Libsvn_wc, its responsibility is to manage jobs for jobs. To accomplish this, this library stores all the management information of all work version directories in a specific subdirectory. The name of this subdirectory is .SVN, stored in the directory of each work version, and contains some files and subdirectories for recording status and providing a private space for management. Similar to CVS is that there is also a CVS directory in the CVS working directory.

Subversion's client library, libsvn_client features a wide range of features; it mixes the functionality of the work repository and the Repository Access layer, then provides the highest layer API function, which can be used for any application that wants to perform a revision control. For example: Functions SVN_CLINET_CHECKOUT has a URL parameter. It passes a URL to the RA layer and opens an Authenticated Session for a specific repository. Then it queries a particular tree to the repository and sends this tree to the working repository, and the work version library will write the entire work version to the disk.

The design purpose of the client library is for the application. Although the Subversion source code contains a command-based client, it is easy to design a GUI client on the basis of the client library. The new Guis does not have a client that includes the command line, which can use the libsvn_client API function to access the same functions, data, and callback mechanisms as the command line client.

Binding Directly (direct binding)

Why should your GUI program bind directly to the libsvn_client library instead of encapsulating a client program? In addition to efficiency issues, it also relates to potential correctness issues. A command line program bound to the client library must have an efficient translation engine that converts C's data into readable information, and finally outputs this readable information. And this conversion process may cause loss of data. That is to say, the command line program output may not be all information acquired by the API function, or in order to express more information, some information is merged.

If you encapsulate such a command line program, the information that encapsulates its program is the information parsed by this command line client (as mentioned earlier, this information may make it incomplete), and then The information will be parsed according to the intention of the new program. Every time you install a layer, the integrity of the original information may lose.

Use API functions

Developing applications using Subversion's API function is simple. All common headers are saved in the Source Tree Subversion / INCLUDE directory. When you build and install Subversion, these headers will be copied to your system directory. In these header files, all the functions and data types in the Subversion library that users can access are displayed.

The first thing you need to pay attention is that the Subverion's data type and function is protected by named space. Name of any common Subversion symbol is starting from SVN_, followed by the name of this name, such as "WC", "Client", "FS", etc., then a downline, the remaining part is symbolic name. The SEMI-PUBLIC function (used in the source code file of a given library, but not the output encoding of the library, can be found in the library directory) Different from the named architecture above, it is in the code of the library plus double Underline ("__"). The source file private function does not have a special prefix and is declared as static. Of course, the compiler is not interested in these naming specifications, but it can help us distinguish these functions and data types.

Apache Lightweight Runtime Library

In addition to Subversion's own data type, you can also see some referenced data types, which start with "APR_", which comes from the APCHE Lightweight Run (APR) library. This is from the efforts that will depend on the code from the specific platform. The result is a common, cross-platform API function, although Apache is the first use of this library, but Subversion developers immediately The value of using ARP was found. This means that there is no code to depend on the specific platform in Subversion itself. Of course, this means that Subversion's clients and servers can compile operations anywhere. The platform currently supported includes: UNIX, WIN32, BEOS, OS / 2 and Mac OS X.

In addition to providing cross-platform calls, the APR also provides access to Subversion, access to certain custom data types, like dynamic arrays and hash tables. Subversion has widely used these data types in your code. But the data type that can be visible almost in the Subversion code is the APR_POOL_T. Subversion uses Pool in a place where you need to allocate memory (unless an extended library requires different data management structures, it is used to deliver it in the API), although this does not need to do this when using the API code, but requires the user to give the API The function provides Pool. This means that users who use the API function must also be connected to the APR, and the APR_Initialize () function must be called to initialize the APR subsystem, and then you must get a POOL for the API call.

URL and path requirements

The remote version of the control operation runs throughout the Subversion, so that internationalization issues are worthy of attention. Of course, the remote concept may refer to the "entire office", but it may also refer to "global". In order to solve this problem, all accept paths are parameter Subversion public interfaces, which require path to follow UTF-8 specification. This means that new, useful client programs driven by livsvn_client, when passing the path to the Subversion library, the local path format must be converted to the UTF-8 format, and when the path output is output, To convert the path format of the UTF-8 standard to the local format. Fortunately, Subversion provides a set of functions, (Reference: Subversion / Include / SVN_UTF.H) Allows any program to complete this conversion.

Similarly, the Subversion API function requires all URL parameters to be URI encoding. Therefore, for this URL: File: /// Home / username / my file.txt, its file name is My file.txt, must be converted to: file /// Home / YSERNAME / MY% 20File.txt. In addition, Subversion also provides two help functions: -svn_path_url_encode and svn_path_url_decode, which are used for encoding and decoding. Inside the work version management area

As we mentioned earlier, there is a subdirectory in each working version. SVN, which is used to store management information of the work version. Subversion uses this information to track the following transactions:

• The repository location of the file or subdirectory in the work version directory.

· The revision of the current file and directory currently in the work version.

· Any of these files and directories, user-defined properties.

· The original (without editing) work version file copy.

If there are several different data stored in the .svn directory, we will use the most important projects.

ENTRIES file

Perhaps the most important file in the .svn directory is Entries file. This file is an XML document that contains a lot of version resource management information about the work version. This file is used to track repository's URLS, original revision, file Checksums, original text, and attribute timestamp, timing arrangements and conflict status information, and the last submitted information (author, revised version, timestamp), local copy History - Almost all Subversion clients have to know all about version controlled resources can be found here!

The following is an example of an actual entries file:

Example typical. SVN / Entries file

XMLns = "SVN:">

COMMITTED-REV = "1"

Name = "svn: this_dir"

Committed-date = "2002-09-24T17: 12: 44.064475z"

URL = "http://svn.red-bean.com/tests/.greek-repo/a/d"

Kind = "DIR"

Revision = "1" />

COMMITTED-REV = "1"

Name = "gamma"

Text-time = "2002-09-26T21: 09: 02.000000Z"

Committed-date = "2002-09-24T17: 12: 44.064475z"

Checksum = "qse4vwd9zm0cmvr7 / ykxq =="

Kind = "file"

PROP-TIME = "2002-09-26T21: 09: 02.000000Z" />

Name = "zeta"

Kind = "file"

Schedule = "add"

Revision = "0" />

URL = "http://svn.red-bean.com/tests/.greek-repo/a/b/delta"

Name = "Delta"

Kind = "file" schedule = "add"

Revision = "0" />

Name = "g"

Kind = "DIR" />

Name = "h"

Kind = "DIR"

Schedule = "delete" />

As you can see, Entries file is essentially a list of projects. A project label describes 3 things: work version itself (Name property is set to "SVN: this-Dir"), indicating that this is a file in the working directory (Kind property setting bit file), or this is a The directory in the work version (Kind property is set to "DIR"). The file or sub-directory stored in this file can be under version control, or it is newly added in the working version. When the next submission version is modified, add a version control (on top In the example, the name of the file is ZATE is this case). Each project has a unique name, and each project has a node type.

The developer should pay attention to some special rules used by Subversion when reading and writing Entries file. Although each project has a Revision and URL, it is not that all Entry tags have a Recision and URL properties. When the value of these two attributes is the same as "SVN: THIS-DIR" in the project, the Subversion allows the two values ​​to be not explicitly specified when it is simply calculated by it. Note, the same, for sub-directory projects, the Subversion value stores a vital Name, Kind, Revision, and Schedule properties. In order to reduce the redundancy of information, the Subversion specifies that all information to obtain subdirectory, you can go deep into this subdirectory, and get the appropriate information in its own .svn / ertries file in its own. SVN / ERTRIES file. However, if you drink a subdirectory saved in the parent Entries file, when this subdirectory is not on the hard disk, it still has enough information to perform basic version control operations.

Original copy and attribute file

As mentioned earlier, the .svn directory also saves the original, version of the file for text. They can be found in. SVN / Text-Base. The advantage of the original copy is diverse - can be separated from the network to detect local modifications and "DIFF" reports, separated from the network recovery, or deleted files, only send modified content to the server - but the damage is every revision version It is necessary to store twice on the disk. Such problems can be ignored for most files.

Similar to "Text-Base" is attribute files and their original "prop-base" copies, they are located in .svn / props and .svn / prop-base files. Because the directory can also be attribute, there is therefore also existing .svn / dir-props and .svn / div-prop-base file. Each attribute file (working version or base version) uses a simple "Hash-on-Disk" file format to store the name of the property and their values.

WebDAV

WebDAV ("Web-Based Distributed Authoring and Versioning" is a standard HTTP protocol extension, the main design is to let the web can read and write, thereby changing the current state. The principle is that the files and directories can be shared with readable and writable via web. REC2518 and 3253 describe the extension of WebDAV / Deltev to HTTP. Details about it can be obtained in www.webdav.org. Many operating systems browser have been able to use the WebDAV to load the network directory. On Win32, IE can display a "WebFolders" like a normal shared folder. Mac OS X also has this function.

So how do these applied to the Subversion? Mod_dav_svn apache modules use WebDAV and DeltaV extended HTTP as its main network protocol. Subversion's initial designers decided to use the RFCS2518 and 3253 to implement the Subversion function, not to create a new private protocol.

Use memory pool programming

Almost every developer using the C design program is a headache for memory management. How to assign enough memory, track them, and release them when you don't need it, it is a very complicated thing. If the processing is not good, it may make the program crash, even worse, let the computer crashed. Fortunately, an APR_POOL_T data type is provided in the APR library used by Subversion, which provides a memory pool.

The memory pool is an abstract concept that is assigned to the program used. Instead, the operating system is directly assigned a single memory (using the MALLC () function), the program connected to the APR is not as simple as simply requiring a memory pool (using the APR_POOL_CREATE () function). The APR will assign a properly sized memory from the operating system, which can be used immediately. At any time, when the program needs the memory in the pool, just use one of the APR POOL API functions: APR_Palloc (), it will return a normal memory location in the pool. The program can continuously request a point or a memory, and the APR will always meet these requests. Because of the continuous application of the program, the size of the memory pool will gradually increase until the system does not have memory to be assigned.

If the above is the full characteristics of the memory pool, then there will be no more people interested, fortunately, things are not like this. Pool can not only be created, but also use APR_POOL_CLEAR () and Apr_Pool_Destory () to clear and destroy Pool. This gives the programmer a lot of freedom, you can create a few, even thousands of transactions in the memory pool, and then clear them all with a function! In the future, Pool will be hierarchically, you can create Subpool in a pool, when cleared or destroying Pool, SubPool will be cleared and destroyed at the same time.

Prior to the next step, developers should pay attention to the Pool function mentioned earlier, and may not be found in Subveresion. This is because ARP POOL provides some additional mechanisms, such as attaching custom "User Data" to pool, when you destroy Pool, call register clearance functions. Subversion sometimes needs to use these extensions. Therefore, Subversion provides package functions: svn_pool_vreate (), svn_pool_clear (), and svn_pool_destory.

Pool has a lot of use in basic memory management, and Pool's structure is very useful in cyclic and recursive. Because the number of cycles and the recursive levels are generally unpredictable. These situations can be easily managed using nested poores.

The following example is used to demonstrate, in a recursive, irregular directory tree, perform an authority method for all branches in the tree, using nested pools in this. Example: Effectively use pool

/ * Trade Directory Directory, add all file subdirectories to the files array, and perform some tasks for each directory. Use pool as all temporary storage and store Hash Paths in the same POOL. * /

Static Apr_Status_t

Crawl_dir (APR_ARRAY_HEADER_T * FILES, Const Char * Directory,

APR_POOL_T * POOL)

{

APR_POOL_T * HASH_POOL = files-> pool; / * array pool * /

APR_POOL_T * SUBPOOL = SVN_POOL_CREATE (POOL); / * Repeated POOL * /

APR_DIR_T * DIR;

APR_FINFO_T FINFO;

APR_STATUS_T APR_ERR;

APR_INT32_T FLAGS = APR_FINFO_TYPE | APR_FINFO_NAME;

APR_ERR = APR_DIR_OPEN (& DIR, DIRECTORY, POOL);

IF (APR_ERR)

Return Apr_ERR;

/ * Traversing the directory entity, empty Subpool * /

For (APR_ERR = APR_DIR_READ (& Finfo, Flags, DIR);

APR_ERR == APR_SUCCESS;

APR_ERR = APR_DIR_READ (& FINFO, FLAGS, DIR))

{

Const char * child_path;

/ * Skip catalog ('.') And parent directory ('..') * /

IF (FINFO.FILETYPE == APR_DIR)

{

IF (Finfo.name [0] == '.'

&& (FINFO.NAME [1] == '/ 0'

|| (Finfo.name [1] == '.' &&finfo.name [2] == '/ 0')))))))

CONTINUE;

}

/ * Build a child_path * / with Directory and Finfo.name

Child_path = svn_path_join (Directory, Finfo.name, Subpool);

/ * Execute some tasks for this directory * /

Do_some_task (child_path, subspool);

/ * Use the recursive processing subdirectory to pass the SubPool, used for temporary storage * /

IF (FINFO.FILETYPE == APR_DIR)

{

APR_ERR = CRAWL_DIR (files, child_path, subs);

IF (APR_ERR)

Return Apr_ERR;

}

/ * Add the directory of the file to the files array * /

Else if (Finfo.fileType == APR_REG)

{

/ * Copy the path of the file to the files array POOL * /

Child_path = APR_PSTRDUP (Hash_Pool, Child_Path);

/ * Add the path to the array * /

(* (const char **) APR_ARRAY_PUSH (FILES))))))))

}

/ * Clear each repeated SubPool. * /

SVN_POOL_CLEAR (SUBPOOL);

}

/ * Detection cycle normal exit * /

IF (APR_ERR)

Return Apr_ERR;

/ * Turn off DIR * /

APR_ERR = APR_DIR_CLOSE (DIR);

IF (APR_ERR)

Return Apr_ERR;

/ * Destroy Subpool. * /

SVN_POOL_DESTROY (SUBPOOL);

RETURN APR_SUCCESS;

}

The above example exemplifies the use of poores in both cyclic and recursive cases. Each recursive needs to pass the Pool's SubPool to a function. This subpool is used to cycle, and it is emptied at each cycle. Pool is very important for Subversion, as a developer of Subversion, should grow this ability.

转载请注明原文地址:https://www.9cbs.com/read-14075.html

New Post(0)