Initial commit of Seafile server core.
This commit is contained in:
commit
2643119433
352 changed files with 85573 additions and 0 deletions
129
.gitignore
vendored
Normal file
129
.gitignore
vendored
Normal file
|
@ -0,0 +1,129 @@
|
|||
*~
|
||||
*.bak
|
||||
*.o
|
||||
*.exe
|
||||
cscope*
|
||||
*#
|
||||
Makefile.in
|
||||
ltmain.sh
|
||||
libtool
|
||||
*.lo
|
||||
*.la
|
||||
install-sh
|
||||
depcomp
|
||||
config.guess
|
||||
config.h
|
||||
config.log
|
||||
config.status
|
||||
config.sub
|
||||
config.cache
|
||||
configure
|
||||
*/.deps
|
||||
autom4te*
|
||||
po/POTFILES
|
||||
po/Makefile*
|
||||
po/stamp-it
|
||||
po/*.gmo
|
||||
po/*.pot
|
||||
missing
|
||||
mkinstalldirs
|
||||
stamp-h1
|
||||
*.libs/
|
||||
Makefile
|
||||
aclocal.m4
|
||||
*core
|
||||
m4/intltool.m4
|
||||
m4/libtool.m4
|
||||
m4/ltoptions.m4
|
||||
m4/ltsugar.m4
|
||||
m4/ltversion.m4
|
||||
m4/lt~obsolete.m4
|
||||
ccnet-*.tar.gz
|
||||
config.h.in
|
||||
py-compile
|
||||
intltool-extract.in
|
||||
intltool-merge.in
|
||||
intltool-update.in
|
||||
*.stamp
|
||||
*.pyc
|
||||
*.tmp.ui
|
||||
*.defs
|
||||
*.log
|
||||
.deps
|
||||
*.db
|
||||
*.dll
|
||||
*.aps
|
||||
*.so
|
||||
build-stamp
|
||||
debian/files
|
||||
debian/seafile
|
||||
debian/*.substvars
|
||||
lib/searpc-marshal.h
|
||||
lib/searpc-signature.h
|
||||
lib/*.tmp
|
||||
lib/dir.c
|
||||
lib/dirent.c
|
||||
lib/seafile-object.h
|
||||
lib/task.c
|
||||
lib/webaccess.c
|
||||
lib/branch.c
|
||||
lib/commit.c
|
||||
lib/crypt.c
|
||||
lib/repo.c
|
||||
lib/copy-task.c
|
||||
app/seafile
|
||||
app/seafserv-tool
|
||||
daemon/seaf-daemon
|
||||
gui/gtk/seafile-applet
|
||||
server/seaf-server
|
||||
server/gc/seafserv-gc
|
||||
monitor/seaf-mon
|
||||
controller/seafile-controller
|
||||
fileserver/fileserver
|
||||
tests/common-conf.sh
|
||||
tools/seaf-server-init
|
||||
*.mo
|
||||
seafile-web
|
||||
tests/basic/conf1/c882e263e9d02c63ca6b61c68508761cbc74c358.peer
|
||||
tests/basic/conf1/c882e263e9d02c63ca6b61c68508761cbc74c358.user
|
||||
tests/basic/conf1/group-db/
|
||||
tests/basic/conf1/peer-db/
|
||||
tests/basic/conf1/user-db/
|
||||
tests/basic/conf2/376cf9b6ef33a6839cf1fc096131893b5ecc673f.peer
|
||||
tests/basic/conf2/376cf9b6ef33a6839cf1fc096131893b5ecc673f.user
|
||||
tests/basic/conf2/group-db/
|
||||
tests/basic/conf2/peer-db/
|
||||
tests/basic/conf2/user-db/
|
||||
tests/basic/conf3/1e5b5e0f49010b94aa6c2995a6e7b7cba462d388.peer
|
||||
tests/basic/conf3/1e5b5e0f49010b94aa6c2995a6e7b7cba462d388.user
|
||||
tests/basic/conf3/group-db/
|
||||
tests/basic/conf3/peer-db/
|
||||
tests/basic/conf3/user-db/
|
||||
tests/basic/conf4/93ae3e01eea6667cbdd03c4afde413ccd9f1eb43.peer
|
||||
tests/basic/conf4/93ae3e01eea6667cbdd03c4afde413ccd9f1eb43.user
|
||||
tests/basic/conf4/peer-db/
|
||||
tests/basic/conf4/user-db/
|
||||
web/local_settings.py
|
||||
gui/win/applet-po-gbk.h
|
||||
*.dylib
|
||||
.DS_Store
|
||||
gui/mac/seafile/build/
|
||||
gui/mac/seafile/ccnet
|
||||
gui/mac/seafile/seaf-daemon
|
||||
gui/mac/seafile/seafileweb.app
|
||||
web/build/
|
||||
web/dist/
|
||||
tests/basic/conf2/misc/
|
||||
tests/basic/conf2/seafile-data/
|
||||
tests/test-cdc
|
||||
tests/test-index
|
||||
tests/test-seafile-fmt
|
||||
*.pc
|
||||
seaf-fsck
|
||||
*.tar.gz
|
||||
fuse/seaf-fuse
|
||||
server/gc/seaf-migrate
|
||||
/compile
|
||||
/test-driver
|
||||
*.dmp
|
||||
/symbols
|
32
.travis.yml
Normal file
32
.travis.yml
Normal file
|
@ -0,0 +1,32 @@
|
|||
sudo: false
|
||||
# Must set language to python so we can install custom python pacakges in
|
||||
# docker-based travis builds
|
||||
language: python
|
||||
compiler:
|
||||
- gcc
|
||||
- clang
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- valac
|
||||
- uuid-dev
|
||||
- libevent-dev
|
||||
- libarchive-dev
|
||||
- intltool
|
||||
- re2c
|
||||
- libjansson-dev
|
||||
- libonig-dev
|
||||
- libfuse-dev
|
||||
- net-tools
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.cache/pip
|
||||
- $HOME/.ccache
|
||||
- $HOME/downloads
|
||||
before_install:
|
||||
- ccache -s
|
||||
- export PATH=/usr/lib/ccache:${PATH}
|
||||
install:
|
||||
- ./integration-tests/install-deps.sh
|
||||
script:
|
||||
- ./integration-tests/run.py
|
246
LICENSE.txt
Normal file
246
LICENSE.txt
Normal file
|
@ -0,0 +1,246 @@
|
|||
This program is released under Affero GPLv3, with the following additional
|
||||
permission to link with OpenSSL library.
|
||||
|
||||
If you modify this program, or any covered work, by linking or
|
||||
combining it with the OpenSSL project's OpenSSL library (or a
|
||||
modified version of that library), containing parts covered by the
|
||||
terms of the OpenSSL or SSLeay licenses, Seafile Ltd.
|
||||
grants you additional permission to convey the resulting work.
|
||||
Corresponding Source for a non-source form of such a combination
|
||||
shall include the source code for the parts of OpenSSL used as well
|
||||
as that of the covered work.
|
||||
|
||||
The source code files under 'python' directory is released under
|
||||
Apache License v2.0. You can find Apache License 2.0 file in that
|
||||
directory.
|
||||
|
||||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
|
||||
Version 3, 19 November 2007
|
||||
|
||||
Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU Affero General Public License is a free, copyleft license for software and other kinds of works, specifically designed to ensure cooperation with the community in the case of network server software.
|
||||
|
||||
The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.
|
||||
|
||||
Developers that use our General Public Licenses protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License which gives you legal permission to copy, distribute and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that improvements made in alternate versions of the program, if they receive widespread use, become available for other developers to incorporate. Many developers of free software are heartened and encouraged by the resulting cooperation. However, in the case of software used on network servers, this result may fail to come about. The GNU General Public License permits making a modified version and letting the public access it on a server without ever releasing its source code to the public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to ensure that, in such cases, the modified source code becomes available to the community. It requires the operator of a network server to provide the source code of the modified version running there to the users of that server. Therefore, public use of a modified version, on a publicly accessible server, gives the public access to the source code of the modified version.
|
||||
|
||||
An older license, called the Affero General Public License and published by Affero, was designed to accomplish similar goals. This is a different license, not a version of the Affero GPL, but Affero has released a new version of the Affero GPL which permits relicensing under this license.
|
||||
|
||||
The precise terms and conditions for copying, distribution and modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified it, and giving a relevant date.
|
||||
b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices".
|
||||
c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it.
|
||||
d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so.
|
||||
A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange.
|
||||
b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge.
|
||||
c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b.
|
||||
d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements.
|
||||
e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d.
|
||||
A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or
|
||||
b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or
|
||||
c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or
|
||||
d) Limiting the use for publicity purposes of names of licensors or authors of the material; or
|
||||
e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or
|
||||
f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors.
|
||||
All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the Program, your modified version must prominently offer all users interacting with it remotely through a computer network (if your version supports such interaction) an opportunity to receive the Corresponding Source of your version by providing access to the Corresponding Source from a network server at no charge, through some standard or customary means of facilitating copying of software. This Corresponding Source shall include the Corresponding Source for any work covered by version 3 of the GNU General Public License that is incorporated pursuant to the following paragraph.
|
||||
|
||||
Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the work with which it is combined will remain governed by version 3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of the GNU Affero General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU Affero General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU Affero General Public License, you may choose any version ever published by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future versions of the GNU Affero General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If your software can interact with users remotely through a computer network, you should also make sure that it provides a way for users to get its source. For example, if your program is a web application, its interface could display a "Source" link that leads users to an archive of the code. There are many ways you could offer source, and different solutions will be better for different programs; see section 13 for the specific requirements.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU AGPL, see <http://www.gnu.org/licenses/>.
|
31
Makefile.am
Normal file
31
Makefile.am
Normal file
|
@ -0,0 +1,31 @@
|
|||
MAKE_CLIENT =
|
||||
|
||||
if WIN32
|
||||
MAKE_CONTROLLER =
|
||||
else
|
||||
MAKE_CONTROLLER = controller
|
||||
endif
|
||||
|
||||
if COMPILE_FUSE
|
||||
MAKE_FUSE = fuse
|
||||
else
|
||||
MAKE_FUSE =
|
||||
endif
|
||||
|
||||
MAKE_SERVER = server tools $(MAKE_CONTROLLER) $(MAKE_FUSE)
|
||||
|
||||
SUBDIRS = include lib common python $(MAKE_SERVER)
|
||||
|
||||
DIST_SUBDIRS = include lib common python server tools controller fuse
|
||||
|
||||
INTLTOOL = \
|
||||
intltool-extract.in \
|
||||
intltool-merge.in \
|
||||
intltool-update.in
|
||||
|
||||
EXTRA_DIST = install-sh $(INTLTOOL) README.markdown scripts LICENSE.txt
|
||||
|
||||
ACLOCAL_AMFLAGS = -I m4
|
||||
|
||||
dist-hook:
|
||||
git log --format='%H' -1 > $(distdir)/latest_commit
|
28
README.markdown
Normal file
28
README.markdown
Normal file
|
@ -0,0 +1,28 @@
|
|||
Seafile Server Core
|
||||
============
|
||||
|
||||
Seafile is an open source cloud storage system with features on privacy protection and teamwork. Collections of files are called libraries, and each library can be synced separately. A library can also be encrypted with a user chosen password. Seafile also allows users to create groups and easily sharing files into groups.
|
||||
|
||||
This is the core component of Seafile server. It provides RPC to the web front-end (Seahub) to access files, and provides HTTP APIs to the desktop clients for syncing files.
|
||||
|
||||
Build and Run
|
||||
=============
|
||||
|
||||
See <http://manual.seafile.com/build_seafile/server.html>
|
||||
|
||||
Contributing
|
||||
===========
|
||||
|
||||
For more informations read [Contribution](http://manual.seafile.com/contribution.html).
|
||||
|
||||
License
|
||||
=======
|
||||
|
||||
The Seafile server core is published under AGPLv3. Other components of Seafile have different licenses. Please refer to the coresponding projects.
|
||||
|
||||
Contact
|
||||
=======
|
||||
|
||||
Twitter: @seafile <https://twitter.com/seafile>
|
||||
|
||||
Forum: <https://forum.seafile.com>
|
129
autogen.sh
Executable file
129
autogen.sh
Executable file
|
@ -0,0 +1,129 @@
|
|||
#!/bin/bash
|
||||
# Run this to generate all the initial makefiles, etc.
|
||||
|
||||
: ${AUTOCONF=autoconf}
|
||||
: ${AUTOHEADER=autoheader}
|
||||
: ${AUTOMAKE=automake}
|
||||
: ${ACLOCAL=aclocal}
|
||||
if test "$(uname)" != "Darwin"; then
|
||||
: ${LIBTOOLIZE=libtoolize}
|
||||
else
|
||||
: ${LIBTOOLIZE=glibtoolize}
|
||||
fi
|
||||
: ${INTLTOOLIZE=intltoolize}
|
||||
: ${LIBTOOL=libtool}
|
||||
|
||||
srcdir=`dirname $0`
|
||||
test -z "$srcdir" && srcdir=.
|
||||
|
||||
ORIGDIR=`pwd`
|
||||
cd $srcdir
|
||||
PROJECT=ccnet
|
||||
TEST_TYPE=-f
|
||||
FILE=net/main.c
|
||||
CONFIGURE=configure.ac
|
||||
|
||||
DIE=0
|
||||
|
||||
($AUTOCONF --version) < /dev/null > /dev/null 2>&1 || {
|
||||
echo
|
||||
echo "You must have autoconf installed to compile $PROJECT."
|
||||
echo "Download the appropriate package for your distribution,"
|
||||
echo "or get the source tarball at ftp://ftp.gnu.org/pub/gnu/"
|
||||
DIE=1
|
||||
}
|
||||
|
||||
(grep "^AC_PROG_INTLTOOL" $srcdir/$CONFIGURE >/dev/null) && {
|
||||
($INTLTOOLIZE --version) < /dev/null > /dev/null 2>&1 || {
|
||||
echo
|
||||
echo "You must have \`intltoolize' installed to compile $PROJECT."
|
||||
echo "Get ftp://ftp.gnome.org/pub/GNOME/stable/sources/intltool/intltool-0.22.tar.gz"
|
||||
echo "(or a newer version if it is available)"
|
||||
DIE=1
|
||||
}
|
||||
}
|
||||
|
||||
($AUTOMAKE --version) < /dev/null > /dev/null 2>&1 || {
|
||||
echo
|
||||
echo "You must have automake installed to compile $PROJECT."
|
||||
echo "Get ftp://sourceware.cygnus.com/pub/automake/automake-1.7.tar.gz"
|
||||
echo "(or a newer version if it is available)"
|
||||
DIE=1
|
||||
}
|
||||
|
||||
if test "$(uname)" != "Darwin"; then
|
||||
(grep "^AC_PROG_LIBTOOL" $CONFIGURE >/dev/null) && {
|
||||
($LIBTOOL --version) < /dev/null > /dev/null 2>&1 || {
|
||||
echo
|
||||
echo "**Error**: You must have \`libtool' installed to compile $PROJECT."
|
||||
echo "Get ftp://ftp.gnu.org/pub/gnu/libtool-1.4.tar.gz"
|
||||
echo "(or a newer version if it is available)"
|
||||
DIE=1
|
||||
}
|
||||
}
|
||||
fi
|
||||
|
||||
|
||||
if grep "^AM_[A-Z0-9_]\{1,\}_GETTEXT" "$CONFIGURE" >/dev/null; then
|
||||
if grep "sed.*POTFILES" "$CONFIGURE" >/dev/null; then
|
||||
GETTEXTIZE=""
|
||||
else
|
||||
if grep "^AM_GLIB_GNU_GETTEXT" "$CONFIGURE" >/dev/null; then
|
||||
GETTEXTIZE="glib-gettextize"
|
||||
GETTEXTIZE_URL="ftp://ftp.gtk.org/pub/gtk/v2.0/glib-2.0.0.tar.gz"
|
||||
else
|
||||
GETTEXTIZE="gettextize"
|
||||
GETTEXTIZE_URL="ftp://alpha.gnu.org/gnu/gettext-0.10.35.tar.gz"
|
||||
fi
|
||||
|
||||
$GETTEXTIZE --version < /dev/null > /dev/null 2>&1
|
||||
if test $? -ne 0; then
|
||||
echo
|
||||
echo "**Error**: You must have \`$GETTEXTIZE' installed to compile $PKG_NAME."
|
||||
echo "Get $GETTEXTIZE_URL"
|
||||
echo "(or a newer version if it is available)"
|
||||
DIE=1
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if test "$DIE" -eq 1; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
dr=`dirname .`
|
||||
echo processing $dr
|
||||
aclocalinclude="$aclocalinclude -I m4"
|
||||
|
||||
if test x"$MSYSTEM" = x"MINGW32"; then
|
||||
aclocalinclude="$aclocalinclude -I /mingw32/share/aclocal"
|
||||
elif test "$(uname)" = "Darwin"; then
|
||||
aclocalinclude="$aclocalinclude -I /opt/local/share/aclocal"
|
||||
fi
|
||||
|
||||
|
||||
echo "Creating $dr/aclocal.m4 ..."
|
||||
test -r $dr/aclocal.m4 || touch $dr/aclocal.m4
|
||||
echo "Running glib-gettextize... Ignore non-fatal messages."
|
||||
echo "no" | glib-gettextize --force --copy
|
||||
|
||||
echo "Making $dr/aclocal.m4 writable ..."
|
||||
test -r $dr/aclocal.m4 && chmod u+w $dr/aclocal.m4
|
||||
|
||||
echo "Running intltoolize..."
|
||||
intltoolize --copy --force --automake
|
||||
|
||||
echo "Running $LIBTOOLIZE..."
|
||||
$LIBTOOLIZE --force --copy
|
||||
|
||||
echo "Running $ACLOCAL $aclocalinclude ..."
|
||||
$ACLOCAL $aclocalinclude
|
||||
|
||||
echo "Running $AUTOHEADER..."
|
||||
$AUTOHEADER
|
||||
|
||||
echo "Running $AUTOMAKE --gnu $am_opt ..."
|
||||
$AUTOMAKE --add-missing --gnu $am_opt
|
||||
|
||||
echo "Running $AUTOCONF ..."
|
||||
$AUTOCONF
|
27
common/Makefile.am
Normal file
27
common/Makefile.am
Normal file
|
@ -0,0 +1,27 @@
|
|||
SUBDIRS = cdc db-wrapper
|
||||
|
||||
proc_headers =
|
||||
$(addprefix processors/, \
|
||||
objecttx-common.h)
|
||||
|
||||
noinst_HEADERS = \
|
||||
diff-simple.h \
|
||||
seafile-crypt.h \
|
||||
common.h \
|
||||
branch-mgr.h \
|
||||
fs-mgr.h \
|
||||
block-mgr.h \
|
||||
commit-mgr.h \
|
||||
log.h \
|
||||
object-list.h \
|
||||
vc-common.h \
|
||||
seaf-utils.h \
|
||||
obj-store.h \
|
||||
obj-backend.h \
|
||||
block-backend.h \
|
||||
block.h \
|
||||
mq-mgr.h \
|
||||
seaf-db.h \
|
||||
merge-new.h \
|
||||
block-tx-utils.h \
|
||||
$(proc_headers)
|
505
common/block-backend-fs.c
Normal file
505
common/block-backend-fs.c
Normal file
|
@ -0,0 +1,505 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef _WIN32_WINNT
|
||||
#define _WIN32_WINNT 0x500
|
||||
#endif
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include "block-backend.h"
|
||||
#include "obj-store.h"
|
||||
|
||||
|
||||
struct _BHandle {
|
||||
char *store_id;
|
||||
int version;
|
||||
char block_id[41];
|
||||
int fd;
|
||||
int rw_type;
|
||||
char *tmp_file;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
char *v0_block_dir;
|
||||
int v0_block_dir_len;
|
||||
char *block_dir;
|
||||
int block_dir_len;
|
||||
char *tmp_dir;
|
||||
int tmp_dir_len;
|
||||
} FsPriv;
|
||||
|
||||
static char *
|
||||
get_block_path (BlockBackend *bend,
|
||||
const char *block_sha1,
|
||||
char path[],
|
||||
const char *store_id,
|
||||
int version);
|
||||
|
||||
static int
|
||||
open_tmp_file (BlockBackend *bend,
|
||||
const char *basename,
|
||||
char **path);
|
||||
|
||||
static BHandle *
|
||||
block_backend_fs_open_block (BlockBackend *bend,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id,
|
||||
int rw_type)
|
||||
{
|
||||
BHandle *handle;
|
||||
int fd = -1;
|
||||
char *tmp_file;
|
||||
|
||||
g_return_val_if_fail (block_id != NULL, NULL);
|
||||
g_return_val_if_fail (strlen(block_id) == 40, NULL);
|
||||
g_return_val_if_fail (rw_type == BLOCK_READ || rw_type == BLOCK_WRITE, NULL);
|
||||
|
||||
if (rw_type == BLOCK_READ) {
|
||||
char path[SEAF_PATH_MAX];
|
||||
get_block_path (bend, block_id, path, store_id, version);
|
||||
fd = g_open (path, O_RDONLY | O_BINARY, 0);
|
||||
if (fd < 0) {
|
||||
ccnet_warning ("[block bend] failed to open block %s for read: %s\n",
|
||||
block_id, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
fd = open_tmp_file (bend, block_id, &tmp_file);
|
||||
if (fd < 0) {
|
||||
ccnet_warning ("[block bend] failed to open block %s for write: %s\n",
|
||||
block_id, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
handle = g_new0(BHandle, 1);
|
||||
handle->fd = fd;
|
||||
memcpy (handle->block_id, block_id, 41);
|
||||
handle->rw_type = rw_type;
|
||||
if (rw_type == BLOCK_WRITE)
|
||||
handle->tmp_file = tmp_file;
|
||||
if (store_id)
|
||||
handle->store_id = g_strdup(store_id);
|
||||
handle->version = version;
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
static int
|
||||
block_backend_fs_read_block (BlockBackend *bend,
|
||||
BHandle *handle,
|
||||
void *buf, int len)
|
||||
{
|
||||
return (readn (handle->fd, buf, len));
|
||||
}
|
||||
|
||||
static int
|
||||
block_backend_fs_write_block (BlockBackend *bend,
|
||||
BHandle *handle,
|
||||
const void *buf, int len)
|
||||
{
|
||||
return (writen (handle->fd, buf, len));
|
||||
}
|
||||
|
||||
static int
|
||||
block_backend_fs_close_block (BlockBackend *bend,
|
||||
BHandle *handle)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = close (handle->fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
block_backend_fs_block_handle_free (BlockBackend *bend,
|
||||
BHandle *handle)
|
||||
{
|
||||
if (handle->rw_type == BLOCK_WRITE) {
|
||||
/* make sure the tmp file is removed even on failure. */
|
||||
g_unlink (handle->tmp_file);
|
||||
g_free (handle->tmp_file);
|
||||
}
|
||||
g_free (handle->store_id);
|
||||
g_free (handle);
|
||||
}
|
||||
|
||||
static int
|
||||
create_parent_path (const char *path)
|
||||
{
|
||||
char *dir = g_path_get_dirname (path);
|
||||
if (!dir)
|
||||
return -1;
|
||||
|
||||
if (g_file_test (dir, G_FILE_TEST_EXISTS)) {
|
||||
g_free (dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (g_mkdir_with_parents (dir, 0777) < 0) {
|
||||
seaf_warning ("Failed to create object parent path: %s.\n", dir);
|
||||
g_free (dir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_free (dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
block_backend_fs_commit_block (BlockBackend *bend,
|
||||
BHandle *handle)
|
||||
{
|
||||
char path[SEAF_PATH_MAX];
|
||||
|
||||
g_return_val_if_fail (handle->rw_type == BLOCK_WRITE, -1);
|
||||
|
||||
get_block_path (bend, handle->block_id, path, handle->store_id, handle->version);
|
||||
|
||||
if (create_parent_path (path) < 0) {
|
||||
seaf_warning ("Failed to create path for block %s:%s.\n",
|
||||
handle->store_id, handle->block_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (g_rename (handle->tmp_file, path) < 0) {
|
||||
seaf_warning ("[block bend] failed to commit block %s:%s: %s\n",
|
||||
handle->store_id, handle->block_id, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
block_backend_fs_block_exists (BlockBackend *bend,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_sha1)
|
||||
{
|
||||
char block_path[SEAF_PATH_MAX];
|
||||
|
||||
get_block_path (bend, block_sha1, block_path, store_id, version);
|
||||
if (g_access (block_path, F_OK) == 0)
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static int
|
||||
block_backend_fs_remove_block (BlockBackend *bend,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id)
|
||||
{
|
||||
char path[SEAF_PATH_MAX];
|
||||
|
||||
get_block_path (bend, block_id, path, store_id, version);
|
||||
|
||||
return g_unlink (path);
|
||||
}
|
||||
|
||||
static BMetadata *
|
||||
block_backend_fs_stat_block (BlockBackend *bend,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id)
|
||||
{
|
||||
char path[SEAF_PATH_MAX];
|
||||
SeafStat st;
|
||||
BMetadata *block_md;
|
||||
|
||||
get_block_path (bend, block_id, path, store_id, version);
|
||||
if (seaf_stat (path, &st) < 0) {
|
||||
seaf_warning ("[block bend] Failed to stat block %s:%s at %s: %s.\n",
|
||||
store_id, block_id, path, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
block_md = g_new0(BMetadata, 1);
|
||||
memcpy (block_md->id, block_id, 40);
|
||||
block_md->size = (uint32_t) st.st_size;
|
||||
|
||||
return block_md;
|
||||
}
|
||||
|
||||
static BMetadata *
|
||||
block_backend_fs_stat_block_by_handle (BlockBackend *bend,
|
||||
BHandle *handle)
|
||||
{
|
||||
SeafStat st;
|
||||
BMetadata *block_md;
|
||||
|
||||
if (seaf_fstat (handle->fd, &st) < 0) {
|
||||
seaf_warning ("[block bend] Failed to stat block %s:%s.\n",
|
||||
handle->store_id, handle->block_id);
|
||||
return NULL;
|
||||
}
|
||||
block_md = g_new0(BMetadata, 1);
|
||||
memcpy (block_md->id, handle->block_id, 40);
|
||||
block_md->size = (uint32_t) st.st_size;
|
||||
|
||||
return block_md;
|
||||
}
|
||||
|
||||
static int
|
||||
block_backend_fs_foreach_block (BlockBackend *bend,
|
||||
const char *store_id,
|
||||
int version,
|
||||
SeafBlockFunc process,
|
||||
void *user_data)
|
||||
{
|
||||
FsPriv *priv = bend->be_priv;
|
||||
char *block_dir = NULL;
|
||||
int dir_len;
|
||||
GDir *dir1 = NULL, *dir2;
|
||||
const char *dname1, *dname2;
|
||||
char block_id[128];
|
||||
char path[SEAF_PATH_MAX], *pos;
|
||||
int ret = 0;
|
||||
|
||||
#if defined MIGRATION
|
||||
if (version > 0)
|
||||
block_dir = g_build_filename (priv->block_dir, store_id, NULL);
|
||||
else
|
||||
block_dir = g_strdup(priv->v0_block_dir);
|
||||
#else
|
||||
block_dir = g_build_filename (priv->block_dir, store_id, NULL);
|
||||
#endif
|
||||
dir_len = strlen (block_dir);
|
||||
|
||||
dir1 = g_dir_open (block_dir, 0, NULL);
|
||||
if (!dir1) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
memcpy (path, block_dir, dir_len);
|
||||
pos = path + dir_len;
|
||||
|
||||
while ((dname1 = g_dir_read_name(dir1)) != NULL) {
|
||||
snprintf (pos, sizeof(path) - dir_len, "/%s", dname1);
|
||||
|
||||
dir2 = g_dir_open (path, 0, NULL);
|
||||
if (!dir2) {
|
||||
seaf_warning ("Failed to open block dir %s.\n", path);
|
||||
continue;
|
||||
}
|
||||
|
||||
while ((dname2 = g_dir_read_name(dir2)) != NULL) {
|
||||
snprintf (block_id, sizeof(block_id), "%s%s", dname1, dname2);
|
||||
if (!process (store_id, version, block_id, user_data)) {
|
||||
g_dir_close (dir2);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
g_dir_close (dir2);
|
||||
}
|
||||
|
||||
out:
|
||||
if (dir1)
|
||||
g_dir_close (dir1);
|
||||
g_free (block_dir);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
block_backend_fs_copy (BlockBackend *bend,
|
||||
const char *src_store_id,
|
||||
int src_version,
|
||||
const char *dst_store_id,
|
||||
int dst_version,
|
||||
const char *block_id)
|
||||
{
|
||||
char src_path[SEAF_PATH_MAX];
|
||||
char dst_path[SEAF_PATH_MAX];
|
||||
|
||||
get_block_path (bend, block_id, src_path, src_store_id, src_version);
|
||||
get_block_path (bend, block_id, dst_path, dst_store_id, dst_version);
|
||||
|
||||
if (g_file_test (dst_path, G_FILE_TEST_EXISTS))
|
||||
return 0;
|
||||
|
||||
if (create_parent_path (dst_path) < 0) {
|
||||
seaf_warning ("Failed to create dst path %s for block %s.\n",
|
||||
dst_path, block_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
if (!CreateHardLink (dst_path, src_path, NULL)) {
|
||||
seaf_warning ("Failed to link %s to %s: %lu.\n",
|
||||
src_path, dst_path, GetLastError());
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
int ret = link (src_path, dst_path);
|
||||
if (ret < 0 && errno != EEXIST) {
|
||||
seaf_warning ("Failed to link %s to %s: %s.\n",
|
||||
src_path, dst_path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int
|
||||
block_backend_fs_remove_store (BlockBackend *bend, const char *store_id)
|
||||
{
|
||||
FsPriv *priv = bend->be_priv;
|
||||
char *block_dir = NULL;
|
||||
GDir *dir1, *dir2;
|
||||
const char *dname1, *dname2;
|
||||
char *path1, *path2;
|
||||
|
||||
block_dir = g_build_filename (priv->block_dir, store_id, NULL);
|
||||
|
||||
dir1 = g_dir_open (block_dir, 0, NULL);
|
||||
if (!dir1) {
|
||||
g_free (block_dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((dname1 = g_dir_read_name(dir1)) != NULL) {
|
||||
path1 = g_build_filename (block_dir, dname1, NULL);
|
||||
|
||||
dir2 = g_dir_open (path1, 0, NULL);
|
||||
if (!dir2) {
|
||||
seaf_warning ("Failed to open block dir %s.\n", path1);
|
||||
g_dir_close (dir1);
|
||||
g_free (path1);
|
||||
g_free (block_dir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ((dname2 = g_dir_read_name(dir2)) != NULL) {
|
||||
path2 = g_build_filename (path1, dname2, NULL);
|
||||
g_unlink (path2);
|
||||
g_free (path2);
|
||||
}
|
||||
g_dir_close (dir2);
|
||||
|
||||
g_rmdir (path1);
|
||||
g_free (path1);
|
||||
}
|
||||
|
||||
g_dir_close (dir1);
|
||||
g_rmdir (block_dir);
|
||||
g_free (block_dir);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char *
|
||||
get_block_path (BlockBackend *bend,
|
||||
const char *block_sha1,
|
||||
char path[],
|
||||
const char *store_id,
|
||||
int version)
|
||||
{
|
||||
FsPriv *priv = bend->be_priv;
|
||||
char *pos = path;
|
||||
int n;
|
||||
|
||||
#if defined MIGRATION
|
||||
if (version > 0) {
|
||||
n = snprintf (path, SEAF_PATH_MAX, "%s/%s/", priv->block_dir, store_id);
|
||||
pos += n;
|
||||
} else {
|
||||
memcpy (pos, priv->v0_block_dir, priv->v0_block_dir_len);
|
||||
pos[priv->v0_block_dir_len] = '/';
|
||||
pos += priv->v0_block_dir_len + 1;
|
||||
}
|
||||
#else
|
||||
n = snprintf (path, SEAF_PATH_MAX, "%s/%s/", priv->block_dir, store_id);
|
||||
pos += n;
|
||||
#endif
|
||||
|
||||
memcpy (pos, block_sha1, 2);
|
||||
pos[2] = '/';
|
||||
pos += 3;
|
||||
|
||||
memcpy (pos, block_sha1 + 2, 41 - 2);
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
static int
|
||||
open_tmp_file (BlockBackend *bend,
|
||||
const char *basename,
|
||||
char **path)
|
||||
{
|
||||
FsPriv *priv = bend->be_priv;
|
||||
int fd;
|
||||
|
||||
*path = g_strdup_printf ("%s/%s.XXXXXX", priv->tmp_dir, basename);
|
||||
fd = g_mkstemp (*path);
|
||||
if (fd < 0)
|
||||
g_free (*path);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
BlockBackend *
|
||||
block_backend_fs_new (const char *seaf_dir, const char *tmp_dir)
|
||||
{
|
||||
BlockBackend *bend;
|
||||
FsPriv *priv;
|
||||
|
||||
bend = g_new0(BlockBackend, 1);
|
||||
priv = g_new0(FsPriv, 1);
|
||||
bend->be_priv = priv;
|
||||
|
||||
priv->v0_block_dir = g_build_filename (seaf_dir, "blocks", NULL);
|
||||
priv->v0_block_dir_len = strlen(priv->v0_block_dir);
|
||||
|
||||
priv->block_dir = g_build_filename (seaf_dir, "storage", "blocks", NULL);
|
||||
priv->block_dir_len = strlen (priv->block_dir);
|
||||
|
||||
priv->tmp_dir = g_strdup (tmp_dir);
|
||||
priv->tmp_dir_len = strlen (tmp_dir);
|
||||
|
||||
if (g_mkdir_with_parents (priv->block_dir, 0777) < 0) {
|
||||
seaf_warning ("Block dir %s does not exist and"
|
||||
" is unable to create\n", priv->block_dir);
|
||||
goto onerror;
|
||||
}
|
||||
|
||||
if (g_mkdir_with_parents (tmp_dir, 0777) < 0) {
|
||||
seaf_warning ("Blocks tmp dir %s does not exist and"
|
||||
" is unable to create\n", tmp_dir);
|
||||
goto onerror;
|
||||
}
|
||||
|
||||
bend->open_block = block_backend_fs_open_block;
|
||||
bend->read_block = block_backend_fs_read_block;
|
||||
bend->write_block = block_backend_fs_write_block;
|
||||
bend->commit_block = block_backend_fs_commit_block;
|
||||
bend->close_block = block_backend_fs_close_block;
|
||||
bend->exists = block_backend_fs_block_exists;
|
||||
bend->remove_block = block_backend_fs_remove_block;
|
||||
bend->stat_block = block_backend_fs_stat_block;
|
||||
bend->stat_block_by_handle = block_backend_fs_stat_block_by_handle;
|
||||
bend->block_handle_free = block_backend_fs_block_handle_free;
|
||||
bend->foreach_block = block_backend_fs_foreach_block;
|
||||
bend->remove_store = block_backend_fs_remove_store;
|
||||
bend->copy = block_backend_fs_copy;
|
||||
|
||||
return bend;
|
||||
|
||||
onerror:
|
||||
g_free (bend);
|
||||
g_free (bend->be_priv);
|
||||
|
||||
return NULL;
|
||||
}
|
56
common/block-backend.c
Normal file
56
common/block-backend.c
Normal file
|
@ -0,0 +1,56 @@
|
|||
|
||||
#include "common.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include "block-backend.h"
|
||||
|
||||
extern BlockBackend *
|
||||
block_backend_fs_new (const char *block_dir, const char *tmp_dir);
|
||||
|
||||
BlockBackend*
|
||||
load_filesystem_block_backend(GKeyFile *config)
|
||||
{
|
||||
BlockBackend *bend;
|
||||
char *tmp_dir;
|
||||
char *block_dir;
|
||||
|
||||
block_dir = g_key_file_get_string (config, "block_backend", "block_dir", NULL);
|
||||
if (!block_dir) {
|
||||
seaf_warning ("Block dir not set in config.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tmp_dir = g_key_file_get_string (config, "block_backend", "tmp_dir", NULL);
|
||||
if (!tmp_dir) {
|
||||
seaf_warning ("Block tmp dir not set in config.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bend = block_backend_fs_new (block_dir, tmp_dir);
|
||||
|
||||
g_free (block_dir);
|
||||
g_free (tmp_dir);
|
||||
return bend;
|
||||
}
|
||||
|
||||
BlockBackend*
|
||||
load_block_backend (GKeyFile *config)
|
||||
{
|
||||
char *backend;
|
||||
BlockBackend *bend;
|
||||
|
||||
backend = g_key_file_get_string (config, "block_backend", "name", NULL);
|
||||
if (!backend) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (strcmp(backend, "filesystem") == 0) {
|
||||
bend = load_filesystem_block_backend(config);
|
||||
g_free (backend);
|
||||
return bend;
|
||||
}
|
||||
|
||||
seaf_warning ("Unknown backend\n");
|
||||
return NULL;
|
||||
}
|
64
common/block-backend.h
Normal file
64
common/block-backend.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef BLOCK_BACKEND_H
|
||||
#define BLOCK_BACKEND_H
|
||||
|
||||
#include "block.h"
|
||||
|
||||
typedef struct BlockBackend BlockBackend;
|
||||
|
||||
struct BlockBackend {
|
||||
|
||||
BHandle* (*open_block) (BlockBackend *bend,
|
||||
const char *store_id, int version,
|
||||
const char *block_id, int rw_type);
|
||||
|
||||
int (*read_block) (BlockBackend *bend, BHandle *handle, void *buf, int len);
|
||||
|
||||
int (*write_block) (BlockBackend *bend, BHandle *handle, const void *buf, int len);
|
||||
|
||||
int (*commit_block) (BlockBackend *bend, BHandle *handle);
|
||||
|
||||
int (*close_block) (BlockBackend *bend, BHandle *handle);
|
||||
|
||||
int (*exists) (BlockBackend *bend,
|
||||
const char *store_id, int version,
|
||||
const char *block_id);
|
||||
|
||||
int (*remove_block) (BlockBackend *bend,
|
||||
const char *store_id, int version,
|
||||
const char *block_id);
|
||||
|
||||
BMetadata* (*stat_block) (BlockBackend *bend,
|
||||
const char *store_id, int version,
|
||||
const char *block_id);
|
||||
|
||||
BMetadata* (*stat_block_by_handle) (BlockBackend *bend, BHandle *handle);
|
||||
|
||||
void (*block_handle_free) (BlockBackend *bend, BHandle *handle);
|
||||
|
||||
int (*foreach_block) (BlockBackend *bend,
|
||||
const char *store_id,
|
||||
int version,
|
||||
SeafBlockFunc process,
|
||||
void *user_data);
|
||||
|
||||
int (*copy) (BlockBackend *bend,
|
||||
const char *src_store_id,
|
||||
int src_version,
|
||||
const char *dst_store_id,
|
||||
int dst_version,
|
||||
const char *block_id);
|
||||
|
||||
/* Only valid for version 1 repo. Remove all blocks for the repo. */
|
||||
int (*remove_store) (BlockBackend *bend,
|
||||
const char *store_id);
|
||||
|
||||
void* be_priv; /* backend private field */
|
||||
|
||||
};
|
||||
|
||||
|
||||
BlockBackend* load_block_backend (GKeyFile *config);
|
||||
|
||||
#endif
|
268
common/block-mgr.c
Normal file
268
common/block-mgr.c
Normal file
|
@ -0,0 +1,268 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "seafile-session.h"
|
||||
#include "utils.h"
|
||||
#include "seaf-utils.h"
|
||||
#include "block-mgr.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#include "block-backend.h"
|
||||
|
||||
#define SEAF_BLOCK_DIR "blocks"
|
||||
|
||||
|
||||
extern BlockBackend *
|
||||
block_backend_fs_new (const char *block_dir, const char *tmp_dir);
|
||||
|
||||
|
||||
SeafBlockManager *
|
||||
seaf_block_manager_new (struct _SeafileSession *seaf,
|
||||
const char *seaf_dir)
|
||||
{
|
||||
SeafBlockManager *mgr;
|
||||
|
||||
mgr = g_new0 (SeafBlockManager, 1);
|
||||
mgr->seaf = seaf;
|
||||
|
||||
mgr->backend = block_backend_fs_new (seaf_dir, seaf->tmp_file_dir);
|
||||
if (!mgr->backend) {
|
||||
seaf_warning ("[Block mgr] Failed to load backend.\n");
|
||||
goto onerror;
|
||||
}
|
||||
|
||||
return mgr;
|
||||
|
||||
onerror:
|
||||
g_free (mgr);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_block_manager_init (SeafBlockManager *mgr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
BlockHandle *
|
||||
seaf_block_manager_open_block (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id,
|
||||
int rw_type)
|
||||
{
|
||||
if (!store_id || !is_uuid_valid(store_id) ||
|
||||
!block_id || !is_object_id_valid(block_id))
|
||||
return NULL;
|
||||
|
||||
return mgr->backend->open_block (mgr->backend,
|
||||
store_id, version,
|
||||
block_id, rw_type);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_block_manager_read_block (SeafBlockManager *mgr,
|
||||
BlockHandle *handle,
|
||||
void *buf, int len)
|
||||
{
|
||||
return mgr->backend->read_block (mgr->backend, handle, buf, len);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_block_manager_write_block (SeafBlockManager *mgr,
|
||||
BlockHandle *handle,
|
||||
const void *buf, int len)
|
||||
{
|
||||
return mgr->backend->write_block (mgr->backend, handle, buf, len);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_block_manager_close_block (SeafBlockManager *mgr,
|
||||
BlockHandle *handle)
|
||||
{
|
||||
return mgr->backend->close_block (mgr->backend, handle);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_block_manager_block_handle_free (SeafBlockManager *mgr,
|
||||
BlockHandle *handle)
|
||||
{
|
||||
return mgr->backend->block_handle_free (mgr->backend, handle);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_block_manager_commit_block (SeafBlockManager *mgr,
|
||||
BlockHandle *handle)
|
||||
{
|
||||
return mgr->backend->commit_block (mgr->backend, handle);
|
||||
}
|
||||
|
||||
gboolean seaf_block_manager_block_exists (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id)
|
||||
{
|
||||
if (!store_id || !is_uuid_valid(store_id) ||
|
||||
!block_id || !is_object_id_valid(block_id))
|
||||
return FALSE;
|
||||
|
||||
return mgr->backend->exists (mgr->backend, store_id, version, block_id);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_block_manager_remove_block (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id)
|
||||
{
|
||||
if (!store_id || !is_uuid_valid(store_id) ||
|
||||
!block_id || !is_object_id_valid(block_id))
|
||||
return -1;
|
||||
|
||||
return mgr->backend->remove_block (mgr->backend, store_id, version, block_id);
|
||||
}
|
||||
|
||||
BlockMetadata *
|
||||
seaf_block_manager_stat_block (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id)
|
||||
{
|
||||
if (!store_id || !is_uuid_valid(store_id) ||
|
||||
!block_id || !is_object_id_valid(block_id))
|
||||
return NULL;
|
||||
|
||||
return mgr->backend->stat_block (mgr->backend, store_id, version, block_id);
|
||||
}
|
||||
|
||||
BlockMetadata *
|
||||
seaf_block_manager_stat_block_by_handle (SeafBlockManager *mgr,
|
||||
BlockHandle *handle)
|
||||
{
|
||||
return mgr->backend->stat_block_by_handle (mgr->backend, handle);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_block_manager_foreach_block (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
SeafBlockFunc process,
|
||||
void *user_data)
|
||||
{
|
||||
return mgr->backend->foreach_block (mgr->backend,
|
||||
store_id, version,
|
||||
process, user_data);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_block_manager_copy_block (SeafBlockManager *mgr,
|
||||
const char *src_store_id,
|
||||
int src_version,
|
||||
const char *dst_store_id,
|
||||
int dst_version,
|
||||
const char *block_id)
|
||||
{
|
||||
if (strcmp (block_id, EMPTY_SHA1) == 0)
|
||||
return 0;
|
||||
|
||||
return mgr->backend->copy (mgr->backend,
|
||||
src_store_id,
|
||||
src_version,
|
||||
dst_store_id,
|
||||
dst_version,
|
||||
block_id);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
get_block_number (const char *store_id,
|
||||
int version,
|
||||
const char *block_id,
|
||||
void *data)
|
||||
{
|
||||
guint64 *n_blocks = data;
|
||||
|
||||
++(*n_blocks);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
guint64
|
||||
seaf_block_manager_get_block_number (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version)
|
||||
{
|
||||
guint64 n_blocks = 0;
|
||||
|
||||
seaf_block_manager_foreach_block (mgr, store_id, version,
|
||||
get_block_number, &n_blocks);
|
||||
|
||||
return n_blocks;
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_block_manager_verify_block (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id,
|
||||
gboolean *io_error)
|
||||
{
|
||||
BlockHandle *h;
|
||||
char buf[10240];
|
||||
int n;
|
||||
SHA_CTX ctx;
|
||||
guint8 sha1[20];
|
||||
char check_id[41];
|
||||
|
||||
h = seaf_block_manager_open_block (mgr,
|
||||
store_id, version,
|
||||
block_id, BLOCK_READ);
|
||||
if (!h) {
|
||||
seaf_warning ("Failed to open block %s:%.8s.\n", store_id, block_id);
|
||||
*io_error = TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
SHA1_Init (&ctx);
|
||||
while (1) {
|
||||
n = seaf_block_manager_read_block (mgr, h, buf, sizeof(buf));
|
||||
if (n < 0) {
|
||||
seaf_warning ("Failed to read block %s:%.8s.\n", store_id, block_id);
|
||||
*io_error = TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
if (n == 0)
|
||||
break;
|
||||
|
||||
SHA1_Update (&ctx, buf, n);
|
||||
}
|
||||
|
||||
seaf_block_manager_close_block (mgr, h);
|
||||
seaf_block_manager_block_handle_free (mgr, h);
|
||||
|
||||
SHA1_Final (sha1, &ctx);
|
||||
rawdata_to_hex (sha1, check_id, 20);
|
||||
|
||||
if (strcmp (check_id, block_id) == 0)
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_block_manager_remove_store (SeafBlockManager *mgr,
|
||||
const char *store_id)
|
||||
{
|
||||
return mgr->backend->remove_store (mgr->backend, store_id);
|
||||
}
|
154
common/block-mgr.h
Normal file
154
common/block-mgr.h
Normal file
|
@ -0,0 +1,154 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef SEAF_BLOCK_MGR_H
|
||||
#define SEAF_BLOCK_MGR_H
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib-object.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "block.h"
|
||||
|
||||
struct _SeafileSession;
|
||||
|
||||
typedef struct _SeafBlockManager SeafBlockManager;
|
||||
|
||||
struct _SeafBlockManager {
|
||||
struct _SeafileSession *seaf;
|
||||
|
||||
struct BlockBackend *backend;
|
||||
};
|
||||
|
||||
|
||||
SeafBlockManager *
|
||||
seaf_block_manager_new (struct _SeafileSession *seaf,
|
||||
const char *seaf_dir);
|
||||
|
||||
/*
|
||||
* Open a block for read or write.
|
||||
*
|
||||
* @store_id: id for the block store
|
||||
* @version: data format version for the repo
|
||||
* @block_id: ID of block.
|
||||
* @rw_type: BLOCK_READ or BLOCK_WRITE.
|
||||
* Returns: A handle for the block.
|
||||
*/
|
||||
BlockHandle *
|
||||
seaf_block_manager_open_block (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id,
|
||||
int rw_type);
|
||||
|
||||
/*
|
||||
* Read data from a block.
|
||||
* The semantics is similar to readn.
|
||||
*
|
||||
* @handle: Hanlde returned by seaf_block_manager_open_block().
|
||||
* @buf: Data wuold be copied into this buf.
|
||||
* @len: At most @len bytes would be read.
|
||||
*
|
||||
* Returns: the bytes read.
|
||||
*/
|
||||
int
|
||||
seaf_block_manager_read_block (SeafBlockManager *mgr,
|
||||
BlockHandle *handle,
|
||||
void *buf, int len);
|
||||
|
||||
/*
|
||||
* Write data to a block.
|
||||
* The semantics is similar to writen.
|
||||
*
|
||||
* @handle: Hanlde returned by seaf_block_manager_open_block().
|
||||
* @buf: Data to be written to the block.
|
||||
* @len: At most @len bytes would be written.
|
||||
*
|
||||
* Returns: the bytes written.
|
||||
*/
|
||||
int
|
||||
seaf_block_manager_write_block (SeafBlockManager *mgr,
|
||||
BlockHandle *handle,
|
||||
const void *buf, int len);
|
||||
|
||||
/*
|
||||
* Commit a block to storage.
|
||||
* The block must be opened for write.
|
||||
*
|
||||
* @handle: Hanlde returned by seaf_block_manager_open_block().
|
||||
*
|
||||
* Returns: 0 on success, -1 on error.
|
||||
*/
|
||||
int
|
||||
seaf_block_manager_commit_block (SeafBlockManager *mgr,
|
||||
BlockHandle *handle);
|
||||
|
||||
/*
|
||||
* Close an open block.
|
||||
*
|
||||
* @handle: Hanlde returned by seaf_block_manager_open_block().
|
||||
*
|
||||
* Returns: 0 on success, -1 on error.
|
||||
*/
|
||||
int
|
||||
seaf_block_manager_close_block (SeafBlockManager *mgr,
|
||||
BlockHandle *handle);
|
||||
|
||||
void
|
||||
seaf_block_manager_block_handle_free (SeafBlockManager *mgr,
|
||||
BlockHandle *handle);
|
||||
|
||||
gboolean
|
||||
seaf_block_manager_block_exists (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id);
|
||||
|
||||
int
|
||||
seaf_block_manager_remove_block (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id);
|
||||
|
||||
BlockMetadata *
|
||||
seaf_block_manager_stat_block (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id);
|
||||
|
||||
BlockMetadata *
|
||||
seaf_block_manager_stat_block_by_handle (SeafBlockManager *mgr,
|
||||
BlockHandle *handle);
|
||||
|
||||
int
|
||||
seaf_block_manager_foreach_block (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
SeafBlockFunc process,
|
||||
void *user_data);
|
||||
|
||||
int
|
||||
seaf_block_manager_copy_block (SeafBlockManager *mgr,
|
||||
const char *src_store_id,
|
||||
int src_version,
|
||||
const char *dst_store_id,
|
||||
int dst_version,
|
||||
const char *block_id);
|
||||
|
||||
/* Remove all blocks for a repo. Only valid for version 1 repo. */
|
||||
int
|
||||
seaf_block_manager_remove_store (SeafBlockManager *mgr,
|
||||
const char *store_id);
|
||||
|
||||
guint64
|
||||
seaf_block_manager_get_block_number (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version);
|
||||
|
||||
gboolean
|
||||
seaf_block_manager_verify_block (SeafBlockManager *mgr,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *block_id,
|
||||
gboolean *io_error);
|
||||
|
||||
#endif
|
296
common/block-tx-utils.c
Normal file
296
common/block-tx-utils.c
Normal file
|
@ -0,0 +1,296 @@
|
|||
#include "common.h"
|
||||
#define DEBUG_FLAG SEAFILE_DEBUG_TRANSFER
|
||||
#include "log.h"
|
||||
|
||||
#include "utils.h"
|
||||
#include "block-tx-utils.h"
|
||||
|
||||
/* Utility functions for block transfer protocol. */
|
||||
|
||||
/* Encryption related functions. */
|
||||
|
||||
void
|
||||
blocktx_generate_encrypt_key (unsigned char *session_key, int sk_len,
|
||||
unsigned char *key, unsigned char *iv)
|
||||
{
|
||||
EVP_BytesToKey (EVP_aes_256_cbc(), /* cipher mode */
|
||||
EVP_sha1(), /* message digest */
|
||||
NULL, /* salt */
|
||||
session_key,
|
||||
sk_len,
|
||||
3, /* iteration times */
|
||||
key, /* the derived key */
|
||||
iv); /* IV, initial vector */
|
||||
}
|
||||
|
||||
int
|
||||
blocktx_encrypt_init (EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char *key,
|
||||
const unsigned char *iv)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* Prepare CTX for encryption. */
|
||||
EVP_CIPHER_CTX_init (ctx);
|
||||
|
||||
ret = EVP_EncryptInit_ex (ctx,
|
||||
EVP_aes_256_cbc(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
key, /* derived key */
|
||||
iv); /* initial vector */
|
||||
if (ret == 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
blocktx_decrypt_init (EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char *key,
|
||||
const unsigned char *iv)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* Prepare CTX for decryption. */
|
||||
EVP_CIPHER_CTX_init (ctx);
|
||||
|
||||
ret = EVP_DecryptInit_ex (ctx,
|
||||
EVP_aes_256_cbc(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
key, /* derived key */
|
||||
iv); /* initial vector */
|
||||
if (ret == 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Sending frame */
|
||||
|
||||
int
|
||||
send_encrypted_data_frame_begin (evutil_socket_t data_fd,
|
||||
int frame_len)
|
||||
{
|
||||
/* Compute data size after encryption.
|
||||
* Block size is 16 bytes and AES always add one padding block.
|
||||
*/
|
||||
int enc_frame_len;
|
||||
|
||||
enc_frame_len = ((frame_len >> 4) + 1) << 4;
|
||||
enc_frame_len = htonl (enc_frame_len);
|
||||
|
||||
if (sendn (data_fd, &enc_frame_len, sizeof(int)) < 0) {
|
||||
seaf_warning ("Failed to send frame length: %s.\n",
|
||||
evutil_socket_error_to_string(evutil_socket_geterror(data_fd)));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
send_encrypted_data (EVP_CIPHER_CTX *ctx,
|
||||
evutil_socket_t data_fd,
|
||||
const void *buf, int len)
|
||||
{
|
||||
char out_buf[len + ENC_BLOCK_SIZE];
|
||||
int out_len;
|
||||
|
||||
if (EVP_EncryptUpdate (ctx,
|
||||
(unsigned char *)out_buf, &out_len,
|
||||
(unsigned char *)buf, len) == 0) {
|
||||
seaf_warning ("Failed to encrypt data.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sendn (data_fd, out_buf, out_len) < 0) {
|
||||
seaf_warning ("Failed to write data: %s.\n",
|
||||
evutil_socket_error_to_string(evutil_socket_geterror(data_fd)));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
send_encrypted_data_frame_end (EVP_CIPHER_CTX *ctx,
|
||||
evutil_socket_t data_fd)
|
||||
{
|
||||
char out_buf[ENC_BLOCK_SIZE];
|
||||
int out_len;
|
||||
|
||||
if (EVP_EncryptFinal_ex (ctx, (unsigned char *)out_buf, &out_len) == 0) {
|
||||
seaf_warning ("Failed to encrypt data.\n");
|
||||
return -1;
|
||||
}
|
||||
if (sendn (data_fd, out_buf, out_len) < 0) {
|
||||
seaf_warning ("Failed to write data: %s.\n",
|
||||
evutil_socket_error_to_string(evutil_socket_geterror(data_fd)));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Receiving frame */
|
||||
|
||||
static int
|
||||
handle_frame_content (struct evbuffer *buf, FrameParser *parser)
|
||||
{
|
||||
char *frame;
|
||||
EVP_CIPHER_CTX ctx;
|
||||
char *out;
|
||||
int outlen, outlen2;
|
||||
int ret = 0;
|
||||
|
||||
struct evbuffer *input = buf;
|
||||
|
||||
if (evbuffer_get_length (input) < parser->enc_frame_len)
|
||||
return 0;
|
||||
|
||||
if (parser->version == 1)
|
||||
blocktx_decrypt_init (&ctx, parser->key, parser->iv);
|
||||
else if (parser->version == 2)
|
||||
blocktx_decrypt_init (&ctx, parser->key_v2, parser->iv_v2);
|
||||
|
||||
frame = g_malloc (parser->enc_frame_len);
|
||||
out = g_malloc (parser->enc_frame_len + ENC_BLOCK_SIZE);
|
||||
|
||||
evbuffer_remove (input, frame, parser->enc_frame_len);
|
||||
|
||||
if (EVP_DecryptUpdate (&ctx,
|
||||
(unsigned char *)out, &outlen,
|
||||
(unsigned char *)frame,
|
||||
parser->enc_frame_len) == 0) {
|
||||
seaf_warning ("Failed to decrypt frame content.\n");
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (EVP_DecryptFinal_ex (&ctx, (unsigned char *)(out + outlen), &outlen2) == 0)
|
||||
{
|
||||
seaf_warning ("Failed to decrypt frame content.\n");
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = parser->content_cb (out, outlen + outlen2, parser->cbarg);
|
||||
|
||||
out:
|
||||
g_free (frame);
|
||||
g_free (out);
|
||||
parser->enc_frame_len = 0;
|
||||
EVP_CIPHER_CTX_cleanup (&ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
handle_one_frame (struct evbuffer *buf, FrameParser *parser)
|
||||
{
|
||||
struct evbuffer *input = buf;
|
||||
|
||||
if (!parser->enc_frame_len) {
|
||||
/* Read the length of the encrypted frame first. */
|
||||
if (evbuffer_get_length (input) < sizeof(int))
|
||||
return 0;
|
||||
|
||||
int frame_len;
|
||||
evbuffer_remove (input, &frame_len, sizeof(int));
|
||||
parser->enc_frame_len = ntohl (frame_len);
|
||||
|
||||
if (evbuffer_get_length (input) > 0)
|
||||
return handle_frame_content (buf, parser);
|
||||
|
||||
return 0;
|
||||
} else {
|
||||
return handle_frame_content (buf, parser);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
handle_frame_fragment_content (struct evbuffer *buf, FrameParser *parser)
|
||||
{
|
||||
char *fragment = NULL, *out = NULL;
|
||||
int fragment_len, outlen;
|
||||
int ret = 0;
|
||||
|
||||
struct evbuffer *input = buf;
|
||||
|
||||
fragment_len = evbuffer_get_length (input);
|
||||
fragment = g_malloc (fragment_len);
|
||||
evbuffer_remove (input, fragment, fragment_len);
|
||||
|
||||
out = g_malloc (fragment_len + ENC_BLOCK_SIZE);
|
||||
|
||||
if (EVP_DecryptUpdate (&parser->ctx,
|
||||
(unsigned char *)out, &outlen,
|
||||
(unsigned char *)fragment, fragment_len) == 0) {
|
||||
seaf_warning ("Failed to decrypt frame fragment.\n");
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = parser->fragment_cb (out, outlen, 0, parser->cbarg);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
|
||||
parser->remain -= fragment_len;
|
||||
|
||||
if (parser->remain <= 0) {
|
||||
if (EVP_DecryptFinal_ex (&parser->ctx,
|
||||
(unsigned char *)out,
|
||||
&outlen) == 0) {
|
||||
seaf_warning ("Failed to decrypt frame fragment.\n");
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = parser->fragment_cb (out, outlen, 1, parser->cbarg);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
|
||||
EVP_CIPHER_CTX_cleanup (&parser->ctx);
|
||||
parser->enc_init = FALSE;
|
||||
parser->enc_frame_len = 0;
|
||||
}
|
||||
|
||||
out:
|
||||
g_free (fragment);
|
||||
g_free (out);
|
||||
if (ret < 0) {
|
||||
EVP_CIPHER_CTX_cleanup (&parser->ctx);
|
||||
parser->enc_init = FALSE;
|
||||
parser->enc_frame_len = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
handle_frame_fragments (struct evbuffer *buf, FrameParser *parser)
|
||||
{
|
||||
struct evbuffer *input = buf;
|
||||
|
||||
if (!parser->enc_frame_len) {
|
||||
/* Read the length of the encrypted frame first. */
|
||||
if (evbuffer_get_length (input) < sizeof(int))
|
||||
return 0;
|
||||
|
||||
int frame_len;
|
||||
evbuffer_remove (input, &frame_len, sizeof(int));
|
||||
parser->enc_frame_len = ntohl (frame_len);
|
||||
parser->remain = parser->enc_frame_len;
|
||||
|
||||
if (parser->version == 1)
|
||||
blocktx_decrypt_init (&parser->ctx, parser->key, parser->iv);
|
||||
else if (parser->version == 2)
|
||||
blocktx_decrypt_init (&parser->ctx, parser->key_v2, parser->iv_v2);
|
||||
parser->enc_init = TRUE;
|
||||
|
||||
if (evbuffer_get_length (input) > 0)
|
||||
return handle_frame_fragment_content (buf, parser);
|
||||
|
||||
return 0;
|
||||
} else {
|
||||
return handle_frame_fragment_content (buf, parser);
|
||||
}
|
||||
}
|
142
common/block-tx-utils.h
Normal file
142
common/block-tx-utils.h
Normal file
|
@ -0,0 +1,142 @@
|
|||
#ifndef BLOCK_TX_UTILS_H
|
||||
#define BLOCK_TX_UTILS_H
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/util.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
/* Common structures and contants shared by the client and server. */
|
||||
|
||||
/* We use AES 256 */
|
||||
#define ENC_KEY_SIZE 32
|
||||
#define ENC_BLOCK_SIZE 16
|
||||
|
||||
#define BLOCK_PROTOCOL_VERSION 2
|
||||
|
||||
enum {
|
||||
STATUS_OK = 0,
|
||||
STATUS_VERSION_MISMATCH,
|
||||
STATUS_BAD_REQUEST,
|
||||
STATUS_ACCESS_DENIED,
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
STATUS_NOT_FOUND,
|
||||
};
|
||||
|
||||
struct _HandshakeRequest {
|
||||
gint32 version;
|
||||
gint32 key_len;
|
||||
char enc_session_key[0];
|
||||
} __attribute__((__packed__));
|
||||
|
||||
typedef struct _HandshakeRequest HandshakeRequest;
|
||||
|
||||
struct _HandshakeResponse {
|
||||
gint32 status;
|
||||
gint32 version;
|
||||
} __attribute__((__packed__));
|
||||
|
||||
typedef struct _HandshakeResponse HandshakeResponse;
|
||||
|
||||
struct _AuthResponse {
|
||||
gint32 status;
|
||||
} __attribute__((__packed__));
|
||||
|
||||
typedef struct _AuthResponse AuthResponse;
|
||||
|
||||
enum {
|
||||
REQUEST_COMMAND_GET = 0,
|
||||
REQUEST_COMMAND_PUT,
|
||||
};
|
||||
|
||||
struct _RequestHeader {
|
||||
gint32 command;
|
||||
char block_id[40];
|
||||
} __attribute__((__packed__));
|
||||
|
||||
typedef struct _RequestHeader RequestHeader;
|
||||
|
||||
struct _ResponseHeader {
|
||||
gint32 status;
|
||||
} __attribute__((__packed__));
|
||||
|
||||
typedef struct _ResponseHeader ResponseHeader;
|
||||
|
||||
/* Utility functions for encryption. */
|
||||
|
||||
void
|
||||
blocktx_generate_encrypt_key (unsigned char *session_key, int sk_len,
|
||||
unsigned char *key, unsigned char *iv);
|
||||
|
||||
int
|
||||
blocktx_encrypt_init (EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char *key,
|
||||
const unsigned char *iv);
|
||||
|
||||
int
|
||||
blocktx_decrypt_init (EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char *key,
|
||||
const unsigned char *iv);
|
||||
|
||||
/*
|
||||
* Encrypted data is sent in "frames".
|
||||
* Format of a frame:
|
||||
*
|
||||
* length of data in the frame after encryption + encrypted data.
|
||||
*
|
||||
* Each frame can contain three types of contents:
|
||||
* 1. Auth request or response;
|
||||
* 2. Block request or response header;
|
||||
* 3. Block content.
|
||||
*/
|
||||
|
||||
int
|
||||
send_encrypted_data_frame_begin (evutil_socket_t data_fd,
|
||||
int frame_len);
|
||||
|
||||
int
|
||||
send_encrypted_data (EVP_CIPHER_CTX *ctx,
|
||||
evutil_socket_t data_fd,
|
||||
const void *buf, int len);
|
||||
|
||||
int
|
||||
send_encrypted_data_frame_end (EVP_CIPHER_CTX *ctx,
|
||||
evutil_socket_t data_fd);
|
||||
|
||||
typedef int (*FrameContentCB) (char *, int, void *);
|
||||
|
||||
typedef int (*FrameFragmentCB) (char *, int, int, void *);
|
||||
|
||||
typedef struct _FrameParser {
|
||||
int enc_frame_len;
|
||||
|
||||
unsigned char key[ENC_KEY_SIZE];
|
||||
unsigned char iv[ENC_BLOCK_SIZE];
|
||||
gboolean enc_init;
|
||||
EVP_CIPHER_CTX ctx;
|
||||
|
||||
unsigned char key_v2[ENC_KEY_SIZE];
|
||||
unsigned char iv_v2[ENC_BLOCK_SIZE];
|
||||
|
||||
int version;
|
||||
|
||||
/* Used when parsing fragments */
|
||||
int remain;
|
||||
|
||||
FrameContentCB content_cb;
|
||||
FrameFragmentCB fragment_cb;
|
||||
void *cbarg;
|
||||
} FrameParser;
|
||||
|
||||
/* Handle entire frame all at once.
|
||||
* parser->content_cb() will be called after the entire frame is read.
|
||||
*/
|
||||
int
|
||||
handle_one_frame (struct evbuffer *buf, FrameParser *parser);
|
||||
|
||||
/* Handle a frame fragment by fragment.
|
||||
* parser->fragment_cb() will be called when any amount data is read.
|
||||
*/
|
||||
int
|
||||
handle_frame_fragments (struct evbuffer *buf, FrameParser *parser);
|
||||
|
||||
#endif
|
29
common/block.h
Normal file
29
common/block.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef BLOCK_H
|
||||
#define BLOCK_H
|
||||
|
||||
typedef struct _BMetadata BlockMetadata;
|
||||
typedef struct _BMetadata BMetadata;
|
||||
|
||||
struct _BMetadata {
|
||||
char id[41];
|
||||
uint32_t size;
|
||||
};
|
||||
|
||||
/* Opaque block handle.
|
||||
*/
|
||||
typedef struct _BHandle BlockHandle;
|
||||
typedef struct _BHandle BHandle;
|
||||
|
||||
enum {
|
||||
BLOCK_READ,
|
||||
BLOCK_WRITE,
|
||||
};
|
||||
|
||||
typedef gboolean (*SeafBlockFunc) (const char *store_id,
|
||||
int version,
|
||||
const char *block_id,
|
||||
void *user_data);
|
||||
|
||||
#endif
|
663
common/branch-mgr.c
Normal file
663
common/branch-mgr.c
Normal file
|
@ -0,0 +1,663 @@
|
|||
#include "common.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#ifndef SEAFILE_SERVER
|
||||
#include "db.h"
|
||||
#else
|
||||
#include "seaf-db.h"
|
||||
#endif
|
||||
|
||||
#include "seafile-session.h"
|
||||
|
||||
#include "branch-mgr.h"
|
||||
|
||||
#define BRANCH_DB "branch.db"
|
||||
|
||||
SeafBranch *
|
||||
seaf_branch_new (const char *name, const char *repo_id, const char *commit_id)
|
||||
{
|
||||
SeafBranch *branch;
|
||||
|
||||
branch = g_new0 (SeafBranch, 1);
|
||||
|
||||
branch->name = g_strdup (name);
|
||||
memcpy (branch->repo_id, repo_id, 36);
|
||||
branch->repo_id[36] = '\0';
|
||||
memcpy (branch->commit_id, commit_id, 40);
|
||||
branch->commit_id[40] = '\0';
|
||||
|
||||
branch->ref = 1;
|
||||
|
||||
return branch;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_branch_free (SeafBranch *branch)
|
||||
{
|
||||
if (branch == NULL) return;
|
||||
g_free (branch->name);
|
||||
g_free (branch);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_branch_list_free (GList *blist)
|
||||
{
|
||||
GList *ptr;
|
||||
|
||||
for (ptr = blist; ptr; ptr = ptr->next) {
|
||||
seaf_branch_unref (ptr->data);
|
||||
}
|
||||
g_list_free (blist);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
seaf_branch_set_commit (SeafBranch *branch, const char *commit_id)
|
||||
{
|
||||
memcpy (branch->commit_id, commit_id, 40);
|
||||
branch->commit_id[40] = '\0';
|
||||
}
|
||||
|
||||
void
|
||||
seaf_branch_ref (SeafBranch *branch)
|
||||
{
|
||||
branch->ref++;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_branch_unref (SeafBranch *branch)
|
||||
{
|
||||
if (!branch)
|
||||
return;
|
||||
|
||||
if (--branch->ref <= 0)
|
||||
seaf_branch_free (branch);
|
||||
}
|
||||
|
||||
struct _SeafBranchManagerPriv {
|
||||
sqlite3 *db;
|
||||
#ifndef SEAFILE_SERVER
|
||||
pthread_mutex_t db_lock;
|
||||
#endif
|
||||
|
||||
#if defined( SEAFILE_SERVER ) && defined( FULL_FEATURE )
|
||||
uint32_t cevent_id;
|
||||
#endif
|
||||
};
|
||||
|
||||
#if defined( SEAFILE_SERVER ) && defined( FULL_FEATURE )
|
||||
|
||||
#include "mq-mgr.h"
|
||||
#include <ccnet/cevent.h>
|
||||
static void publish_repo_update_event (CEvent *event, void *data);
|
||||
|
||||
#endif
|
||||
|
||||
static int open_db (SeafBranchManager *mgr);
|
||||
|
||||
SeafBranchManager *
|
||||
seaf_branch_manager_new (struct _SeafileSession *seaf)
|
||||
{
|
||||
SeafBranchManager *mgr;
|
||||
|
||||
mgr = g_new0 (SeafBranchManager, 1);
|
||||
mgr->priv = g_new0 (SeafBranchManagerPriv, 1);
|
||||
mgr->seaf = seaf;
|
||||
|
||||
#ifndef SEAFILE_SERVER
|
||||
pthread_mutex_init (&mgr->priv->db_lock, NULL);
|
||||
#endif
|
||||
|
||||
return mgr;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_branch_manager_init (SeafBranchManager *mgr)
|
||||
{
|
||||
#if defined( SEAFILE_SERVER ) && defined( FULL_FEATURE )
|
||||
mgr->priv->cevent_id = cevent_manager_register (seaf->ev_mgr,
|
||||
(cevent_handler)publish_repo_update_event,
|
||||
NULL);
|
||||
#endif
|
||||
|
||||
return open_db (mgr);
|
||||
}
|
||||
|
||||
static int
|
||||
open_db (SeafBranchManager *mgr)
|
||||
{
|
||||
#ifndef SEAFILE_SERVER
|
||||
|
||||
char *db_path;
|
||||
const char *sql;
|
||||
|
||||
db_path = g_build_filename (mgr->seaf->seaf_dir, BRANCH_DB, NULL);
|
||||
if (sqlite_open_db (db_path, &mgr->priv->db) < 0) {
|
||||
g_critical ("[Branch mgr] Failed to open branch db\n");
|
||||
g_free (db_path);
|
||||
return -1;
|
||||
}
|
||||
g_free (db_path);
|
||||
|
||||
sql = "CREATE TABLE IF NOT EXISTS Branch ("
|
||||
"name TEXT, repo_id TEXT, commit_id TEXT);";
|
||||
if (sqlite_query_exec (mgr->priv->db, sql) < 0)
|
||||
return -1;
|
||||
|
||||
sql = "CREATE INDEX IF NOT EXISTS branch_index ON Branch(repo_id, name);";
|
||||
if (sqlite_query_exec (mgr->priv->db, sql) < 0)
|
||||
return -1;
|
||||
|
||||
#elif defined FULL_FEATURE
|
||||
|
||||
char *sql;
|
||||
switch (seaf_db_type (mgr->seaf->db)) {
|
||||
case SEAF_DB_TYPE_MYSQL:
|
||||
sql = "CREATE TABLE IF NOT EXISTS Branch ("
|
||||
"name VARCHAR(10), repo_id CHAR(41), commit_id CHAR(41),"
|
||||
"PRIMARY KEY (repo_id, name)) ENGINE = INNODB";
|
||||
if (seaf_db_query (mgr->seaf->db, sql) < 0)
|
||||
return -1;
|
||||
break;
|
||||
case SEAF_DB_TYPE_PGSQL:
|
||||
sql = "CREATE TABLE IF NOT EXISTS Branch ("
|
||||
"name VARCHAR(10), repo_id CHAR(40), commit_id CHAR(40),"
|
||||
"PRIMARY KEY (repo_id, name))";
|
||||
if (seaf_db_query (mgr->seaf->db, sql) < 0)
|
||||
return -1;
|
||||
break;
|
||||
case SEAF_DB_TYPE_SQLITE:
|
||||
sql = "CREATE TABLE IF NOT EXISTS Branch ("
|
||||
"name VARCHAR(10), repo_id CHAR(41), commit_id CHAR(41),"
|
||||
"PRIMARY KEY (repo_id, name))";
|
||||
if (seaf_db_query (mgr->seaf->db, sql) < 0)
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_branch_manager_add_branch (SeafBranchManager *mgr, SeafBranch *branch)
|
||||
{
|
||||
#ifndef SEAFILE_SERVER
|
||||
char sql[256];
|
||||
|
||||
pthread_mutex_lock (&mgr->priv->db_lock);
|
||||
|
||||
sqlite3_snprintf (sizeof(sql), sql,
|
||||
"SELECT 1 FROM Branch WHERE name=%Q and repo_id=%Q",
|
||||
branch->name, branch->repo_id);
|
||||
if (sqlite_check_for_existence (mgr->priv->db, sql))
|
||||
sqlite3_snprintf (sizeof(sql), sql,
|
||||
"UPDATE Branch SET commit_id=%Q WHERE "
|
||||
"name=%Q and repo_id=%Q",
|
||||
branch->commit_id, branch->name, branch->repo_id);
|
||||
else
|
||||
sqlite3_snprintf (sizeof(sql), sql,
|
||||
"INSERT INTO Branch VALUES (%Q, %Q, %Q)",
|
||||
branch->name, branch->repo_id, branch->commit_id);
|
||||
|
||||
sqlite_query_exec (mgr->priv->db, sql);
|
||||
|
||||
pthread_mutex_unlock (&mgr->priv->db_lock);
|
||||
|
||||
return 0;
|
||||
#else
|
||||
char *sql;
|
||||
SeafDB *db = mgr->seaf->db;
|
||||
|
||||
if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL) {
|
||||
gboolean exists, err;
|
||||
int rc;
|
||||
|
||||
sql = "SELECT repo_id FROM Branch WHERE name=? AND repo_id=?";
|
||||
exists = seaf_db_statement_exists(db, sql, &err,
|
||||
2, "string", branch->name,
|
||||
"string", branch->repo_id);
|
||||
if (err)
|
||||
return -1;
|
||||
|
||||
if (exists)
|
||||
rc = seaf_db_statement_query (db,
|
||||
"UPDATE Branch SET commit_id=? "
|
||||
"WHERE name=? AND repo_id=?",
|
||||
3, "string", branch->commit_id,
|
||||
"string", branch->name,
|
||||
"string", branch->repo_id);
|
||||
else
|
||||
rc = seaf_db_statement_query (db,
|
||||
"INSERT INTO Branch VALUES (?, ?, ?)",
|
||||
3, "string", branch->name,
|
||||
"string", branch->repo_id,
|
||||
"string", branch->commit_id);
|
||||
if (rc < 0)
|
||||
return -1;
|
||||
} else {
|
||||
int rc = seaf_db_statement_query (db,
|
||||
"REPLACE INTO Branch VALUES (?, ?, ?)",
|
||||
3, "string", branch->name,
|
||||
"string", branch->repo_id,
|
||||
"string", branch->commit_id);
|
||||
if (rc < 0)
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
seaf_branch_manager_del_branch (SeafBranchManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *name)
|
||||
{
|
||||
#ifndef SEAFILE_SERVER
|
||||
char *sql;
|
||||
|
||||
pthread_mutex_lock (&mgr->priv->db_lock);
|
||||
|
||||
sql = sqlite3_mprintf ("DELETE FROM Branch WHERE name = %Q AND "
|
||||
"repo_id = '%s'", name, repo_id);
|
||||
if (sqlite_query_exec (mgr->priv->db, sql) < 0)
|
||||
seaf_warning ("Delete branch %s failed\n", name);
|
||||
sqlite3_free (sql);
|
||||
|
||||
pthread_mutex_unlock (&mgr->priv->db_lock);
|
||||
|
||||
return 0;
|
||||
#else
|
||||
int rc = seaf_db_statement_query (mgr->seaf->db,
|
||||
"DELETE FROM Branch WHERE name=? AND repo_id=?",
|
||||
2, "string", name, "string", repo_id);
|
||||
if (rc < 0)
|
||||
return -1;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
seaf_branch_manager_update_branch (SeafBranchManager *mgr, SeafBranch *branch)
|
||||
{
|
||||
#ifndef SEAFILE_SERVER
|
||||
sqlite3 *db;
|
||||
char *sql;
|
||||
|
||||
pthread_mutex_lock (&mgr->priv->db_lock);
|
||||
|
||||
db = mgr->priv->db;
|
||||
sql = sqlite3_mprintf ("UPDATE Branch SET commit_id = %Q "
|
||||
"WHERE name = %Q AND repo_id = %Q",
|
||||
branch->commit_id, branch->name, branch->repo_id);
|
||||
sqlite_query_exec (db, sql);
|
||||
sqlite3_free (sql);
|
||||
|
||||
pthread_mutex_unlock (&mgr->priv->db_lock);
|
||||
|
||||
return 0;
|
||||
#else
|
||||
int rc = seaf_db_statement_query (mgr->seaf->db,
|
||||
"UPDATE Branch SET commit_id = ? "
|
||||
"WHERE name = ? AND repo_id = ?",
|
||||
3, "string", branch->commit_id,
|
||||
"string", branch->name,
|
||||
"string", branch->repo_id);
|
||||
if (rc < 0)
|
||||
return -1;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined( SEAFILE_SERVER ) && defined( FULL_FEATURE )
|
||||
|
||||
static gboolean
|
||||
get_commit_id (SeafDBRow *row, void *data)
|
||||
{
|
||||
char *out_commit_id = data;
|
||||
const char *commit_id;
|
||||
|
||||
commit_id = seaf_db_row_get_column_text (row, 0);
|
||||
memcpy (out_commit_id, commit_id, 41);
|
||||
out_commit_id[40] = '\0';
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
char *repo_id;
|
||||
char *commit_id;
|
||||
} RepoUpdateEventData;
|
||||
|
||||
static void
|
||||
publish_repo_update_event (CEvent *event, void *data)
|
||||
{
|
||||
RepoUpdateEventData *rdata = event->data;
|
||||
|
||||
char buf[128];
|
||||
snprintf (buf, sizeof(buf), "repo-update\t%s\t%s",
|
||||
rdata->repo_id, rdata->commit_id);
|
||||
|
||||
seaf_mq_manager_publish_event (seaf->mq_mgr, buf);
|
||||
|
||||
g_free (rdata->repo_id);
|
||||
g_free (rdata->commit_id);
|
||||
g_free (rdata);
|
||||
}
|
||||
|
||||
static void
|
||||
on_branch_updated (SeafBranchManager *mgr, SeafBranch *branch)
|
||||
{
|
||||
if (seaf_repo_manager_is_virtual_repo (seaf->repo_mgr, branch->repo_id))
|
||||
return;
|
||||
|
||||
RepoUpdateEventData *rdata = g_new0 (RepoUpdateEventData, 1);
|
||||
|
||||
rdata->repo_id = g_strdup (branch->repo_id);
|
||||
rdata->commit_id = g_strdup (branch->commit_id);
|
||||
|
||||
cevent_manager_add_event (seaf->ev_mgr, mgr->priv->cevent_id, rdata);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_branch_manager_test_and_update_branch (SeafBranchManager *mgr,
|
||||
SeafBranch *branch,
|
||||
const char *old_commit_id)
|
||||
{
|
||||
SeafDBTrans *trans;
|
||||
char *sql;
|
||||
char commit_id[41] = { 0 };
|
||||
|
||||
trans = seaf_db_begin_transaction (mgr->seaf->db);
|
||||
if (!trans)
|
||||
return -1;
|
||||
|
||||
switch (seaf_db_type (mgr->seaf->db)) {
|
||||
case SEAF_DB_TYPE_MYSQL:
|
||||
case SEAF_DB_TYPE_PGSQL:
|
||||
sql = "SELECT commit_id FROM Branch WHERE name=? "
|
||||
"AND repo_id=? FOR UPDATE";
|
||||
break;
|
||||
case SEAF_DB_TYPE_SQLITE:
|
||||
sql = "SELECT commit_id FROM Branch WHERE name=? "
|
||||
"AND repo_id=?";
|
||||
break;
|
||||
default:
|
||||
g_return_val_if_reached (-1);
|
||||
}
|
||||
if (seaf_db_trans_foreach_selected_row (trans, sql,
|
||||
get_commit_id, commit_id,
|
||||
2, "string", branch->name,
|
||||
"string", branch->repo_id) < 0) {
|
||||
seaf_db_rollback (trans);
|
||||
seaf_db_trans_close (trans);
|
||||
return -1;
|
||||
}
|
||||
if (strcmp (old_commit_id, commit_id) != 0) {
|
||||
seaf_db_rollback (trans);
|
||||
seaf_db_trans_close (trans);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sql = "UPDATE Branch SET commit_id = ? "
|
||||
"WHERE name = ? AND repo_id = ?";
|
||||
if (seaf_db_trans_query (trans, sql, 3, "string", branch->commit_id,
|
||||
"string", branch->name,
|
||||
"string", branch->repo_id) < 0) {
|
||||
seaf_db_rollback (trans);
|
||||
seaf_db_trans_close (trans);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (seaf_db_commit (trans) < 0) {
|
||||
seaf_db_rollback (trans);
|
||||
seaf_db_trans_close (trans);
|
||||
return -1;
|
||||
}
|
||||
|
||||
seaf_db_trans_close (trans);
|
||||
|
||||
on_branch_updated (mgr, branch);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef SEAFILE_SERVER
|
||||
static SeafBranch *
|
||||
real_get_branch (SeafBranchManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *name)
|
||||
{
|
||||
SeafBranch *branch = NULL;
|
||||
sqlite3_stmt *stmt;
|
||||
sqlite3 *db;
|
||||
char *sql;
|
||||
int result;
|
||||
|
||||
pthread_mutex_lock (&mgr->priv->db_lock);
|
||||
|
||||
db = mgr->priv->db;
|
||||
sql = sqlite3_mprintf ("SELECT commit_id FROM Branch "
|
||||
"WHERE name = %Q and repo_id='%s'",
|
||||
name, repo_id);
|
||||
if (!(stmt = sqlite_query_prepare (db, sql))) {
|
||||
seaf_warning ("[Branch mgr] Couldn't prepare query %s\n", sql);
|
||||
sqlite3_free (sql);
|
||||
pthread_mutex_unlock (&mgr->priv->db_lock);
|
||||
return NULL;
|
||||
}
|
||||
sqlite3_free (sql);
|
||||
|
||||
result = sqlite3_step (stmt);
|
||||
if (result == SQLITE_ROW) {
|
||||
char *commit_id = (char *)sqlite3_column_text (stmt, 0);
|
||||
|
||||
branch = seaf_branch_new (name, repo_id, commit_id);
|
||||
pthread_mutex_unlock (&mgr->priv->db_lock);
|
||||
sqlite3_finalize (stmt);
|
||||
return branch;
|
||||
} else if (result == SQLITE_ERROR) {
|
||||
const char *str = sqlite3_errmsg (db);
|
||||
seaf_warning ("Couldn't prepare query, error: %d->'%s'\n",
|
||||
result, str ? str : "no error given");
|
||||
}
|
||||
|
||||
sqlite3_finalize (stmt);
|
||||
pthread_mutex_unlock (&mgr->priv->db_lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SeafBranch *
|
||||
seaf_branch_manager_get_branch (SeafBranchManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *name)
|
||||
{
|
||||
SeafBranch *branch;
|
||||
|
||||
/* "fetch_head" maps to "local" or "master" on client (LAN sync) */
|
||||
if (strcmp (name, "fetch_head") == 0) {
|
||||
branch = real_get_branch (mgr, repo_id, "local");
|
||||
if (!branch) {
|
||||
branch = real_get_branch (mgr, repo_id, "master");
|
||||
}
|
||||
return branch;
|
||||
} else {
|
||||
return real_get_branch (mgr, repo_id, name);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static gboolean
|
||||
get_branch (SeafDBRow *row, void *vid)
|
||||
{
|
||||
char *ret = vid;
|
||||
const char *commit_id;
|
||||
|
||||
commit_id = seaf_db_row_get_column_text (row, 0);
|
||||
memcpy (ret, commit_id, 41);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static SeafBranch *
|
||||
real_get_branch (SeafBranchManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *name)
|
||||
{
|
||||
char commit_id[41];
|
||||
char *sql;
|
||||
|
||||
commit_id[0] = 0;
|
||||
sql = "SELECT commit_id FROM Branch WHERE name=? AND repo_id=?";
|
||||
if (seaf_db_statement_foreach_row (mgr->seaf->db, sql,
|
||||
get_branch, commit_id,
|
||||
2, "string", name, "string", repo_id) < 0) {
|
||||
seaf_warning ("[branch mgr] DB error when get branch %s.\n", name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (commit_id[0] == 0)
|
||||
return NULL;
|
||||
|
||||
return seaf_branch_new (name, repo_id, commit_id);
|
||||
}
|
||||
|
||||
SeafBranch *
|
||||
seaf_branch_manager_get_branch (SeafBranchManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *name)
|
||||
{
|
||||
SeafBranch *branch;
|
||||
|
||||
/* "fetch_head" maps to "master" on server. */
|
||||
if (strcmp (name, "fetch_head") == 0) {
|
||||
branch = real_get_branch (mgr, repo_id, "master");
|
||||
return branch;
|
||||
} else {
|
||||
return real_get_branch (mgr, repo_id, name);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* not SEAFILE_SERVER */
|
||||
|
||||
gboolean
|
||||
seaf_branch_manager_branch_exists (SeafBranchManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *name)
|
||||
{
|
||||
#ifndef SEAFILE_SERVER
|
||||
char *sql;
|
||||
gboolean ret;
|
||||
|
||||
pthread_mutex_lock (&mgr->priv->db_lock);
|
||||
|
||||
sql = sqlite3_mprintf ("SELECT name FROM Branch WHERE name = %Q "
|
||||
"AND repo_id='%s'", name, repo_id);
|
||||
ret = sqlite_check_for_existence (mgr->priv->db, sql);
|
||||
sqlite3_free (sql);
|
||||
|
||||
pthread_mutex_unlock (&mgr->priv->db_lock);
|
||||
return ret;
|
||||
#else
|
||||
gboolean db_err = FALSE;
|
||||
|
||||
return seaf_db_statement_exists (mgr->seaf->db,
|
||||
"SELECT name FROM Branch WHERE name=? "
|
||||
"AND repo_id=?", &db_err,
|
||||
2, "string", name, "string", repo_id);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef SEAFILE_SERVER
|
||||
GList *
|
||||
seaf_branch_manager_get_branch_list (SeafBranchManager *mgr,
|
||||
const char *repo_id)
|
||||
{
|
||||
sqlite3 *db = mgr->priv->db;
|
||||
|
||||
int result;
|
||||
sqlite3_stmt *stmt;
|
||||
char sql[256];
|
||||
char *name;
|
||||
char *commit_id;
|
||||
GList *ret = NULL;
|
||||
SeafBranch *branch;
|
||||
|
||||
snprintf (sql, 256, "SELECT name, commit_id FROM branch WHERE repo_id ='%s'",
|
||||
repo_id);
|
||||
|
||||
pthread_mutex_lock (&mgr->priv->db_lock);
|
||||
|
||||
if ( !(stmt = sqlite_query_prepare(db, sql)) ) {
|
||||
pthread_mutex_unlock (&mgr->priv->db_lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
result = sqlite3_step (stmt);
|
||||
if (result == SQLITE_ROW) {
|
||||
name = (char *)sqlite3_column_text(stmt, 0);
|
||||
commit_id = (char *)sqlite3_column_text(stmt, 1);
|
||||
branch = seaf_branch_new (name, repo_id, commit_id);
|
||||
ret = g_list_prepend (ret, branch);
|
||||
}
|
||||
if (result == SQLITE_DONE)
|
||||
break;
|
||||
if (result == SQLITE_ERROR) {
|
||||
const gchar *str = sqlite3_errmsg (db);
|
||||
seaf_warning ("Couldn't prepare query, error: %d->'%s'\n",
|
||||
result, str ? str : "no error given");
|
||||
sqlite3_finalize (stmt);
|
||||
seaf_branch_list_free (ret);
|
||||
pthread_mutex_unlock (&mgr->priv->db_lock);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize (stmt);
|
||||
pthread_mutex_unlock (&mgr->priv->db_lock);
|
||||
return g_list_reverse(ret);
|
||||
}
|
||||
#else
|
||||
static gboolean
|
||||
get_branches (SeafDBRow *row, void *vplist)
|
||||
{
|
||||
GList **plist = vplist;
|
||||
const char *commit_id;
|
||||
const char *name;
|
||||
const char *repo_id;
|
||||
SeafBranch *branch;
|
||||
|
||||
name = seaf_db_row_get_column_text (row, 0);
|
||||
repo_id = seaf_db_row_get_column_text (row, 1);
|
||||
commit_id = seaf_db_row_get_column_text (row, 2);
|
||||
|
||||
branch = seaf_branch_new (name, repo_id, commit_id);
|
||||
*plist = g_list_prepend (*plist, branch);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
GList *
|
||||
seaf_branch_manager_get_branch_list (SeafBranchManager *mgr,
|
||||
const char *repo_id)
|
||||
{
|
||||
GList *ret = NULL;
|
||||
char *sql;
|
||||
|
||||
sql = "SELECT name, repo_id, commit_id FROM Branch WHERE repo_id=?";
|
||||
if (seaf_db_statement_foreach_row (mgr->seaf->db, sql,
|
||||
get_branches, &ret,
|
||||
1, "string", repo_id) < 0) {
|
||||
seaf_warning ("[branch mgr] DB error when get branch list.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
84
common/branch-mgr.h
Normal file
84
common/branch-mgr.h
Normal file
|
@ -0,0 +1,84 @@
|
|||
#ifndef SEAF_BRANCH_MGR_H
|
||||
#define SEAF_BRANCH_MGR_H
|
||||
|
||||
#include "commit-mgr.h"
|
||||
#define NO_BRANCH "-"
|
||||
|
||||
typedef struct _SeafBranch SeafBranch;
|
||||
|
||||
struct _SeafBranch {
|
||||
int ref;
|
||||
char *name;
|
||||
char repo_id[37];
|
||||
char commit_id[41];
|
||||
};
|
||||
|
||||
SeafBranch *seaf_branch_new (const char *name,
|
||||
const char *repo_id,
|
||||
const char *commit_id);
|
||||
void seaf_branch_free (SeafBranch *branch);
|
||||
void seaf_branch_set_commit (SeafBranch *branch, const char *commit_id);
|
||||
|
||||
void seaf_branch_ref (SeafBranch *branch);
|
||||
void seaf_branch_unref (SeafBranch *branch);
|
||||
|
||||
|
||||
typedef struct _SeafBranchManager SeafBranchManager;
|
||||
typedef struct _SeafBranchManagerPriv SeafBranchManagerPriv;
|
||||
|
||||
struct _SeafileSession;
|
||||
struct _SeafBranchManager {
|
||||
struct _SeafileSession *seaf;
|
||||
|
||||
SeafBranchManagerPriv *priv;
|
||||
};
|
||||
|
||||
SeafBranchManager *seaf_branch_manager_new (struct _SeafileSession *seaf);
|
||||
int seaf_branch_manager_init (SeafBranchManager *mgr);
|
||||
|
||||
int
|
||||
seaf_branch_manager_add_branch (SeafBranchManager *mgr, SeafBranch *branch);
|
||||
|
||||
int
|
||||
seaf_branch_manager_del_branch (SeafBranchManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *name);
|
||||
|
||||
void
|
||||
seaf_branch_list_free (GList *blist);
|
||||
|
||||
int
|
||||
seaf_branch_manager_update_branch (SeafBranchManager *mgr,
|
||||
SeafBranch *branch);
|
||||
|
||||
#ifdef SEAFILE_SERVER
|
||||
/**
|
||||
* Atomically test whether the current head commit id on @branch
|
||||
* is the same as @old_commit_id and update branch in db.
|
||||
*/
|
||||
int
|
||||
seaf_branch_manager_test_and_update_branch (SeafBranchManager *mgr,
|
||||
SeafBranch *branch,
|
||||
const char *old_commit_id);
|
||||
#endif
|
||||
|
||||
SeafBranch *
|
||||
seaf_branch_manager_get_branch (SeafBranchManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *name);
|
||||
|
||||
|
||||
gboolean
|
||||
seaf_branch_manager_branch_exists (SeafBranchManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *name);
|
||||
|
||||
GList *
|
||||
seaf_branch_manager_get_branch_list (SeafBranchManager *mgr,
|
||||
const char *repo_id);
|
||||
|
||||
gint64
|
||||
seaf_branch_manager_calculate_branch_size (SeafBranchManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *commit_id);
|
||||
#endif /* SEAF_BRANCH_MGR_H */
|
12
common/cdc/Makefile.am
Normal file
12
common/cdc/Makefile.am
Normal file
|
@ -0,0 +1,12 @@
|
|||
AM_CFLAGS = -I$(top_srcdir)/common -I$(top_srcdir)/lib \
|
||||
-Wall @GLIB2_CFLAGS@ @MSVC_CFLAGS@
|
||||
|
||||
noinst_LTLIBRARIES = libcdc.la
|
||||
|
||||
noinst_HEADERS = cdc.h rabin-checksum.h
|
||||
|
||||
libcdc_la_SOURCES = cdc.c rabin-checksum.c
|
||||
|
||||
libcdc_la_LDFLAGS = -Wl,-z -Wl,defs
|
||||
libcdc_la_LIBADD = @SSL_LIBS@ @GLIB2_LIBS@ \
|
||||
$(top_builddir)/lib/libseafile_common.la
|
235
common/cdc/cdc.c
Normal file
235
common/cdc/cdc.c
Normal file
|
@ -0,0 +1,235 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include "cdc.h"
|
||||
#include "../seafile-crypt.h"
|
||||
|
||||
#include "rabin-checksum.h"
|
||||
#define finger rabin_checksum
|
||||
#define rolling_finger rabin_rolling_checksum
|
||||
|
||||
#define READ_SIZE 1024 * 4
|
||||
|
||||
#define BYTE_TO_HEX(b) (((b)>=10)?('a'+b-10):('0'+b))
|
||||
|
||||
static int default_write_chunk (CDCDescriptor *chunk_descr)
|
||||
{
|
||||
char filename[NAME_MAX_SZ];
|
||||
char chksum_str[CHECKSUM_LENGTH *2 + 1];
|
||||
int fd_chunk, ret;
|
||||
|
||||
memset(chksum_str, 0, sizeof(chksum_str));
|
||||
rawdata_to_hex (chunk_descr->checksum, chksum_str, CHECKSUM_LENGTH);
|
||||
snprintf (filename, NAME_MAX_SZ, "./%s", chksum_str);
|
||||
fd_chunk = g_open (filename, O_RDWR | O_CREAT | O_BINARY, 0644);
|
||||
if (fd_chunk < 0)
|
||||
return -1;
|
||||
|
||||
ret = writen (fd_chunk, chunk_descr->block_buf, chunk_descr->len);
|
||||
close (fd_chunk);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int init_cdc_file_descriptor (int fd,
|
||||
uint64_t file_size,
|
||||
CDCFileDescriptor *file_descr)
|
||||
{
|
||||
int max_block_nr = 0;
|
||||
int block_min_sz = 0;
|
||||
|
||||
file_descr->block_nr = 0;
|
||||
|
||||
if (file_descr->block_min_sz <= 0)
|
||||
file_descr->block_min_sz = BLOCK_MIN_SZ;
|
||||
if (file_descr->block_max_sz <= 0)
|
||||
file_descr->block_max_sz = BLOCK_MAX_SZ;
|
||||
if (file_descr->block_sz <= 0)
|
||||
file_descr->block_sz = BLOCK_SZ;
|
||||
|
||||
if (file_descr->write_block == NULL)
|
||||
file_descr->write_block = (WriteblockFunc)default_write_chunk;
|
||||
|
||||
block_min_sz = file_descr->block_min_sz;
|
||||
max_block_nr = ((file_size + block_min_sz - 1) / block_min_sz);
|
||||
file_descr->blk_sha1s = (uint8_t *)calloc (sizeof(uint8_t),
|
||||
max_block_nr * CHECKSUM_LENGTH);
|
||||
file_descr->max_block_nr = max_block_nr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define WRITE_CDC_BLOCK(block_sz, write_data) \
|
||||
do { \
|
||||
int _block_sz = (block_sz); \
|
||||
chunk_descr.len = _block_sz; \
|
||||
chunk_descr.offset = offset; \
|
||||
ret = file_descr->write_block (file_descr->repo_id, \
|
||||
file_descr->version, \
|
||||
&chunk_descr, \
|
||||
crypt, chunk_descr.checksum, \
|
||||
(write_data)); \
|
||||
if (ret < 0) { \
|
||||
free (buf); \
|
||||
g_warning ("CDC: failed to write chunk.\n"); \
|
||||
return -1; \
|
||||
} \
|
||||
memcpy (file_descr->blk_sha1s + \
|
||||
file_descr->block_nr * CHECKSUM_LENGTH, \
|
||||
chunk_descr.checksum, CHECKSUM_LENGTH); \
|
||||
SHA1_Update (&file_ctx, chunk_descr.checksum, 20); \
|
||||
file_descr->block_nr++; \
|
||||
offset += _block_sz; \
|
||||
\
|
||||
memmove (buf, buf + _block_sz, tail - _block_sz); \
|
||||
tail = tail - _block_sz; \
|
||||
cur = 0; \
|
||||
}while(0);
|
||||
|
||||
/* content-defined chunking */
|
||||
int file_chunk_cdc(int fd_src,
|
||||
CDCFileDescriptor *file_descr,
|
||||
SeafileCrypt *crypt,
|
||||
gboolean write_data)
|
||||
{
|
||||
char *buf;
|
||||
uint32_t buf_sz;
|
||||
SHA_CTX file_ctx;
|
||||
CDCDescriptor chunk_descr;
|
||||
SHA1_Init (&file_ctx);
|
||||
|
||||
SeafStat sb;
|
||||
if (seaf_fstat (fd_src, &sb) < 0) {
|
||||
seaf_warning ("CDC: failed to stat: %s.\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
uint64_t expected_size = sb.st_size;
|
||||
|
||||
init_cdc_file_descriptor (fd_src, expected_size, file_descr);
|
||||
uint32_t block_min_sz = file_descr->block_min_sz;
|
||||
uint32_t block_mask = file_descr->block_sz - 1;
|
||||
|
||||
int fingerprint = 0;
|
||||
int offset = 0;
|
||||
int ret = 0;
|
||||
int tail, cur, rsize;
|
||||
|
||||
buf_sz = file_descr->block_max_sz;
|
||||
buf = chunk_descr.block_buf = malloc (buf_sz);
|
||||
if (!buf)
|
||||
return -1;
|
||||
|
||||
/* buf: a fix-sized buffer.
|
||||
* cur: data behind (inclusive) this offset has been scanned.
|
||||
* cur + 1 is the bytes that has been scanned.
|
||||
* tail: length of data loaded into memory. buf[tail] is invalid.
|
||||
*/
|
||||
tail = cur = 0;
|
||||
while (1) {
|
||||
if (tail < block_min_sz) {
|
||||
rsize = block_min_sz - tail + READ_SIZE;
|
||||
} else {
|
||||
rsize = (buf_sz - tail < READ_SIZE) ? (buf_sz - tail) : READ_SIZE;
|
||||
}
|
||||
ret = readn (fd_src, buf + tail, rsize);
|
||||
if (ret < 0) {
|
||||
seaf_warning ("CDC: failed to read: %s.\n", strerror(errno));
|
||||
free (buf);
|
||||
return -1;
|
||||
}
|
||||
tail += ret;
|
||||
file_descr->file_size += ret;
|
||||
|
||||
if (file_descr->file_size > expected_size) {
|
||||
seaf_warning ("File size changed while chunking.\n");
|
||||
free (buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* We've read all the data in this file. Output the block immediately
|
||||
* in two cases:
|
||||
* 1. The data left in the file is less than block_min_sz;
|
||||
* 2. We cannot find the break value until the end of this file.
|
||||
*/
|
||||
if (tail < block_min_sz || cur >= tail) {
|
||||
if (tail > 0) {
|
||||
if (file_descr->block_nr == file_descr->max_block_nr) {
|
||||
seaf_warning ("Block id array is not large enough, bail out.\n");
|
||||
free (buf);
|
||||
return -1;
|
||||
}
|
||||
WRITE_CDC_BLOCK (tail, write_data);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* A block is at least of size block_min_sz.
|
||||
*/
|
||||
if (cur < block_min_sz - 1)
|
||||
cur = block_min_sz - 1;
|
||||
|
||||
while (cur < tail) {
|
||||
fingerprint = (cur == block_min_sz - 1) ?
|
||||
finger(buf + cur - BLOCK_WIN_SZ + 1, BLOCK_WIN_SZ) :
|
||||
rolling_finger (fingerprint, BLOCK_WIN_SZ,
|
||||
*(buf+cur-BLOCK_WIN_SZ), *(buf + cur));
|
||||
|
||||
/* get a chunk, write block info to chunk file */
|
||||
if (((fingerprint & block_mask) == ((BREAK_VALUE & block_mask)))
|
||||
|| cur + 1 >= file_descr->block_max_sz)
|
||||
{
|
||||
if (file_descr->block_nr == file_descr->max_block_nr) {
|
||||
seaf_warning ("Block id array is not large enough, bail out.\n");
|
||||
free (buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
WRITE_CDC_BLOCK (cur + 1, write_data);
|
||||
break;
|
||||
} else {
|
||||
cur ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SHA1_Final (file_descr->file_sum, &file_ctx);
|
||||
|
||||
free (buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int filename_chunk_cdc(const char *filename,
|
||||
CDCFileDescriptor *file_descr,
|
||||
SeafileCrypt *crypt,
|
||||
gboolean write_data)
|
||||
{
|
||||
int fd_src = seaf_util_open (filename, O_RDONLY | O_BINARY);
|
||||
if (fd_src < 0) {
|
||||
seaf_warning ("CDC: failed to open %s.\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = file_chunk_cdc (fd_src, file_descr, crypt, write_data);
|
||||
close (fd_src);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void cdc_init ()
|
||||
{
|
||||
rabin_init (BLOCK_WIN_SZ);
|
||||
}
|
82
common/cdc/cdc.h
Normal file
82
common/cdc/cdc.h
Normal file
|
@ -0,0 +1,82 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef _CDC_H
|
||||
#define _CDC_H
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define BLOCK_SZ (1024*1024*1)
|
||||
#define BLOCK_MIN_SZ (1024*256)
|
||||
#define BLOCK_MAX_SZ (1024*1024*4)
|
||||
#define BLOCK_WIN_SZ 48
|
||||
|
||||
#define NAME_MAX_SZ 4096
|
||||
|
||||
#define BREAK_VALUE 0x0013 ///0x0513
|
||||
|
||||
|
||||
#ifdef HAVE_MD5
|
||||
#include "md5.h"
|
||||
#define get_checksum md5
|
||||
#define CHECKSUM_LENGTH 16
|
||||
#else
|
||||
#include <openssl/sha.h>
|
||||
#define get_checksum sha1
|
||||
#define CHECKSUM_LENGTH 20
|
||||
#endif
|
||||
|
||||
#ifndef O_BINARY
|
||||
#define O_BINARY 0
|
||||
#endif
|
||||
|
||||
struct _CDCFileDescriptor;
|
||||
struct _CDCDescriptor;
|
||||
struct SeafileCrypt;
|
||||
|
||||
typedef int (*WriteblockFunc)(const char *repo_id,
|
||||
int version,
|
||||
struct _CDCDescriptor *chunk_descr,
|
||||
struct SeafileCrypt *crypt,
|
||||
uint8_t *checksum,
|
||||
gboolean write_data);
|
||||
|
||||
/* define chunk file header and block entry */
|
||||
typedef struct _CDCFileDescriptor {
|
||||
uint32_t block_min_sz;
|
||||
uint32_t block_max_sz;
|
||||
uint32_t block_sz;
|
||||
uint64_t file_size;
|
||||
|
||||
uint32_t block_nr;
|
||||
uint8_t *blk_sha1s;
|
||||
int max_block_nr;
|
||||
uint8_t file_sum[CHECKSUM_LENGTH];
|
||||
|
||||
WriteblockFunc write_block;
|
||||
|
||||
char repo_id[37];
|
||||
int version;
|
||||
} CDCFileDescriptor;
|
||||
|
||||
typedef struct _CDCDescriptor {
|
||||
uint64_t offset;
|
||||
uint32_t len;
|
||||
uint8_t checksum[CHECKSUM_LENGTH];
|
||||
char *block_buf;
|
||||
int result;
|
||||
} CDCDescriptor;
|
||||
|
||||
int file_chunk_cdc(int fd_src,
|
||||
CDCFileDescriptor *file_descr,
|
||||
struct SeafileCrypt *crypt,
|
||||
gboolean write_data);
|
||||
|
||||
int filename_chunk_cdc(const char *filename,
|
||||
CDCFileDescriptor *file_descr,
|
||||
struct SeafileCrypt *crypt,
|
||||
gboolean write_data);
|
||||
|
||||
void cdc_init ();
|
||||
|
||||
#endif
|
180
common/cdc/rabin-checksum.c
Normal file
180
common/cdc/rabin-checksum.c
Normal file
|
@ -0,0 +1,180 @@
|
|||
#include <sys/types.h>
|
||||
#include "rabin-checksum.h"
|
||||
|
||||
#ifdef WIN32
|
||||
#include <stdint.h>
|
||||
#ifndef u_int
|
||||
typedef unsigned int u_int;
|
||||
#endif
|
||||
|
||||
#ifndef u_char
|
||||
typedef unsigned char u_char;
|
||||
#endif
|
||||
|
||||
#ifndef u_short
|
||||
typedef unsigned short u_short;
|
||||
#endif
|
||||
|
||||
#ifndef u_long
|
||||
typedef unsigned long u_long;
|
||||
#endif
|
||||
|
||||
#ifndef u_int16_t
|
||||
typedef uint16_t u_int16_t;
|
||||
#endif
|
||||
|
||||
#ifndef u_int32_t
|
||||
typedef uint32_t u_int32_t;
|
||||
#endif
|
||||
|
||||
#ifndef u_int64_t
|
||||
typedef uint64_t u_int64_t;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define INT64(n) n##LL
|
||||
#define MSB64 INT64(0x8000000000000000)
|
||||
|
||||
static u_int64_t poly = 0xbfe6b8a5bf378d83LL;
|
||||
static u_int64_t T[256];
|
||||
static u_int64_t U[256];
|
||||
static int shift;
|
||||
|
||||
/* Highest bit set in a byte */
|
||||
static const char bytemsb[0x100] = {
|
||||
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
|
||||
5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
};
|
||||
|
||||
/* Find last set (most significant bit) */
|
||||
static inline u_int fls32 (u_int32_t v)
|
||||
{
|
||||
if (v & 0xffff0000) {
|
||||
if (v & 0xff000000)
|
||||
return 24 + bytemsb[v>>24];
|
||||
else
|
||||
return 16 + bytemsb[v>>16];
|
||||
}
|
||||
if (v & 0x0000ff00)
|
||||
return 8 + bytemsb[v>>8];
|
||||
else
|
||||
return bytemsb[v];
|
||||
}
|
||||
|
||||
static inline char fls64 (u_int64_t v)
|
||||
{
|
||||
u_int32_t h;
|
||||
if ((h = v >> 32))
|
||||
return 32 + fls32 (h);
|
||||
else
|
||||
return fls32 ((u_int32_t) v);
|
||||
}
|
||||
|
||||
u_int64_t polymod (u_int64_t nh, u_int64_t nl, u_int64_t d)
|
||||
{
|
||||
int i = 0;
|
||||
int k = fls64 (d) - 1;
|
||||
|
||||
d <<= 63 - k;
|
||||
|
||||
if (nh) {
|
||||
if (nh & MSB64)
|
||||
nh ^= d;
|
||||
for (i = 62; i >= 0; i--)
|
||||
if (nh & ((u_int64_t) 1) << i) {
|
||||
nh ^= d >> (63 - i);
|
||||
nl ^= d << (i + 1);
|
||||
}
|
||||
}
|
||||
for (i = 63; i >= k; i--)
|
||||
{
|
||||
if (nl & INT64 (1) << i)
|
||||
nl ^= d >> (63 - i);
|
||||
}
|
||||
|
||||
return nl;
|
||||
}
|
||||
|
||||
void polymult (u_int64_t *php, u_int64_t *plp, u_int64_t x, u_int64_t y)
|
||||
{
|
||||
int i;
|
||||
u_int64_t ph = 0, pl = 0;
|
||||
if (x & 1)
|
||||
pl = y;
|
||||
for (i = 1; i < 64; i++)
|
||||
if (x & (INT64 (1) << i)) {
|
||||
ph ^= y >> (64 - i);
|
||||
pl ^= y << i;
|
||||
}
|
||||
if (php)
|
||||
*php = ph;
|
||||
if (plp)
|
||||
*plp = pl;
|
||||
}
|
||||
|
||||
u_int64_t polymmult (u_int64_t x, u_int64_t y, u_int64_t d)
|
||||
{
|
||||
u_int64_t h, l;
|
||||
polymult (&h, &l, x, y);
|
||||
return polymod (h, l, d);
|
||||
}
|
||||
|
||||
static u_int64_t append8 (u_int64_t p, u_char m)
|
||||
{
|
||||
return ((p << 8) | m) ^ T[p >> shift];
|
||||
}
|
||||
|
||||
static void calcT (u_int64_t poly)
|
||||
{
|
||||
int j = 0;
|
||||
int xshift = fls64 (poly) - 1;
|
||||
shift = xshift - 8;
|
||||
u_int64_t T1 = polymod (0, INT64 (1) << xshift, poly);
|
||||
for (j = 0; j < 256; j++) {
|
||||
T[j] = polymmult (j, T1, poly) | ((u_int64_t) j << xshift);
|
||||
}
|
||||
}
|
||||
|
||||
static void calcU(int size)
|
||||
{
|
||||
int i;
|
||||
u_int64_t sizeshift = 1;
|
||||
for (i = 1; i < size; i++)
|
||||
sizeshift = append8 (sizeshift, 0);
|
||||
for (i = 0; i < 256; i++)
|
||||
U[i] = polymmult (i, sizeshift, poly);
|
||||
}
|
||||
|
||||
void rabin_init(int len)
|
||||
{
|
||||
calcT(poly);
|
||||
calcU(len);
|
||||
}
|
||||
|
||||
/*
|
||||
* a simple 32 bit checksum that can be upadted from end
|
||||
*/
|
||||
unsigned int rabin_checksum(char *buf, int len)
|
||||
{
|
||||
int i;
|
||||
unsigned int sum = 0;
|
||||
for (i = 0; i < len; ++i) {
|
||||
sum = rabin_rolling_checksum (sum, len, 0, buf[i]);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
unsigned int rabin_rolling_checksum(unsigned int csum, int len,
|
||||
char c1, char c2)
|
||||
{
|
||||
return append8(csum ^ U[(unsigned char)c1], c2);
|
||||
}
|
10
common/cdc/rabin-checksum.h
Normal file
10
common/cdc/rabin-checksum.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef _RABIN_CHECKSUM_H
|
||||
#define _RABIN_CHECKSUM_H
|
||||
|
||||
unsigned int rabin_checksum(char *buf, int len);
|
||||
|
||||
unsigned int rabin_rolling_checksum(unsigned int csum, int len, char c1, char c2);
|
||||
|
||||
void rabin_init (int len);
|
||||
|
||||
#endif
|
888
common/commit-mgr.c
Normal file
888
common/commit-mgr.c
Normal file
|
@ -0,0 +1,888 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include <jansson.h>
|
||||
#include <openssl/sha.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "db.h"
|
||||
#include "searpc-utils.h"
|
||||
|
||||
#include "seafile-session.h"
|
||||
#include "commit-mgr.h"
|
||||
#include "seaf-utils.h"
|
||||
|
||||
#define MAX_TIME_SKEW 259200 /* 3 days */
|
||||
|
||||
struct _SeafCommitManagerPriv {
|
||||
int dummy;
|
||||
};
|
||||
|
||||
static SeafCommit *
|
||||
load_commit (SeafCommitManager *mgr,
|
||||
const char *repo_id, int version,
|
||||
const char *commit_id);
|
||||
static int
|
||||
save_commit (SeafCommitManager *manager,
|
||||
const char *repo_id, int version,
|
||||
SeafCommit *commit);
|
||||
static void
|
||||
delete_commit (SeafCommitManager *mgr,
|
||||
const char *repo_id, int version,
|
||||
const char *id);
|
||||
static json_t *
|
||||
commit_to_json_object (SeafCommit *commit);
|
||||
static SeafCommit *
|
||||
commit_from_json_object (const char *id, json_t *object);
|
||||
|
||||
static void compute_commit_id (SeafCommit* commit)
|
||||
{
|
||||
SHA_CTX ctx;
|
||||
uint8_t sha1[20];
|
||||
gint64 ctime_n;
|
||||
|
||||
SHA1_Init (&ctx);
|
||||
SHA1_Update (&ctx, commit->root_id, 41);
|
||||
SHA1_Update (&ctx, commit->creator_id, 41);
|
||||
if (commit->creator_name)
|
||||
SHA1_Update (&ctx, commit->creator_name, strlen(commit->creator_name)+1);
|
||||
SHA1_Update (&ctx, commit->desc, strlen(commit->desc)+1);
|
||||
|
||||
/* convert to network byte order */
|
||||
ctime_n = hton64 (commit->ctime);
|
||||
SHA1_Update (&ctx, &ctime_n, sizeof(ctime_n));
|
||||
SHA1_Final (sha1, &ctx);
|
||||
|
||||
rawdata_to_hex (sha1, commit->commit_id, 20);
|
||||
}
|
||||
|
||||
SeafCommit*
|
||||
seaf_commit_new (const char *commit_id,
|
||||
const char *repo_id,
|
||||
const char *root_id,
|
||||
const char *creator_name,
|
||||
const char *creator_id,
|
||||
const char *desc,
|
||||
guint64 ctime)
|
||||
{
|
||||
SeafCommit *commit;
|
||||
|
||||
g_return_val_if_fail (repo_id != NULL, NULL);
|
||||
g_return_val_if_fail (root_id != NULL && creator_id != NULL, NULL);
|
||||
|
||||
commit = g_new0 (SeafCommit, 1);
|
||||
|
||||
memcpy (commit->repo_id, repo_id, 36);
|
||||
commit->repo_id[36] = '\0';
|
||||
|
||||
memcpy (commit->root_id, root_id, 40);
|
||||
commit->root_id[40] = '\0';
|
||||
|
||||
commit->creator_name = g_strdup (creator_name);
|
||||
|
||||
memcpy (commit->creator_id, creator_id, 40);
|
||||
commit->creator_id[40] = '\0';
|
||||
|
||||
commit->desc = g_strdup (desc);
|
||||
|
||||
if (ctime == 0) {
|
||||
/* TODO: use more precise timer */
|
||||
commit->ctime = (gint64)time(NULL);
|
||||
} else
|
||||
commit->ctime = ctime;
|
||||
|
||||
if (commit_id == NULL)
|
||||
compute_commit_id (commit);
|
||||
else {
|
||||
memcpy (commit->commit_id, commit_id, 40);
|
||||
commit->commit_id[40] = '\0';
|
||||
}
|
||||
|
||||
commit->ref = 1;
|
||||
return commit;
|
||||
}
|
||||
|
||||
char *
|
||||
seaf_commit_to_data (SeafCommit *commit, gsize *len)
|
||||
{
|
||||
json_t *object;
|
||||
char *json_data;
|
||||
char *ret;
|
||||
|
||||
object = commit_to_json_object (commit);
|
||||
|
||||
json_data = json_dumps (object, 0);
|
||||
*len = strlen (json_data);
|
||||
json_decref (object);
|
||||
|
||||
ret = g_strdup (json_data);
|
||||
free (json_data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
SeafCommit *
|
||||
seaf_commit_from_data (const char *id, char *data, gsize len)
|
||||
{
|
||||
json_t *object;
|
||||
SeafCommit *commit;
|
||||
json_error_t jerror;
|
||||
|
||||
object = json_loadb (data, len, 0, &jerror);
|
||||
if (!object) {
|
||||
/* Perhaps the commit object contains invalid UTF-8 character. */
|
||||
if (data[len-1] == 0)
|
||||
clean_utf8_data (data, len - 1);
|
||||
else
|
||||
clean_utf8_data (data, len);
|
||||
|
||||
object = json_loadb (data, len, 0, &jerror);
|
||||
if (!object) {
|
||||
if (jerror.text)
|
||||
seaf_warning ("Failed to load commit json: %s.\n", jerror.text);
|
||||
else
|
||||
seaf_warning ("Failed to load commit json.\n");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
commit = commit_from_json_object (id, object);
|
||||
|
||||
json_decref (object);
|
||||
|
||||
return commit;
|
||||
}
|
||||
|
||||
static void
|
||||
seaf_commit_free (SeafCommit *commit)
|
||||
{
|
||||
g_free (commit->desc);
|
||||
g_free (commit->creator_name);
|
||||
if (commit->parent_id) g_free (commit->parent_id);
|
||||
if (commit->second_parent_id) g_free (commit->second_parent_id);
|
||||
if (commit->repo_name) g_free (commit->repo_name);
|
||||
if (commit->repo_desc) g_free (commit->repo_desc);
|
||||
if (commit->device_name) g_free (commit->device_name);
|
||||
g_free (commit->magic);
|
||||
g_free (commit->random_key);
|
||||
g_free (commit);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_commit_ref (SeafCommit *commit)
|
||||
{
|
||||
commit->ref++;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_commit_unref (SeafCommit *commit)
|
||||
{
|
||||
if (!commit)
|
||||
return;
|
||||
|
||||
if (--commit->ref <= 0)
|
||||
seaf_commit_free (commit);
|
||||
}
|
||||
|
||||
SeafCommitManager*
|
||||
seaf_commit_manager_new (SeafileSession *seaf)
|
||||
{
|
||||
SeafCommitManager *mgr = g_new0 (SeafCommitManager, 1);
|
||||
|
||||
mgr->priv = g_new0 (SeafCommitManagerPriv, 1);
|
||||
mgr->seaf = seaf;
|
||||
mgr->obj_store = seaf_obj_store_new (mgr->seaf, "commits");
|
||||
|
||||
return mgr;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_commit_manager_init (SeafCommitManager *mgr)
|
||||
{
|
||||
#ifdef SEAFILE_SERVER
|
||||
|
||||
#ifdef FULL_FEATURE
|
||||
if (seaf_obj_store_init (mgr->obj_store, TRUE, seaf->ev_mgr) < 0) {
|
||||
seaf_warning ("[commit mgr] Failed to init commit object store.\n");
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
if (seaf_obj_store_init (mgr->obj_store, FALSE, NULL) < 0) {
|
||||
seaf_warning ("[commit mgr] Failed to init commit object store.\n");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#else
|
||||
if (seaf_obj_store_init (mgr->obj_store, TRUE, seaf->ev_mgr) < 0) {
|
||||
seaf_warning ("[commit mgr] Failed to init commit object store.\n");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
inline static void
|
||||
add_commit_to_cache (SeafCommitManager *mgr, SeafCommit *commit)
|
||||
{
|
||||
g_hash_table_insert (mgr->priv->commit_cache,
|
||||
g_strdup(commit->commit_id),
|
||||
commit);
|
||||
seaf_commit_ref (commit);
|
||||
}
|
||||
|
||||
inline static void
|
||||
remove_commit_from_cache (SeafCommitManager *mgr, SeafCommit *commit)
|
||||
{
|
||||
g_hash_table_remove (mgr->priv->commit_cache, commit->commit_id);
|
||||
seaf_commit_unref (commit);
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
seaf_commit_manager_add_commit (SeafCommitManager *mgr,
|
||||
SeafCommit *commit)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* add_commit_to_cache (mgr, commit); */
|
||||
if ((ret = save_commit (mgr, commit->repo_id, commit->version, commit)) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_commit_manager_del_commit (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *id)
|
||||
{
|
||||
g_return_if_fail (id != NULL);
|
||||
|
||||
#if 0
|
||||
commit = g_hash_table_lookup(mgr->priv->commit_cache, id);
|
||||
if (!commit)
|
||||
goto delete;
|
||||
|
||||
/*
|
||||
* Catch ref count bug here. We have bug in commit ref, the
|
||||
* following assert can't pass. TODO: fix the commit ref bug
|
||||
*/
|
||||
/* g_assert (commit->ref <= 1); */
|
||||
remove_commit_from_cache (mgr, commit);
|
||||
|
||||
delete:
|
||||
#endif
|
||||
|
||||
delete_commit (mgr, repo_id, version, id);
|
||||
}
|
||||
|
||||
SeafCommit*
|
||||
seaf_commit_manager_get_commit (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *id)
|
||||
{
|
||||
SeafCommit *commit;
|
||||
|
||||
#if 0
|
||||
commit = g_hash_table_lookup (mgr->priv->commit_cache, id);
|
||||
if (commit != NULL) {
|
||||
seaf_commit_ref (commit);
|
||||
return commit;
|
||||
}
|
||||
#endif
|
||||
|
||||
commit = load_commit (mgr, repo_id, version, id);
|
||||
if (!commit)
|
||||
return NULL;
|
||||
|
||||
/* add_commit_to_cache (mgr, commit); */
|
||||
|
||||
return commit;
|
||||
}
|
||||
|
||||
SeafCommit *
|
||||
seaf_commit_manager_get_commit_compatible (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *id)
|
||||
{
|
||||
SeafCommit *commit = NULL;
|
||||
|
||||
/* First try version 1 layout. */
|
||||
commit = seaf_commit_manager_get_commit (mgr, repo_id, 1, id);
|
||||
if (commit)
|
||||
return commit;
|
||||
|
||||
#if defined MIGRATION || defined SEAFILE_CLIENT
|
||||
/* For compatibility with version 0. */
|
||||
commit = seaf_commit_manager_get_commit (mgr, repo_id, 0, id);
|
||||
#endif
|
||||
return commit;
|
||||
}
|
||||
|
||||
static gint
|
||||
compare_commit_by_time (gconstpointer a, gconstpointer b, gpointer unused)
|
||||
{
|
||||
const SeafCommit *commit_a = a;
|
||||
const SeafCommit *commit_b = b;
|
||||
|
||||
/* Latest commit comes first in the list. */
|
||||
return (commit_b->ctime - commit_a->ctime);
|
||||
}
|
||||
|
||||
inline static int
|
||||
insert_parent_commit (GList **list, GHashTable *hash,
|
||||
const char *repo_id, int version,
|
||||
const char *parent_id, gboolean allow_truncate)
|
||||
{
|
||||
SeafCommit *p;
|
||||
char *key;
|
||||
|
||||
if (g_hash_table_lookup (hash, parent_id) != NULL)
|
||||
return 0;
|
||||
|
||||
p = seaf_commit_manager_get_commit (seaf->commit_mgr,
|
||||
repo_id, version,
|
||||
parent_id);
|
||||
if (!p) {
|
||||
if (allow_truncate)
|
||||
return 0;
|
||||
seaf_warning ("Failed to find commit %s\n", parent_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*list = g_list_insert_sorted_with_data (*list, p,
|
||||
compare_commit_by_time,
|
||||
NULL);
|
||||
|
||||
key = g_strdup (parent_id);
|
||||
g_hash_table_replace (hash, key, key);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_commit_manager_traverse_commit_tree_with_limit (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *head,
|
||||
CommitTraverseFunc func,
|
||||
int limit,
|
||||
void *data,
|
||||
gboolean skip_errors)
|
||||
{
|
||||
SeafCommit *commit;
|
||||
GList *list = NULL;
|
||||
GHashTable *commit_hash;
|
||||
gboolean ret = TRUE;
|
||||
|
||||
/* A hash table for recording id of traversed commits. */
|
||||
commit_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
|
||||
|
||||
commit = seaf_commit_manager_get_commit (mgr, repo_id, version, head);
|
||||
if (!commit) {
|
||||
seaf_warning ("Failed to find commit %s.\n", head);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
list = g_list_insert_sorted_with_data (list, commit,
|
||||
compare_commit_by_time,
|
||||
NULL);
|
||||
|
||||
char *key = g_strdup (commit->commit_id);
|
||||
g_hash_table_replace (commit_hash, key, key);
|
||||
|
||||
int count = 0;
|
||||
while (list) {
|
||||
gboolean stop = FALSE;
|
||||
commit = list->data;
|
||||
list = g_list_delete_link (list, list);
|
||||
|
||||
if (!func (commit, data, &stop)) {
|
||||
if (!skip_errors) {
|
||||
seaf_commit_unref (commit);
|
||||
ret = FALSE;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
/* Stop when limit is reached. If limit < 0, there is no limit; */
|
||||
if (limit > 0 && ++count == limit) {
|
||||
seaf_commit_unref (commit);
|
||||
break;
|
||||
}
|
||||
|
||||
if (stop) {
|
||||
seaf_commit_unref (commit);
|
||||
/* stop traverse down from this commit,
|
||||
* but not stop traversing the tree
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
|
||||
if (commit->parent_id) {
|
||||
if (insert_parent_commit (&list, commit_hash, repo_id, version,
|
||||
commit->parent_id, FALSE) < 0) {
|
||||
if (!skip_errors) {
|
||||
seaf_commit_unref (commit);
|
||||
ret = FALSE;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (commit->second_parent_id) {
|
||||
if (insert_parent_commit (&list, commit_hash, repo_id, version,
|
||||
commit->second_parent_id, FALSE) < 0) {
|
||||
if (!skip_errors) {
|
||||
seaf_commit_unref (commit);
|
||||
ret = FALSE;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
}
|
||||
seaf_commit_unref (commit);
|
||||
}
|
||||
|
||||
out:
|
||||
g_hash_table_destroy (commit_hash);
|
||||
while (list) {
|
||||
commit = list->data;
|
||||
seaf_commit_unref (commit);
|
||||
list = g_list_delete_link (list, list);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
traverse_commit_tree_common (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *head,
|
||||
CommitTraverseFunc func,
|
||||
void *data,
|
||||
gboolean skip_errors,
|
||||
gboolean allow_truncate)
|
||||
{
|
||||
SeafCommit *commit;
|
||||
GList *list = NULL;
|
||||
GHashTable *commit_hash;
|
||||
gboolean ret = TRUE;
|
||||
|
||||
commit = seaf_commit_manager_get_commit (mgr, repo_id, version, head);
|
||||
if (!commit) {
|
||||
seaf_warning ("Failed to find commit %s.\n", head);
|
||||
// For head commit damaged, directly return FALSE
|
||||
// user can repair head by fsck then retraverse the tree
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* A hash table for recording id of traversed commits. */
|
||||
commit_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
|
||||
|
||||
list = g_list_insert_sorted_with_data (list, commit,
|
||||
compare_commit_by_time,
|
||||
NULL);
|
||||
|
||||
char *key = g_strdup (commit->commit_id);
|
||||
g_hash_table_replace (commit_hash, key, key);
|
||||
|
||||
while (list) {
|
||||
gboolean stop = FALSE;
|
||||
commit = list->data;
|
||||
list = g_list_delete_link (list, list);
|
||||
|
||||
if (!func (commit, data, &stop)) {
|
||||
seaf_warning("[comit-mgr] CommitTraverseFunc failed\n");
|
||||
|
||||
/* If skip errors, continue to traverse parents. */
|
||||
if (!skip_errors) {
|
||||
seaf_commit_unref (commit);
|
||||
ret = FALSE;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
if (stop) {
|
||||
seaf_commit_unref (commit);
|
||||
/* stop traverse down from this commit,
|
||||
* but not stop traversing the tree
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
|
||||
if (commit->parent_id) {
|
||||
if (insert_parent_commit (&list, commit_hash, repo_id, version,
|
||||
commit->parent_id, allow_truncate) < 0) {
|
||||
seaf_warning("[comit-mgr] insert parent commit failed\n");
|
||||
|
||||
/* If skip errors, try insert second parent. */
|
||||
if (!skip_errors) {
|
||||
seaf_commit_unref (commit);
|
||||
ret = FALSE;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (commit->second_parent_id) {
|
||||
if (insert_parent_commit (&list, commit_hash, repo_id, version,
|
||||
commit->second_parent_id, allow_truncate) < 0) {
|
||||
seaf_warning("[comit-mgr]insert second parent commit failed\n");
|
||||
|
||||
if (!skip_errors) {
|
||||
seaf_commit_unref (commit);
|
||||
ret = FALSE;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
}
|
||||
seaf_commit_unref (commit);
|
||||
}
|
||||
|
||||
out:
|
||||
g_hash_table_destroy (commit_hash);
|
||||
while (list) {
|
||||
commit = list->data;
|
||||
seaf_commit_unref (commit);
|
||||
list = g_list_delete_link (list, list);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_commit_manager_traverse_commit_tree (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *head,
|
||||
CommitTraverseFunc func,
|
||||
void *data,
|
||||
gboolean skip_errors)
|
||||
{
|
||||
return traverse_commit_tree_common (mgr, repo_id, version, head,
|
||||
func, data, skip_errors, FALSE);
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_commit_manager_traverse_commit_tree_truncated (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *head,
|
||||
CommitTraverseFunc func,
|
||||
void *data,
|
||||
gboolean skip_errors)
|
||||
{
|
||||
return traverse_commit_tree_common (mgr, repo_id, version, head,
|
||||
func, data, skip_errors, TRUE);
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_commit_manager_commit_exists (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *id)
|
||||
{
|
||||
#if 0
|
||||
commit = g_hash_table_lookup (mgr->priv->commit_cache, id);
|
||||
if (commit != NULL)
|
||||
return TRUE;
|
||||
#endif
|
||||
|
||||
return seaf_obj_store_obj_exists (mgr->obj_store, repo_id, version, id);
|
||||
}
|
||||
|
||||
static json_t *
|
||||
commit_to_json_object (SeafCommit *commit)
|
||||
{
|
||||
json_t *object;
|
||||
|
||||
object = json_object ();
|
||||
|
||||
json_object_set_string_member (object, "commit_id", commit->commit_id);
|
||||
json_object_set_string_member (object, "root_id", commit->root_id);
|
||||
json_object_set_string_member (object, "repo_id", commit->repo_id);
|
||||
if (commit->creator_name)
|
||||
json_object_set_string_member (object, "creator_name", commit->creator_name);
|
||||
json_object_set_string_member (object, "creator", commit->creator_id);
|
||||
json_object_set_string_member (object, "description", commit->desc);
|
||||
json_object_set_int_member (object, "ctime", (gint64)commit->ctime);
|
||||
json_object_set_string_or_null_member (object, "parent_id", commit->parent_id);
|
||||
json_object_set_string_or_null_member (object, "second_parent_id",
|
||||
commit->second_parent_id);
|
||||
/*
|
||||
* also save repo's properties to commit file, for easy sharing of
|
||||
* repo info
|
||||
*/
|
||||
json_object_set_string_member (object, "repo_name", commit->repo_name);
|
||||
json_object_set_string_member (object, "repo_desc",
|
||||
commit->repo_desc);
|
||||
json_object_set_string_or_null_member (object, "repo_category",
|
||||
commit->repo_category);
|
||||
if (commit->device_name)
|
||||
json_object_set_string_member (object, "device_name", commit->device_name);
|
||||
|
||||
if (commit->encrypted)
|
||||
json_object_set_string_member (object, "encrypted", "true");
|
||||
|
||||
if (commit->encrypted) {
|
||||
json_object_set_int_member (object, "enc_version", commit->enc_version);
|
||||
if (commit->enc_version >= 1)
|
||||
json_object_set_string_member (object, "magic", commit->magic);
|
||||
if (commit->enc_version == 2)
|
||||
json_object_set_string_member (object, "key", commit->random_key);
|
||||
}
|
||||
if (commit->no_local_history)
|
||||
json_object_set_int_member (object, "no_local_history", 1);
|
||||
if (commit->version != 0)
|
||||
json_object_set_int_member (object, "version", commit->version);
|
||||
if (commit->conflict)
|
||||
json_object_set_int_member (object, "conflict", 1);
|
||||
if (commit->new_merge)
|
||||
json_object_set_int_member (object, "new_merge", 1);
|
||||
if (commit->repaired)
|
||||
json_object_set_int_member (object, "repaired", 1);
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
static SeafCommit *
|
||||
commit_from_json_object (const char *commit_id, json_t *object)
|
||||
{
|
||||
SeafCommit *commit = NULL;
|
||||
const char *root_id;
|
||||
const char *repo_id;
|
||||
const char *creator_name = NULL;
|
||||
const char *creator;
|
||||
const char *desc;
|
||||
gint64 ctime;
|
||||
const char *parent_id, *second_parent_id;
|
||||
const char *repo_name;
|
||||
const char *repo_desc;
|
||||
const char *repo_category;
|
||||
const char *device_name;
|
||||
const char *encrypted = NULL;
|
||||
int enc_version = 0;
|
||||
const char *magic = NULL;
|
||||
const char *random_key = NULL;
|
||||
int no_local_history = 0;
|
||||
int version = 0;
|
||||
int conflict = 0, new_merge = 0;
|
||||
int repaired = 0;
|
||||
|
||||
root_id = json_object_get_string_member (object, "root_id");
|
||||
repo_id = json_object_get_string_member (object, "repo_id");
|
||||
if (json_object_has_member (object, "creator_name"))
|
||||
creator_name = json_object_get_string_or_null_member (object, "creator_name");
|
||||
creator = json_object_get_string_member (object, "creator");
|
||||
desc = json_object_get_string_member (object, "description");
|
||||
if (!desc)
|
||||
desc = "";
|
||||
ctime = (guint64) json_object_get_int_member (object, "ctime");
|
||||
parent_id = json_object_get_string_or_null_member (object, "parent_id");
|
||||
second_parent_id = json_object_get_string_or_null_member (object, "second_parent_id");
|
||||
|
||||
repo_name = json_object_get_string_member (object, "repo_name");
|
||||
if (!repo_name)
|
||||
repo_name = "";
|
||||
repo_desc = json_object_get_string_member (object, "repo_desc");
|
||||
if (!repo_desc)
|
||||
repo_desc = "";
|
||||
repo_category = json_object_get_string_or_null_member (object, "repo_category");
|
||||
device_name = json_object_get_string_or_null_member (object, "device_name");
|
||||
|
||||
if (json_object_has_member (object, "encrypted"))
|
||||
encrypted = json_object_get_string_or_null_member (object, "encrypted");
|
||||
|
||||
if (encrypted && strcmp(encrypted, "true") == 0
|
||||
&& json_object_has_member (object, "enc_version")) {
|
||||
enc_version = json_object_get_int_member (object, "enc_version");
|
||||
magic = json_object_get_string_member (object, "magic");
|
||||
}
|
||||
|
||||
if (enc_version == 2)
|
||||
random_key = json_object_get_string_member (object, "key");
|
||||
|
||||
if (json_object_has_member (object, "no_local_history"))
|
||||
no_local_history = json_object_get_int_member (object, "no_local_history");
|
||||
|
||||
if (json_object_has_member (object, "version"))
|
||||
version = json_object_get_int_member (object, "version");
|
||||
if (json_object_has_member (object, "new_merge"))
|
||||
new_merge = json_object_get_int_member (object, "new_merge");
|
||||
|
||||
if (json_object_has_member (object, "conflict"))
|
||||
conflict = json_object_get_int_member (object, "conflict");
|
||||
|
||||
if (json_object_has_member (object, "repaired"))
|
||||
repaired = json_object_get_int_member (object, "repaired");
|
||||
|
||||
|
||||
/* sanity check for incoming values. */
|
||||
if (!repo_id || !is_uuid_valid(repo_id) ||
|
||||
!root_id || !is_object_id_valid(root_id) ||
|
||||
!creator || strlen(creator) != 40 ||
|
||||
(parent_id && !is_object_id_valid(parent_id)) ||
|
||||
(second_parent_id && !is_object_id_valid(second_parent_id)))
|
||||
return commit;
|
||||
|
||||
switch (enc_version) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
if (!magic || strlen(magic) != 32)
|
||||
return NULL;
|
||||
break;
|
||||
case 2:
|
||||
if (!magic || strlen(magic) != 64)
|
||||
return NULL;
|
||||
if (!random_key || strlen(random_key) != 96)
|
||||
return NULL;
|
||||
break;
|
||||
default:
|
||||
seaf_warning ("Unknown encryption version %d.\n", enc_version);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *creator_name_l = creator_name ? g_ascii_strdown (creator_name, -1) : NULL;
|
||||
commit = seaf_commit_new (commit_id, repo_id, root_id,
|
||||
creator_name_l, creator, desc, ctime);
|
||||
g_free (creator_name_l);
|
||||
|
||||
commit->parent_id = parent_id ? g_strdup(parent_id) : NULL;
|
||||
commit->second_parent_id = second_parent_id ? g_strdup(second_parent_id) : NULL;
|
||||
|
||||
commit->repo_name = g_strdup(repo_name);
|
||||
commit->repo_desc = g_strdup(repo_desc);
|
||||
if (encrypted && strcmp(encrypted, "true") == 0)
|
||||
commit->encrypted = TRUE;
|
||||
else
|
||||
commit->encrypted = FALSE;
|
||||
if (repo_category)
|
||||
commit->repo_category = g_strdup(repo_category);
|
||||
commit->device_name = g_strdup(device_name);
|
||||
|
||||
if (commit->encrypted) {
|
||||
commit->enc_version = enc_version;
|
||||
if (enc_version >= 1)
|
||||
commit->magic = g_strdup(magic);
|
||||
if (enc_version == 2)
|
||||
commit->random_key = g_strdup (random_key);
|
||||
}
|
||||
if (no_local_history)
|
||||
commit->no_local_history = TRUE;
|
||||
commit->version = version;
|
||||
if (new_merge)
|
||||
commit->new_merge = TRUE;
|
||||
if (conflict)
|
||||
commit->conflict = TRUE;
|
||||
if (repaired)
|
||||
commit->repaired = TRUE;
|
||||
|
||||
return commit;
|
||||
}
|
||||
|
||||
static SeafCommit *
|
||||
load_commit (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *commit_id)
|
||||
{
|
||||
char *data = NULL;
|
||||
int len;
|
||||
SeafCommit *commit = NULL;
|
||||
json_t *object = NULL;
|
||||
json_error_t jerror;
|
||||
|
||||
if (!commit_id || strlen(commit_id) != 40)
|
||||
return NULL;
|
||||
|
||||
if (seaf_obj_store_read_obj (mgr->obj_store, repo_id, version,
|
||||
commit_id, (void **)&data, &len) < 0)
|
||||
return NULL;
|
||||
|
||||
object = json_loadb (data, len, 0, &jerror);
|
||||
if (!object) {
|
||||
/* Perhaps the commit object contains invalid UTF-8 character. */
|
||||
if (data[len-1] == 0)
|
||||
clean_utf8_data (data, len - 1);
|
||||
else
|
||||
clean_utf8_data (data, len);
|
||||
|
||||
object = json_loadb (data, len, 0, &jerror);
|
||||
if (!object) {
|
||||
if (jerror.text)
|
||||
seaf_warning ("Failed to load commit json object: %s.\n", jerror.text);
|
||||
else
|
||||
seaf_warning ("Failed to load commit json object.\n");
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
commit = commit_from_json_object (commit_id, object);
|
||||
if (commit)
|
||||
commit->manager = mgr;
|
||||
|
||||
out:
|
||||
if (object) json_decref (object);
|
||||
g_free (data);
|
||||
|
||||
return commit;
|
||||
}
|
||||
|
||||
static int
|
||||
save_commit (SeafCommitManager *manager,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
SeafCommit *commit)
|
||||
{
|
||||
json_t *object = NULL;
|
||||
char *data;
|
||||
gsize len;
|
||||
|
||||
object = commit_to_json_object (commit);
|
||||
|
||||
data = json_dumps (object, 0);
|
||||
len = strlen (data);
|
||||
|
||||
json_decref (object);
|
||||
|
||||
#ifdef SEAFILE_SERVER
|
||||
if (seaf_obj_store_write_obj (manager->obj_store,
|
||||
repo_id, version,
|
||||
commit->commit_id,
|
||||
data, (int)len, TRUE) < 0) {
|
||||
g_free (data);
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
if (seaf_obj_store_write_obj (manager->obj_store,
|
||||
repo_id, version,
|
||||
commit->commit_id,
|
||||
data, (int)len, FALSE) < 0) {
|
||||
g_free (data);
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
free (data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
delete_commit (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *id)
|
||||
{
|
||||
seaf_obj_store_delete_obj (mgr->obj_store, repo_id, version, id);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_commit_manager_remove_store (SeafCommitManager *mgr,
|
||||
const char *store_id)
|
||||
{
|
||||
return seaf_obj_store_remove_store (mgr->obj_store, store_id);
|
||||
}
|
194
common/commit-mgr.h
Normal file
194
common/commit-mgr.h
Normal file
|
@ -0,0 +1,194 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef SEAF_COMMIT_MGR_H
|
||||
#define SEAF_COMMIT_MGR_H
|
||||
|
||||
struct _SeafCommitManager;
|
||||
typedef struct _SeafCommit SeafCommit;
|
||||
|
||||
#include <glib/gstdio.h>
|
||||
#include "db.h"
|
||||
|
||||
#include "obj-store.h"
|
||||
|
||||
struct _SeafCommit {
|
||||
struct _SeafCommitManager *manager;
|
||||
|
||||
int ref;
|
||||
|
||||
char commit_id[41];
|
||||
char repo_id[37];
|
||||
char root_id[41]; /* the fs root */
|
||||
char *desc;
|
||||
char *creator_name;
|
||||
char creator_id[41];
|
||||
guint64 ctime; /* creation time */
|
||||
char *parent_id;
|
||||
char *second_parent_id;
|
||||
char *repo_name;
|
||||
char *repo_desc;
|
||||
char *repo_category;
|
||||
char *device_name;
|
||||
|
||||
gboolean encrypted;
|
||||
int enc_version;
|
||||
char *magic;
|
||||
char *random_key;
|
||||
gboolean no_local_history;
|
||||
|
||||
int version;
|
||||
gboolean new_merge;
|
||||
gboolean conflict;
|
||||
gboolean repaired;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @commit_id: if this is NULL, will create a new id.
|
||||
* @ctime: if this is 0, will use current time.
|
||||
*
|
||||
* Any new commit should be added to commit manager before used.
|
||||
*/
|
||||
SeafCommit *
|
||||
seaf_commit_new (const char *commit_id,
|
||||
const char *repo_id,
|
||||
const char *root_id,
|
||||
const char *author_name,
|
||||
const char *creator_id,
|
||||
const char *desc,
|
||||
guint64 ctime);
|
||||
|
||||
char *
|
||||
seaf_commit_to_data (SeafCommit *commit, gsize *len);
|
||||
|
||||
SeafCommit *
|
||||
seaf_commit_from_data (const char *id, char *data, gsize len);
|
||||
|
||||
void
|
||||
seaf_commit_ref (SeafCommit *commit);
|
||||
|
||||
void
|
||||
seaf_commit_unref (SeafCommit *commit);
|
||||
|
||||
/* Set stop to TRUE if you want to stop traversing a branch in the history graph.
|
||||
Note, if currently there are multi branches, this function will be called again.
|
||||
So, set stop to TRUE not always stop traversing the history graph.
|
||||
*/
|
||||
typedef gboolean (*CommitTraverseFunc) (SeafCommit *commit, void *data, gboolean *stop);
|
||||
|
||||
struct _SeafileSession;
|
||||
|
||||
typedef struct _SeafCommitManager SeafCommitManager;
|
||||
typedef struct _SeafCommitManagerPriv SeafCommitManagerPriv;
|
||||
|
||||
struct _SeafCommitManager {
|
||||
struct _SeafileSession *seaf;
|
||||
|
||||
sqlite3 *db;
|
||||
struct SeafObjStore *obj_store;
|
||||
|
||||
SeafCommitManagerPriv *priv;
|
||||
};
|
||||
|
||||
SeafCommitManager *
|
||||
seaf_commit_manager_new (struct _SeafileSession *seaf);
|
||||
|
||||
int
|
||||
seaf_commit_manager_init (SeafCommitManager *mgr);
|
||||
|
||||
/**
|
||||
* Add a commit to commit manager and persist it to disk.
|
||||
* Any new commit should be added to commit manager before used.
|
||||
* This function increments ref count of the commit object.
|
||||
* Not MT safe.
|
||||
*/
|
||||
int
|
||||
seaf_commit_manager_add_commit (SeafCommitManager *mgr, SeafCommit *commit);
|
||||
|
||||
/**
|
||||
* Delete a commit from commit manager and permanently remove it from disk.
|
||||
* A commit object to be deleted should have ref cournt <= 1.
|
||||
* Not MT safe.
|
||||
*/
|
||||
void
|
||||
seaf_commit_manager_del_commit (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *id);
|
||||
|
||||
/**
|
||||
* Find a commit object.
|
||||
* This function increments ref count of returned object.
|
||||
* Not MT safe.
|
||||
*/
|
||||
SeafCommit*
|
||||
seaf_commit_manager_get_commit (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *id);
|
||||
|
||||
/**
|
||||
* Get a commit object, with compatibility between version 0 and version 1.
|
||||
* It will first try to get commit with version 1 layout; if fails, will
|
||||
* try version 0 layout for compatibility.
|
||||
* This is useful for loading a repo. In that case, we don't know the version
|
||||
* of the repo before loading its head commit.
|
||||
*/
|
||||
SeafCommit *
|
||||
seaf_commit_manager_get_commit_compatible (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
const char *id);
|
||||
|
||||
/**
|
||||
* Traverse the commits DAG start from head in topological order.
|
||||
* The ordering is based on commit time.
|
||||
* return FALSE if some commits is missing, TRUE otherwise.
|
||||
*/
|
||||
gboolean
|
||||
seaf_commit_manager_traverse_commit_tree (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *head,
|
||||
CommitTraverseFunc func,
|
||||
void *data,
|
||||
gboolean skip_errors);
|
||||
|
||||
/*
|
||||
* The same as the above function, but stops traverse down if parent commit
|
||||
* doesn't exists, instead of returning error.
|
||||
*/
|
||||
gboolean
|
||||
seaf_commit_manager_traverse_commit_tree_truncated (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *head,
|
||||
CommitTraverseFunc func,
|
||||
void *data,
|
||||
gboolean skip_errors);
|
||||
|
||||
/**
|
||||
* Works the same as seaf_commit_manager_traverse_commit_tree, but stops
|
||||
* traversing when a total number of _limit_ commits is reached. If
|
||||
* limit <= 0, there is no limit
|
||||
*/
|
||||
gboolean
|
||||
seaf_commit_manager_traverse_commit_tree_with_limit (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *head,
|
||||
CommitTraverseFunc func,
|
||||
int limit,
|
||||
void *data,
|
||||
gboolean skip_errors);
|
||||
|
||||
gboolean
|
||||
seaf_commit_manager_commit_exists (SeafCommitManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *id);
|
||||
|
||||
int
|
||||
seaf_commit_manager_remove_store (SeafCommitManager *mgr,
|
||||
const char *store_id);
|
||||
|
||||
#endif
|
52
common/common.h
Normal file
52
common/common.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h> /* uint32_t */
|
||||
#include <sys/types.h> /* size_t */
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <utime.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#define EMPTY_SHA1 "0000000000000000000000000000000000000000"
|
||||
|
||||
#define CURRENT_ENC_VERSION 2
|
||||
|
||||
#define DEFAULT_PROTO_VERSION 1
|
||||
#define CURRENT_PROTO_VERSION 7
|
||||
|
||||
#define CURRENT_REPO_VERSION 1
|
||||
|
||||
/* For compatibility with the old protocol, use an UUID for signature.
|
||||
* Listen manager on the server will use the new block tx protocol if it
|
||||
* receives this signature as "token".
|
||||
*/
|
||||
#define BLOCK_PROTOCOL_SIGNATURE "529319a0-577f-4d6b-a6c3-3c20f56f290c"
|
||||
|
||||
#define SEAF_PATH_MAX 4096
|
||||
|
||||
#ifndef ccnet_warning
|
||||
#define ccnet_warning(fmt, ...) g_warning("%s(%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#ifndef ccnet_error
|
||||
#define ccnet_error(fmt, ...) g_error("%s(%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#ifndef ccnet_message
|
||||
#define ccnet_message(fmt, ...) g_message("%s(%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#endif
|
11
common/db-wrapper/Makefile.am
Normal file
11
common/db-wrapper/Makefile.am
Normal file
|
@ -0,0 +1,11 @@
|
|||
AM_CFLAGS = -I$(top_srcdir)/common -I$(top_srcdir)/lib \
|
||||
-Wall @GLIB2_CFLAGS@ @MSVC_CFLAGS@ @MYSQL_CFLAGS@ @PGSQL_CFLAGS@
|
||||
|
||||
noinst_LTLIBRARIES = libdbwrapper.la
|
||||
|
||||
noinst_HEADERS = db-wrapper.h mysql-db-ops.h sqlite-db-ops.h pgsql-db-ops.h
|
||||
|
||||
libdbwrapper_la_SOURCES = db-wrapper.c mysql-db-ops.c sqlite-db-ops.c pgsql-db-ops.c
|
||||
|
||||
libdbwrapper_la_LDFLAGS = -Wl,-z -Wl,defs
|
||||
libdbwrapper_la_LIBADD = @SSL_LIBS@ @GLIB2_LIBS@ @MYSQL_LIBS@ -lsqlite3 @PGSQL_LIBS@
|
359
common/db-wrapper/db-wrapper.c
Normal file
359
common/db-wrapper/db-wrapper.c
Normal file
|
@ -0,0 +1,359 @@
|
|||
#include "common.h"
|
||||
|
||||
#include "db-wrapper.h"
|
||||
#include "mysql-db-ops.h"
|
||||
#include "sqlite-db-ops.h"
|
||||
#include "pgsql-db-ops.h"
|
||||
|
||||
typedef struct DBOperations {
|
||||
void (*db_conn_pool_free) (DBConnPool *);
|
||||
DBConnection* (*get_db_connection) (DBConnPool *, GError **);
|
||||
void (*db_connection_close) (DBConnection *);
|
||||
gboolean (*db_connection_execute) (DBConnection *, const char *, GError **);
|
||||
ResultSet* (*db_connection_execute_query) (DBConnection *, const char *, GError **);
|
||||
gboolean (*result_set_next) (ResultSet *, GError **);
|
||||
const char* (*result_set_get_string) (ResultSet *, int, GError **);
|
||||
void (*result_set_free) (ResultSet *);
|
||||
int (*result_set_get_column_count) (ResultSet *);
|
||||
DBStmt* (*db_connection_prepare_statement) (DBConnection *, const char *, GError **);
|
||||
gboolean (*db_stmt_set_int) (DBStmt *, int, int, GError **);
|
||||
gboolean (*db_stmt_set_int64) (DBStmt *, int, gint64, GError **);
|
||||
gboolean (*db_stmt_set_string) (DBStmt *, int, const char *, GError **);
|
||||
gboolean (*db_stmt_execute) (DBStmt *, GError **);
|
||||
ResultSet* (*db_stmt_execute_query) (DBStmt *, GError **);
|
||||
void (*db_stmt_free) (DBStmt *);
|
||||
gboolean (*db_connection_begin_transaction) (DBConnection *, GError **);
|
||||
gboolean (*db_connection_commit) (DBConnection *, GError **);
|
||||
gboolean (*db_connection_rollback) (DBConnection *, GError **);
|
||||
} DBOperations;
|
||||
|
||||
static DBOperations db_ops;
|
||||
|
||||
/* DB Connection Pool. */
|
||||
|
||||
DBConnPool *
|
||||
db_conn_pool_new_mysql (const char *host,
|
||||
const char *user,
|
||||
const char *password,
|
||||
unsigned int port,
|
||||
const char *db_name,
|
||||
const char *unix_socket,
|
||||
gboolean use_ssl,
|
||||
const char *charset,
|
||||
int max_connections)
|
||||
{
|
||||
db_ops.db_conn_pool_free = mysql_db_conn_pool_free;
|
||||
db_ops.get_db_connection = mysql_get_db_connection;
|
||||
db_ops.db_connection_close = mysql_db_connection_close;
|
||||
db_ops.db_connection_execute = mysql_db_connection_execute;
|
||||
db_ops.db_connection_execute_query = mysql_execute_query;
|
||||
db_ops.result_set_next = mysql_result_set_next;
|
||||
db_ops.result_set_get_string = mysql_result_set_get_string;
|
||||
db_ops.result_set_free = mysql_result_set_free;
|
||||
db_ops.result_set_get_column_count = mysql_result_set_get_column_count;
|
||||
db_ops.db_connection_prepare_statement = mysql_prepare_statement;
|
||||
db_ops.db_stmt_set_int = mysql_stmt_set_int;
|
||||
db_ops.db_stmt_set_int64 = mysql_stmt_set_int64;
|
||||
db_ops.db_stmt_set_string = mysql_stmt_set_string;
|
||||
db_ops.db_stmt_execute = mysql_db_stmt_execute;
|
||||
db_ops.db_stmt_execute_query = mysql_db_stmt_execute_query;
|
||||
db_ops.db_stmt_free = mysql_db_stmt_free;
|
||||
db_ops.db_connection_begin_transaction = mysql_db_begin_transaction;
|
||||
db_ops.db_connection_commit = mysql_db_commit;
|
||||
db_ops.db_connection_rollback = mysql_db_rollback;
|
||||
|
||||
return mysql_db_conn_pool_new (host, user, password, port, db_name, unix_socket,
|
||||
use_ssl, charset, max_connections);
|
||||
}
|
||||
|
||||
DBConnPool *
|
||||
db_conn_pool_new_pgsql (const char *host,
|
||||
const char *user,
|
||||
const char *password,
|
||||
const char *db_name,
|
||||
const char *unix_socket)
|
||||
{
|
||||
db_ops.db_conn_pool_free = pgsql_db_conn_pool_free;
|
||||
db_ops.get_db_connection = pgsql_get_db_connection;
|
||||
db_ops.db_connection_close = pgsql_db_connection_close;
|
||||
db_ops.db_connection_execute = pgsql_db_connection_execute;
|
||||
db_ops.db_connection_execute_query = pgsql_execute_query;
|
||||
db_ops.result_set_next = pgsql_result_set_next;
|
||||
db_ops.result_set_get_string = pgsql_result_set_get_string;
|
||||
db_ops.result_set_free = pgsql_result_set_free;
|
||||
db_ops.result_set_get_column_count = pgsql_result_set_get_column_count;
|
||||
db_ops.db_connection_prepare_statement = pgsql_prepare_statement;
|
||||
db_ops.db_stmt_set_int = pgsql_stmt_set_int;
|
||||
db_ops.db_stmt_set_int64 = pgsql_stmt_set_int64;
|
||||
db_ops.db_stmt_set_string = pgsql_stmt_set_string;
|
||||
db_ops.db_stmt_execute = pgsql_db_stmt_execute;
|
||||
db_ops.db_stmt_execute_query = pgsql_db_stmt_execute_query;
|
||||
db_ops.db_stmt_free = pgsql_db_stmt_free;
|
||||
db_ops.db_connection_begin_transaction = pgsql_db_begin_transaction;
|
||||
db_ops.db_connection_commit = pgsql_db_commit;
|
||||
db_ops.db_connection_rollback = pgsql_db_rollback;
|
||||
|
||||
return pgsql_db_conn_pool_new (host, user, password, db_name, unix_socket);
|
||||
}
|
||||
|
||||
DBConnPool *
|
||||
db_conn_pool_new_sqlite (const char *db_path, int max_connections)
|
||||
{
|
||||
db_ops.db_conn_pool_free = sqlite_db_conn_pool_free;
|
||||
db_ops.get_db_connection = sqlite_get_db_connection;
|
||||
db_ops.db_connection_close = sqlite_db_connection_close;
|
||||
db_ops.db_connection_execute = sqlite_db_connection_execute;
|
||||
db_ops.db_connection_execute_query = sqlite_execute_query;
|
||||
db_ops.result_set_next = sqlite_result_set_next;
|
||||
db_ops.result_set_get_string = sqlite_result_set_get_string;
|
||||
db_ops.result_set_free = sqlite_result_set_free;
|
||||
db_ops.result_set_get_column_count = sqlite_result_set_get_column_count;
|
||||
db_ops.db_connection_prepare_statement = sqlite_prepare_statement;
|
||||
db_ops.db_stmt_set_int = sqlite_stmt_set_int;
|
||||
db_ops.db_stmt_set_int64 = sqlite_stmt_set_int64;
|
||||
db_ops.db_stmt_set_string = sqlite_stmt_set_string;
|
||||
db_ops.db_stmt_execute = sqlite_db_stmt_execute;
|
||||
db_ops.db_stmt_execute_query = sqlite_db_stmt_execute_query;
|
||||
db_ops.db_stmt_free = sqlite_db_stmt_free;
|
||||
db_ops.db_connection_begin_transaction = sqlite_db_begin_transaction;
|
||||
db_ops.db_connection_commit = sqlite_db_commit;
|
||||
db_ops.db_connection_rollback = sqlite_db_rollback;
|
||||
|
||||
return sqlite_db_conn_pool_new (db_path, max_connections);
|
||||
}
|
||||
|
||||
void
|
||||
db_conn_pool_free (DBConnPool *pool)
|
||||
{
|
||||
return db_ops.db_conn_pool_free (pool);
|
||||
}
|
||||
|
||||
/* DB Connections. */
|
||||
|
||||
DBConnection *
|
||||
db_conn_pool_get_connection (DBConnPool *pool, GError **error)
|
||||
{
|
||||
return db_ops.get_db_connection (pool, error);
|
||||
}
|
||||
|
||||
static void
|
||||
db_connection_clear (DBConnection *conn)
|
||||
{
|
||||
result_set_free (conn->result_set);
|
||||
|
||||
db_stmt_free (conn->stmt);
|
||||
}
|
||||
|
||||
void
|
||||
db_connection_close (DBConnection *conn)
|
||||
{
|
||||
if (!conn)
|
||||
return;
|
||||
|
||||
db_connection_clear (conn);
|
||||
|
||||
db_ops.db_connection_close (conn);
|
||||
}
|
||||
|
||||
gboolean
|
||||
db_connection_execute (DBConnection *conn, const char *sql, GError **error)
|
||||
{
|
||||
return db_ops.db_connection_execute (conn, sql, error);
|
||||
}
|
||||
|
||||
/* Result Sets. */
|
||||
|
||||
void
|
||||
result_set_free (ResultSet *r)
|
||||
{
|
||||
if (!r)
|
||||
return;
|
||||
|
||||
return db_ops.result_set_free (r);
|
||||
}
|
||||
|
||||
ResultSet *
|
||||
db_connection_execute_query (DBConnection *conn, const char *sql, GError **error)
|
||||
{
|
||||
ResultSet *result_set;
|
||||
|
||||
if (conn->result_set) {
|
||||
result_set_free (conn->result_set);
|
||||
conn->result_set = NULL;
|
||||
}
|
||||
|
||||
result_set = db_ops.db_connection_execute_query (conn, sql, error);
|
||||
|
||||
if (result_set)
|
||||
conn->result_set = result_set;
|
||||
|
||||
return result_set;
|
||||
}
|
||||
|
||||
gboolean
|
||||
result_set_next (ResultSet *r, GError **error)
|
||||
{
|
||||
return db_ops.result_set_next (r, error);
|
||||
}
|
||||
|
||||
const char *
|
||||
result_set_get_string (ResultSet *r, int idx, GError **error)
|
||||
{
|
||||
return db_ops.result_set_get_string (r, idx, error);
|
||||
}
|
||||
|
||||
int
|
||||
result_set_get_int (ResultSet *r, int idx, GError **error)
|
||||
{
|
||||
const char *str;
|
||||
char *e;
|
||||
int ret;
|
||||
|
||||
str = db_ops.result_set_get_string (r, idx, error);
|
||||
if (*error) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!str) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
ret = strtol (str, &e, 10);
|
||||
if (errno || (e == str)) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Number conversion failed.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
gint64
|
||||
result_set_get_int64 (ResultSet *r, int idx, GError **error)
|
||||
{
|
||||
const char *str;
|
||||
char *e;
|
||||
gint64 ret;
|
||||
|
||||
str = db_ops.result_set_get_string (r, idx, error);
|
||||
if (*error) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!str) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
ret = strtoll (str, &e, 10);
|
||||
if (errno || (e == str)) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Number conversion failed.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
result_set_get_column_count (ResultSet *r)
|
||||
{
|
||||
return db_ops.result_set_get_column_count (r);
|
||||
}
|
||||
|
||||
/* Prepared Statements. */
|
||||
|
||||
DBStmt *
|
||||
db_connection_prepare_statement (DBConnection *conn, const char *sql, GError **error)
|
||||
{
|
||||
DBStmt *stmt;
|
||||
|
||||
if (conn->stmt) {
|
||||
db_stmt_free (conn->stmt);
|
||||
conn->stmt = NULL;
|
||||
}
|
||||
|
||||
stmt = db_ops.db_connection_prepare_statement (conn, sql, error);
|
||||
|
||||
if (stmt)
|
||||
conn->stmt = stmt;
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
int
|
||||
db_stmt_set_int (DBStmt *stmt, int idx, int x, GError **error)
|
||||
{
|
||||
return db_ops.db_stmt_set_int (stmt, idx, x, error);
|
||||
}
|
||||
|
||||
int
|
||||
db_stmt_set_int64 (DBStmt *stmt, int idx, gint64 x, GError **error)
|
||||
{
|
||||
return db_ops.db_stmt_set_int64 (stmt, idx, x, error);
|
||||
}
|
||||
|
||||
int
|
||||
db_stmt_set_string (DBStmt *stmt, int idx, const char *s, GError **error)
|
||||
{
|
||||
return db_ops.db_stmt_set_string (stmt, idx, s, error);
|
||||
}
|
||||
|
||||
gboolean
|
||||
db_stmt_execute (DBStmt *stmt, GError **error)
|
||||
{
|
||||
return db_ops.db_stmt_execute (stmt, error);
|
||||
}
|
||||
|
||||
ResultSet *
|
||||
db_stmt_execute_query (DBStmt *stmt, GError **error)
|
||||
{
|
||||
ResultSet *result_set;
|
||||
|
||||
if (stmt->result_set) {
|
||||
result_set_free (stmt->result_set);
|
||||
stmt->result_set = NULL;
|
||||
}
|
||||
|
||||
result_set = db_ops.db_stmt_execute_query (stmt, error);
|
||||
|
||||
if (result_set)
|
||||
stmt->result_set = result_set;
|
||||
|
||||
return result_set;
|
||||
}
|
||||
|
||||
void
|
||||
db_stmt_free (DBStmt *stmt)
|
||||
{
|
||||
if (!stmt)
|
||||
return;
|
||||
|
||||
if (stmt->result_set)
|
||||
result_set_free (stmt->result_set);
|
||||
|
||||
return db_ops.db_stmt_free (stmt);
|
||||
}
|
||||
|
||||
/* Transactions. */
|
||||
|
||||
gboolean
|
||||
db_connection_begin_transaction (DBConnection *conn, GError **error)
|
||||
{
|
||||
return db_ops.db_connection_begin_transaction (conn, error);
|
||||
}
|
||||
|
||||
gboolean
|
||||
db_connection_commit (DBConnection *conn, GError **error)
|
||||
{
|
||||
return db_ops.db_connection_commit (conn, error);
|
||||
}
|
||||
|
||||
gboolean
|
||||
db_connection_rollback (DBConnection *conn, GError **error)
|
||||
{
|
||||
db_connection_clear (conn);
|
||||
|
||||
return db_ops.db_connection_rollback (conn, error);
|
||||
}
|
129
common/db-wrapper/db-wrapper.h
Normal file
129
common/db-wrapper/db-wrapper.h
Normal file
|
@ -0,0 +1,129 @@
|
|||
#ifndef DB_WRAPPER_H
|
||||
#define DB_WARPPER_H
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#define SEAF_DB_ERROR_DOMAIN g_quark_from_string("SEAF_DB")
|
||||
#define SEAF_DB_ERROR_CODE 0
|
||||
|
||||
/* DB Connection Pool. */
|
||||
|
||||
struct DBConnPool {
|
||||
int max_connections;
|
||||
};
|
||||
typedef struct DBConnPool DBConnPool;
|
||||
|
||||
DBConnPool *
|
||||
db_conn_pool_new_mysql (const char *host,
|
||||
const char *user,
|
||||
const char *password,
|
||||
unsigned int port,
|
||||
const char *db_name,
|
||||
const char *unix_socket,
|
||||
gboolean use_ssl,
|
||||
const char *charset,
|
||||
int max_connections);
|
||||
|
||||
DBConnPool *
|
||||
db_conn_pool_new_pgsql (const char *host,
|
||||
const char *user,
|
||||
const char *password,
|
||||
const char *db_name,
|
||||
const char *unix_socket);
|
||||
|
||||
DBConnPool *
|
||||
db_conn_pool_new_sqlite (const char *db_path, int max_connections);
|
||||
|
||||
void
|
||||
db_conn_pool_free (DBConnPool *pool);
|
||||
|
||||
/* DB Connections. */
|
||||
|
||||
struct ResultSet;
|
||||
typedef struct ResultSet ResultSet;
|
||||
|
||||
struct DBStmt;
|
||||
typedef struct DBStmt DBStmt;
|
||||
|
||||
struct DBConnection {
|
||||
DBConnPool *pool;
|
||||
ResultSet *result_set;
|
||||
DBStmt *stmt;
|
||||
};
|
||||
typedef struct DBConnection DBConnection;
|
||||
|
||||
DBConnection *
|
||||
db_conn_pool_get_connection (DBConnPool *pool, GError **error);
|
||||
|
||||
void
|
||||
db_connection_close (DBConnection *conn);
|
||||
|
||||
gboolean
|
||||
db_connection_execute (DBConnection *conn, const char *sql, GError **error);
|
||||
|
||||
/* Result Sets. */
|
||||
|
||||
struct ResultSet {
|
||||
/* Empty */
|
||||
};
|
||||
|
||||
ResultSet *
|
||||
db_connection_execute_query (DBConnection *conn, const char *sql, GError **error);
|
||||
|
||||
gboolean
|
||||
result_set_next (ResultSet *r, GError **error);
|
||||
|
||||
const char *
|
||||
result_set_get_string (ResultSet *r, int idx, GError **error);
|
||||
|
||||
int
|
||||
result_set_get_int (ResultSet *r, int idx, GError **error);
|
||||
|
||||
gint64
|
||||
result_set_get_int64 (ResultSet *r, int idx, GError **error);
|
||||
|
||||
int
|
||||
result_set_get_column_count (ResultSet *r);
|
||||
|
||||
void
|
||||
result_set_free (ResultSet *r);
|
||||
|
||||
/* Prepared Statements. */
|
||||
|
||||
struct DBStmt {
|
||||
ResultSet *result_set;
|
||||
};
|
||||
|
||||
DBStmt *
|
||||
db_connection_prepare_statement (DBConnection *conn, const char *sql, GError **error);
|
||||
|
||||
gboolean
|
||||
db_stmt_set_int (DBStmt *stmt, int idx, int x, GError **error);
|
||||
|
||||
gboolean
|
||||
db_stmt_set_int64 (DBStmt *stmt, int idx, gint64 x, GError **error);
|
||||
|
||||
gboolean
|
||||
db_stmt_set_string (DBStmt *stmt, int idx, const char *s, GError **error);
|
||||
|
||||
gboolean
|
||||
db_stmt_execute (DBStmt *stmt, GError **error);
|
||||
|
||||
ResultSet *
|
||||
db_stmt_execute_query (DBStmt *stmt, GError **error);
|
||||
|
||||
void
|
||||
db_stmt_free (DBStmt *stmt);
|
||||
|
||||
/* Transactions. */
|
||||
|
||||
gboolean
|
||||
db_connection_begin_transaction (DBConnection *conn, GError **error);
|
||||
|
||||
gboolean
|
||||
db_connection_commit (DBConnection *conn, GError **error);
|
||||
|
||||
gboolean
|
||||
db_connection_rollback (DBConnection *conn, GError **error);
|
||||
|
||||
#endif
|
580
common/db-wrapper/mysql-db-ops.c
Normal file
580
common/db-wrapper/mysql-db-ops.c
Normal file
|
@ -0,0 +1,580 @@
|
|||
#include "common.h"
|
||||
|
||||
#include "db-wrapper.h"
|
||||
#include "mysql-db-ops.h"
|
||||
|
||||
#include <mysql.h>
|
||||
|
||||
/* Connection Pool. */
|
||||
|
||||
typedef struct MySQLDBConnPool {
|
||||
DBConnPool parent;
|
||||
char *host;
|
||||
char *user;
|
||||
char *password;
|
||||
unsigned int port;
|
||||
char *db_name;
|
||||
char *unix_socket;
|
||||
gboolean use_ssl;
|
||||
char *charset;
|
||||
} MySQLDBConnPool;
|
||||
|
||||
DBConnPool *
|
||||
mysql_db_conn_pool_new (const char *host,
|
||||
const char *user,
|
||||
const char *password,
|
||||
unsigned int port,
|
||||
const char *db_name,
|
||||
const char *unix_socket,
|
||||
gboolean use_ssl,
|
||||
const char *charset,
|
||||
int max_connections)
|
||||
{
|
||||
MySQLDBConnPool *pool = g_new0 (MySQLDBConnPool, 1);
|
||||
|
||||
pool->parent.max_connections = max_connections;
|
||||
pool->host = g_strdup (host);
|
||||
pool->user = g_strdup (user);
|
||||
pool->password = g_strdup (password);
|
||||
pool->port = port;
|
||||
pool->db_name = g_strdup(db_name);
|
||||
pool->unix_socket = g_strdup(unix_socket);
|
||||
pool->use_ssl = use_ssl;
|
||||
pool->charset = g_strdup(charset);
|
||||
|
||||
mysql_library_init (0, NULL, NULL);
|
||||
|
||||
return (DBConnPool *)pool;
|
||||
}
|
||||
|
||||
void
|
||||
mysql_db_conn_pool_free (DBConnPool *vpool)
|
||||
{
|
||||
MySQLDBConnPool *pool = (MySQLDBConnPool *)vpool;
|
||||
|
||||
g_free (pool->host);
|
||||
g_free (pool->user);
|
||||
g_free (pool->password);
|
||||
g_free (pool->db_name);
|
||||
g_free (pool->unix_socket);
|
||||
g_free (pool->charset);
|
||||
|
||||
g_free (pool);
|
||||
}
|
||||
|
||||
/* Connection. */
|
||||
|
||||
typedef struct MySQLDBConnection {
|
||||
DBConnection parent;
|
||||
MYSQL *db;
|
||||
} MySQLDBConnection;
|
||||
|
||||
#define SQL_DEFAULT_TCP_TIMEOUT 3
|
||||
|
||||
static MYSQL *
|
||||
connect_mysql (MySQLDBConnPool *pool, GError **error)
|
||||
{
|
||||
my_bool yes = 1;
|
||||
volatile int connect_timeout = SQL_DEFAULT_TCP_TIMEOUT;
|
||||
unsigned long client_flags = CLIENT_MULTI_STATEMENTS;
|
||||
MYSQL *db;
|
||||
|
||||
db = mysql_init (NULL);
|
||||
if (!db) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Failed to allocate mysql handle.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pool->use_ssl)
|
||||
mysql_ssl_set(db, 0,0,0,0,0);
|
||||
|
||||
if (pool->charset)
|
||||
mysql_options(db, MYSQL_SET_CHARSET_NAME, pool->charset);
|
||||
|
||||
mysql_options(db, MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&connect_timeout);
|
||||
mysql_options(db, MYSQL_OPT_RECONNECT, (const char*)&yes);
|
||||
|
||||
if (mysql_real_connect(db, pool->host, pool->user, pool->password,
|
||||
pool->db_name, pool->port,
|
||||
pool->unix_socket, client_flags)) {
|
||||
return db;
|
||||
} else {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Failed to connect to MySQL: %s", mysql_error(db));
|
||||
mysql_close (db);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
DBConnection *
|
||||
mysql_get_db_connection (DBConnPool *vpool, GError **error)
|
||||
{
|
||||
MySQLDBConnPool *pool = (MySQLDBConnPool *)vpool;
|
||||
MySQLDBConnection *conn;
|
||||
MYSQL *db = connect_mysql (pool, error);
|
||||
if (!db)
|
||||
return NULL;
|
||||
conn = g_new0 (MySQLDBConnection, 1);
|
||||
conn->db = db;
|
||||
conn->parent.pool = vpool;
|
||||
return (DBConnection *)conn;
|
||||
}
|
||||
|
||||
void
|
||||
mysql_db_connection_close (DBConnection *vconn)
|
||||
{
|
||||
if (!vconn)
|
||||
return;
|
||||
|
||||
MySQLDBConnection *conn = (MySQLDBConnection *)vconn;
|
||||
|
||||
mysql_close (conn->db);
|
||||
|
||||
g_free (conn);
|
||||
}
|
||||
|
||||
gboolean
|
||||
mysql_db_connection_execute (DBConnection *vconn, const char *sql, GError **error)
|
||||
{
|
||||
MySQLDBConnection *conn = (MySQLDBConnection *)vconn;
|
||||
|
||||
int rc = mysql_real_query (conn->db, sql, strlen(sql));
|
||||
if (rc != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"MySQL failed to execute: %s", mysql_error(conn->db));
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* Result Set. */
|
||||
|
||||
#define DEFAULT_COLUMN_SIZE 256
|
||||
|
||||
typedef struct MySQLResultSet {
|
||||
ResultSet parent;
|
||||
MYSQL_STMT *stmt;
|
||||
int column_count;
|
||||
MYSQL_BIND *bind;
|
||||
int need_rebind;
|
||||
} MySQLResultSet;
|
||||
|
||||
void
|
||||
mysql_result_set_free (ResultSet *vr)
|
||||
{
|
||||
if (!vr)
|
||||
return;
|
||||
|
||||
MySQLResultSet *r = (MySQLResultSet *)vr;
|
||||
|
||||
mysql_stmt_free_result (r->stmt);
|
||||
mysql_stmt_close (r->stmt);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < r->column_count; ++i) {
|
||||
g_free (r->bind[i].buffer);
|
||||
g_free (r->bind[i].length);
|
||||
g_free (r->bind[i].is_null);
|
||||
}
|
||||
g_free (r->bind);
|
||||
g_free (r);
|
||||
}
|
||||
|
||||
static MySQLResultSet *
|
||||
mysql_result_set_new (MYSQL_STMT *stmt, GError **error)
|
||||
{
|
||||
MySQLResultSet *r = g_new0 (MySQLResultSet, 1);
|
||||
int i;
|
||||
|
||||
r->stmt = stmt;
|
||||
r->column_count = mysql_stmt_field_count (stmt);
|
||||
r->bind = g_new0 (MYSQL_BIND, r->column_count);
|
||||
for (i = 0; i < r->column_count; ++i) {
|
||||
r->bind[i].buffer = g_malloc (DEFAULT_COLUMN_SIZE + 1);
|
||||
r->bind[i].buffer_type = MYSQL_TYPE_STRING;
|
||||
r->bind[i].buffer_length = DEFAULT_COLUMN_SIZE;
|
||||
r->bind[i].length = g_new0 (unsigned long, 1);
|
||||
r->bind[i].is_null = g_new0 (my_bool, 1);
|
||||
}
|
||||
|
||||
if (mysql_stmt_bind_result (stmt, r->bind) != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_bind_result failed: %s\n", mysql_stmt_error(stmt));
|
||||
mysql_result_set_free ((ResultSet*)r);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static MYSQL_STMT *
|
||||
prepare (MYSQL *db, const char *sql, GError **error)
|
||||
{
|
||||
MYSQL_STMT *stmt;
|
||||
|
||||
stmt = mysql_stmt_init (db);
|
||||
if (!stmt) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_init out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (mysql_stmt_prepare (stmt, sql, strlen(sql)) != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_prepare failed: %s",
|
||||
mysql_stmt_error(stmt));
|
||||
mysql_stmt_close (stmt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
ResultSet *
|
||||
mysql_execute_query (DBConnection *vconn, const char *sql, GError **error)
|
||||
{
|
||||
MySQLDBConnection *conn = (MySQLDBConnection *)vconn;
|
||||
MYSQL_STMT *stmt;
|
||||
MySQLResultSet *r;
|
||||
|
||||
stmt = prepare (conn->db, sql, error);
|
||||
if (!stmt) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned long cursor = CURSOR_TYPE_READ_ONLY;
|
||||
mysql_stmt_attr_set (stmt, STMT_ATTR_CURSOR_TYPE, &cursor);
|
||||
|
||||
if (mysql_stmt_execute (stmt) != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_execute failed: %s",
|
||||
mysql_stmt_error(stmt));
|
||||
mysql_stmt_close (stmt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
r = mysql_result_set_new (stmt, error);
|
||||
if (!r) {
|
||||
mysql_stmt_close (stmt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (ResultSet *)r;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
check_mysql_column_size (MySQLResultSet *r, int i, GError **error)
|
||||
{
|
||||
unsigned long real_length = *(r->bind[i].length);
|
||||
|
||||
if ((real_length > r->bind[i].buffer_length)) {
|
||||
/* Column was truncated, resize and fetch column directly. */
|
||||
g_free (r->bind[i].buffer);
|
||||
r->bind[i].buffer = g_malloc (real_length + 1);
|
||||
r->bind[i].buffer_length = real_length;
|
||||
if (mysql_stmt_fetch_column (r->stmt, &r->bind[i], i, 0) != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_fetch_column failed: %s",
|
||||
mysql_stmt_error(r->stmt));
|
||||
return FALSE;
|
||||
}
|
||||
r->need_rebind = TRUE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
mysql_result_set_next (ResultSet *vr, GError **error)
|
||||
{
|
||||
MySQLResultSet *r = (MySQLResultSet *)vr;
|
||||
|
||||
if (r->need_rebind) {
|
||||
if (mysql_stmt_bind_result (r->stmt, r->bind) != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_bind_result failed: %s",
|
||||
mysql_stmt_error(r->stmt));
|
||||
return FALSE;
|
||||
}
|
||||
r->need_rebind = FALSE;
|
||||
}
|
||||
|
||||
int rc = mysql_stmt_fetch (r->stmt);
|
||||
if (rc == 1) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_fetch failed: %s", mysql_stmt_error(r->stmt));
|
||||
return FALSE;
|
||||
}
|
||||
return ((rc == 0) || (rc == MYSQL_DATA_TRUNCATED));
|
||||
}
|
||||
|
||||
const char *
|
||||
mysql_result_set_get_string (ResultSet *vr, int i, GError **error)
|
||||
{
|
||||
MySQLResultSet *r = (MySQLResultSet *)vr;
|
||||
char *ret;
|
||||
|
||||
if (i < 0 || i >= r->column_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (*(r->bind[i].is_null)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!check_mysql_column_size (r, i, error)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = r->bind[i].buffer;
|
||||
ret[r->bind[i].buffer_length] = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
mysql_result_set_get_column_count (ResultSet *vr)
|
||||
{
|
||||
MySQLResultSet *r = (MySQLResultSet *)vr;
|
||||
|
||||
return r->column_count;
|
||||
}
|
||||
|
||||
typedef struct MySQLDBStmt {
|
||||
DBStmt parent;
|
||||
int param_count;
|
||||
MYSQL_STMT *stmt;
|
||||
MYSQL_BIND *bind;
|
||||
} MySQLDBStmt;
|
||||
|
||||
static MySQLDBStmt *
|
||||
mysql_stmt_new (MYSQL_STMT *stmt)
|
||||
{
|
||||
MySQLDBStmt *p = g_new0 (MySQLDBStmt, 1);
|
||||
|
||||
p->stmt = stmt;
|
||||
p->param_count = (int)mysql_stmt_param_count(stmt);
|
||||
if (p->param_count>0) {
|
||||
p->bind = g_new0 (MYSQL_BIND, p->param_count);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
DBStmt *
|
||||
mysql_prepare_statement (DBConnection *vconn, const char *sql, GError **error)
|
||||
{
|
||||
MySQLDBConnection *conn = (MySQLDBConnection *)vconn;
|
||||
MYSQL_STMT *stmt;
|
||||
MySQLDBStmt *ret;
|
||||
|
||||
stmt = prepare (conn->db, sql, error);
|
||||
if (!stmt) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = mysql_stmt_new (stmt);
|
||||
|
||||
return (DBStmt*)ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
mysql_stmt_set_int (DBStmt *vstmt, int i, int x, GError **error)
|
||||
{
|
||||
MySQLDBStmt *stmt = (MySQLDBStmt *)vstmt;
|
||||
int *pval;
|
||||
|
||||
if (i < 0 || i >= stmt->param_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
pval = g_new (int, 1);
|
||||
*pval = x;
|
||||
|
||||
stmt->bind[i].buffer_type = MYSQL_TYPE_LONG;
|
||||
stmt->bind[i].buffer = (char *)pval;
|
||||
stmt->bind[i].is_null = 0;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
mysql_stmt_set_int64 (DBStmt *vstmt, int i, gint64 x, GError **error)
|
||||
{
|
||||
MySQLDBStmt *stmt = (MySQLDBStmt *)vstmt;
|
||||
gint64 *pval;
|
||||
|
||||
if (i < 0 || i >= stmt->param_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
pval = g_new (gint64, 1);
|
||||
*pval = x;
|
||||
|
||||
stmt->bind[i].buffer_type = MYSQL_TYPE_LONGLONG;
|
||||
stmt->bind[i].buffer = (char *)pval;
|
||||
stmt->bind[i].is_null = 0;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
mysql_stmt_set_string (DBStmt *vstmt, int i, const char *s, GError **error)
|
||||
{
|
||||
MySQLDBStmt *stmt = (MySQLDBStmt *)vstmt;
|
||||
static my_bool yes = TRUE;
|
||||
unsigned long *plen;
|
||||
|
||||
if (i < 0 || i >= stmt->param_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
stmt->bind[i].buffer_type = MYSQL_TYPE_STRING;
|
||||
stmt->bind[i].buffer = g_strdup(s);
|
||||
plen = g_new (unsigned long, 1);
|
||||
stmt->bind[i].length = plen;
|
||||
if (!s) {
|
||||
*plen = 0;
|
||||
stmt->bind[i].is_null = &yes;
|
||||
} else {
|
||||
*plen = strlen(s);
|
||||
stmt->bind[i].is_null = 0;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
mysql_db_stmt_execute (DBStmt *vstmt, GError **error)
|
||||
{
|
||||
MySQLDBStmt *stmt = (MySQLDBStmt *)vstmt;
|
||||
|
||||
if (stmt->param_count > 0 &&
|
||||
mysql_stmt_bind_param (stmt->stmt, stmt->bind) != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_bind_param failed: %s",
|
||||
mysql_stmt_error(stmt->stmt));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
unsigned long cursor = CURSOR_TYPE_NO_CURSOR;
|
||||
mysql_stmt_attr_set (stmt->stmt, STMT_ATTR_CURSOR_TYPE, &cursor);
|
||||
|
||||
if (mysql_stmt_execute (stmt->stmt) != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_execute failed: %s", mysql_stmt_error(stmt->stmt));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
mysql_stmt_reset (stmt->stmt);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
ResultSet *
|
||||
mysql_db_stmt_execute_query (DBStmt *vstmt, GError **error)
|
||||
{
|
||||
MySQLDBStmt *stmt = (MySQLDBStmt *)vstmt;
|
||||
|
||||
if (stmt->param_count > 0 &&
|
||||
mysql_stmt_bind_param (stmt->stmt, stmt->bind) != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_bind_param failed: %s",
|
||||
mysql_stmt_error(stmt->stmt));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned long cursor = CURSOR_TYPE_READ_ONLY;
|
||||
mysql_stmt_attr_set (stmt->stmt, STMT_ATTR_CURSOR_TYPE, &cursor);
|
||||
|
||||
if (mysql_stmt_execute (stmt->stmt) != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"mysql_stmt_execute failed: %s", mysql_stmt_error(stmt->stmt));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
MySQLResultSet *r = mysql_result_set_new (stmt->stmt, error);
|
||||
if (*error) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (ResultSet *)r;
|
||||
}
|
||||
|
||||
void
|
||||
mysql_db_stmt_free (DBStmt *vstmt)
|
||||
{
|
||||
if (!vstmt)
|
||||
return;
|
||||
|
||||
MySQLDBStmt *stmt = (MySQLDBStmt *)vstmt;
|
||||
|
||||
/* If there is a result set associated with this stmt, the mysql stmt
|
||||
* will be freed when freeing the result set.
|
||||
*/
|
||||
if (!stmt->parent.result_set) {
|
||||
mysql_stmt_free_result (stmt->stmt);
|
||||
mysql_stmt_close (stmt->stmt);
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < stmt->param_count; ++i) {
|
||||
g_free (stmt->bind[i].buffer);
|
||||
g_free (stmt->bind[i].length);
|
||||
}
|
||||
g_free (stmt->bind);
|
||||
|
||||
g_free (stmt);
|
||||
}
|
||||
|
||||
/* Transaction. */
|
||||
|
||||
gboolean
|
||||
mysql_db_begin_transaction (DBConnection *vconn, GError **error)
|
||||
{
|
||||
MySQLDBConnection *conn = (MySQLDBConnection *)vconn;
|
||||
|
||||
int rc = mysql_query (conn->db, "START TRANSACTION;");
|
||||
if (rc != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Failed to begin transaction: %s", mysql_error(conn->db));
|
||||
}
|
||||
|
||||
return (rc == 0);
|
||||
}
|
||||
|
||||
gboolean
|
||||
mysql_db_commit (DBConnection *vconn, GError **error)
|
||||
{
|
||||
MySQLDBConnection *conn = (MySQLDBConnection *)vconn;
|
||||
|
||||
int rc = mysql_query (conn->db, "COMMIT;");
|
||||
if (rc != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Failed to commit transaction: %s", mysql_error(conn->db));
|
||||
}
|
||||
|
||||
return (rc == 0);
|
||||
}
|
||||
|
||||
gboolean
|
||||
mysql_db_rollback (DBConnection *vconn, GError **error)
|
||||
{
|
||||
MySQLDBConnection *conn = (MySQLDBConnection *)vconn;
|
||||
|
||||
int rc = mysql_query (conn->db, "ROLLBACK;");
|
||||
if (rc != 0) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Failed to rollback transaction: %s", mysql_error(conn->db));
|
||||
}
|
||||
|
||||
return (rc == 0);
|
||||
}
|
72
common/db-wrapper/mysql-db-ops.h
Normal file
72
common/db-wrapper/mysql-db-ops.h
Normal file
|
@ -0,0 +1,72 @@
|
|||
#ifndef MYSQL_DB_OPS_H
|
||||
#define MYSQL_DB_OPS_H
|
||||
|
||||
DBConnPool *
|
||||
mysql_db_conn_pool_new (const char *host,
|
||||
const char *user,
|
||||
const char *password,
|
||||
unsigned int port,
|
||||
const char *db_name,
|
||||
const char *unix_socket,
|
||||
gboolean use_ssl,
|
||||
const char *charset,
|
||||
int max_connections);
|
||||
|
||||
void
|
||||
mysql_db_conn_pool_free (DBConnPool *vpool);
|
||||
|
||||
DBConnection *
|
||||
mysql_get_db_connection (DBConnPool *vpool, GError **error);
|
||||
|
||||
void
|
||||
mysql_db_connection_close (DBConnection *vconn);
|
||||
|
||||
gboolean
|
||||
mysql_db_connection_execute (DBConnection *vconn, const char *sql, GError **error);
|
||||
|
||||
void
|
||||
mysql_result_set_free (ResultSet *vr);
|
||||
|
||||
ResultSet *
|
||||
mysql_execute_query (DBConnection *vconn, const char *sql, GError **error);
|
||||
|
||||
gboolean
|
||||
mysql_result_set_next (ResultSet *vr, GError **error);
|
||||
|
||||
const char *
|
||||
mysql_result_set_get_string (ResultSet *vr, int i, GError **error);
|
||||
|
||||
int
|
||||
mysql_result_set_get_column_count (ResultSet *vr);
|
||||
|
||||
DBStmt *
|
||||
mysql_prepare_statement (DBConnection *vconn, const char *sql, GError **error);
|
||||
|
||||
gboolean
|
||||
mysql_stmt_set_int (DBStmt *vstmt, int i, int x, GError **error);
|
||||
|
||||
gboolean
|
||||
mysql_stmt_set_int64 (DBStmt *vstmt, int i, gint64 x, GError **error);
|
||||
|
||||
gboolean
|
||||
mysql_stmt_set_string (DBStmt *vstmt, int i, const char *s, GError **error);
|
||||
|
||||
gboolean
|
||||
mysql_db_stmt_execute (DBStmt *vstmt, GError **error);
|
||||
|
||||
ResultSet *
|
||||
mysql_db_stmt_execute_query (DBStmt *vstmt, GError **error);
|
||||
|
||||
void
|
||||
mysql_db_stmt_free (DBStmt *vstmt);
|
||||
|
||||
gboolean
|
||||
mysql_db_begin_transaction (DBConnection *vconn, GError **error);
|
||||
|
||||
gboolean
|
||||
mysql_db_commit (DBConnection *vconn, GError **error);
|
||||
|
||||
gboolean
|
||||
mysql_db_rollback (DBConnection *vconn, GError **error);
|
||||
|
||||
#endif
|
475
common/db-wrapper/pgsql-db-ops.c
Normal file
475
common/db-wrapper/pgsql-db-ops.c
Normal file
|
@ -0,0 +1,475 @@
|
|||
#include "common.h"
|
||||
|
||||
#include "db-wrapper.h"
|
||||
#include "pgsql-db-ops.h"
|
||||
|
||||
#include <libpq-fe.h>
|
||||
|
||||
typedef struct PGDBConnPool {
|
||||
DBConnPool parent;
|
||||
char *host;
|
||||
char *user;
|
||||
char *password;
|
||||
char *db_name;
|
||||
char *unix_socket;
|
||||
} PGDBConnPool;
|
||||
|
||||
DBConnPool *
|
||||
pgsql_db_conn_pool_new (const char *host,
|
||||
const char *user,
|
||||
const char *password,
|
||||
const char *db_name,
|
||||
const char *unix_socket)
|
||||
{
|
||||
PGDBConnPool *pool = g_new0 (PGDBConnPool, 1);
|
||||
|
||||
pool->host = g_strdup (host);
|
||||
pool->user = g_strdup (user);
|
||||
pool->password = g_strdup (password);
|
||||
pool->db_name = g_strdup(db_name);
|
||||
pool->unix_socket = g_strdup(unix_socket);
|
||||
|
||||
return (DBConnPool *)pool;
|
||||
}
|
||||
|
||||
void
|
||||
pgsql_db_conn_pool_free (DBConnPool *vpool)
|
||||
{
|
||||
PGDBConnPool *pool = (PGDBConnPool *)vpool;
|
||||
|
||||
g_free (pool->host);
|
||||
g_free (pool->user);
|
||||
g_free (pool->password);
|
||||
g_free (pool->db_name);
|
||||
g_free (pool->unix_socket);
|
||||
|
||||
g_free (pool);
|
||||
}
|
||||
|
||||
typedef struct PGDBConnection {
|
||||
DBConnection parent;
|
||||
PGconn *db;
|
||||
} PGDBConnection;
|
||||
|
||||
static char *
|
||||
escape_string_pgsql_connect (const char *str)
|
||||
{
|
||||
GString *buf = g_string_new (NULL);
|
||||
const char *p;
|
||||
|
||||
for (p = str; *p != '\0'; ++p) {
|
||||
if (*p == '\'' || *p == '\\') {
|
||||
g_string_append_c (buf, '\\');
|
||||
g_string_append_c (buf, *p);
|
||||
} else {
|
||||
g_string_append_c (buf, *p);
|
||||
}
|
||||
}
|
||||
|
||||
return g_string_free (buf, FALSE);
|
||||
}
|
||||
|
||||
static PGconn *
|
||||
connect_pgsql (PGDBConnPool *pool, GError **error)
|
||||
{
|
||||
GString *buf = g_string_new("");
|
||||
char *esc_password = NULL;
|
||||
PGconn *db;
|
||||
|
||||
g_string_append_printf (buf, "user='%s' ", pool->user);
|
||||
|
||||
esc_password = escape_string_pgsql_connect (pool->password);
|
||||
g_string_append_printf (buf, "password='%s' ", esc_password);
|
||||
g_free (esc_password);
|
||||
|
||||
if (pool->unix_socket) {
|
||||
g_string_append_printf (buf, "host='%s' ", pool->unix_socket);
|
||||
} else {
|
||||
g_string_append_printf (buf, "host='%s' ", pool->host);
|
||||
}
|
||||
|
||||
g_string_append_printf (buf, "dbname='%s' ", pool->db_name);
|
||||
|
||||
db = PQconnectdb (buf->str);
|
||||
if (PQstatus (db) != CONNECTION_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"PQconnectdb failed: %s", PQerrorMessage (db));
|
||||
PQfinish (db);
|
||||
db = NULL;
|
||||
}
|
||||
|
||||
g_string_free (buf, TRUE);
|
||||
return db;
|
||||
}
|
||||
|
||||
DBConnection *
|
||||
pgsql_get_db_connection (DBConnPool *vpool, GError **error)
|
||||
{
|
||||
PGDBConnPool *pool = (PGDBConnPool *)vpool;
|
||||
PGDBConnection *conn;
|
||||
|
||||
PGconn *db = connect_pgsql (pool, error);
|
||||
if (!db)
|
||||
return NULL;
|
||||
|
||||
conn = g_new0 (PGDBConnection, 1);
|
||||
conn->db = db;
|
||||
conn->parent.pool = vpool;
|
||||
|
||||
return (DBConnection *)conn;
|
||||
}
|
||||
|
||||
void
|
||||
pgsql_db_connection_close (DBConnection *vconn)
|
||||
{
|
||||
if (!vconn)
|
||||
return;
|
||||
|
||||
PGDBConnection *conn = (PGDBConnection *)vconn;
|
||||
|
||||
PQfinish (conn->db);
|
||||
|
||||
g_free (conn);
|
||||
}
|
||||
|
||||
gboolean
|
||||
pgsql_db_connection_execute (DBConnection *vconn, const char *sql, GError **error)
|
||||
{
|
||||
PGDBConnection *conn = (PGDBConnection *)vconn;
|
||||
PGresult *res;
|
||||
gboolean ret = TRUE;
|
||||
|
||||
res = PQexec (conn->db, sql);
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"PQexec failed: %s", PQresultErrorMessage(res));
|
||||
ret = FALSE;
|
||||
}
|
||||
PQclear (res);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
typedef struct PGResultSet {
|
||||
ResultSet parent;
|
||||
PGresult *res;
|
||||
int curr_row;
|
||||
int column_count;
|
||||
int row_count;
|
||||
} PGResultSet;
|
||||
|
||||
void
|
||||
pgsql_result_set_free (ResultSet *vr)
|
||||
{
|
||||
if (!vr)
|
||||
return;
|
||||
|
||||
PGResultSet *r = (PGResultSet *)vr;
|
||||
|
||||
PQclear (r->res);
|
||||
g_free (r);
|
||||
}
|
||||
|
||||
static PGResultSet *
|
||||
pgsql_result_set_new (PGresult *res)
|
||||
{
|
||||
PGResultSet *r;
|
||||
|
||||
r = g_new0 (PGResultSet, 1);
|
||||
r->curr_row = -1;
|
||||
r->column_count = PQnfields(res);
|
||||
r->row_count = PQntuples(res);
|
||||
r->res = res;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
ResultSet *
|
||||
pgsql_execute_query (DBConnection *vconn, const char *sql, GError **error)
|
||||
{
|
||||
PGDBConnection *conn = (PGDBConnection *)vconn;
|
||||
PGresult *res;
|
||||
PGResultSet *r;
|
||||
|
||||
res = PQexec (conn->db, sql);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"PQexec failed: %s", PQresultErrorMessage(res));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
r = pgsql_result_set_new (res);
|
||||
|
||||
return (ResultSet *)r;
|
||||
}
|
||||
|
||||
gboolean
|
||||
pgsql_result_set_next (ResultSet *vr, GError **error)
|
||||
{
|
||||
PGResultSet *r = (PGResultSet *)vr;
|
||||
|
||||
return ((r->curr_row)++ < (r->row_count - 1));
|
||||
}
|
||||
|
||||
const char *
|
||||
pgsql_result_set_get_string (ResultSet *vr, int i, GError **error)
|
||||
{
|
||||
PGResultSet *r = (PGResultSet *)vr;
|
||||
|
||||
if (PQgetisnull(r->res, r->curr_row, i))
|
||||
return NULL;
|
||||
return PQgetvalue(r->res, r->curr_row, i);
|
||||
}
|
||||
|
||||
int
|
||||
pgsql_result_set_get_column_count (ResultSet *vr)
|
||||
{
|
||||
PGResultSet *r = (PGResultSet *)vr;
|
||||
return r->column_count;
|
||||
}
|
||||
|
||||
typedef struct PGDBStmt {
|
||||
DBStmt parent;
|
||||
char *name;
|
||||
PGconn *db;
|
||||
int param_count;
|
||||
char **values;
|
||||
int *lengths;
|
||||
int *formats;
|
||||
} PGDBStmt;
|
||||
|
||||
static PGDBStmt *
|
||||
pgsql_stmt_new (PGconn *db, char *name, int param_count)
|
||||
{
|
||||
PGDBStmt *stmt = g_new0 (PGDBStmt, 1);
|
||||
|
||||
stmt->name = g_strdup(name);
|
||||
stmt->db = db;
|
||||
stmt->param_count = param_count;
|
||||
|
||||
if (stmt->param_count) {
|
||||
stmt->values = g_new0 (char *, param_count);
|
||||
stmt->lengths = g_new0 (int, param_count);
|
||||
stmt->formats = g_new0 (int, param_count);
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
/* Convert '?' in the query string to $1, $2, etc. */
|
||||
static char *
|
||||
pgsql_format_query_string (const char *sql, int *param_count)
|
||||
{
|
||||
GString *buf = g_string_new (NULL);
|
||||
const char *p;
|
||||
int i = 0;
|
||||
|
||||
for (p = sql; *p != '\0'; ++p) {
|
||||
if (*p == '?') {
|
||||
++i;
|
||||
g_string_append_c (buf, '$');
|
||||
g_string_append_printf (buf, "%d", i);
|
||||
} else {
|
||||
g_string_append_c (buf, *p);
|
||||
}
|
||||
}
|
||||
|
||||
*param_count = i;
|
||||
|
||||
return g_string_free (buf, FALSE);
|
||||
}
|
||||
|
||||
static gint stmt_id = 0;
|
||||
|
||||
DBStmt *
|
||||
pgsql_prepare_statement (DBConnection *vconn, const char *sql, GError **error)
|
||||
{
|
||||
PGDBConnection *conn = (PGDBConnection *)vconn;
|
||||
char *query;
|
||||
int param_count;
|
||||
char *name;
|
||||
PGresult *res;
|
||||
PGDBStmt *stmt = NULL;
|
||||
|
||||
query = pgsql_format_query_string (sql, ¶m_count);
|
||||
|
||||
g_atomic_int_inc (&stmt_id);
|
||||
name = g_strdup_printf ("%d", stmt_id);
|
||||
|
||||
res = PQprepare (conn->db, name, query, 0, NULL);
|
||||
ExecStatusType status = PQresultStatus(res);
|
||||
if (res && (status == PGRES_EMPTY_QUERY || status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK)) {
|
||||
stmt = pgsql_stmt_new (conn->db, name, param_count);
|
||||
} else {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"PQprepare failed: %s", PQresultErrorMessage(res));
|
||||
}
|
||||
|
||||
PQclear (res);
|
||||
g_free (name);
|
||||
g_free (query);
|
||||
return (DBStmt *)stmt;
|
||||
}
|
||||
|
||||
gboolean
|
||||
pgsql_stmt_set_int (DBStmt *vstmt, int i, int x, GError **error)
|
||||
{
|
||||
PGDBStmt *stmt = (PGDBStmt *)vstmt;
|
||||
|
||||
if (i < 0 || i >= stmt->param_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
stmt->values[i] = g_strdup_printf("%d", x);
|
||||
stmt->lengths[i] = 0;
|
||||
stmt->formats[i] = 0;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
pgsql_stmt_set_int64 (DBStmt *vstmt, int i, gint64 x, GError **error)
|
||||
{
|
||||
PGDBStmt *stmt = (PGDBStmt *)vstmt;
|
||||
|
||||
if (i < 0 || i >= stmt->param_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
stmt->values[i] = g_strdup_printf("%"G_GINT64_FORMAT, x);
|
||||
stmt->lengths[i] = 0;
|
||||
stmt->formats[i] = 0;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
pgsql_stmt_set_string (DBStmt *vstmt, int i, const char *s, GError **error)
|
||||
{
|
||||
PGDBStmt *stmt = (PGDBStmt *)vstmt;
|
||||
|
||||
if (i < 0 || i >= stmt->param_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
stmt->values[i] = g_strdup(s);
|
||||
stmt->lengths[i] = 0;
|
||||
stmt->formats[i] = 0;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
pgsql_db_stmt_execute (DBStmt *vstmt, GError **error)
|
||||
{
|
||||
PGDBStmt *stmt = (PGDBStmt *)vstmt;
|
||||
PGresult *res;
|
||||
gboolean ret;
|
||||
|
||||
res = PQexecPrepared (stmt->db, stmt->name, stmt->param_count,
|
||||
(const char **)stmt->values, stmt->lengths, stmt->formats, 0);
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"PGexecPrepared failed: %s", PQresultErrorMessage(res));
|
||||
ret = FALSE;
|
||||
}
|
||||
|
||||
ret = TRUE;
|
||||
|
||||
PQclear(res);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ResultSet *
|
||||
pgsql_db_stmt_execute_query (DBStmt *vstmt, GError **error)
|
||||
{
|
||||
PGDBStmt *stmt = (PGDBStmt *)vstmt;
|
||||
PGresult *res;
|
||||
PGResultSet *ret = NULL;
|
||||
|
||||
res = PQexecPrepared (stmt->db, stmt->name, stmt->param_count,
|
||||
(const char **)stmt->values, stmt->lengths, stmt->formats, 0);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"PGexecPrepared failed: %s", PQresultErrorMessage(res));
|
||||
}
|
||||
|
||||
ret = pgsql_result_set_new (res);
|
||||
|
||||
return (ResultSet *)ret;
|
||||
}
|
||||
|
||||
void
|
||||
pgsql_db_stmt_free (DBStmt *vstmt)
|
||||
{
|
||||
if (!vstmt)
|
||||
return;
|
||||
|
||||
PGDBStmt *stmt = (PGDBStmt *)vstmt;
|
||||
|
||||
g_free (stmt->name);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < stmt->param_count; ++i)
|
||||
g_free (stmt->values[i]);
|
||||
g_free (stmt->values);
|
||||
|
||||
g_free (stmt->lengths);
|
||||
g_free (stmt->formats);
|
||||
g_free (stmt);
|
||||
}
|
||||
|
||||
gboolean
|
||||
pgsql_db_begin_transaction (DBConnection *vconn, GError **error)
|
||||
{
|
||||
PGDBConnection *conn = (PGDBConnection *)vconn;
|
||||
gboolean ret = TRUE;
|
||||
|
||||
PGresult *res = PQexec(conn->db, "BEGIN TRANSACTION;");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Begin transaction failed: %s", PQresultErrorMessage(res));
|
||||
ret = FALSE;
|
||||
}
|
||||
PQclear(res);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
pgsql_db_commit (DBConnection *vconn, GError **error)
|
||||
{
|
||||
PGDBConnection *conn = (PGDBConnection *)vconn;
|
||||
gboolean ret = TRUE;
|
||||
|
||||
PGresult *res = PQexec(conn->db, "COMMIT TRANSACTION;");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Commit transaction failed: %s", PQresultErrorMessage(res));
|
||||
ret = FALSE;
|
||||
}
|
||||
PQclear(res);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
pgsql_db_rollback (DBConnection *vconn, GError **error)
|
||||
{
|
||||
PGDBConnection *conn = (PGDBConnection *)vconn;
|
||||
gboolean ret = TRUE;
|
||||
|
||||
PGresult *res = PQexec(conn->db, "ROLLBACK TRANSACTION;");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Rollback transaction failed: %s", PQresultErrorMessage(res));
|
||||
ret = FALSE;
|
||||
}
|
||||
PQclear(res);
|
||||
return ret;
|
||||
}
|
68
common/db-wrapper/pgsql-db-ops.h
Normal file
68
common/db-wrapper/pgsql-db-ops.h
Normal file
|
@ -0,0 +1,68 @@
|
|||
#ifndef PGSQL_DB_OPS_H
|
||||
#define PGSQL_DB_OPS_H
|
||||
|
||||
DBConnPool *
|
||||
pgsql_db_conn_pool_new (const char *host,
|
||||
const char *user,
|
||||
const char *password,
|
||||
const char *db_name,
|
||||
const char *unix_socket);
|
||||
|
||||
void
|
||||
pgsql_db_conn_pool_free (DBConnPool *vpool);
|
||||
|
||||
DBConnection *
|
||||
pgsql_get_db_connection (DBConnPool *vpool, GError **error);
|
||||
|
||||
void
|
||||
pgsql_db_connection_close (DBConnection *vconn);
|
||||
|
||||
gboolean
|
||||
pgsql_db_connection_execute (DBConnection *vconn, const char *sql, GError **error);
|
||||
|
||||
void
|
||||
pgsql_result_set_free (ResultSet *vr);
|
||||
|
||||
ResultSet *
|
||||
pgsql_execute_query (DBConnection *vconn, const char *sql, GError **error);
|
||||
|
||||
gboolean
|
||||
pgsql_result_set_next (ResultSet *vr, GError **error);
|
||||
|
||||
const char *
|
||||
pgsql_result_set_get_string (ResultSet *vr, int i, GError **error);
|
||||
|
||||
int
|
||||
pgsql_result_set_get_column_count (ResultSet *vr);
|
||||
|
||||
DBStmt *
|
||||
pgsql_prepare_statement (DBConnection *vconn, const char *sql, GError **error);
|
||||
|
||||
gboolean
|
||||
pgsql_stmt_set_int (DBStmt *vstmt, int i, int x, GError **error);
|
||||
|
||||
gboolean
|
||||
pgsql_stmt_set_int64 (DBStmt *vstmt, int i, gint64 x, GError **error);
|
||||
|
||||
gboolean
|
||||
pgsql_stmt_set_string (DBStmt *vstmt, int i, const char *s, GError **error);
|
||||
|
||||
gboolean
|
||||
pgsql_db_stmt_execute (DBStmt *vstmt, GError **error);
|
||||
|
||||
ResultSet *
|
||||
pgsql_db_stmt_execute_query (DBStmt *vstmt, GError **error);
|
||||
|
||||
void
|
||||
pgsql_db_stmt_free (DBStmt *vstmt);
|
||||
|
||||
gboolean
|
||||
pgsql_db_begin_transaction (DBConnection *vconn, GError **error);
|
||||
|
||||
gboolean
|
||||
pgsql_db_commit (DBConnection *vconn, GError **error);
|
||||
|
||||
gboolean
|
||||
pgsql_db_rollback (DBConnection *vconn, GError **error);
|
||||
|
||||
#endif
|
454
common/db-wrapper/sqlite-db-ops.c
Normal file
454
common/db-wrapper/sqlite-db-ops.c
Normal file
|
@ -0,0 +1,454 @@
|
|||
#include "common.h"
|
||||
|
||||
#include "db-wrapper.h"
|
||||
#include "sqlite-db-ops.h"
|
||||
|
||||
#include <sqlite3.h>
|
||||
#include <pthread.h>
|
||||
|
||||
/* SQLite thread synchronization rountines. */
|
||||
|
||||
typedef struct UnlockNotification {
|
||||
int fired;
|
||||
pthread_cond_t cond;
|
||||
pthread_mutex_t mutex;
|
||||
} UnlockNotification;
|
||||
|
||||
static void
|
||||
unlock_notify_cb(void **ap_arg, int n_arg)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_arg; i++) {
|
||||
UnlockNotification *p = (UnlockNotification *)ap_arg[i];
|
||||
pthread_mutex_lock (&p->mutex);
|
||||
p->fired = 1;
|
||||
pthread_cond_signal (&p->cond);
|
||||
pthread_mutex_unlock (&p->mutex);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
wait_for_unlock_notify(sqlite3 *db)
|
||||
{
|
||||
UnlockNotification un;
|
||||
un.fired = 0;
|
||||
pthread_mutex_init (&un.mutex, NULL);
|
||||
pthread_cond_init (&un.cond, NULL);
|
||||
|
||||
int rc = sqlite3_unlock_notify(db, unlock_notify_cb, (void *)&un);
|
||||
|
||||
if (rc == SQLITE_OK) {
|
||||
pthread_mutex_lock(&un.mutex);
|
||||
if (!un.fired)
|
||||
pthread_cond_wait (&un.cond, &un.mutex);
|
||||
pthread_mutex_unlock(&un.mutex);
|
||||
}
|
||||
|
||||
pthread_cond_destroy (&un.cond);
|
||||
pthread_mutex_destroy (&un.mutex);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
sqlite3_blocking_step(sqlite3_stmt *stmt)
|
||||
{
|
||||
int rc;
|
||||
while (SQLITE_LOCKED == (rc = sqlite3_step(stmt))) {
|
||||
rc = wait_for_unlock_notify(sqlite3_db_handle(stmt));
|
||||
if (rc != SQLITE_OK)
|
||||
break;
|
||||
sqlite3_reset(stmt);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
sqlite3_blocking_prepare_v2(sqlite3 *db, const char *sql, int sql_len, sqlite3_stmt **pstmt, const char **pz)
|
||||
{
|
||||
int rc;
|
||||
while (SQLITE_LOCKED == (rc = sqlite3_prepare_v2(db, sql, sql_len, pstmt, pz))) {
|
||||
rc = wait_for_unlock_notify(db);
|
||||
if (rc != SQLITE_OK)
|
||||
break;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
sqlite3_blocking_exec(sqlite3 *db, const char *sql, int (*callback)(void *, int, char **, char **), void *arg, char **errmsg)
|
||||
{
|
||||
int rc;
|
||||
while (SQLITE_LOCKED == (rc = sqlite3_exec(db, sql, callback, arg, errmsg))) {
|
||||
rc = wait_for_unlock_notify(db);
|
||||
if (rc != SQLITE_OK)
|
||||
break;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
typedef struct SQLiteDBConnPool {
|
||||
DBConnPool parent;
|
||||
char *db_path;
|
||||
int max_connections;
|
||||
} SQLiteDBConnPool;
|
||||
|
||||
DBConnPool *
|
||||
sqlite_db_conn_pool_new (const char *db_path, int max_connections)
|
||||
{
|
||||
SQLiteDBConnPool *pool = g_new0 (SQLiteDBConnPool, 1);
|
||||
pool->db_path = g_strdup(db_path);
|
||||
pool->max_connections = max_connections;
|
||||
|
||||
return (DBConnPool *)pool;
|
||||
}
|
||||
|
||||
void
|
||||
sqlite_db_conn_pool_free (DBConnPool *vpool)
|
||||
{
|
||||
if (!vpool)
|
||||
return;
|
||||
|
||||
SQLiteDBConnPool *pool = (SQLiteDBConnPool *)vpool;
|
||||
|
||||
g_free (pool->db_path);
|
||||
g_free (pool);
|
||||
}
|
||||
|
||||
typedef struct SQLiteDBConnection {
|
||||
DBConnection parent;
|
||||
sqlite3 *db;
|
||||
} SQLiteDBConnection;
|
||||
|
||||
DBConnection *
|
||||
sqlite_get_db_connection (DBConnPool *vpool, GError **error)
|
||||
{
|
||||
SQLiteDBConnPool *pool = (SQLiteDBConnPool *)vpool;
|
||||
sqlite3 *db;
|
||||
int result;
|
||||
const char *errmsg;
|
||||
SQLiteDBConnection *conn;
|
||||
|
||||
result = sqlite3_open (pool->db_path, &db);
|
||||
if (result != SQLITE_OK) {
|
||||
errmsg = sqlite3_errmsg(db);
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Failed to open sqlite db: %s",
|
||||
errmsg ? errmsg : "no error given");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
conn = g_new0 (SQLiteDBConnection, 1);
|
||||
conn->db = db;
|
||||
|
||||
return (DBConnection *)conn;
|
||||
}
|
||||
|
||||
void
|
||||
sqlite_db_connection_close (DBConnection *vconn)
|
||||
{
|
||||
if (!vconn)
|
||||
return;
|
||||
|
||||
SQLiteDBConnection *conn = (SQLiteDBConnection *)vconn;
|
||||
|
||||
sqlite3_close (conn->db);
|
||||
|
||||
g_free (conn);
|
||||
}
|
||||
|
||||
gboolean
|
||||
sqlite_db_connection_execute (DBConnection *vconn, const char *sql, GError **error)
|
||||
{
|
||||
SQLiteDBConnection *conn = (SQLiteDBConnection *)vconn;
|
||||
char *errmsg = NULL;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_blocking_exec (conn->db, sql, NULL, NULL, &errmsg);
|
||||
if (rc != SQLITE_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"sqlite3_exec failed: %s",
|
||||
errmsg ? errmsg : "no error given");
|
||||
if (errmsg)
|
||||
sqlite3_free (errmsg);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
typedef struct SQLiteResultSet {
|
||||
ResultSet parent;
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt *stmt;
|
||||
int column_count;
|
||||
} SQLiteResultSet;
|
||||
|
||||
void
|
||||
sqlite_result_set_free (ResultSet *vr)
|
||||
{
|
||||
if (!vr)
|
||||
return;
|
||||
|
||||
SQLiteResultSet *r = (SQLiteResultSet *)vr;
|
||||
|
||||
sqlite3_finalize (r->stmt);
|
||||
|
||||
g_free (r);
|
||||
}
|
||||
|
||||
ResultSet *
|
||||
sqlite_execute_query (DBConnection *vconn, const char *sql, GError **error)
|
||||
{
|
||||
SQLiteDBConnection *conn = (SQLiteDBConnection *)vconn;
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
SQLiteResultSet *r;
|
||||
|
||||
rc = sqlite3_blocking_prepare_v2 (conn->db, sql, -1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"sqlite3_prepare_v2 failed: %s", sqlite3_errmsg(conn->db));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
r = g_new0 (SQLiteResultSet, 1);
|
||||
r->db = conn->db;
|
||||
r->stmt = stmt;
|
||||
r->column_count = sqlite3_column_count (stmt);
|
||||
|
||||
return (ResultSet *)r;
|
||||
}
|
||||
|
||||
gboolean
|
||||
sqlite_result_set_next (ResultSet *vr, GError **error)
|
||||
{
|
||||
SQLiteResultSet *r = (SQLiteResultSet *)vr;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_blocking_step (r->stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"sqlite3_step failed: %s", sqlite3_errmsg(r->db));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (rc == SQLITE_ROW);
|
||||
}
|
||||
|
||||
const char *
|
||||
sqlite_result_set_get_string (ResultSet *vr, int i, GError **error)
|
||||
{
|
||||
SQLiteResultSet *r = (SQLiteResultSet *)vr;
|
||||
|
||||
if (i < 0 || i >= r->column_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (const char *)sqlite3_column_text (r->stmt, i);
|
||||
}
|
||||
|
||||
int
|
||||
sqlite_result_set_get_column_count (ResultSet *vr)
|
||||
{
|
||||
return ((SQLiteResultSet *)vr)->column_count;
|
||||
}
|
||||
|
||||
typedef struct SQLiteDBStmt {
|
||||
DBStmt parent;
|
||||
int param_count;
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt *stmt;
|
||||
} SQLiteDBStmt;
|
||||
|
||||
DBStmt *
|
||||
sqlite_prepare_statement (DBConnection *vconn, const char *sql, GError **error)
|
||||
{
|
||||
SQLiteDBConnection *conn = (SQLiteDBConnection *)vconn;
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
SQLiteDBStmt *ret;
|
||||
|
||||
rc = sqlite3_blocking_prepare_v2 (conn->db, sql, -1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"sqlite3_prepare_v2 failed: %s", sqlite3_errmsg(conn->db));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = g_new0 (SQLiteDBStmt, 1);
|
||||
ret->stmt = stmt;
|
||||
ret->db = conn->db;
|
||||
ret->param_count = sqlite3_bind_parameter_count (stmt);
|
||||
|
||||
return (DBStmt *)ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
sqlite_stmt_set_int (DBStmt *vstmt, int i, int x, GError **error)
|
||||
{
|
||||
SQLiteDBStmt *stmt = (SQLiteDBStmt *)vstmt;
|
||||
int rc;
|
||||
|
||||
if (i < 0 || i >= stmt->param_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
rc = sqlite3_bind_int (stmt->stmt, i+1, x);
|
||||
if (rc != SQLITE_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"sqlite3_bind_int failed: %s", sqlite3_errstr(rc));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
sqlite_stmt_set_int64 (DBStmt *vstmt, int i, gint64 x, GError **error)
|
||||
{
|
||||
SQLiteDBStmt *stmt = (SQLiteDBStmt *)vstmt;
|
||||
int rc;
|
||||
|
||||
if (i < 0 || i >= stmt->param_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
rc = sqlite3_bind_int64 (stmt->stmt, i+1, x);
|
||||
if (rc != SQLITE_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"sqlite3_bind_int failed: %s", sqlite3_errstr(rc));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
sqlite_stmt_set_string (DBStmt *vstmt, int i, const char *s, GError **error)
|
||||
{
|
||||
SQLiteDBStmt *stmt = (SQLiteDBStmt *)vstmt;
|
||||
int rc;
|
||||
|
||||
if (i < 0 || i >= stmt->param_count) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Column index is out of range.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
rc = sqlite3_bind_text (stmt->stmt, i+1, s, -1, SQLITE_TRANSIENT);
|
||||
if (rc != SQLITE_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"sqlite3_bind_int failed: %s", sqlite3_errstr(rc));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
sqlite_db_stmt_execute (DBStmt *vstmt, GError **error)
|
||||
{
|
||||
SQLiteDBStmt *stmt = (SQLiteDBStmt *)vstmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_blocking_step (stmt->stmt);
|
||||
if (rc == SQLITE_DONE) {
|
||||
sqlite3_reset (stmt->stmt);
|
||||
return TRUE;
|
||||
} else if (rc == SQLITE_ROW) {
|
||||
sqlite3_reset (stmt->stmt);
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"Select statement not allowed in db_stmt_execute.");
|
||||
return FALSE;
|
||||
} else {
|
||||
sqlite3_reset (stmt->stmt);
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"sqlite3_step failed: %s", sqlite3_errmsg(stmt->db));
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
ResultSet *
|
||||
sqlite_db_stmt_execute_query (DBStmt *vstmt, GError **error)
|
||||
{
|
||||
SQLiteDBStmt *stmt = (SQLiteDBStmt *)vstmt;
|
||||
SQLiteResultSet *r;
|
||||
|
||||
r = g_new0 (SQLiteResultSet, 1);
|
||||
r->db = stmt->db;
|
||||
r->stmt = stmt->stmt;
|
||||
r->column_count = sqlite3_column_count (r->stmt);
|
||||
|
||||
return (ResultSet *)r;
|
||||
}
|
||||
|
||||
void
|
||||
sqlite_db_stmt_free (DBStmt *vstmt)
|
||||
{
|
||||
if (!vstmt)
|
||||
return;
|
||||
|
||||
SQLiteDBStmt *stmt = (SQLiteDBStmt *)vstmt;
|
||||
|
||||
if (!stmt->parent.result_set) {
|
||||
sqlite3_finalize (stmt->stmt);
|
||||
}
|
||||
|
||||
g_free (stmt);
|
||||
}
|
||||
|
||||
gboolean
|
||||
sqlite_db_begin_transaction (DBConnection *vconn, GError **error)
|
||||
{
|
||||
SQLiteDBConnection *conn = (SQLiteDBConnection *)vconn;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_blocking_exec (conn->db, "BEGIN TRANSACTION;", NULL, NULL, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"begin transaction failed: %s", sqlite3_errmsg(conn->db));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
sqlite_db_commit (DBConnection *vconn, GError **error)
|
||||
{
|
||||
SQLiteDBConnection *conn = (SQLiteDBConnection *)vconn;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_blocking_exec (conn->db, "COMMIT TRANSACTION;", NULL, NULL, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"commit transaction failed: %s", sqlite3_errmsg(conn->db));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
sqlite_db_rollback (DBConnection *vconn, GError **error)
|
||||
{
|
||||
SQLiteDBConnection *conn = (SQLiteDBConnection *)vconn;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_blocking_exec (conn->db, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
g_set_error (error, SEAF_DB_ERROR_DOMAIN, SEAF_DB_ERROR_CODE,
|
||||
"rollback transaction failed: %s", sqlite3_errmsg(conn->db));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
64
common/db-wrapper/sqlite-db-ops.h
Normal file
64
common/db-wrapper/sqlite-db-ops.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
#ifndef SQLITE_DB_OPS_H
|
||||
#define SQLITE_DB_OPS_H
|
||||
|
||||
DBConnPool *
|
||||
sqlite_db_conn_pool_new (const char *db_path, int max_connections);
|
||||
|
||||
void
|
||||
sqlite_db_conn_pool_free (DBConnPool *vpool);
|
||||
|
||||
DBConnection *
|
||||
sqlite_get_db_connection (DBConnPool *vpool, GError **error);
|
||||
|
||||
void
|
||||
sqlite_db_connection_close (DBConnection *vconn);
|
||||
|
||||
gboolean
|
||||
sqlite_db_connection_execute (DBConnection *vconn, const char *sql, GError **error);
|
||||
|
||||
void
|
||||
sqlite_result_set_free (ResultSet *vr);
|
||||
|
||||
ResultSet *
|
||||
sqlite_execute_query (DBConnection *vconn, const char *sql, GError **error);
|
||||
|
||||
gboolean
|
||||
sqlite_result_set_next (ResultSet *vr, GError **error);
|
||||
|
||||
const char *
|
||||
sqlite_result_set_get_string (ResultSet *vr, int i, GError **error);
|
||||
|
||||
int
|
||||
sqlite_result_set_get_column_count (ResultSet *vr);
|
||||
|
||||
DBStmt *
|
||||
sqlite_prepare_statement (DBConnection *vconn, const char *sql, GError **error);
|
||||
|
||||
gboolean
|
||||
sqlite_stmt_set_int (DBStmt *vstmt, int i, int x, GError **error);
|
||||
|
||||
gboolean
|
||||
sqlite_stmt_set_int64 (DBStmt *vstmt, int i, gint64 x, GError **error);
|
||||
|
||||
gboolean
|
||||
sqlite_stmt_set_string (DBStmt *vstmt, int i, const char *s, GError **error);
|
||||
|
||||
gboolean
|
||||
sqlite_db_stmt_execute (DBStmt *vstmt, GError **error);
|
||||
|
||||
ResultSet *
|
||||
sqlite_db_stmt_execute_query (DBStmt *vstmt, GError **error);
|
||||
|
||||
void
|
||||
sqlite_db_stmt_free (DBStmt *vstmt);
|
||||
|
||||
gboolean
|
||||
sqlite_db_begin_transaction (DBConnection *vconn, GError **error);
|
||||
|
||||
gboolean
|
||||
sqlite_db_commit (DBConnection *vconn, GError **error);
|
||||
|
||||
gboolean
|
||||
sqlite_db_rollback (DBConnection *vconn, GError **error);
|
||||
|
||||
#endif
|
844
common/diff-simple.c
Normal file
844
common/diff-simple.c
Normal file
|
@ -0,0 +1,844 @@
|
|||
#include "common.h"
|
||||
#include "diff-simple.h"
|
||||
#include "utils.h"
|
||||
#include "log.h"
|
||||
|
||||
DiffEntry *
|
||||
diff_entry_new (char type, char status, unsigned char *sha1, const char *name)
|
||||
{
|
||||
DiffEntry *de = g_new0 (DiffEntry, 1);
|
||||
|
||||
de->type = type;
|
||||
de->status = status;
|
||||
memcpy (de->sha1, sha1, 20);
|
||||
de->name = g_strdup(name);
|
||||
|
||||
return de;
|
||||
}
|
||||
|
||||
DiffEntry *
|
||||
diff_entry_new_from_dirent (char type, char status,
|
||||
SeafDirent *dent, const char *basedir)
|
||||
{
|
||||
DiffEntry *de = g_new0 (DiffEntry, 1);
|
||||
unsigned char sha1[20];
|
||||
char *path;
|
||||
|
||||
hex_to_rawdata (dent->id, sha1, 20);
|
||||
path = g_strconcat (basedir, dent->name, NULL);
|
||||
|
||||
de->type = type;
|
||||
de->status = status;
|
||||
memcpy (de->sha1, sha1, 20);
|
||||
de->name = path;
|
||||
|
||||
#ifdef SEAFILE_CLIENT
|
||||
if (type == DIFF_TYPE_COMMITS &&
|
||||
(status == DIFF_STATUS_ADDED ||
|
||||
status == DIFF_STATUS_MODIFIED ||
|
||||
status == DIFF_STATUS_DIR_ADDED ||
|
||||
status == DIFF_STATUS_DIR_DELETED)) {
|
||||
de->mtime = dent->mtime;
|
||||
de->mode = dent->mode;
|
||||
de->modifier = g_strdup(dent->modifier);
|
||||
de->size = dent->size;
|
||||
}
|
||||
#endif
|
||||
|
||||
return de;
|
||||
}
|
||||
|
||||
void
|
||||
diff_entry_free (DiffEntry *de)
|
||||
{
|
||||
g_free (de->name);
|
||||
if (de->new_name)
|
||||
g_free (de->new_name);
|
||||
|
||||
#ifdef SEAFILE_CLIENT
|
||||
g_free (de->modifier);
|
||||
#endif
|
||||
|
||||
g_free (de);
|
||||
}
|
||||
|
||||
inline static gboolean
|
||||
dirent_same (SeafDirent *denta, SeafDirent *dentb)
|
||||
{
|
||||
return (strcmp (dentb->id, denta->id) == 0 &&
|
||||
denta->mode == dentb->mode &&
|
||||
denta->mtime == dentb->mtime);
|
||||
}
|
||||
|
||||
static int
|
||||
diff_files (int n, SeafDirent *dents[], const char *basedir, DiffOptions *opt)
|
||||
{
|
||||
SeafDirent *files[3];
|
||||
int i, n_files = 0;
|
||||
|
||||
memset (files, 0, sizeof(files[0])*n);
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (dents[i] && S_ISREG(dents[i]->mode)) {
|
||||
files[i] = dents[i];
|
||||
++n_files;
|
||||
}
|
||||
}
|
||||
|
||||
if (n_files == 0)
|
||||
return 0;
|
||||
|
||||
return opt->file_cb (n, basedir, files, opt->data);
|
||||
}
|
||||
|
||||
static int
|
||||
diff_trees_recursive (int n, SeafDir *trees[],
|
||||
const char *basedir, DiffOptions *opt);
|
||||
|
||||
static int
|
||||
diff_directories (int n, SeafDirent *dents[], const char *basedir, DiffOptions *opt)
|
||||
{
|
||||
SeafDirent *dirs[3];
|
||||
int i, n_dirs = 0;
|
||||
char *dirname = "";
|
||||
int ret;
|
||||
SeafDir *sub_dirs[3], *dir;
|
||||
|
||||
memset (dirs, 0, sizeof(dirs[0])*n);
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (dents[i] && S_ISDIR(dents[i]->mode)) {
|
||||
dirs[i] = dents[i];
|
||||
++n_dirs;
|
||||
}
|
||||
}
|
||||
|
||||
if (n_dirs == 0)
|
||||
return 0;
|
||||
|
||||
gboolean recurse = TRUE;
|
||||
ret = opt->dir_cb (n, basedir, dirs, opt->data, &recurse);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
if (!recurse)
|
||||
return 0;
|
||||
|
||||
memset (sub_dirs, 0, sizeof(sub_dirs[0])*n);
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (dents[i] != NULL && S_ISDIR(dents[i]->mode)) {
|
||||
dir = seaf_fs_manager_get_seafdir (seaf->fs_mgr,
|
||||
opt->store_id,
|
||||
opt->version,
|
||||
dents[i]->id);
|
||||
if (!dir) {
|
||||
seaf_warning ("Failed to find dir %s:%s.\n",
|
||||
opt->store_id, dents[i]->id);
|
||||
ret = -1;
|
||||
goto free_sub_dirs;
|
||||
}
|
||||
sub_dirs[i] = dir;
|
||||
|
||||
dirname = dents[i]->name;
|
||||
}
|
||||
}
|
||||
|
||||
char *new_basedir = g_strconcat (basedir, dirname, "/", NULL);
|
||||
|
||||
ret = diff_trees_recursive (n, sub_dirs, new_basedir, opt);
|
||||
|
||||
g_free (new_basedir);
|
||||
|
||||
free_sub_dirs:
|
||||
for (i = 0; i < n; ++i)
|
||||
seaf_dir_free (sub_dirs[i]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
diff_trees_recursive (int n, SeafDir *trees[],
|
||||
const char *basedir, DiffOptions *opt)
|
||||
{
|
||||
GList *ptrs[3];
|
||||
SeafDirent *dents[3];
|
||||
int i;
|
||||
SeafDirent *dent;
|
||||
char *first_name;
|
||||
gboolean done;
|
||||
int ret = 0;
|
||||
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (trees[i])
|
||||
ptrs[i] = trees[i]->entries;
|
||||
else
|
||||
ptrs[i] = NULL;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
first_name = NULL;
|
||||
memset (dents, 0, sizeof(dents[0])*n);
|
||||
done = TRUE;
|
||||
|
||||
/* Find the "largest" name, assuming dirents are sorted. */
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (ptrs[i] != NULL) {
|
||||
done = FALSE;
|
||||
dent = ptrs[i]->data;
|
||||
if (!first_name)
|
||||
first_name = dent->name;
|
||||
else if (strcmp(dent->name, first_name) > 0)
|
||||
first_name = dent->name;
|
||||
}
|
||||
}
|
||||
|
||||
if (done)
|
||||
break;
|
||||
|
||||
/*
|
||||
* Setup dir entries for all names that equal to first_name
|
||||
*/
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (ptrs[i] != NULL) {
|
||||
dent = ptrs[i]->data;
|
||||
if (strcmp(first_name, dent->name) == 0) {
|
||||
dents[i] = dent;
|
||||
ptrs[i] = ptrs[i]->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (n == 2 && dents[0] && dents[1] && dirent_same(dents[0], dents[1]))
|
||||
continue;
|
||||
|
||||
if (n == 3 && dents[0] && dents[1] && dents[2] &&
|
||||
dirent_same(dents[0], dents[1]) && dirent_same(dents[0], dents[2]))
|
||||
continue;
|
||||
|
||||
/* Diff files of this level. */
|
||||
ret = diff_files (n, dents, basedir, opt);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
/* Recurse into sub level. */
|
||||
ret = diff_directories (n, dents, basedir, opt);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
diff_trees (int n, const char *roots[], DiffOptions *opt)
|
||||
{
|
||||
SeafDir **trees, *root;
|
||||
int i, ret;
|
||||
|
||||
g_return_val_if_fail (n == 2 || n == 3, -1);
|
||||
|
||||
trees = g_new0 (SeafDir *, n);
|
||||
for (i = 0; i < n; ++i) {
|
||||
root = seaf_fs_manager_get_seafdir (seaf->fs_mgr,
|
||||
opt->store_id,
|
||||
opt->version,
|
||||
roots[i]);
|
||||
if (!root) {
|
||||
seaf_warning ("Failed to find dir %s:%s.\n", opt->store_id, roots[i]);
|
||||
g_free (trees);
|
||||
return -1;
|
||||
}
|
||||
trees[i] = root;
|
||||
}
|
||||
|
||||
ret = diff_trees_recursive (n, trees, "", opt);
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
seaf_dir_free (trees[i]);
|
||||
g_free (trees);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
typedef struct DiffData {
|
||||
GList **results;
|
||||
gboolean fold_dir_diff;
|
||||
} DiffData;
|
||||
|
||||
static int
|
||||
twoway_diff_files (int n, const char *basedir, SeafDirent *files[], void *vdata)
|
||||
{
|
||||
DiffData *data = vdata;
|
||||
GList **results = data->results;
|
||||
DiffEntry *de;
|
||||
SeafDirent *tree1 = files[0];
|
||||
SeafDirent *tree2 = files[1];
|
||||
|
||||
if (!tree1) {
|
||||
de = diff_entry_new_from_dirent (DIFF_TYPE_COMMITS, DIFF_STATUS_ADDED,
|
||||
tree2, basedir);
|
||||
*results = g_list_prepend (*results, de);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!tree2) {
|
||||
de = diff_entry_new_from_dirent (DIFF_TYPE_COMMITS, DIFF_STATUS_DELETED,
|
||||
tree1, basedir);
|
||||
*results = g_list_prepend (*results, de);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!dirent_same (tree1, tree2)) {
|
||||
de = diff_entry_new_from_dirent (DIFF_TYPE_COMMITS, DIFF_STATUS_MODIFIED,
|
||||
tree2, basedir);
|
||||
*results = g_list_prepend (*results, de);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
twoway_diff_dirs (int n, const char *basedir, SeafDirent *dirs[], void *vdata,
|
||||
gboolean *recurse)
|
||||
{
|
||||
DiffData *data = vdata;
|
||||
GList **results = data->results;
|
||||
DiffEntry *de;
|
||||
SeafDirent *tree1 = dirs[0];
|
||||
SeafDirent *tree2 = dirs[1];
|
||||
|
||||
if (!tree1) {
|
||||
if (strcmp (tree2->id, EMPTY_SHA1) == 0 || data->fold_dir_diff) {
|
||||
de = diff_entry_new_from_dirent (DIFF_TYPE_COMMITS, DIFF_STATUS_DIR_ADDED,
|
||||
tree2, basedir);
|
||||
*results = g_list_prepend (*results, de);
|
||||
*recurse = FALSE;
|
||||
} else
|
||||
*recurse = TRUE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!tree2) {
|
||||
de = diff_entry_new_from_dirent (DIFF_TYPE_COMMITS,
|
||||
DIFF_STATUS_DIR_DELETED,
|
||||
tree1, basedir);
|
||||
*results = g_list_prepend (*results, de);
|
||||
|
||||
if (data->fold_dir_diff) {
|
||||
*recurse = FALSE;
|
||||
} else
|
||||
*recurse = TRUE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
diff_commits (SeafCommit *commit1, SeafCommit *commit2, GList **results,
|
||||
gboolean fold_dir_diff)
|
||||
{
|
||||
SeafRepo *repo = NULL;
|
||||
DiffOptions opt;
|
||||
const char *roots[2];
|
||||
|
||||
repo = seaf_repo_manager_get_repo (seaf->repo_mgr, commit1->repo_id);
|
||||
if (!repo) {
|
||||
seaf_warning ("Failed to get repo %s.\n", commit1->repo_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
DiffData data;
|
||||
memset (&data, 0, sizeof(data));
|
||||
data.results = results;
|
||||
data.fold_dir_diff = fold_dir_diff;
|
||||
|
||||
memset (&opt, 0, sizeof(opt));
|
||||
#ifdef SEAFILE_SERVER
|
||||
memcpy (opt.store_id, repo->store_id, 36);
|
||||
#else
|
||||
memcpy (opt.store_id, repo->id, 36);
|
||||
#endif
|
||||
opt.version = repo->version;
|
||||
opt.file_cb = twoway_diff_files;
|
||||
opt.dir_cb = twoway_diff_dirs;
|
||||
opt.data = &data;
|
||||
|
||||
#ifdef SEAFILE_SERVER
|
||||
seaf_repo_unref (repo);
|
||||
#endif
|
||||
|
||||
roots[0] = commit1->root_id;
|
||||
roots[1] = commit2->root_id;
|
||||
|
||||
diff_trees (2, roots, &opt);
|
||||
diff_resolve_renames (results);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
diff_commit_roots (const char *store_id, int version,
|
||||
const char *root1, const char *root2, GList **results,
|
||||
gboolean fold_dir_diff)
|
||||
{
|
||||
DiffOptions opt;
|
||||
const char *roots[2];
|
||||
|
||||
DiffData data;
|
||||
memset (&data, 0, sizeof(data));
|
||||
data.results = results;
|
||||
data.fold_dir_diff = fold_dir_diff;
|
||||
|
||||
memset (&opt, 0, sizeof(opt));
|
||||
memcpy (opt.store_id, store_id, 36);
|
||||
opt.version = version;
|
||||
opt.file_cb = twoway_diff_files;
|
||||
opt.dir_cb = twoway_diff_dirs;
|
||||
opt.data = &data;
|
||||
|
||||
roots[0] = root1;
|
||||
roots[1] = root2;
|
||||
|
||||
diff_trees (2, roots, &opt);
|
||||
diff_resolve_renames (results);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
threeway_diff_files (int n, const char *basedir, SeafDirent *files[], void *vdata)
|
||||
{
|
||||
DiffData *data = vdata;
|
||||
SeafDirent *m = files[0];
|
||||
SeafDirent *p1 = files[1];
|
||||
SeafDirent *p2 = files[2];
|
||||
GList **results = data->results;
|
||||
DiffEntry *de;
|
||||
|
||||
/* diff m with both p1 and p2. */
|
||||
if (m && p1 && p2) {
|
||||
if (!dirent_same(m, p1) && !dirent_same (m, p2)) {
|
||||
de = diff_entry_new_from_dirent (DIFF_TYPE_COMMITS, DIFF_STATUS_MODIFIED,
|
||||
m, basedir);
|
||||
*results = g_list_prepend (*results, de);
|
||||
}
|
||||
} else if (!m && p1 && p2) {
|
||||
de = diff_entry_new_from_dirent (DIFF_TYPE_COMMITS, DIFF_STATUS_DELETED,
|
||||
p1, basedir);
|
||||
*results = g_list_prepend (*results, de);
|
||||
} else if (m && !p1 && p2) {
|
||||
if (!dirent_same (m, p2)) {
|
||||
de = diff_entry_new_from_dirent (DIFF_TYPE_COMMITS, DIFF_STATUS_MODIFIED,
|
||||
m, basedir);
|
||||
*results = g_list_prepend (*results, de);
|
||||
}
|
||||
} else if (m && p1 && !p2) {
|
||||
if (!dirent_same (m, p1)) {
|
||||
de = diff_entry_new_from_dirent (DIFF_TYPE_COMMITS, DIFF_STATUS_MODIFIED,
|
||||
m, basedir);
|
||||
*results = g_list_prepend (*results, de);
|
||||
}
|
||||
} else if (m && !p1 && !p2) {
|
||||
de = diff_entry_new_from_dirent (DIFF_TYPE_COMMITS, DIFF_STATUS_ADDED,
|
||||
m, basedir);
|
||||
*results = g_list_prepend (*results, de);
|
||||
}
|
||||
/* Nothing to do for:
|
||||
* 1. !m && p1 && !p2;
|
||||
* 2. !m && !p1 && p2;
|
||||
* 3. !m && !p1 && !p2 (should not happen)
|
||||
*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
threeway_diff_dirs (int n, const char *basedir, SeafDirent *dirs[], void *vdata,
|
||||
gboolean *recurse)
|
||||
{
|
||||
*recurse = TRUE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
diff_merge (SeafCommit *merge, GList **results, gboolean fold_dir_diff)
|
||||
{
|
||||
SeafRepo *repo = NULL;
|
||||
DiffOptions opt;
|
||||
const char *roots[3];
|
||||
SeafCommit *parent1, *parent2;
|
||||
|
||||
g_return_val_if_fail (*results == NULL, -1);
|
||||
g_return_val_if_fail (merge->parent_id != NULL &&
|
||||
merge->second_parent_id != NULL,
|
||||
-1);
|
||||
|
||||
repo = seaf_repo_manager_get_repo (seaf->repo_mgr, merge->repo_id);
|
||||
if (!repo) {
|
||||
seaf_warning ("Failed to get repo %s.\n", merge->repo_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
parent1 = seaf_commit_manager_get_commit (seaf->commit_mgr,
|
||||
repo->id,
|
||||
repo->version,
|
||||
merge->parent_id);
|
||||
if (!parent1) {
|
||||
seaf_warning ("failed to find commit %s:%s.\n", repo->id, merge->parent_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
parent2 = seaf_commit_manager_get_commit (seaf->commit_mgr,
|
||||
repo->id,
|
||||
repo->version,
|
||||
merge->second_parent_id);
|
||||
if (!parent2) {
|
||||
seaf_warning ("failed to find commit %s:%s.\n",
|
||||
repo->id, merge->second_parent_id);
|
||||
seaf_commit_unref (parent1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
DiffData data;
|
||||
memset (&data, 0, sizeof(data));
|
||||
data.results = results;
|
||||
data.fold_dir_diff = fold_dir_diff;
|
||||
|
||||
memset (&opt, 0, sizeof(opt));
|
||||
#ifdef SEAFILE_SERVER
|
||||
memcpy (opt.store_id, repo->store_id, 36);
|
||||
#else
|
||||
memcpy (opt.store_id, repo->id, 36);
|
||||
#endif
|
||||
opt.version = repo->version;
|
||||
opt.file_cb = threeway_diff_files;
|
||||
opt.dir_cb = threeway_diff_dirs;
|
||||
opt.data = &data;
|
||||
|
||||
#ifdef SEAFILE_SERVER
|
||||
seaf_repo_unref (repo);
|
||||
#endif
|
||||
|
||||
roots[0] = merge->root_id;
|
||||
roots[1] = parent1->root_id;
|
||||
roots[2] = parent2->root_id;
|
||||
|
||||
int ret = diff_trees (3, roots, &opt);
|
||||
diff_resolve_renames (results);
|
||||
|
||||
seaf_commit_unref (parent1);
|
||||
seaf_commit_unref (parent2);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
diff_merge_roots (const char *store_id, int version,
|
||||
const char *merged_root, const char *p1_root, const char *p2_root,
|
||||
GList **results, gboolean fold_dir_diff)
|
||||
{
|
||||
DiffOptions opt;
|
||||
const char *roots[3];
|
||||
|
||||
g_return_val_if_fail (*results == NULL, -1);
|
||||
|
||||
DiffData data;
|
||||
memset (&data, 0, sizeof(data));
|
||||
data.results = results;
|
||||
data.fold_dir_diff = fold_dir_diff;
|
||||
|
||||
memset (&opt, 0, sizeof(opt));
|
||||
memcpy (opt.store_id, store_id, 36);
|
||||
opt.version = version;
|
||||
opt.file_cb = threeway_diff_files;
|
||||
opt.dir_cb = threeway_diff_dirs;
|
||||
opt.data = &data;
|
||||
|
||||
roots[0] = merged_root;
|
||||
roots[1] = p1_root;
|
||||
roots[2] = p2_root;
|
||||
|
||||
diff_trees (3, roots, &opt);
|
||||
diff_resolve_renames (results);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* This function only resolve "strict" rename, i.e. two files must be
|
||||
* exactly the same.
|
||||
* Don't detect rename of empty files and empty dirs.
|
||||
*/
|
||||
void
|
||||
diff_resolve_renames (GList **diff_entries)
|
||||
{
|
||||
GHashTable *deleted;
|
||||
GList *p;
|
||||
GList *added = NULL;
|
||||
DiffEntry *de;
|
||||
unsigned char empty_sha1[20];
|
||||
|
||||
memset (empty_sha1, 0, 20);
|
||||
|
||||
/* Hash and equal functions for raw sha1. */
|
||||
deleted = g_hash_table_new (ccnet_sha1_hash, ccnet_sha1_equal);
|
||||
|
||||
/* Collect all "deleted" entries. */
|
||||
for (p = *diff_entries; p != NULL; p = p->next) {
|
||||
de = p->data;
|
||||
if ((de->status == DIFF_STATUS_DELETED ||
|
||||
de->status == DIFF_STATUS_DIR_DELETED) &&
|
||||
memcmp (de->sha1, empty_sha1, 20) != 0)
|
||||
g_hash_table_insert (deleted, de->sha1, p);
|
||||
}
|
||||
|
||||
/* Collect all "added" entries into a separate list. */
|
||||
for (p = *diff_entries; p != NULL; p = p->next) {
|
||||
de = p->data;
|
||||
if ((de->status == DIFF_STATUS_ADDED ||
|
||||
de->status == DIFF_STATUS_DIR_ADDED) &&
|
||||
memcmp (de->sha1, empty_sha1, 20) != 0)
|
||||
added = g_list_prepend (added, p);
|
||||
}
|
||||
|
||||
/* For each "added" entry, if we find a "deleted" entry with
|
||||
* the same content, we find a rename pair.
|
||||
*/
|
||||
p = added;
|
||||
while (p != NULL) {
|
||||
GList *p_add, *p_del;
|
||||
DiffEntry *de_add, *de_del, *de_rename;
|
||||
int rename_status;
|
||||
|
||||
p_add = p->data;
|
||||
de_add = p_add->data;
|
||||
|
||||
p_del = g_hash_table_lookup (deleted, de_add->sha1);
|
||||
if (p_del) {
|
||||
de_del = p_del->data;
|
||||
|
||||
if (de_add->status == DIFF_STATUS_DIR_ADDED)
|
||||
rename_status = DIFF_STATUS_DIR_RENAMED;
|
||||
else
|
||||
rename_status = DIFF_STATUS_RENAMED;
|
||||
|
||||
de_rename = diff_entry_new (de_del->type, rename_status,
|
||||
de_del->sha1, de_del->name);
|
||||
de_rename->new_name = g_strdup(de_add->name);
|
||||
|
||||
*diff_entries = g_list_delete_link (*diff_entries, p_add);
|
||||
*diff_entries = g_list_delete_link (*diff_entries, p_del);
|
||||
*diff_entries = g_list_prepend (*diff_entries, de_rename);
|
||||
|
||||
g_hash_table_remove (deleted, de_add->sha1);
|
||||
|
||||
diff_entry_free (de_add);
|
||||
diff_entry_free (de_del);
|
||||
}
|
||||
|
||||
p = g_list_delete_link (p, p);
|
||||
}
|
||||
|
||||
g_hash_table_destroy (deleted);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
is_redundant_empty_dir (DiffEntry *de_dir, DiffEntry *de_file)
|
||||
{
|
||||
int dir_len;
|
||||
|
||||
if (de_dir->status == DIFF_STATUS_DIR_ADDED &&
|
||||
de_file->status == DIFF_STATUS_DELETED)
|
||||
{
|
||||
dir_len = strlen (de_dir->name);
|
||||
if (strlen (de_file->name) > dir_len &&
|
||||
strncmp (de_dir->name, de_file->name, dir_len) == 0)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (de_dir->status == DIFF_STATUS_DIR_DELETED &&
|
||||
de_file->status == DIFF_STATUS_ADDED)
|
||||
{
|
||||
dir_len = strlen (de_dir->name);
|
||||
if (strlen (de_file->name) > dir_len &&
|
||||
strncmp (de_dir->name, de_file->name, dir_len) == 0)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/*
|
||||
* An empty dir entry may be added by deleting all the files under it.
|
||||
* Similarly, an empty dir entry may be deleted by adding some file in it.
|
||||
* In both cases, we don't want to include the empty dir entry in the
|
||||
* diff results.
|
||||
*/
|
||||
void
|
||||
diff_resolve_empty_dirs (GList **diff_entries)
|
||||
{
|
||||
GList *empty_dirs = NULL;
|
||||
GList *p, *dir, *file;
|
||||
DiffEntry *de, *de_dir, *de_file;
|
||||
|
||||
for (p = *diff_entries; p != NULL; p = p->next) {
|
||||
de = p->data;
|
||||
if (de->status == DIFF_STATUS_DIR_ADDED ||
|
||||
de->status == DIFF_STATUS_DIR_DELETED)
|
||||
empty_dirs = g_list_prepend (empty_dirs, p);
|
||||
}
|
||||
|
||||
for (dir = empty_dirs; dir != NULL; dir = dir->next) {
|
||||
de_dir = ((GList *)dir->data)->data;
|
||||
for (file = *diff_entries; file != NULL; file = file->next) {
|
||||
de_file = file->data;
|
||||
if (is_redundant_empty_dir (de_dir, de_file)) {
|
||||
*diff_entries = g_list_delete_link (*diff_entries, dir->data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
g_list_free (empty_dirs);
|
||||
}
|
||||
|
||||
int diff_unmerged_state(int mask)
|
||||
{
|
||||
mask >>= 1;
|
||||
switch (mask) {
|
||||
case 7:
|
||||
return STATUS_UNMERGED_BOTH_CHANGED;
|
||||
case 3:
|
||||
return STATUS_UNMERGED_OTHERS_REMOVED;
|
||||
case 5:
|
||||
return STATUS_UNMERGED_I_REMOVED;
|
||||
case 6:
|
||||
return STATUS_UNMERGED_BOTH_ADDED;
|
||||
case 2:
|
||||
return STATUS_UNMERGED_DFC_I_ADDED_FILE;
|
||||
case 4:
|
||||
return STATUS_UNMERGED_DFC_OTHERS_ADDED_FILE;
|
||||
default:
|
||||
seaf_warning ("Unexpected unmerged case\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
format_diff_results(GList *results)
|
||||
{
|
||||
GList *ptr;
|
||||
GString *fmt_status;
|
||||
DiffEntry *de;
|
||||
|
||||
fmt_status = g_string_new("");
|
||||
|
||||
for (ptr = results; ptr; ptr = ptr->next) {
|
||||
de = ptr->data;
|
||||
|
||||
if (de->status != DIFF_STATUS_RENAMED)
|
||||
g_string_append_printf(fmt_status, "%c %c %d %u %s\n",
|
||||
de->type, de->status, de->unmerge_state,
|
||||
(int)strlen(de->name), de->name);
|
||||
else
|
||||
g_string_append_printf(fmt_status, "%c %c %d %u %s %u %s\n",
|
||||
de->type, de->status, de->unmerge_state,
|
||||
(int)strlen(de->name), de->name,
|
||||
(int)strlen(de->new_name), de->new_name);
|
||||
}
|
||||
|
||||
return g_string_free(fmt_status, FALSE);
|
||||
}
|
||||
|
||||
inline static char *
|
||||
get_basename (char *path)
|
||||
{
|
||||
char *slash;
|
||||
slash = strrchr (path, '/');
|
||||
if (!slash)
|
||||
return path;
|
||||
return (slash + 1);
|
||||
}
|
||||
|
||||
char *
|
||||
diff_results_to_description (GList *results)
|
||||
{
|
||||
GList *p;
|
||||
DiffEntry *de;
|
||||
char *add_mod_file = NULL, *removed_file = NULL;
|
||||
char *renamed_file = NULL;
|
||||
char *new_dir = NULL, *removed_dir = NULL;
|
||||
int n_add_mod = 0, n_removed = 0, n_renamed = 0;
|
||||
int n_new_dir = 0, n_removed_dir = 0;
|
||||
GString *desc;
|
||||
|
||||
if (results == NULL)
|
||||
return NULL;
|
||||
|
||||
for (p = results; p != NULL; p = p->next) {
|
||||
de = p->data;
|
||||
switch (de->status) {
|
||||
case DIFF_STATUS_ADDED:
|
||||
if (n_add_mod == 0)
|
||||
add_mod_file = get_basename(de->name);
|
||||
n_add_mod++;
|
||||
break;
|
||||
case DIFF_STATUS_DELETED:
|
||||
if (n_removed == 0)
|
||||
removed_file = get_basename(de->name);
|
||||
n_removed++;
|
||||
break;
|
||||
case DIFF_STATUS_RENAMED:
|
||||
if (n_renamed == 0)
|
||||
renamed_file = get_basename(de->name);
|
||||
n_renamed++;
|
||||
break;
|
||||
case DIFF_STATUS_MODIFIED:
|
||||
if (n_add_mod == 0)
|
||||
add_mod_file = get_basename(de->name);
|
||||
n_add_mod++;
|
||||
break;
|
||||
case DIFF_STATUS_DIR_ADDED:
|
||||
if (n_new_dir == 0)
|
||||
new_dir = get_basename(de->name);
|
||||
n_new_dir++;
|
||||
break;
|
||||
case DIFF_STATUS_DIR_DELETED:
|
||||
if (n_removed_dir == 0)
|
||||
removed_dir = get_basename(de->name);
|
||||
n_removed_dir++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
desc = g_string_new ("");
|
||||
|
||||
if (n_add_mod == 1)
|
||||
g_string_append_printf (desc, "Added or modified \"%s\".\n", add_mod_file);
|
||||
else if (n_add_mod > 1)
|
||||
g_string_append_printf (desc, "Added or modified \"%s\" and %d more files.\n",
|
||||
add_mod_file, n_add_mod - 1);
|
||||
|
||||
if (n_removed == 1)
|
||||
g_string_append_printf (desc, "Deleted \"%s\".\n", removed_file);
|
||||
else if (n_removed > 1)
|
||||
g_string_append_printf (desc, "Deleted \"%s\" and %d more files.\n",
|
||||
removed_file, n_removed - 1);
|
||||
|
||||
if (n_renamed == 1)
|
||||
g_string_append_printf (desc, "Renamed \"%s\".\n", renamed_file);
|
||||
else if (n_renamed > 1)
|
||||
g_string_append_printf (desc, "Renamed \"%s\" and %d more files.\n",
|
||||
renamed_file, n_renamed - 1);
|
||||
|
||||
if (n_new_dir == 1)
|
||||
g_string_append_printf (desc, "Added directory \"%s\".\n", new_dir);
|
||||
else if (n_new_dir > 1)
|
||||
g_string_append_printf (desc, "Added \"%s\" and %d more directories.\n",
|
||||
new_dir, n_new_dir - 1);
|
||||
|
||||
if (n_removed_dir == 1)
|
||||
g_string_append_printf (desc, "Removed directory \"%s\".\n", removed_dir);
|
||||
else if (n_removed_dir > 1)
|
||||
g_string_append_printf (desc, "Removed \"%s\" and %d more directories.\n",
|
||||
removed_dir, n_removed_dir - 1);
|
||||
|
||||
return g_string_free (desc, FALSE);
|
||||
}
|
121
common/diff-simple.h
Normal file
121
common/diff-simple.h
Normal file
|
@ -0,0 +1,121 @@
|
|||
#ifndef DIFF_SIMPLE_H
|
||||
#define DIFF_SIMPLE_H
|
||||
|
||||
#include <glib.h>
|
||||
#include "seafile-session.h"
|
||||
|
||||
#define DIFF_TYPE_WORKTREE 'W' /* diff from index to worktree */
|
||||
#define DIFF_TYPE_INDEX 'I' /* diff from commit to index */
|
||||
#define DIFF_TYPE_COMMITS 'C' /* diff between two commits*/
|
||||
|
||||
#define DIFF_STATUS_ADDED 'A'
|
||||
#define DIFF_STATUS_DELETED 'D'
|
||||
#define DIFF_STATUS_MODIFIED 'M'
|
||||
#define DIFF_STATUS_RENAMED 'R'
|
||||
#define DIFF_STATUS_UNMERGED 'U'
|
||||
#define DIFF_STATUS_DIR_ADDED 'B'
|
||||
#define DIFF_STATUS_DIR_DELETED 'C'
|
||||
#define DIFF_STATUS_DIR_RENAMED 'E'
|
||||
|
||||
enum {
|
||||
STATUS_UNMERGED_NONE,
|
||||
/* I and others modified the same file differently. */
|
||||
STATUS_UNMERGED_BOTH_CHANGED,
|
||||
/* I and others created the same file with different contents. */
|
||||
STATUS_UNMERGED_BOTH_ADDED,
|
||||
/* I removed a file while others modified it. */
|
||||
STATUS_UNMERGED_I_REMOVED,
|
||||
/* Others removed a file while I modified it. */
|
||||
STATUS_UNMERGED_OTHERS_REMOVED,
|
||||
/* I replace a directory with a file while others modified files under the directory. */
|
||||
STATUS_UNMERGED_DFC_I_ADDED_FILE,
|
||||
/* Others replace a directory with a file while I modified files under the directory. */
|
||||
STATUS_UNMERGED_DFC_OTHERS_ADDED_FILE,
|
||||
};
|
||||
|
||||
typedef struct DiffEntry {
|
||||
char type;
|
||||
char status;
|
||||
int unmerge_state;
|
||||
unsigned char sha1[20]; /* used for resolve rename */
|
||||
char *name;
|
||||
char *new_name; /* only used in rename. */
|
||||
|
||||
#ifdef SEAFILE_CLIENT
|
||||
/* Fields only used for ADDED, DIR_ADDED, MODIFIED types,
|
||||
* used in check out files/dirs.*/
|
||||
gint64 mtime;
|
||||
unsigned int mode;
|
||||
char *modifier;
|
||||
gint64 size;
|
||||
#endif
|
||||
} DiffEntry;
|
||||
|
||||
DiffEntry *
|
||||
diff_entry_new (char type, char status, unsigned char *sha1, const char *name);
|
||||
|
||||
void
|
||||
diff_entry_free (DiffEntry *de);
|
||||
|
||||
/*
|
||||
* @fold_dir_diff: if TRUE, only the top level directory will be included
|
||||
* in the diff result if a directory with files is added or removed.
|
||||
* Otherwise all the files in the direcotory will be recursively
|
||||
* included in the diff result.
|
||||
*/
|
||||
int
|
||||
diff_commits (SeafCommit *commit1, SeafCommit *commit2, GList **results,
|
||||
gboolean fold_dir_diff);
|
||||
|
||||
int
|
||||
diff_commit_roots (const char *store_id, int version,
|
||||
const char *root1, const char *root2, GList **results,
|
||||
gboolean fold_dir_diff);
|
||||
|
||||
int
|
||||
diff_merge (SeafCommit *merge, GList **results, gboolean fold_dir_diff);
|
||||
|
||||
int
|
||||
diff_merge_roots (const char *store_id, int version,
|
||||
const char *merged_root, const char *p1_root, const char *p2_root,
|
||||
GList **results, gboolean fold_dir_diff);
|
||||
|
||||
void
|
||||
diff_resolve_renames (GList **diff_entries);
|
||||
|
||||
void
|
||||
diff_resolve_empty_dirs (GList **diff_entries);
|
||||
|
||||
int
|
||||
diff_unmerged_state(int mask);
|
||||
|
||||
char *
|
||||
format_diff_results(GList *results);
|
||||
|
||||
char *
|
||||
diff_results_to_description (GList *results);
|
||||
|
||||
typedef int (*DiffFileCB) (int n,
|
||||
const char *basedir,
|
||||
SeafDirent *files[],
|
||||
void *data);
|
||||
|
||||
typedef int (*DiffDirCB) (int n,
|
||||
const char *basedir,
|
||||
SeafDirent *dirs[],
|
||||
void *data,
|
||||
gboolean *recurse);
|
||||
|
||||
typedef struct DiffOptions {
|
||||
char store_id[37];
|
||||
int version;
|
||||
|
||||
DiffFileCB file_cb;
|
||||
DiffDirCB dir_cb;
|
||||
void *data;
|
||||
} DiffOptions;
|
||||
|
||||
int
|
||||
diff_trees (int n, const char *roots[], DiffOptions *opt);
|
||||
|
||||
#endif
|
3033
common/fs-mgr.c
Normal file
3033
common/fs-mgr.c
Normal file
File diff suppressed because it is too large
Load diff
423
common/fs-mgr.h
Normal file
423
common/fs-mgr.h
Normal file
|
@ -0,0 +1,423 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef SEAF_FILE_MGR_H
|
||||
#define SEAF_FILE_MGR_H
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "seafile-object.h"
|
||||
|
||||
#include "obj-store.h"
|
||||
|
||||
#include "cdc/cdc.h"
|
||||
#include "../common/seafile-crypt.h"
|
||||
|
||||
#define CURRENT_DIR_OBJ_VERSION 1
|
||||
#define CURRENT_SEAFILE_OBJ_VERSION 1
|
||||
|
||||
typedef struct _SeafFSManager SeafFSManager;
|
||||
typedef struct _SeafFSObject SeafFSObject;
|
||||
typedef struct _Seafile Seafile;
|
||||
typedef struct _SeafDir SeafDir;
|
||||
typedef struct _SeafDirent SeafDirent;
|
||||
|
||||
typedef enum {
|
||||
SEAF_METADATA_TYPE_INVALID,
|
||||
SEAF_METADATA_TYPE_FILE,
|
||||
SEAF_METADATA_TYPE_LINK,
|
||||
SEAF_METADATA_TYPE_DIR,
|
||||
} SeafMetadataType;
|
||||
|
||||
/* Common to seafile and seafdir objects. */
|
||||
struct _SeafFSObject {
|
||||
int type;
|
||||
};
|
||||
|
||||
struct _Seafile {
|
||||
SeafFSObject object;
|
||||
int version;
|
||||
char file_id[41];
|
||||
guint64 file_size;
|
||||
guint32 n_blocks;
|
||||
char **blk_sha1s;
|
||||
int ref_count;
|
||||
};
|
||||
|
||||
void
|
||||
seafile_ref (Seafile *seafile);
|
||||
|
||||
void
|
||||
seafile_unref (Seafile *seafile);
|
||||
|
||||
int
|
||||
seafile_save (SeafFSManager *fs_mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
Seafile *file);
|
||||
|
||||
#define SEAF_DIR_NAME_LEN 256
|
||||
|
||||
struct _SeafDirent {
|
||||
int version;
|
||||
guint32 mode;
|
||||
char id[41];
|
||||
guint32 name_len;
|
||||
char *name;
|
||||
|
||||
/* attributes for version > 0 */
|
||||
gint64 mtime;
|
||||
char *modifier; /* for files only */
|
||||
gint64 size; /* for files only */
|
||||
};
|
||||
|
||||
struct _SeafDir {
|
||||
SeafFSObject object;
|
||||
int version;
|
||||
char dir_id[41];
|
||||
GList *entries;
|
||||
|
||||
/* data in on-disk format. */
|
||||
void *ondisk;
|
||||
int ondisk_size;
|
||||
};
|
||||
|
||||
SeafDir *
|
||||
seaf_dir_new (const char *id, GList *entries, int version);
|
||||
|
||||
void
|
||||
seaf_dir_free (SeafDir *dir);
|
||||
|
||||
SeafDir *
|
||||
seaf_dir_from_data (const char *dir_id, uint8_t *data, int len,
|
||||
gboolean is_json);
|
||||
|
||||
void *
|
||||
seaf_dir_to_data (SeafDir *dir, int *len);
|
||||
|
||||
int
|
||||
seaf_dir_save (SeafFSManager *fs_mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
SeafDir *dir);
|
||||
|
||||
SeafDirent *
|
||||
seaf_dirent_new (int version, const char *sha1, int mode, const char *name,
|
||||
gint64 mtime, const char *modifier, gint64 size);
|
||||
|
||||
void
|
||||
seaf_dirent_free (SeafDirent *dent);
|
||||
|
||||
SeafDirent *
|
||||
seaf_dirent_dup (SeafDirent *dent);
|
||||
|
||||
int
|
||||
seaf_metadata_type_from_data (const char *obj_id,
|
||||
uint8_t *data, int len, gboolean is_json);
|
||||
|
||||
/* Parse an fs object without knowing its type. */
|
||||
SeafFSObject *
|
||||
seaf_fs_object_from_data (const char *obj_id,
|
||||
uint8_t *data, int len,
|
||||
gboolean is_json);
|
||||
|
||||
void
|
||||
seaf_fs_object_free (SeafFSObject *obj);
|
||||
|
||||
typedef struct {
|
||||
/* TODO: GHashTable may be inefficient when we have large number of IDs. */
|
||||
GHashTable *block_hash;
|
||||
GPtrArray *block_ids;
|
||||
uint32_t n_blocks;
|
||||
uint32_t n_valid_blocks;
|
||||
} BlockList;
|
||||
|
||||
BlockList *
|
||||
block_list_new ();
|
||||
|
||||
void
|
||||
block_list_free (BlockList *bl);
|
||||
|
||||
void
|
||||
block_list_insert (BlockList *bl, const char *block_id);
|
||||
|
||||
/* Return a blocklist containing block ids which are in @bl1 but
|
||||
* not in @bl2.
|
||||
*/
|
||||
BlockList *
|
||||
block_list_difference (BlockList *bl1, BlockList *bl2);
|
||||
|
||||
struct _SeafileSession;
|
||||
|
||||
typedef struct _SeafFSManagerPriv SeafFSManagerPriv;
|
||||
|
||||
struct _SeafFSManager {
|
||||
struct _SeafileSession *seaf;
|
||||
|
||||
struct SeafObjStore *obj_store;
|
||||
|
||||
SeafFSManagerPriv *priv;
|
||||
};
|
||||
|
||||
SeafFSManager *
|
||||
seaf_fs_manager_new (struct _SeafileSession *seaf,
|
||||
const char *seaf_dir);
|
||||
|
||||
int
|
||||
seaf_fs_manager_init (SeafFSManager *mgr);
|
||||
|
||||
#ifndef SEAFILE_SERVER
|
||||
|
||||
int
|
||||
seaf_fs_manager_checkout_file (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *file_id,
|
||||
const char *file_path,
|
||||
guint32 mode,
|
||||
guint64 mtime,
|
||||
struct SeafileCrypt *crypt,
|
||||
const char *in_repo_path,
|
||||
const char *conflict_head_id,
|
||||
gboolean force_conflict,
|
||||
gboolean *conflicted,
|
||||
const char *email);
|
||||
|
||||
#endif /* not SEAFILE_SERVER */
|
||||
|
||||
/**
|
||||
* Check in blocks and create seafile/symlink object.
|
||||
* Returns sha1 id for the seafile/symlink object in @sha1 parameter.
|
||||
*/
|
||||
int
|
||||
seaf_fs_manager_index_file_blocks (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
GList *paths,
|
||||
GList *blockids,
|
||||
unsigned char sha1[],
|
||||
gint64 file_size);
|
||||
|
||||
int
|
||||
seaf_fs_manager_index_raw_blocks (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
GList *paths,
|
||||
GList *blockids);
|
||||
|
||||
int
|
||||
seaf_fs_manager_index_existed_file_blocks (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
GList *blockids,
|
||||
unsigned char sha1[],
|
||||
gint64 file_size);
|
||||
int
|
||||
seaf_fs_manager_index_blocks (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *file_path,
|
||||
unsigned char sha1[],
|
||||
gint64 *size,
|
||||
SeafileCrypt *crypt,
|
||||
gboolean write_data,
|
||||
gboolean use_cdc);
|
||||
|
||||
Seafile *
|
||||
seaf_fs_manager_get_seafile (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *file_id);
|
||||
|
||||
SeafDir *
|
||||
seaf_fs_manager_get_seafdir (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *dir_id);
|
||||
|
||||
/* Make sure entries in the returned dir is sorted in descending order.
|
||||
*/
|
||||
SeafDir *
|
||||
seaf_fs_manager_get_seafdir_sorted (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *dir_id);
|
||||
|
||||
SeafDir *
|
||||
seaf_fs_manager_get_seafdir_sorted_by_path (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id,
|
||||
const char *path);
|
||||
|
||||
int
|
||||
seaf_fs_manager_populate_blocklist (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id,
|
||||
BlockList *bl);
|
||||
|
||||
/*
|
||||
* For dir object, set *stop to TRUE to stop traversing the subtree.
|
||||
*/
|
||||
typedef gboolean (*TraverseFSTreeCallback) (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
int type,
|
||||
void *user_data,
|
||||
gboolean *stop);
|
||||
|
||||
int
|
||||
seaf_fs_manager_traverse_tree (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id,
|
||||
TraverseFSTreeCallback callback,
|
||||
void *user_data,
|
||||
gboolean skip_errors);
|
||||
|
||||
typedef gboolean (*TraverseFSPathCallback) (SeafFSManager *mgr,
|
||||
const char *path,
|
||||
SeafDirent *dent,
|
||||
void *user_data,
|
||||
gboolean *stop);
|
||||
|
||||
int
|
||||
seaf_fs_manager_traverse_path (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id,
|
||||
const char *dir_path,
|
||||
TraverseFSPathCallback callback,
|
||||
void *user_data);
|
||||
|
||||
gboolean
|
||||
seaf_fs_manager_object_exists (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *id);
|
||||
|
||||
void
|
||||
seaf_fs_manager_delete_object (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *id);
|
||||
|
||||
gint64
|
||||
seaf_fs_manager_get_file_size (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *file_id);
|
||||
|
||||
gint64
|
||||
seaf_fs_manager_get_fs_size (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id);
|
||||
|
||||
#ifndef SEAFILE_SERVER
|
||||
int
|
||||
seafile_write_chunk (const char *repo_id,
|
||||
int version,
|
||||
CDCDescriptor *chunk,
|
||||
SeafileCrypt *crypt,
|
||||
uint8_t *checksum,
|
||||
gboolean write_data);
|
||||
int
|
||||
seafile_check_write_chunk (CDCDescriptor *chunk,
|
||||
uint8_t *sha1,
|
||||
gboolean write_data);
|
||||
#endif /* SEAFILE_SERVER */
|
||||
|
||||
uint32_t
|
||||
calculate_chunk_size (uint64_t total_size);
|
||||
|
||||
int
|
||||
seaf_fs_manager_count_fs_files (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id);
|
||||
|
||||
SeafDir *
|
||||
seaf_fs_manager_get_seafdir_by_path(SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id,
|
||||
const char *path,
|
||||
GError **error);
|
||||
char *
|
||||
seaf_fs_manager_get_seafile_id_by_path (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id,
|
||||
const char *path,
|
||||
GError **error);
|
||||
|
||||
char *
|
||||
seaf_fs_manager_path_to_obj_id (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id,
|
||||
const char *path,
|
||||
guint32 *mode,
|
||||
GError **error);
|
||||
|
||||
char *
|
||||
seaf_fs_manager_get_seafdir_id_by_path (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id,
|
||||
const char *path,
|
||||
GError **error);
|
||||
|
||||
SeafDirent *
|
||||
seaf_fs_manager_get_dirent_by_path (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id,
|
||||
const char *path,
|
||||
GError **error);
|
||||
|
||||
/* Check object integrity. */
|
||||
|
||||
gboolean
|
||||
seaf_fs_manager_verify_seafdir (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *dir_id,
|
||||
gboolean verify_id,
|
||||
gboolean *io_error);
|
||||
|
||||
gboolean
|
||||
seaf_fs_manager_verify_seafile (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *file_id,
|
||||
gboolean verify_id,
|
||||
gboolean *io_error);
|
||||
|
||||
gboolean
|
||||
seaf_fs_manager_verify_object (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
gboolean verify_id,
|
||||
gboolean *io_error);
|
||||
|
||||
int
|
||||
dir_version_from_repo_version (int repo_version);
|
||||
|
||||
int
|
||||
seafile_version_from_repo_version (int repo_version);
|
||||
|
||||
struct _CDCFileDescriptor;
|
||||
void
|
||||
seaf_fs_manager_calculate_seafile_id_json (int repo_version,
|
||||
struct _CDCFileDescriptor *cdc,
|
||||
guint8 *file_id_sha1);
|
||||
|
||||
int
|
||||
seaf_fs_manager_remove_store (SeafFSManager *mgr,
|
||||
const char *store_id);
|
||||
|
||||
#endif
|
232
common/log.c
Normal file
232
common/log.c
Normal file
|
@ -0,0 +1,232 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#ifndef WIN32
|
||||
#ifdef SEAFILE_SERVER
|
||||
#include <sys/syslog.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
/* message with greater log levels will be ignored */
|
||||
static int ccnet_log_level;
|
||||
static int seafile_log_level;
|
||||
static char *logfile;
|
||||
static FILE *logfp;
|
||||
|
||||
#ifndef WIN32
|
||||
#ifdef SEAFILE_SERVER
|
||||
static gboolean enable_syslog;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef WIN32
|
||||
#ifdef SEAFILE_SERVER
|
||||
static int
|
||||
get_syslog_level (GLogLevelFlags level)
|
||||
{
|
||||
switch (level) {
|
||||
case G_LOG_LEVEL_DEBUG:
|
||||
return LOG_DEBUG;
|
||||
case G_LOG_LEVEL_INFO:
|
||||
return LOG_INFO;
|
||||
case G_LOG_LEVEL_WARNING:
|
||||
return LOG_WARNING;
|
||||
case G_LOG_LEVEL_ERROR:
|
||||
return LOG_ERR;
|
||||
default:
|
||||
return LOG_DEBUG;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static void
|
||||
seafile_log (const gchar *log_domain, GLogLevelFlags log_level,
|
||||
const gchar *message, gpointer user_data)
|
||||
{
|
||||
time_t t;
|
||||
struct tm *tm;
|
||||
char buf[1024];
|
||||
int len;
|
||||
|
||||
if (log_level > seafile_log_level)
|
||||
return;
|
||||
|
||||
t = time(NULL);
|
||||
tm = localtime(&t);
|
||||
len = strftime (buf, 1024, "[%x %X] ", tm);
|
||||
g_return_if_fail (len < 1024);
|
||||
if (logfp) {
|
||||
fputs (buf, logfp);
|
||||
fputs (message, logfp);
|
||||
fflush (logfp);
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
#ifdef SEAFILE_SERVER
|
||||
if (enable_syslog)
|
||||
syslog (get_syslog_level (log_level), "%s", message);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
ccnet_log (const gchar *log_domain, GLogLevelFlags log_level,
|
||||
const gchar *message, gpointer user_data)
|
||||
{
|
||||
time_t t;
|
||||
struct tm *tm;
|
||||
char buf[1024];
|
||||
int len;
|
||||
|
||||
if (log_level > ccnet_log_level)
|
||||
return;
|
||||
|
||||
t = time(NULL);
|
||||
tm = localtime(&t);
|
||||
len = strftime (buf, 1024, "[%x %X] ", tm);
|
||||
g_return_if_fail (len < 1024);
|
||||
if (logfp) {
|
||||
fputs (buf, logfp);
|
||||
fputs (message, logfp);
|
||||
fflush (logfp);
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
#ifdef SEAFILE_SERVER
|
||||
if (enable_syslog)
|
||||
syslog (get_syslog_level (log_level), "%s", message);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
static int
|
||||
get_debug_level(const char *str, int default_level)
|
||||
{
|
||||
if (strcmp(str, "debug") == 0)
|
||||
return G_LOG_LEVEL_DEBUG;
|
||||
if (strcmp(str, "info") == 0)
|
||||
return G_LOG_LEVEL_INFO;
|
||||
if (strcmp(str, "warning") == 0)
|
||||
return G_LOG_LEVEL_WARNING;
|
||||
return default_level;
|
||||
}
|
||||
|
||||
int
|
||||
seafile_log_init (const char *_logfile, const char *ccnet_debug_level_str,
|
||||
const char *seafile_debug_level_str)
|
||||
{
|
||||
g_log_set_handler (NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
|
||||
| G_LOG_FLAG_RECURSION, seafile_log, NULL);
|
||||
g_log_set_handler ("Ccnet", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
|
||||
| G_LOG_FLAG_RECURSION, ccnet_log, NULL);
|
||||
|
||||
/* record all log message */
|
||||
ccnet_log_level = get_debug_level(ccnet_debug_level_str, G_LOG_LEVEL_INFO);
|
||||
seafile_log_level = get_debug_level(seafile_debug_level_str, G_LOG_LEVEL_DEBUG);
|
||||
|
||||
if (strcmp(_logfile, "-") == 0) {
|
||||
logfp = stdout;
|
||||
logfile = g_strdup (_logfile);
|
||||
}
|
||||
else {
|
||||
logfile = ccnet_expand_path(_logfile);
|
||||
if ((logfp = g_fopen (logfile, "a+")) == NULL) {
|
||||
seaf_message ("Failed to open file %s\n", logfile);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seafile_log_reopen ()
|
||||
{
|
||||
FILE *fp, *oldfp;
|
||||
|
||||
if (strcmp(logfile, "-") == 0)
|
||||
return 0;
|
||||
|
||||
if ((fp = g_fopen (logfile, "a+")) == NULL) {
|
||||
seaf_message ("Failed to open file %s\n", logfile);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//TODO: check file's health
|
||||
|
||||
oldfp = logfp;
|
||||
logfp = fp;
|
||||
if (fclose(oldfp) < 0) {
|
||||
seaf_message ("Failed to close file %s\n", logfile);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SeafileDebugFlags debug_flags = 0;
|
||||
|
||||
static GDebugKey debug_keys[] = {
|
||||
{ "Transfer", SEAFILE_DEBUG_TRANSFER },
|
||||
{ "Sync", SEAFILE_DEBUG_SYNC },
|
||||
{ "Watch", SEAFILE_DEBUG_WATCH },
|
||||
{ "Http", SEAFILE_DEBUG_HTTP },
|
||||
{ "Merge", SEAFILE_DEBUG_MERGE },
|
||||
{ "Other", SEAFILE_DEBUG_OTHER },
|
||||
};
|
||||
|
||||
gboolean
|
||||
seafile_debug_flag_is_set (SeafileDebugFlags flag)
|
||||
{
|
||||
return (debug_flags & flag) != 0;
|
||||
}
|
||||
|
||||
void
|
||||
seafile_debug_set_flags (SeafileDebugFlags flags)
|
||||
{
|
||||
g_message ("Set debug flags %#x\n", flags);
|
||||
debug_flags |= flags;
|
||||
}
|
||||
|
||||
void
|
||||
seafile_debug_set_flags_string (const gchar *flags_string)
|
||||
{
|
||||
guint nkeys = G_N_ELEMENTS (debug_keys);
|
||||
|
||||
if (flags_string)
|
||||
seafile_debug_set_flags (
|
||||
g_parse_debug_string (flags_string, debug_keys, nkeys));
|
||||
}
|
||||
|
||||
void
|
||||
seafile_debug_impl (SeafileDebugFlags flag, const gchar *format, ...)
|
||||
{
|
||||
if (flag & debug_flags) {
|
||||
va_list args;
|
||||
va_start (args, format);
|
||||
g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
|
||||
va_end (args);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
#ifdef SEAFILE_SERVER
|
||||
void
|
||||
set_syslog_config (GKeyFile *config)
|
||||
{
|
||||
enable_syslog = g_key_file_get_boolean (config,
|
||||
"general", "enable_syslog",
|
||||
NULL);
|
||||
if (enable_syslog)
|
||||
openlog (NULL, LOG_NDELAY | LOG_PID, LOG_USER);
|
||||
}
|
||||
#endif
|
||||
#endif
|
49
common/log.h
Normal file
49
common/log.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
#ifndef LOG_H
|
||||
#define LOG_H
|
||||
|
||||
#define SEAFILE_DOMAIN g_quark_from_string("seafile")
|
||||
|
||||
#ifndef seaf_warning
|
||||
#define seaf_warning(fmt, ...) g_warning("%s(%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#ifndef seaf_message
|
||||
#define seaf_message(fmt, ...) g_message("%s(%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
|
||||
int seafile_log_init (const char *logfile, const char *ccnet_debug_level_str,
|
||||
const char *seafile_debug_level_str);
|
||||
int seafile_log_reopen ();
|
||||
|
||||
#ifndef WIN32
|
||||
#ifdef SEAFILE_SERVER
|
||||
void
|
||||
set_syslog_config (GKeyFile *config);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void
|
||||
seafile_debug_set_flags_string (const gchar *flags_string);
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SEAFILE_DEBUG_TRANSFER = 1 << 1,
|
||||
SEAFILE_DEBUG_SYNC = 1 << 2,
|
||||
SEAFILE_DEBUG_WATCH = 1 << 3, /* wt-monitor */
|
||||
SEAFILE_DEBUG_HTTP = 1 << 4, /* http server */
|
||||
SEAFILE_DEBUG_MERGE = 1 << 5,
|
||||
SEAFILE_DEBUG_OTHER = 1 << 6,
|
||||
} SeafileDebugFlags;
|
||||
|
||||
void seafile_debug_impl (SeafileDebugFlags flag, const gchar *format, ...);
|
||||
|
||||
#ifdef DEBUG_FLAG
|
||||
|
||||
#undef seaf_debug
|
||||
#define seaf_debug(fmt, ...) \
|
||||
seafile_debug_impl (DEBUG_FLAG, "%.10s(%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
|
||||
|
||||
#endif /* DEBUG_FLAG */
|
||||
|
||||
#endif
|
589
common/merge-new.c
Normal file
589
common/merge-new.c
Normal file
|
@ -0,0 +1,589 @@
|
|||
#include "seafile-session.h"
|
||||
#include "merge-new.h"
|
||||
#include "vc-common.h"
|
||||
|
||||
#define DEBUG_FLAG SEAFILE_DEBUG_MERGE
|
||||
#include "log.h"
|
||||
|
||||
static int
|
||||
merge_trees_recursive (const char *store_id, int version,
|
||||
int n, SeafDir *trees[],
|
||||
const char *basedir,
|
||||
MergeOptions *opt);
|
||||
|
||||
static char *
|
||||
merge_conflict_filename (const char *store_id, int version,
|
||||
MergeOptions *opt,
|
||||
const char *basedir,
|
||||
const char *filename)
|
||||
{
|
||||
char *path = NULL, *modifier = NULL, *conflict_name = NULL;
|
||||
gint64 mtime;
|
||||
SeafCommit *commit;
|
||||
|
||||
path = g_strconcat (basedir, filename, NULL);
|
||||
|
||||
int rc = get_file_modifier_mtime (opt->remote_repo_id,
|
||||
store_id,
|
||||
version,
|
||||
opt->remote_head,
|
||||
path,
|
||||
&modifier, &mtime);
|
||||
if (rc < 0) {
|
||||
commit = seaf_commit_manager_get_commit (seaf->commit_mgr,
|
||||
opt->remote_repo_id,
|
||||
version,
|
||||
opt->remote_head);
|
||||
if (!commit) {
|
||||
seaf_warning ("Failed to find remote head %s:%s.\n",
|
||||
opt->remote_repo_id, opt->remote_head);
|
||||
goto out;
|
||||
}
|
||||
modifier = g_strdup(commit->creator_name);
|
||||
mtime = (gint64)time(NULL);
|
||||
seaf_commit_unref (commit);
|
||||
}
|
||||
|
||||
conflict_name = gen_conflict_path (filename, modifier, mtime);
|
||||
|
||||
out:
|
||||
g_free (path);
|
||||
g_free (modifier);
|
||||
return conflict_name;
|
||||
}
|
||||
|
||||
static char *
|
||||
merge_conflict_dirname (const char *store_id, int version,
|
||||
MergeOptions *opt,
|
||||
const char *basedir,
|
||||
const char *dirname)
|
||||
{
|
||||
char *modifier = NULL, *conflict_name = NULL;
|
||||
SeafCommit *commit;
|
||||
|
||||
commit = seaf_commit_manager_get_commit (seaf->commit_mgr,
|
||||
opt->remote_repo_id, version,
|
||||
opt->remote_head);
|
||||
if (!commit) {
|
||||
seaf_warning ("Failed to find remote head %s:%s.\n",
|
||||
opt->remote_repo_id, opt->remote_head);
|
||||
goto out;
|
||||
}
|
||||
modifier = g_strdup(commit->creator_name);
|
||||
seaf_commit_unref (commit);
|
||||
|
||||
conflict_name = gen_conflict_path (dirname, modifier, (gint64)time(NULL));
|
||||
|
||||
out:
|
||||
g_free (modifier);
|
||||
return conflict_name;
|
||||
}
|
||||
|
||||
static int
|
||||
merge_entries (const char *store_id, int version,
|
||||
int n, SeafDirent *dents[],
|
||||
const char *basedir,
|
||||
GList **dents_out,
|
||||
MergeOptions *opt)
|
||||
{
|
||||
SeafDirent *files[3];
|
||||
int i;
|
||||
|
||||
memset (files, 0, sizeof(files[0])*n);
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (dents[i] && S_ISREG(dents[i]->mode))
|
||||
files[i] = dents[i];
|
||||
}
|
||||
|
||||
/* If we're running 2-way merge, or the caller requires not to
|
||||
* actually merge contents, just call the callback function.
|
||||
*/
|
||||
if (n == 2 || !opt->do_merge)
|
||||
return opt->callback (basedir, files, opt);
|
||||
|
||||
/* Otherwise, we're doing a real 3-way merge of the trees.
|
||||
* It means merge files and handle any conflicts.
|
||||
*/
|
||||
|
||||
SeafDirent *base, *head, *remote;
|
||||
char *conflict_name;
|
||||
|
||||
base = files[0];
|
||||
head = files[1];
|
||||
remote = files[2];
|
||||
|
||||
if (head && remote) {
|
||||
if (strcmp (head->id, remote->id) == 0) {
|
||||
seaf_debug ("%s%s: files match\n", basedir, head->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(head));
|
||||
} else if (base && strcmp (base->id, head->id) == 0) {
|
||||
seaf_debug ("%s%s: unchanged in head, changed in remote\n",
|
||||
basedir, head->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(remote));
|
||||
} else if (base && strcmp (base->id, remote->id) == 0) {
|
||||
seaf_debug ("%s%s: unchanged in remote, changed in head\n",
|
||||
basedir, head->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(head));
|
||||
} else {
|
||||
/* File content conflict. */
|
||||
|
||||
seaf_debug ("%s%s: files conflict\n", basedir, head->name);
|
||||
|
||||
conflict_name = merge_conflict_filename(store_id, version,
|
||||
opt,
|
||||
basedir,
|
||||
head->name);
|
||||
if (!conflict_name)
|
||||
return -1;
|
||||
|
||||
/* Change remote entry name in place. So opt->callback
|
||||
* will see the conflict name, not the original name.
|
||||
*/
|
||||
g_free (remote->name);
|
||||
remote->name = conflict_name;
|
||||
remote->name_len = strlen (remote->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(head));
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(remote));
|
||||
|
||||
opt->conflict = TRUE;
|
||||
}
|
||||
} else if (base && !head && remote) {
|
||||
if (strcmp (base->id, remote->id) != 0) {
|
||||
if (dents[1] != NULL) {
|
||||
/* D/F conflict:
|
||||
* Head replaces file with dir, while remote change the file.
|
||||
*/
|
||||
seaf_debug ("%s%s: DFC, file -> dir, file\n",
|
||||
basedir, remote->name);
|
||||
|
||||
conflict_name = merge_conflict_filename(store_id, version,
|
||||
opt,
|
||||
basedir,
|
||||
remote->name);
|
||||
if (!conflict_name)
|
||||
return -1;
|
||||
|
||||
/* Change the name of remote, keep dir name in head unchanged.
|
||||
*/
|
||||
g_free (remote->name);
|
||||
remote->name = conflict_name;
|
||||
remote->name_len = strlen (remote->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(remote));
|
||||
|
||||
opt->conflict = TRUE;
|
||||
} else {
|
||||
/* Deleted in head and changed in remote. */
|
||||
|
||||
seaf_debug ("%s%s: deleted in head and changed in remote\n",
|
||||
basedir, remote->name);
|
||||
|
||||
/* Keep version of remote. */
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(remote));
|
||||
}
|
||||
} else {
|
||||
/* If base and remote match, the file should not be added to
|
||||
* the merge result.
|
||||
*/
|
||||
seaf_debug ("%s%s: file deleted in head, unchanged in remote\n",
|
||||
basedir, remote->name);
|
||||
}
|
||||
} else if (base && head && !remote) {
|
||||
if (strcmp (base->id, head->id) != 0) {
|
||||
if (dents[2] != NULL) {
|
||||
/* D/F conflict:
|
||||
* Remote replaces file with dir, while head change the file.
|
||||
*/
|
||||
seaf_debug ("%s%s: DFC, file -> file, dir\n",
|
||||
basedir, head->name);
|
||||
|
||||
/* We use remote head commit author name as conflict
|
||||
* suffix of a dir.
|
||||
*/
|
||||
conflict_name = merge_conflict_dirname (store_id, version,
|
||||
opt,
|
||||
basedir, dents[2]->name);
|
||||
if (!conflict_name)
|
||||
return -1;
|
||||
|
||||
/* Change remote dir name to conflict name in place. */
|
||||
g_free (dents[2]->name);
|
||||
dents[2]->name = conflict_name;
|
||||
dents[2]->name_len = strlen (dents[2]->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(head));
|
||||
|
||||
opt->conflict = TRUE;
|
||||
} else {
|
||||
/* Deleted in remote and changed in head. */
|
||||
|
||||
seaf_debug ("%s%s: deleted in remote and changed in head\n",
|
||||
basedir, head->name);
|
||||
|
||||
/* Keep version of remote. */
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(head));
|
||||
}
|
||||
} else {
|
||||
/* If base and head match, the file should not be added to
|
||||
* the merge result.
|
||||
*/
|
||||
seaf_debug ("%s%s: file deleted in remote, unchanged in head\n",
|
||||
basedir, head->name);
|
||||
}
|
||||
} else if (!base && !head && remote) {
|
||||
if (!dents[1]) {
|
||||
/* Added in remote. */
|
||||
seaf_debug ("%s%s: added in remote\n", basedir, remote->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(remote));
|
||||
} else if (dents[0] != NULL && strcmp(dents[0]->id, dents[1]->id) == 0) {
|
||||
/* Contents in the dir is not changed.
|
||||
* The dir will be deleted in merge_directories().
|
||||
*/
|
||||
seaf_debug ("%s%s: dir in head will be replaced by file in remote\n",
|
||||
basedir, remote->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(remote));
|
||||
} else {
|
||||
/* D/F conflict:
|
||||
* Contents of the dir is changed in head, while
|
||||
* remote replace the dir with a file.
|
||||
*
|
||||
* Or, head adds a new dir, while remote adds a new file,
|
||||
* with the same name.
|
||||
*/
|
||||
|
||||
seaf_debug ("%s%s: DFC, dir -> dir, file\n", basedir, remote->name);
|
||||
|
||||
conflict_name = merge_conflict_filename(store_id, version,
|
||||
opt,
|
||||
basedir,
|
||||
remote->name);
|
||||
if (!conflict_name)
|
||||
return -1;
|
||||
|
||||
g_free (remote->name);
|
||||
remote->name = conflict_name;
|
||||
remote->name_len = strlen (remote->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(remote));
|
||||
|
||||
opt->conflict = TRUE;
|
||||
}
|
||||
} else if (!base && head && !remote) {
|
||||
if (!dents[2]) {
|
||||
/* Added in remote. */
|
||||
seaf_debug ("%s%s: added in head\n", basedir, head->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(head));
|
||||
} else if (dents[0] != NULL && strcmp(dents[0]->id, dents[2]->id) == 0) {
|
||||
/* Contents in the dir is not changed.
|
||||
* The dir will be deleted in merge_directories().
|
||||
*/
|
||||
seaf_debug ("%s%s: dir in remote will be replaced by file in head\n",
|
||||
basedir, head->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(head));
|
||||
} else {
|
||||
/* D/F conflict:
|
||||
* Contents of the dir is changed in remote, while
|
||||
* head replace the dir with a file.
|
||||
*
|
||||
* Or, remote adds a new dir, while head adds a new file,
|
||||
* with the same name.
|
||||
*/
|
||||
|
||||
seaf_debug ("%s%s: DFC, dir -> file, dir\n", basedir, head->name);
|
||||
|
||||
conflict_name = merge_conflict_dirname (store_id, version,
|
||||
opt,
|
||||
basedir, dents[2]->name);
|
||||
if (!conflict_name)
|
||||
return -1;
|
||||
|
||||
g_free (dents[2]->name);
|
||||
dents[2]->name = conflict_name;
|
||||
dents[2]->name_len = strlen (dents[2]->name);
|
||||
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(head));
|
||||
|
||||
opt->conflict = TRUE;
|
||||
}
|
||||
} else if (base && !head && !remote) {
|
||||
/* Don't need to add anything to dents_out. */
|
||||
seaf_debug ("%s%s: deleted in head and remote\n", basedir, base->name);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
merge_directories (const char *store_id, int version,
|
||||
int n, SeafDirent *dents[],
|
||||
const char *basedir,
|
||||
GList **dents_out,
|
||||
MergeOptions *opt)
|
||||
{
|
||||
SeafDir *dir;
|
||||
SeafDir *sub_dirs[3];
|
||||
char *dirname = NULL;
|
||||
char *new_basedir;
|
||||
int ret = 0;
|
||||
int dir_mask = 0, i;
|
||||
SeafDirent *merged_dent;
|
||||
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (dents[i] && S_ISDIR(dents[i]->mode))
|
||||
dir_mask |= 1 << i;
|
||||
}
|
||||
|
||||
seaf_debug ("dir_mask = %d\n", dir_mask);
|
||||
|
||||
if (n == 3 && opt->do_merge) {
|
||||
switch (dir_mask) {
|
||||
case 0:
|
||||
g_return_val_if_reached (-1);
|
||||
case 1:
|
||||
/* head and remote are not dirs, nothing to merge. */
|
||||
seaf_debug ("%s%s: no dir, no need to merge\n", basedir, dents[0]->name);
|
||||
return 0;
|
||||
case 2:
|
||||
/* only head is dir, add to result directly, no need to merge. */
|
||||
seaf_debug ("%s%s: only head is dir\n", basedir, dents[1]->name);
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(dents[1]));
|
||||
return 0;
|
||||
case 3:
|
||||
if (strcmp (dents[0]->id, dents[1]->id) == 0) {
|
||||
/* Base and head are the same, but deleted in remote. */
|
||||
seaf_debug ("%s%s: dir deleted in remote\n", basedir, dents[0]->name);
|
||||
return 0;
|
||||
}
|
||||
seaf_debug ("%s%s: dir changed in head but deleted in remote\n",
|
||||
basedir, dents[1]->name);
|
||||
break;
|
||||
case 4:
|
||||
/* only remote is dir, add to result directly, no need to merge. */
|
||||
seaf_debug ("%s%s: only remote is dir\n", basedir, dents[2]->name);
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(dents[2]));
|
||||
return 0;
|
||||
case 5:
|
||||
if (strcmp (dents[0]->id, dents[2]->id) == 0) {
|
||||
/* Base and remote are the same, but deleted in head. */
|
||||
seaf_debug ("%s%s: dir deleted in head\n", basedir, dents[0]->name);
|
||||
return 0;
|
||||
}
|
||||
seaf_debug ("%s%s: dir changed in remote but deleted in head\n",
|
||||
basedir, dents[2]->name);
|
||||
break;
|
||||
case 6:
|
||||
case 7:
|
||||
if (strcmp (dents[1]->id, dents[2]->id) == 0) {
|
||||
/* Head and remote match. */
|
||||
seaf_debug ("%s%s: dir is the same in head and remote\n",
|
||||
basedir, dents[1]->name);
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(dents[1]));
|
||||
return 0;
|
||||
} else if (dents[0] && strcmp(dents[0]->id, dents[1]->id) == 0) {
|
||||
seaf_debug ("%s%s: dir changed in remote but unchanged in head\n",
|
||||
basedir, dents[1]->name);
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(dents[2]));
|
||||
return 0;
|
||||
} else if (dents[0] && strcmp(dents[0]->id, dents[2]->id) == 0) {
|
||||
seaf_debug ("%s%s: dir changed in head but unchanged in remote\n",
|
||||
basedir, dents[1]->name);
|
||||
*dents_out = g_list_prepend (*dents_out, seaf_dirent_dup(dents[1]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
seaf_debug ("%s%s: dir is changed in both head and remote, "
|
||||
"merge recursively\n", basedir, dents[1]->name);
|
||||
break;
|
||||
default:
|
||||
g_return_val_if_reached (-1);
|
||||
}
|
||||
}
|
||||
|
||||
memset (sub_dirs, 0, sizeof(sub_dirs[0])*n);
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (dents[i] != NULL && S_ISDIR(dents[i]->mode)) {
|
||||
dir = seaf_fs_manager_get_seafdir (seaf->fs_mgr,
|
||||
store_id, version,
|
||||
dents[i]->id);
|
||||
if (!dir) {
|
||||
seaf_warning ("Failed to find dir %s:%s.\n", store_id, dents[i]->id);
|
||||
ret = -1;
|
||||
goto free_sub_dirs;
|
||||
}
|
||||
opt->visit_dirs++;
|
||||
sub_dirs[i] = dir;
|
||||
|
||||
dirname = dents[i]->name;
|
||||
}
|
||||
}
|
||||
|
||||
new_basedir = g_strconcat (basedir, dirname, "/", NULL);
|
||||
|
||||
ret = merge_trees_recursive (store_id, version, n, sub_dirs, new_basedir, opt);
|
||||
|
||||
g_free (new_basedir);
|
||||
|
||||
if (n == 3 && opt->do_merge) {
|
||||
if (dir_mask == 3 || dir_mask == 6 || dir_mask == 7) {
|
||||
merged_dent = seaf_dirent_dup (dents[1]);
|
||||
memcpy (merged_dent->id, opt->merged_tree_root, 40);
|
||||
*dents_out = g_list_prepend (*dents_out, merged_dent);
|
||||
} else if (dir_mask == 5) {
|
||||
merged_dent = seaf_dirent_dup (dents[2]);
|
||||
memcpy (merged_dent->id, opt->merged_tree_root, 40);
|
||||
*dents_out = g_list_prepend (*dents_out, merged_dent);
|
||||
}
|
||||
}
|
||||
|
||||
free_sub_dirs:
|
||||
for (i = 0; i < n; ++i)
|
||||
seaf_dir_free (sub_dirs[i]);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static gint
|
||||
compare_dirents (gconstpointer a, gconstpointer b)
|
||||
{
|
||||
const SeafDirent *denta = a, *dentb = b;
|
||||
|
||||
return strcmp (dentb->name, denta->name);
|
||||
}
|
||||
|
||||
static int
|
||||
merge_trees_recursive (const char *store_id, int version,
|
||||
int n, SeafDir *trees[],
|
||||
const char *basedir,
|
||||
MergeOptions *opt)
|
||||
{
|
||||
GList *ptrs[3];
|
||||
SeafDirent *dents[3];
|
||||
int i;
|
||||
SeafDirent *dent;
|
||||
char *first_name;
|
||||
gboolean done;
|
||||
int ret = 0;
|
||||
SeafDir *merged_tree;
|
||||
GList *merged_dents = NULL;
|
||||
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (trees[i])
|
||||
ptrs[i] = trees[i]->entries;
|
||||
else
|
||||
ptrs[i] = NULL;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
first_name = NULL;
|
||||
memset (dents, 0, sizeof(dents[0])*n);
|
||||
done = TRUE;
|
||||
|
||||
/* Find the "largest" name, assuming dirents are sorted. */
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (ptrs[i] != NULL) {
|
||||
done = FALSE;
|
||||
dent = ptrs[i]->data;
|
||||
if (!first_name)
|
||||
first_name = dent->name;
|
||||
else if (strcmp(dent->name, first_name) > 0)
|
||||
first_name = dent->name;
|
||||
}
|
||||
}
|
||||
|
||||
if (done)
|
||||
break;
|
||||
|
||||
/*
|
||||
* Setup dir entries for all names that equal to first_name
|
||||
*/
|
||||
int n_files = 0, n_dirs = 0;
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (ptrs[i] != NULL) {
|
||||
dent = ptrs[i]->data;
|
||||
if (strcmp(first_name, dent->name) == 0) {
|
||||
if (S_ISREG(dent->mode))
|
||||
++n_files;
|
||||
else if (S_ISDIR(dent->mode))
|
||||
++n_dirs;
|
||||
|
||||
dents[i] = dent;
|
||||
ptrs[i] = ptrs[i]->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Merge entries of this level. */
|
||||
if (n_files > 0) {
|
||||
ret = merge_entries (store_id, version,
|
||||
n, dents, basedir, &merged_dents, opt);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Recurse into sub level. */
|
||||
if (n_dirs > 0) {
|
||||
ret = merge_directories (store_id, version,
|
||||
n, dents, basedir, &merged_dents, opt);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if (n == 3 && opt->do_merge) {
|
||||
merged_dents = g_list_sort (merged_dents, compare_dirents);
|
||||
merged_tree = seaf_dir_new (NULL, merged_dents,
|
||||
dir_version_from_repo_version(version));
|
||||
|
||||
memcpy (opt->merged_tree_root, merged_tree->dir_id, 40);
|
||||
|
||||
if ((trees[1] && strcmp (trees[1]->dir_id, merged_tree->dir_id) == 0) ||
|
||||
(trees[2] && strcmp (trees[2]->dir_id, merged_tree->dir_id) == 0)) {
|
||||
seaf_dir_free (merged_tree);
|
||||
} else {
|
||||
ret = seaf_dir_save (seaf->fs_mgr, store_id, version, merged_tree);
|
||||
seaf_dir_free (merged_tree);
|
||||
if (ret < 0) {
|
||||
seaf_warning ("Failed to save merged tree %s:%s.\n", store_id, basedir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_merge_trees (const char *store_id, int version,
|
||||
int n, const char *roots[], MergeOptions *opt)
|
||||
{
|
||||
SeafDir **trees, *root;
|
||||
int i, ret;
|
||||
|
||||
g_return_val_if_fail (n == 2 || n == 3, -1);
|
||||
|
||||
trees = g_new0 (SeafDir *, n);
|
||||
for (i = 0; i < n; ++i) {
|
||||
root = seaf_fs_manager_get_seafdir (seaf->fs_mgr, store_id, version, roots[i]);
|
||||
if (!root) {
|
||||
seaf_warning ("Failed to find dir %s:%s.\n", store_id, roots[i]);
|
||||
g_free (trees);
|
||||
return -1;
|
||||
}
|
||||
trees[i] = root;
|
||||
}
|
||||
|
||||
ret = merge_trees_recursive (store_id, version, n, trees, "", opt);
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
seaf_dir_free (trees[i]);
|
||||
g_free (trees);
|
||||
|
||||
return ret;
|
||||
}
|
34
common/merge-new.h
Normal file
34
common/merge-new.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
#ifndef MERGE_NEW_H
|
||||
#define MERGE_NEW_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "fs-mgr.h"
|
||||
|
||||
struct MergeOptions;
|
||||
|
||||
typedef int (*MergeCallback) (const char *basedir,
|
||||
SeafDirent *dirents[],
|
||||
struct MergeOptions *opt);
|
||||
|
||||
typedef struct MergeOptions {
|
||||
int n_ways; /* only 2 and 3 way merges are supported. */
|
||||
|
||||
MergeCallback callback;
|
||||
void * data;
|
||||
|
||||
/* options only used in 3-way merge. */
|
||||
char remote_repo_id[37];
|
||||
char remote_head[41];
|
||||
gboolean do_merge; /* really merge the contents
|
||||
* and handle conflicts */
|
||||
char merged_tree_root[41]; /* merge result */
|
||||
int visit_dirs;
|
||||
gboolean conflict;
|
||||
} MergeOptions;
|
||||
|
||||
int
|
||||
seaf_merge_trees (const char *store_id, int version,
|
||||
int n, const char *roots[], MergeOptions *opt);
|
||||
|
||||
#endif
|
141
common/mq-mgr.c
Normal file
141
common/mq-mgr.c
Normal file
|
@ -0,0 +1,141 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include <ccnet.h>
|
||||
|
||||
#include "mq-mgr.h"
|
||||
|
||||
#include "seafile-session.h"
|
||||
#include "log.h"
|
||||
|
||||
typedef struct _SeafMqManagerPriv SeafMqManagerPriv;
|
||||
|
||||
struct _SeafMqManagerPriv {
|
||||
CcnetMqclientProc *mqclient_proc;
|
||||
CcnetTimer *timer;
|
||||
};
|
||||
|
||||
SeafMqManager *
|
||||
seaf_mq_manager_new (SeafileSession *seaf)
|
||||
{
|
||||
CcnetClient *client = seaf->session;
|
||||
SeafMqManager *mgr;
|
||||
SeafMqManagerPriv *priv;
|
||||
|
||||
mgr = g_new0 (SeafMqManager, 1);
|
||||
priv = g_new0 (SeafMqManagerPriv, 1);
|
||||
|
||||
|
||||
mgr->seaf = seaf;
|
||||
mgr->priv = priv;
|
||||
|
||||
priv->mqclient_proc = (CcnetMqclientProc *)
|
||||
ccnet_proc_factory_create_master_processor (client->proc_factory,
|
||||
"mq-client");
|
||||
|
||||
if (!priv->mqclient_proc) {
|
||||
seaf_warning ("Failed to create mqclient proc.\n");
|
||||
g_free (mgr);
|
||||
g_free(priv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return mgr;
|
||||
}
|
||||
|
||||
static int
|
||||
start_mq_client (CcnetMqclientProc *mqclient)
|
||||
{
|
||||
if (ccnet_processor_startl ((CcnetProcessor *)mqclient, NULL) < 0) {
|
||||
ccnet_processor_done ((CcnetProcessor *)mqclient, FALSE);
|
||||
seaf_warning ("Failed to start mqclient proc\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
seaf_message ("[mq client] mq cilent is started\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_mq_manager_init (SeafMqManager *mgr)
|
||||
{
|
||||
SeafMqManagerPriv *priv = mgr->priv;
|
||||
if (start_mq_client(priv->mqclient_proc) < 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_mq_manager_start (SeafMqManager *mgr)
|
||||
{
|
||||
SeafMqManagerPriv *priv = mgr->priv;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline CcnetMessage *
|
||||
create_message (SeafMqManager *mgr, const char *app, const char *body, int flags)
|
||||
{
|
||||
CcnetClient *client = mgr->seaf->session;
|
||||
CcnetMessage *msg;
|
||||
|
||||
char *from = client->base.id;
|
||||
char *to = client->base.id;
|
||||
|
||||
msg = ccnet_message_new (from, to, app, body, flags);
|
||||
return msg;
|
||||
}
|
||||
|
||||
/* Wrap around ccnet_message_new since all messages we use are local. */
|
||||
static inline void
|
||||
_send_message (SeafMqManager *mgr, CcnetMessage *msg)
|
||||
{
|
||||
CcnetMqclientProc *mqclient_proc = mgr->priv->mqclient_proc;
|
||||
ccnet_mqclient_proc_put_message (mqclient_proc, msg);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_mq_manager_publish_message (SeafMqManager *mgr,
|
||||
CcnetMessage *msg)
|
||||
{
|
||||
_send_message (mgr, msg);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_mq_manager_publish_message_full (SeafMqManager *mgr,
|
||||
const char *app,
|
||||
const char *body,
|
||||
int flags)
|
||||
{
|
||||
CcnetMessage *msg = create_message (mgr, app, body, flags);
|
||||
_send_message (mgr, msg);
|
||||
ccnet_message_free (msg);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_mq_manager_publish_notification (SeafMqManager *mgr,
|
||||
const char *type,
|
||||
const char *content)
|
||||
{
|
||||
static const char *app = "seafile.notification";
|
||||
|
||||
GString *buf = g_string_new(NULL);
|
||||
g_string_append_printf (buf, "%s\n%s", type, content);
|
||||
|
||||
CcnetMessage *msg = create_message (mgr, app, buf->str, 0);
|
||||
_send_message (mgr, msg);
|
||||
|
||||
g_string_free (buf, TRUE);
|
||||
ccnet_message_free (msg);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_mq_manager_publish_event (SeafMqManager *mgr, const char *content)
|
||||
{
|
||||
static const char *app = "seaf_server.event";
|
||||
|
||||
CcnetMessage *msg = create_message (mgr, app, content, 0);
|
||||
_send_message (mgr, msg);
|
||||
|
||||
ccnet_message_free (msg);
|
||||
}
|
||||
|
59
common/mq-mgr.h
Normal file
59
common/mq-mgr.h
Normal file
|
@ -0,0 +1,59 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
/*
|
||||
* Mq-manager is responsible for:
|
||||
*
|
||||
* - Publishing heartbeat messages every HEARTBEAT_INTERVAL senconds to
|
||||
* indicate it's alive. If seafile-applet doesn't get the message, it would
|
||||
* check and try to restart seaf-daemon.
|
||||
*
|
||||
* - Provide API for other modules to publish their messages.
|
||||
*
|
||||
* Currently we publish these types of messages:
|
||||
*
|
||||
* - seafile.heartbeat <>
|
||||
* - seafile.transfer <start | stop >
|
||||
* - seafile.repo_sync_done <repo-name>
|
||||
* - seafile.promt_create_repo <worktree>
|
||||
* - seafile.repo_created <repo-name>
|
||||
*
|
||||
* And subscribe to no messages.
|
||||
*/
|
||||
|
||||
#ifndef SEAF_MQ_MANAGER_H
|
||||
#define SEAF_MQ_MANAGER_H
|
||||
|
||||
struct _CcnetMessage;
|
||||
|
||||
typedef struct _SeafMqManager SeafMqManager;
|
||||
|
||||
struct _SeafMqManager {
|
||||
struct _SeafileSession *seaf;
|
||||
struct _SeafMqManagerPriv *priv;
|
||||
};
|
||||
|
||||
SeafMqManager *seaf_mq_manager_new (struct _SeafileSession *seaf);
|
||||
|
||||
int seaf_mq_manager_init (SeafMqManager *mgr);
|
||||
|
||||
int seaf_mq_manager_start (SeafMqManager *mgr);
|
||||
|
||||
|
||||
void seaf_mq_manager_publish_message (SeafMqManager *mgr,
|
||||
struct _CcnetMessage *msg);
|
||||
|
||||
void
|
||||
seaf_mq_manager_publish_message_full (SeafMqManager *mgr,
|
||||
const char *app,
|
||||
const char *body,
|
||||
int flags);
|
||||
|
||||
void
|
||||
seaf_mq_manager_publish_notification (SeafMqManager *mgr,
|
||||
const char *type,
|
||||
const char *content);
|
||||
|
||||
void
|
||||
seaf_mq_manager_publish_event (SeafMqManager *mgr, const char *content);
|
||||
|
||||
#endif
|
550
common/obj-backend-fs.c
Normal file
550
common/obj-backend-fs.c
Normal file
|
@ -0,0 +1,550 @@
|
|||
#ifndef _WIN32_WINNT
|
||||
#define _WIN32_WINNT 0x500
|
||||
#endif
|
||||
|
||||
#include "common.h"
|
||||
#include "utils.h"
|
||||
#include "obj-backend.h"
|
||||
|
||||
#ifndef WIN32
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
#define DEBUG_FLAG SEAFILE_DEBUG_OTHER
|
||||
#include "log.h"
|
||||
|
||||
typedef struct FsPriv {
|
||||
char *v0_obj_dir;
|
||||
int v0_dir_len;
|
||||
char *obj_dir;
|
||||
int dir_len;
|
||||
} FsPriv;
|
||||
|
||||
static void
|
||||
id_to_path (FsPriv *priv, const char *obj_id, char path[],
|
||||
const char *repo_id, int version)
|
||||
{
|
||||
char *pos = path;
|
||||
int n;
|
||||
|
||||
#if defined MIGRATION || defined SEAFILE_CLIENT
|
||||
if (version > 0) {
|
||||
n = snprintf (path, SEAF_PATH_MAX, "%s/%s/", priv->obj_dir, repo_id);
|
||||
pos += n;
|
||||
} else {
|
||||
memcpy (pos, priv->v0_obj_dir, priv->v0_dir_len);
|
||||
pos[priv->v0_dir_len] = '/';
|
||||
pos += priv->v0_dir_len + 1;
|
||||
}
|
||||
#else
|
||||
n = snprintf (path, SEAF_PATH_MAX, "%s/%s/", priv->obj_dir, repo_id);
|
||||
pos += n;
|
||||
#endif
|
||||
|
||||
memcpy (pos, obj_id, 2);
|
||||
pos[2] = '/';
|
||||
pos += 3;
|
||||
|
||||
memcpy (pos, obj_id + 2, 41 - 2);
|
||||
}
|
||||
|
||||
static int
|
||||
obj_backend_fs_read (ObjBackend *bend,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
void **data,
|
||||
int *len)
|
||||
{
|
||||
char path[SEAF_PATH_MAX];
|
||||
gsize tmp_len;
|
||||
GError *error = NULL;
|
||||
|
||||
id_to_path (bend->priv, obj_id, path, repo_id, version);
|
||||
|
||||
/* seaf_debug ("object path: %s\n", path); */
|
||||
|
||||
g_file_get_contents (path, (gchar**)data, &tmp_len, &error);
|
||||
if (error) {
|
||||
#ifdef MIGRATION
|
||||
g_clear_error (&error);
|
||||
id_to_path (bend->priv, obj_id, path, repo_id, 1);
|
||||
g_file_get_contents (path, (gchar**)data, &tmp_len, &error);
|
||||
if (error) {
|
||||
seaf_debug ("[obj backend] Failed to read object %s: %s.\n",
|
||||
obj_id, error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
seaf_debug ("[obj backend] Failed to read object %s: %s.\n",
|
||||
obj_id, error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
*len = (int)tmp_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Flush operating system and disk caches for @fd.
|
||||
*/
|
||||
static int
|
||||
fsync_obj_contents (int fd)
|
||||
{
|
||||
#ifdef __linux__
|
||||
/* Some file systems may not support fsync().
|
||||
* In this case, just skip the error.
|
||||
*/
|
||||
if (fsync (fd) < 0) {
|
||||
if (errno == EINVAL)
|
||||
return 0;
|
||||
else {
|
||||
seaf_warning ("Failed to fsync: %s.\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
/* OS X: fcntl() is required to flush disk cache, fsync() only
|
||||
* flushes operating system cache.
|
||||
*/
|
||||
if (fcntl (fd, F_FULLFSYNC, NULL) < 0) {
|
||||
seaf_warning ("Failed to fsync: %s.\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
HANDLE handle;
|
||||
|
||||
handle = (HANDLE)_get_osfhandle (fd);
|
||||
if (handle == INVALID_HANDLE_VALUE) {
|
||||
seaf_warning ("Failed to get handle from fd.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!FlushFileBuffers (handle)) {
|
||||
seaf_warning ("FlushFileBuffer() failed: %lu.\n", GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Rename file from @tmp_path to @obj_path.
|
||||
* This also makes sure the changes to @obj_path's parent folder
|
||||
* is flushed to disk.
|
||||
*/
|
||||
static int
|
||||
rename_and_sync (const char *tmp_path, const char *obj_path)
|
||||
{
|
||||
#ifdef __linux__
|
||||
char *parent_dir;
|
||||
int ret = 0;
|
||||
|
||||
if (rename (tmp_path, obj_path) < 0) {
|
||||
seaf_warning ("Failed to rename from %s to %s: %s.\n",
|
||||
tmp_path, obj_path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
parent_dir = g_path_get_dirname (obj_path);
|
||||
int dir_fd = open (parent_dir, O_RDONLY);
|
||||
if (dir_fd < 0) {
|
||||
seaf_warning ("Failed to open dir %s: %s.\n", parent_dir, strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Some file systems don't support fsyncing a directory. Just ignore the error.
|
||||
*/
|
||||
if (fsync (dir_fd) < 0) {
|
||||
if (errno != EINVAL) {
|
||||
seaf_warning ("Failed to fsync dir %s: %s.\n",
|
||||
parent_dir, strerror(errno));
|
||||
ret = -1;
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
g_free (parent_dir);
|
||||
if (dir_fd >= 0)
|
||||
close (dir_fd);
|
||||
return ret;
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
/*
|
||||
* OS X garantees an existence of obj_path always exists,
|
||||
* even when the system crashes.
|
||||
*/
|
||||
if (rename (tmp_path, obj_path) < 0) {
|
||||
seaf_warning ("Failed to rename from %s to %s: %s.\n",
|
||||
tmp_path, obj_path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
wchar_t *w_tmp_path = g_utf8_to_utf16 (tmp_path, -1, NULL, NULL, NULL);
|
||||
wchar_t *w_obj_path = g_utf8_to_utf16 (obj_path, -1, NULL, NULL, NULL);
|
||||
int ret = 0;
|
||||
|
||||
if (!MoveFileExW (w_tmp_path, w_obj_path,
|
||||
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
|
||||
seaf_warning ("MoveFilExW failed: %lu.\n", GetLastError());
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
g_free (w_tmp_path);
|
||||
g_free (w_obj_path);
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int
|
||||
save_obj_contents (const char *path, const void *data, int len, gboolean need_sync)
|
||||
{
|
||||
char tmp_path[SEAF_PATH_MAX];
|
||||
int fd;
|
||||
|
||||
snprintf (tmp_path, SEAF_PATH_MAX, "%s.XXXXXX", path);
|
||||
fd = g_mkstemp (tmp_path);
|
||||
if (fd < 0) {
|
||||
seaf_warning ("[obj backend] Failed to open tmp file %s: %s.\n",
|
||||
tmp_path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (writen (fd, data, len) < 0) {
|
||||
seaf_warning ("[obj backend] Failed to write obj %s: %s.\n",
|
||||
tmp_path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (need_sync && fsync_obj_contents (fd) < 0)
|
||||
return -1;
|
||||
|
||||
/* Close may return error, especially in NFS. */
|
||||
if (close (fd) < 0) {
|
||||
seaf_warning ("[obj backend Failed close obj %s: %s.\n",
|
||||
tmp_path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (need_sync) {
|
||||
if (rename_and_sync (tmp_path, path) < 0)
|
||||
return -1;
|
||||
} else {
|
||||
if (g_rename (tmp_path, path) < 0) {
|
||||
seaf_warning ("[obj backend] Failed to rename %s: %s.\n",
|
||||
path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
create_parent_path (const char *path)
|
||||
{
|
||||
char *dir = g_path_get_dirname (path);
|
||||
if (!dir)
|
||||
return -1;
|
||||
|
||||
if (g_file_test (dir, G_FILE_TEST_EXISTS)) {
|
||||
g_free (dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (g_mkdir_with_parents (dir, 0777) < 0) {
|
||||
seaf_warning ("Failed to create object parent path %s: %s.\n",
|
||||
dir, strerror(errno));
|
||||
g_free (dir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_free (dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
obj_backend_fs_write (ObjBackend *bend,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
void *data,
|
||||
int len,
|
||||
gboolean need_sync)
|
||||
{
|
||||
char path[SEAF_PATH_MAX];
|
||||
|
||||
id_to_path (bend->priv, obj_id, path, repo_id, version);
|
||||
|
||||
/* GTimeVal s, e; */
|
||||
|
||||
/* g_get_current_time (&s); */
|
||||
|
||||
if (create_parent_path (path) < 0) {
|
||||
seaf_warning ("[obj backend] Failed to create path for obj %s:%s.\n",
|
||||
repo_id, obj_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (save_obj_contents (path, data, len, need_sync) < 0) {
|
||||
seaf_warning ("[obj backend] Failed to write obj %s:%s.\n",
|
||||
repo_id, obj_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* g_get_current_time (&e); */
|
||||
|
||||
/* seaf_message ("write obj time: %ldus.\n", */
|
||||
/* ((e.tv_sec*1000000+e.tv_usec) - (s.tv_sec*1000000+s.tv_usec))); */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
obj_backend_fs_exists (ObjBackend *bend,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id)
|
||||
{
|
||||
char path[SEAF_PATH_MAX];
|
||||
SeafStat st;
|
||||
|
||||
id_to_path (bend->priv, obj_id, path, repo_id, version);
|
||||
|
||||
if (seaf_stat (path, &st) == 0)
|
||||
return TRUE;
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void
|
||||
obj_backend_fs_delete (ObjBackend *bend,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id)
|
||||
{
|
||||
char path[SEAF_PATH_MAX];
|
||||
|
||||
id_to_path (bend->priv, obj_id, path, repo_id, version);
|
||||
g_unlink (path);
|
||||
}
|
||||
|
||||
static int
|
||||
obj_backend_fs_foreach_obj (ObjBackend *bend,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
SeafObjFunc process,
|
||||
void *user_data)
|
||||
{
|
||||
FsPriv *priv = bend->priv;
|
||||
char *obj_dir = NULL;
|
||||
int dir_len;
|
||||
GDir *dir1 = NULL, *dir2;
|
||||
const char *dname1, *dname2;
|
||||
char obj_id[128];
|
||||
char path[SEAF_PATH_MAX], *pos;
|
||||
int ret = 0;
|
||||
|
||||
#if defined MIGRATION || defined SEAFILE_CLIENT
|
||||
if (version > 0)
|
||||
obj_dir = g_build_filename (priv->obj_dir, repo_id, NULL);
|
||||
else
|
||||
obj_dir = g_strdup(priv->v0_obj_dir);
|
||||
#else
|
||||
obj_dir = g_build_filename (priv->obj_dir, repo_id, NULL);
|
||||
#endif
|
||||
dir_len = strlen (obj_dir);
|
||||
|
||||
dir1 = g_dir_open (obj_dir, 0, NULL);
|
||||
if (!dir1) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
memcpy (path, obj_dir, dir_len);
|
||||
pos = path + dir_len;
|
||||
|
||||
while ((dname1 = g_dir_read_name(dir1)) != NULL) {
|
||||
snprintf (pos, sizeof(path) - dir_len, "/%s", dname1);
|
||||
|
||||
dir2 = g_dir_open (path, 0, NULL);
|
||||
if (!dir2) {
|
||||
seaf_warning ("Failed to open object dir %s.\n", path);
|
||||
continue;
|
||||
}
|
||||
|
||||
while ((dname2 = g_dir_read_name(dir2)) != NULL) {
|
||||
snprintf (obj_id, sizeof(obj_id), "%s%s", dname1, dname2);
|
||||
if (!process (repo_id, version, obj_id, user_data)) {
|
||||
g_dir_close (dir2);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
g_dir_close (dir2);
|
||||
}
|
||||
|
||||
out:
|
||||
if (dir1)
|
||||
g_dir_close (dir1);
|
||||
g_free (obj_dir);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
obj_backend_fs_copy (ObjBackend *bend,
|
||||
const char *src_repo_id,
|
||||
int src_version,
|
||||
const char *dst_repo_id,
|
||||
int dst_version,
|
||||
const char *obj_id)
|
||||
{
|
||||
char src_path[SEAF_PATH_MAX];
|
||||
char dst_path[SEAF_PATH_MAX];
|
||||
|
||||
id_to_path (bend->priv, obj_id, src_path, src_repo_id, src_version);
|
||||
id_to_path (bend->priv, obj_id, dst_path, dst_repo_id, dst_version);
|
||||
|
||||
if (g_file_test (dst_path, G_FILE_TEST_EXISTS))
|
||||
return 0;
|
||||
|
||||
if (create_parent_path (dst_path) < 0) {
|
||||
seaf_warning ("Failed to create dst path %s for obj %s.\n",
|
||||
dst_path, obj_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
if (!CreateHardLink (dst_path, src_path, NULL)) {
|
||||
seaf_warning ("Failed to link %s to %s: %lu.\n",
|
||||
src_path, dst_path, GetLastError());
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
int ret = link (src_path, dst_path);
|
||||
if (ret < 0 && errno != EEXIST) {
|
||||
seaf_warning ("Failed to link %s to %s: %s.\n",
|
||||
src_path, dst_path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int
|
||||
obj_backend_fs_remove_store (ObjBackend *bend, const char *store_id)
|
||||
{
|
||||
FsPriv *priv = bend->priv;
|
||||
char *obj_dir = NULL;
|
||||
GDir *dir1, *dir2;
|
||||
const char *dname1, *dname2;
|
||||
char *path1, *path2;
|
||||
|
||||
obj_dir = g_build_filename (priv->obj_dir, store_id, NULL);
|
||||
|
||||
dir1 = g_dir_open (obj_dir, 0, NULL);
|
||||
if (!dir1) {
|
||||
g_free (obj_dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((dname1 = g_dir_read_name(dir1)) != NULL) {
|
||||
path1 = g_build_filename (obj_dir, dname1, NULL);
|
||||
|
||||
dir2 = g_dir_open (path1, 0, NULL);
|
||||
if (!dir2) {
|
||||
seaf_warning ("Failed to open obj dir %s.\n", path1);
|
||||
g_dir_close (dir1);
|
||||
g_free (path1);
|
||||
g_free (obj_dir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ((dname2 = g_dir_read_name(dir2)) != NULL) {
|
||||
path2 = g_build_filename (path1, dname2, NULL);
|
||||
g_unlink (path2);
|
||||
g_free (path2);
|
||||
}
|
||||
g_dir_close (dir2);
|
||||
|
||||
g_rmdir (path1);
|
||||
g_free (path1);
|
||||
}
|
||||
|
||||
g_dir_close (dir1);
|
||||
g_rmdir (obj_dir);
|
||||
g_free (obj_dir);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ObjBackend *
|
||||
obj_backend_fs_new (const char *seaf_dir, const char *obj_type)
|
||||
{
|
||||
ObjBackend *bend;
|
||||
FsPriv *priv;
|
||||
|
||||
bend = g_new0(ObjBackend, 1);
|
||||
priv = g_new0(FsPriv, 1);
|
||||
bend->priv = priv;
|
||||
|
||||
priv->v0_obj_dir = g_build_filename (seaf_dir, obj_type, NULL);
|
||||
priv->v0_dir_len = strlen(priv->v0_obj_dir);
|
||||
|
||||
priv->obj_dir = g_build_filename (seaf_dir, "storage", obj_type, NULL);
|
||||
priv->dir_len = strlen (priv->obj_dir);
|
||||
|
||||
if (g_mkdir_with_parents (priv->v0_obj_dir, 0777) < 0) {
|
||||
seaf_warning ("[Obj Backend] Objects dir %s does not exist and"
|
||||
" is unable to create\n", priv->v0_obj_dir);
|
||||
goto onerror;
|
||||
}
|
||||
|
||||
if (g_mkdir_with_parents (priv->obj_dir, 0777) < 0) {
|
||||
seaf_warning ("[Obj Backend] Objects dir %s does not exist and"
|
||||
" is unable to create\n", priv->obj_dir);
|
||||
goto onerror;
|
||||
}
|
||||
|
||||
bend->read = obj_backend_fs_read;
|
||||
bend->write = obj_backend_fs_write;
|
||||
bend->exists = obj_backend_fs_exists;
|
||||
bend->delete = obj_backend_fs_delete;
|
||||
bend->foreach_obj = obj_backend_fs_foreach_obj;
|
||||
bend->copy = obj_backend_fs_copy;
|
||||
bend->remove_store = obj_backend_fs_remove_store;
|
||||
|
||||
return bend;
|
||||
|
||||
onerror:
|
||||
g_free (priv->v0_obj_dir);
|
||||
g_free (priv->obj_dir);
|
||||
g_free (priv);
|
||||
g_free (bend);
|
||||
|
||||
return NULL;
|
||||
}
|
148
common/obj-backend-riak.c
Normal file
148
common/obj-backend-riak.c
Normal file
|
@ -0,0 +1,148 @@
|
|||
#include "common.h"
|
||||
#include "log.h"
|
||||
#include "obj-backend.h"
|
||||
|
||||
#ifdef RIAK_BACKEND
|
||||
|
||||
#include "riak-client.h"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
typedef struct RiakPriv {
|
||||
const char *host;
|
||||
const char *port;
|
||||
const char *bucket;
|
||||
int n_write;
|
||||
|
||||
GQueue *conn_pool;
|
||||
pthread_mutex_t lock;
|
||||
} RiakPriv;
|
||||
|
||||
static SeafRiakClient *
|
||||
get_connection (RiakPriv *priv)
|
||||
{
|
||||
SeafRiakClient *connection;
|
||||
|
||||
pthread_mutex_lock (&priv->lock);
|
||||
|
||||
connection = g_queue_pop_head (priv->conn_pool);
|
||||
if (!connection)
|
||||
connection = seaf_riak_client_new (priv->host, priv->port);
|
||||
pthread_mutex_unlock (&priv->lock);
|
||||
return connection;
|
||||
}
|
||||
|
||||
static void
|
||||
return_connection (RiakPriv *priv, SeafRiakClient *connection)
|
||||
{
|
||||
pthread_mutex_lock (&priv->lock);
|
||||
g_queue_push_tail (priv->conn_pool, connection);
|
||||
pthread_mutex_unlock (&priv->lock);
|
||||
}
|
||||
|
||||
static int
|
||||
obj_backend_riak_read (ObjBackend *bend,
|
||||
const char *obj_id,
|
||||
void **data,
|
||||
int *len)
|
||||
{
|
||||
SeafRiakClient *conn = get_connection (bend->priv);
|
||||
RiakPriv *priv = bend->priv;
|
||||
int ret;
|
||||
|
||||
ret = seaf_riak_client_get (conn, priv->bucket, obj_id, data, len);
|
||||
|
||||
return_connection (priv, conn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
obj_backend_riak_write (ObjBackend *bend,
|
||||
const char *obj_id,
|
||||
void *data,
|
||||
int len)
|
||||
{
|
||||
SeafRiakClient *conn = get_connection (bend->priv);
|
||||
RiakPriv *priv = bend->priv;
|
||||
int ret;
|
||||
|
||||
ret = seaf_riak_client_put (conn, priv->bucket, obj_id, data, len,
|
||||
priv->n_write);
|
||||
|
||||
return_connection (priv, conn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
obj_backend_riak_exists (ObjBackend *bend,
|
||||
const char *obj_id)
|
||||
{
|
||||
SeafRiakClient *conn = get_connection (bend->priv);
|
||||
RiakPriv *priv = bend->priv;
|
||||
gboolean ret;
|
||||
|
||||
ret = seaf_riak_client_query (conn, priv->bucket, obj_id);
|
||||
|
||||
return_connection (priv, conn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
obj_backend_riak_delete (ObjBackend *bend,
|
||||
const char *obj_id)
|
||||
{
|
||||
SeafRiakClient *conn = get_connection (bend->priv);
|
||||
RiakPriv *priv = bend->priv;
|
||||
|
||||
seaf_riak_client_delete (conn, priv->bucket, obj_id, priv->n_write);
|
||||
|
||||
return_connection (priv, conn);
|
||||
}
|
||||
|
||||
ObjBackend *
|
||||
obj_backend_riak_new (const char *host,
|
||||
const char *port,
|
||||
const char *bucket,
|
||||
const char *write_policy)
|
||||
{
|
||||
ObjBackend *bend;
|
||||
RiakPriv *priv;
|
||||
|
||||
bend = g_new0(ObjBackend, 1);
|
||||
priv = g_new0(RiakPriv, 1);
|
||||
bend->priv = priv;
|
||||
|
||||
priv->host = g_strdup (host);
|
||||
priv->port = g_strdup (port);
|
||||
priv->bucket = g_strdup (bucket);
|
||||
if (strcmp (write_policy, "quorum") == 0)
|
||||
priv->n_write = RIAK_QUORUM;
|
||||
else if (strcmp (write_policy, "all") == 0)
|
||||
priv->n_write = RIAK_ALL;
|
||||
else
|
||||
g_return_val_if_reached (NULL);
|
||||
|
||||
priv->conn_pool = g_queue_new ();
|
||||
pthread_mutex_init (&priv->lock, NULL);
|
||||
|
||||
bend->read = obj_backend_riak_read;
|
||||
bend->write = obj_backend_riak_write;
|
||||
bend->exists = obj_backend_riak_exists;
|
||||
bend->delete = obj_backend_riak_delete;
|
||||
|
||||
return bend;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
ObjBackend *
|
||||
obj_backend_riak_new (const char *host,
|
||||
const char *port,
|
||||
const char *bucket,
|
||||
const char *write_policy)
|
||||
{
|
||||
seaf_warning ("Riak backend is not enabled.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif /* RIAK_BACKEND */
|
54
common/obj-backend.h
Normal file
54
common/obj-backend.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
#ifndef OBJ_BACKEND_H
|
||||
#define OBJ_BACKEND_H
|
||||
|
||||
#include <glib.h>
|
||||
#include "obj-store.h"
|
||||
|
||||
typedef struct ObjBackend ObjBackend;
|
||||
|
||||
struct ObjBackend {
|
||||
int (*read) (ObjBackend *bend,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
void **data,
|
||||
int *len);
|
||||
|
||||
int (*write) (ObjBackend *bend,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
void *data,
|
||||
int len,
|
||||
gboolean need_sync);
|
||||
|
||||
gboolean (*exists) (ObjBackend *bend,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id);
|
||||
|
||||
void (*delete) (ObjBackend *bend,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id);
|
||||
|
||||
int (*foreach_obj) (ObjBackend *bend,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
SeafObjFunc process,
|
||||
void *user_data);
|
||||
|
||||
int (*copy) (ObjBackend *bend,
|
||||
const char *src_repo_id,
|
||||
int src_version,
|
||||
const char *dst_repo_id,
|
||||
int dst_version,
|
||||
const char *obj_id);
|
||||
|
||||
int (*remove_store) (ObjBackend *bend,
|
||||
const char *store_id);
|
||||
|
||||
void *priv;
|
||||
};
|
||||
|
||||
#endif
|
549
common/obj-store.c
Normal file
549
common/obj-store.c
Normal file
|
@ -0,0 +1,549 @@
|
|||
#include "common.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include <ccnet/cevent.h>
|
||||
#include "seafile-session.h"
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include "obj-backend.h"
|
||||
#include "obj-store.h"
|
||||
|
||||
#define MAX_READER_THREADS 2
|
||||
#define MAX_WRITER_THREADS 2
|
||||
#define MAX_STAT_THREADS 2
|
||||
|
||||
typedef struct AsyncTask {
|
||||
guint32 rw_id;
|
||||
char obj_id[41];
|
||||
void *data;
|
||||
int len;
|
||||
gboolean need_sync;
|
||||
gboolean success;
|
||||
} AsyncTask;
|
||||
|
||||
typedef struct OSCallbackStruct {
|
||||
char repo_id[37];
|
||||
int version;
|
||||
OSAsyncCallback cb;
|
||||
void *cb_data;
|
||||
} OSCallbackStruct;
|
||||
|
||||
struct SeafObjStore {
|
||||
ObjBackend *bend;
|
||||
|
||||
CEventManager *ev_mgr;
|
||||
|
||||
/* For async read. */
|
||||
guint32 next_rd_id;
|
||||
GThreadPool *read_tpool;
|
||||
GHashTable *readers;
|
||||
guint32 read_ev_id;
|
||||
|
||||
/* For async write. */
|
||||
guint32 next_wr_id;
|
||||
GThreadPool *write_tpool;
|
||||
GHashTable *writers;
|
||||
guint32 write_ev_id;
|
||||
|
||||
/* For async stat. */
|
||||
guint32 next_st_id;
|
||||
GThreadPool *stat_tpool;
|
||||
GHashTable *stats;
|
||||
guint32 stat_ev_id;
|
||||
};
|
||||
typedef struct SeafObjStore SeafObjStore;
|
||||
|
||||
static void
|
||||
reader_thread (void *data, void *user_data);
|
||||
static void
|
||||
writer_thread (void *data, void *user_data);
|
||||
static void
|
||||
stat_thread (void *data, void *user_data);
|
||||
|
||||
static void
|
||||
on_read_done (CEvent *event, void *data);
|
||||
static void
|
||||
on_write_done (CEvent *event, void *data);
|
||||
static void
|
||||
on_stat_done (CEvent *event, void *data);
|
||||
|
||||
extern ObjBackend *
|
||||
obj_backend_fs_new (const char *seaf_dir, const char *obj_type);
|
||||
|
||||
struct SeafObjStore *
|
||||
seaf_obj_store_new (SeafileSession *seaf, const char *obj_type)
|
||||
{
|
||||
SeafObjStore *store = g_new0 (SeafObjStore, 1);
|
||||
|
||||
if (!store)
|
||||
return NULL;
|
||||
|
||||
store->bend = obj_backend_fs_new (seaf->seaf_dir, obj_type);
|
||||
if (!store->bend) {
|
||||
seaf_warning ("[Object store] Failed to load backend.\n");
|
||||
g_free (store);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return store;
|
||||
}
|
||||
|
||||
static int
|
||||
async_init (SeafObjStore *obj_store, CEventManager *ev_mgr)
|
||||
{
|
||||
GError *error = NULL;
|
||||
|
||||
obj_store->ev_mgr = ev_mgr;
|
||||
|
||||
obj_store->read_tpool = g_thread_pool_new (reader_thread,
|
||||
obj_store,
|
||||
MAX_READER_THREADS,
|
||||
FALSE,
|
||||
&error);
|
||||
if (error) {
|
||||
seaf_warning ("Failed to start reader thread pool: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
obj_store->readers = g_hash_table_new_full (g_direct_hash, g_direct_equal,
|
||||
NULL, g_free);
|
||||
obj_store->read_ev_id = cevent_manager_register (ev_mgr,
|
||||
on_read_done,
|
||||
obj_store);
|
||||
|
||||
obj_store->write_tpool = g_thread_pool_new (writer_thread,
|
||||
obj_store,
|
||||
MAX_WRITER_THREADS,
|
||||
FALSE,
|
||||
&error);
|
||||
if (error) {
|
||||
seaf_warning ("Failed to start writer thread pool: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
obj_store->writers = g_hash_table_new_full (g_direct_hash, g_direct_equal,
|
||||
NULL, g_free);
|
||||
obj_store->write_ev_id = cevent_manager_register (ev_mgr,
|
||||
on_write_done,
|
||||
obj_store);
|
||||
|
||||
obj_store->stat_tpool = g_thread_pool_new (stat_thread,
|
||||
obj_store,
|
||||
MAX_STAT_THREADS,
|
||||
FALSE,
|
||||
&error);
|
||||
if (error) {
|
||||
seaf_warning ("Failed to start statr thread pool: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
obj_store->stats = g_hash_table_new_full (g_direct_hash, g_direct_equal,
|
||||
NULL, g_free);
|
||||
obj_store->stat_ev_id = cevent_manager_register (ev_mgr,
|
||||
on_stat_done,
|
||||
obj_store);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_obj_store_init (SeafObjStore *obj_store,
|
||||
gboolean enable_async,
|
||||
CEventManager *ev_mgr)
|
||||
{
|
||||
if (enable_async && async_init (obj_store, ev_mgr) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_obj_store_read_obj (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
void **data,
|
||||
int *len)
|
||||
{
|
||||
ObjBackend *bend = obj_store->bend;
|
||||
|
||||
if (!repo_id || !is_uuid_valid(repo_id) ||
|
||||
!obj_id || !is_object_id_valid(obj_id))
|
||||
return -1;
|
||||
|
||||
return bend->read (bend, repo_id, version, obj_id, data, len);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_obj_store_write_obj (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
void *data,
|
||||
int len,
|
||||
gboolean need_sync)
|
||||
{
|
||||
ObjBackend *bend = obj_store->bend;
|
||||
|
||||
if (!repo_id || !is_uuid_valid(repo_id) ||
|
||||
!obj_id || !is_object_id_valid(obj_id))
|
||||
return -1;
|
||||
|
||||
return bend->write (bend, repo_id, version, obj_id, data, len, need_sync);
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_obj_store_obj_exists (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id)
|
||||
{
|
||||
ObjBackend *bend = obj_store->bend;
|
||||
|
||||
if (!repo_id || !is_uuid_valid(repo_id) ||
|
||||
!obj_id || !is_object_id_valid(obj_id))
|
||||
return FALSE;
|
||||
|
||||
return bend->exists (bend, repo_id, version, obj_id);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_obj_store_delete_obj (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id)
|
||||
{
|
||||
ObjBackend *bend = obj_store->bend;
|
||||
|
||||
if (!repo_id || !is_uuid_valid(repo_id) ||
|
||||
!obj_id || !is_object_id_valid(obj_id))
|
||||
return;
|
||||
|
||||
return bend->delete (bend, repo_id, version, obj_id);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_obj_store_foreach_obj (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
SeafObjFunc process,
|
||||
void *user_data)
|
||||
{
|
||||
ObjBackend *bend = obj_store->bend;
|
||||
|
||||
return bend->foreach_obj (bend, repo_id, version, process, user_data);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_obj_store_copy_obj (struct SeafObjStore *obj_store,
|
||||
const char *src_repo_id,
|
||||
int src_version,
|
||||
const char *dst_repo_id,
|
||||
int dst_version,
|
||||
const char *obj_id)
|
||||
{
|
||||
ObjBackend *bend = obj_store->bend;
|
||||
|
||||
if (strcmp (obj_id, EMPTY_SHA1) == 0)
|
||||
return 0;
|
||||
|
||||
return bend->copy (bend, src_repo_id, src_version, dst_repo_id, dst_version, obj_id);
|
||||
}
|
||||
|
||||
static void
|
||||
reader_thread (void *data, void *user_data)
|
||||
{
|
||||
AsyncTask *task = data;
|
||||
SeafObjStore *obj_store = user_data;
|
||||
ObjBackend *bend = obj_store->bend;
|
||||
OSCallbackStruct *callback;
|
||||
|
||||
callback = g_hash_table_lookup (obj_store->readers,
|
||||
(gpointer)(long)(task->rw_id));
|
||||
if (callback) {
|
||||
task->success = TRUE;
|
||||
|
||||
if (bend->read (bend, callback->repo_id, callback->version,
|
||||
task->obj_id, &task->data, &task->len) < 0)
|
||||
task->success = FALSE;
|
||||
}
|
||||
|
||||
cevent_manager_add_event (obj_store->ev_mgr, obj_store->read_ev_id,
|
||||
task);
|
||||
}
|
||||
|
||||
static void
|
||||
stat_thread (void *data, void *user_data)
|
||||
{
|
||||
AsyncTask *task = data;
|
||||
SeafObjStore *obj_store = user_data;
|
||||
ObjBackend *bend = obj_store->bend;
|
||||
OSCallbackStruct *callback;
|
||||
|
||||
callback = g_hash_table_lookup (obj_store->stats,
|
||||
(gpointer)(long)(task->rw_id));
|
||||
if (callback) {
|
||||
task->success = TRUE;
|
||||
|
||||
if (!bend->exists (bend, callback->repo_id, callback->version, task->obj_id))
|
||||
task->success = FALSE;
|
||||
}
|
||||
|
||||
cevent_manager_add_event (obj_store->ev_mgr, obj_store->stat_ev_id,
|
||||
task);
|
||||
}
|
||||
|
||||
static void
|
||||
writer_thread (void *data, void *user_data)
|
||||
{
|
||||
AsyncTask *task = data;
|
||||
SeafObjStore *obj_store = user_data;
|
||||
ObjBackend *bend = obj_store->bend;
|
||||
OSCallbackStruct *callback;
|
||||
|
||||
callback = g_hash_table_lookup (obj_store->writers,
|
||||
(gpointer)(long)(task->rw_id));
|
||||
if (callback) {
|
||||
task->success = TRUE;
|
||||
|
||||
if (bend->write (bend, callback->repo_id, callback->version,
|
||||
task->obj_id, task->data, task->len, task->need_sync) < 0)
|
||||
task->success = FALSE;
|
||||
}
|
||||
|
||||
cevent_manager_add_event (obj_store->ev_mgr, obj_store->write_ev_id,
|
||||
task);
|
||||
}
|
||||
|
||||
static void
|
||||
on_read_done (CEvent *event, void *user_data)
|
||||
{
|
||||
AsyncTask *task = event->data;
|
||||
SeafObjStore *obj_store = user_data;
|
||||
OSCallbackStruct *callback;
|
||||
OSAsyncResult res;
|
||||
|
||||
callback = g_hash_table_lookup (obj_store->readers,
|
||||
(gpointer)(long)(task->rw_id));
|
||||
if (callback) {
|
||||
res.rw_id = task->rw_id;
|
||||
memcpy (res.obj_id, task->obj_id, 41);
|
||||
res.data = task->data;
|
||||
res.len = task->len;
|
||||
res.success = task->success;
|
||||
|
||||
callback->cb (&res, callback->cb_data);
|
||||
}
|
||||
|
||||
g_free (task->data);
|
||||
g_free (task);
|
||||
}
|
||||
|
||||
static void
|
||||
on_stat_done (CEvent *event, void *user_data)
|
||||
{
|
||||
AsyncTask *task = event->data;
|
||||
SeafObjStore *obj_store = user_data;
|
||||
OSCallbackStruct *callback;
|
||||
OSAsyncResult res;
|
||||
|
||||
callback = g_hash_table_lookup (obj_store->stats,
|
||||
(gpointer)(long)(task->rw_id));
|
||||
if (callback) {
|
||||
res.rw_id = task->rw_id;
|
||||
memcpy (res.obj_id, task->obj_id, 41);
|
||||
res.data = NULL;
|
||||
res.len = task->len;
|
||||
res.success = task->success;
|
||||
|
||||
callback->cb (&res, callback->cb_data);
|
||||
}
|
||||
|
||||
g_free (task->data);
|
||||
g_free (task);
|
||||
}
|
||||
|
||||
static void
|
||||
on_write_done (CEvent *event, void *user_data)
|
||||
{
|
||||
AsyncTask *task = event->data;
|
||||
SeafObjStore *obj_store = user_data;
|
||||
OSCallbackStruct *callback;
|
||||
OSAsyncResult res;
|
||||
|
||||
callback = g_hash_table_lookup (obj_store->writers,
|
||||
(gpointer)(long)(task->rw_id));
|
||||
if (callback) {
|
||||
res.rw_id = task->rw_id;
|
||||
memcpy (res.obj_id, task->obj_id, 41);
|
||||
res.data = task->data;
|
||||
res.len = task->len;
|
||||
res.success = task->success;
|
||||
|
||||
callback->cb (&res, callback->cb_data);
|
||||
}
|
||||
|
||||
g_free (task->data);
|
||||
g_free (task);
|
||||
}
|
||||
|
||||
guint32
|
||||
seaf_obj_store_register_async_read (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
OSAsyncCallback callback,
|
||||
void *cb_data)
|
||||
{
|
||||
guint32 id = obj_store->next_rd_id++;
|
||||
OSCallbackStruct *cb_struct = g_new0 (OSCallbackStruct, 1);
|
||||
|
||||
memcpy (cb_struct->repo_id, repo_id, 36);
|
||||
cb_struct->version = version;
|
||||
cb_struct->cb = callback;
|
||||
cb_struct->cb_data = cb_data;
|
||||
|
||||
g_hash_table_insert (obj_store->readers, (gpointer)(long)id, cb_struct);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_obj_store_unregister_async_read (struct SeafObjStore *obj_store,
|
||||
guint32 reader_id)
|
||||
{
|
||||
g_hash_table_remove (obj_store->readers, (gpointer)(long)reader_id);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_obj_store_async_read (struct SeafObjStore *obj_store,
|
||||
guint32 reader_id,
|
||||
const char *obj_id)
|
||||
{
|
||||
AsyncTask *task = g_new0 (AsyncTask, 1);
|
||||
GError *error = NULL;
|
||||
|
||||
task->rw_id = reader_id;
|
||||
memcpy (task->obj_id, obj_id, 41);
|
||||
|
||||
g_thread_pool_push (obj_store->read_tpool, task, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Failed to start aysnc read of %s.\n", obj_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
guint32
|
||||
seaf_obj_store_register_async_stat (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
OSAsyncCallback callback,
|
||||
void *cb_data)
|
||||
{
|
||||
guint32 id = obj_store->next_st_id++;
|
||||
OSCallbackStruct *cb_struct = g_new0 (OSCallbackStruct, 1);
|
||||
|
||||
memcpy (cb_struct->repo_id, repo_id, 36);
|
||||
cb_struct->version = version;
|
||||
cb_struct->cb = callback;
|
||||
cb_struct->cb_data = cb_data;
|
||||
|
||||
g_hash_table_insert (obj_store->stats, (gpointer)(long)id, cb_struct);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_obj_store_unregister_async_stat (struct SeafObjStore *obj_store,
|
||||
guint32 stat_id)
|
||||
{
|
||||
g_hash_table_remove (obj_store->stats, (gpointer)(long)stat_id);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_obj_store_async_stat (struct SeafObjStore *obj_store,
|
||||
guint32 stat_id,
|
||||
const char *obj_id)
|
||||
{
|
||||
AsyncTask *task = g_new0 (AsyncTask, 1);
|
||||
GError *error = NULL;
|
||||
|
||||
task->rw_id = stat_id;
|
||||
memcpy (task->obj_id, obj_id, 41);
|
||||
|
||||
g_thread_pool_push (obj_store->stat_tpool, task, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Failed to start aysnc stat of %s.\n", obj_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
guint32
|
||||
seaf_obj_store_register_async_write (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
OSAsyncCallback callback,
|
||||
void *cb_data)
|
||||
{
|
||||
guint32 id = obj_store->next_rd_id++;
|
||||
OSCallbackStruct *cb_struct = g_new0 (OSCallbackStruct, 1);
|
||||
|
||||
memcpy (cb_struct->repo_id, repo_id, 36);
|
||||
cb_struct->version = version;
|
||||
cb_struct->cb = callback;
|
||||
cb_struct->cb_data = cb_data;
|
||||
|
||||
g_hash_table_insert (obj_store->writers, (gpointer)(long)id, cb_struct);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_obj_store_unregister_async_write (struct SeafObjStore *obj_store,
|
||||
guint32 writer_id)
|
||||
{
|
||||
g_hash_table_remove (obj_store->writers, (gpointer)(long)writer_id);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_obj_store_async_write (struct SeafObjStore *obj_store,
|
||||
guint32 writer_id,
|
||||
const char *obj_id,
|
||||
const void *obj_data,
|
||||
int data_len,
|
||||
gboolean need_sync)
|
||||
{
|
||||
AsyncTask *task = g_new0 (AsyncTask, 1);
|
||||
GError *error = NULL;
|
||||
|
||||
task->rw_id = writer_id;
|
||||
memcpy (task->obj_id, obj_id, 41);
|
||||
task->data = g_memdup (obj_data, data_len);
|
||||
task->len = data_len;
|
||||
task->need_sync = need_sync;
|
||||
|
||||
g_thread_pool_push (obj_store->write_tpool, task, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Failed to start aysnc write of %s.\n", obj_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_obj_store_remove_store (struct SeafObjStore *obj_store,
|
||||
const char *store_id)
|
||||
{
|
||||
ObjBackend *bend = obj_store->bend;
|
||||
|
||||
return bend->remove_store (bend, store_id);
|
||||
}
|
140
common/obj-store.h
Normal file
140
common/obj-store.h
Normal file
|
@ -0,0 +1,140 @@
|
|||
#ifndef OBJ_STORE_H
|
||||
#define OBJ_STORE_H
|
||||
|
||||
#include <glib.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
struct _SeafileSession;
|
||||
struct SeafObjStore;
|
||||
struct CEventManager;
|
||||
|
||||
struct SeafObjStore *
|
||||
seaf_obj_store_new (struct _SeafileSession *seaf, const char *obj_type);
|
||||
|
||||
int
|
||||
seaf_obj_store_init (struct SeafObjStore *obj_store,
|
||||
gboolean enable_async,
|
||||
struct CEventManager *ev_mgr);
|
||||
|
||||
/* Synchronous I/O interface. */
|
||||
|
||||
int
|
||||
seaf_obj_store_read_obj (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
void **data,
|
||||
int *len);
|
||||
|
||||
int
|
||||
seaf_obj_store_write_obj (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
void *data,
|
||||
int len,
|
||||
gboolean need_sync);
|
||||
|
||||
gboolean
|
||||
seaf_obj_store_obj_exists (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id);
|
||||
|
||||
void
|
||||
seaf_obj_store_delete_obj (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id);
|
||||
|
||||
typedef gboolean (*SeafObjFunc) (const char *repo_id,
|
||||
int version,
|
||||
const char *obj_id,
|
||||
void *user_data);
|
||||
|
||||
int
|
||||
seaf_obj_store_foreach_obj (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
SeafObjFunc process,
|
||||
void *user_data);
|
||||
|
||||
int
|
||||
seaf_obj_store_copy_obj (struct SeafObjStore *obj_store,
|
||||
const char *src_store_id,
|
||||
int src_version,
|
||||
const char *dst_store_id,
|
||||
int dst_version,
|
||||
const char *obj_id);
|
||||
|
||||
/* Asynchronous I/O interface. */
|
||||
|
||||
typedef struct OSAsyncResult {
|
||||
guint32 rw_id;
|
||||
char obj_id[41];
|
||||
/* @data is owned by obj-store, don't free it. */
|
||||
void *data;
|
||||
int len;
|
||||
gboolean success;
|
||||
} OSAsyncResult;
|
||||
|
||||
typedef void (*OSAsyncCallback) (OSAsyncResult *res, void *cb_data);
|
||||
|
||||
/* Async read */
|
||||
guint32
|
||||
seaf_obj_store_register_async_read (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
OSAsyncCallback callback,
|
||||
void *cb_data);
|
||||
|
||||
void
|
||||
seaf_obj_store_unregister_async_read (struct SeafObjStore *obj_store,
|
||||
guint32 reader_id);
|
||||
|
||||
int
|
||||
seaf_obj_store_async_read (struct SeafObjStore *obj_store,
|
||||
guint32 reader_id,
|
||||
const char *obj_id);
|
||||
|
||||
/* Async write */
|
||||
guint32
|
||||
seaf_obj_store_register_async_write (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
OSAsyncCallback callback,
|
||||
void *cb_data);
|
||||
|
||||
void
|
||||
seaf_obj_store_unregister_async_write (struct SeafObjStore *obj_store,
|
||||
guint32 writer_id);
|
||||
|
||||
int
|
||||
seaf_obj_store_async_write (struct SeafObjStore *obj_store,
|
||||
guint32 writer_id,
|
||||
const char *obj_id,
|
||||
const void *obj_data,
|
||||
int data_len,
|
||||
gboolean need_sync);
|
||||
|
||||
/* Async stat */
|
||||
guint32
|
||||
seaf_obj_store_register_async_stat (struct SeafObjStore *obj_store,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
OSAsyncCallback callback,
|
||||
void *cb_data);
|
||||
|
||||
void
|
||||
seaf_obj_store_unregister_async_stat (struct SeafObjStore *obj_store,
|
||||
guint32 stat_id);
|
||||
|
||||
int
|
||||
seaf_obj_store_async_stat (struct SeafObjStore *obj_store,
|
||||
guint32 stat_id,
|
||||
const char *obj_id);
|
||||
|
||||
int
|
||||
seaf_obj_store_remove_store (struct SeafObjStore *obj_store,
|
||||
const char *store_id);
|
||||
#endif
|
55
common/object-list.c
Normal file
55
common/object-list.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "object-list.h"
|
||||
|
||||
|
||||
ObjectList *
|
||||
object_list_new ()
|
||||
{
|
||||
ObjectList *ol = g_new0 (ObjectList, 1);
|
||||
|
||||
ol->obj_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
|
||||
ol->obj_ids = g_ptr_array_new_with_free_func (g_free);
|
||||
|
||||
return ol;
|
||||
}
|
||||
|
||||
void
|
||||
object_list_free (ObjectList *ol)
|
||||
{
|
||||
if (ol->obj_hash)
|
||||
g_hash_table_destroy (ol->obj_hash);
|
||||
g_ptr_array_free (ol->obj_ids, TRUE);
|
||||
g_free (ol);
|
||||
}
|
||||
|
||||
void
|
||||
object_list_serialize (ObjectList *ol, uint8_t **buffer, uint32_t *len)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t offset = 0;
|
||||
uint8_t *buf;
|
||||
int ollen = object_list_length(ol);
|
||||
|
||||
buf = g_new (uint8_t, 41 * ollen);
|
||||
for (i = 0; i < ollen; ++i) {
|
||||
memcpy (&buf[offset], g_ptr_array_index(ol->obj_ids, i), 41);
|
||||
offset += 41;
|
||||
}
|
||||
|
||||
*buffer = buf;
|
||||
*len = 41 * ollen;
|
||||
}
|
||||
|
||||
gboolean
|
||||
object_list_insert (ObjectList *ol, const char *object_id)
|
||||
{
|
||||
if (g_hash_table_lookup (ol->obj_hash, object_id))
|
||||
return FALSE;
|
||||
char *id = g_strdup(object_id);
|
||||
g_hash_table_replace (ol->obj_hash, id, id);
|
||||
g_ptr_array_add (ol->obj_ids, id);
|
||||
return TRUE;
|
||||
}
|
42
common/object-list.h
Normal file
42
common/object-list.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef OBJECT_LIST_H
|
||||
#define OBJECT_LIST_H
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
typedef struct {
|
||||
GHashTable *obj_hash;
|
||||
GPtrArray *obj_ids;
|
||||
} ObjectList;
|
||||
|
||||
|
||||
ObjectList *
|
||||
object_list_new ();
|
||||
|
||||
void
|
||||
object_list_free (ObjectList *ol);
|
||||
|
||||
void
|
||||
object_list_serialize (ObjectList *ol, uint8_t **buffer, uint32_t *len);
|
||||
|
||||
/**
|
||||
* Add object to ObjectList.
|
||||
* Return FALSE if it is already in the list, TRUE otherwise.
|
||||
*/
|
||||
gboolean
|
||||
object_list_insert (ObjectList *ol, const char *object_id);
|
||||
|
||||
inline static gboolean
|
||||
object_list_exists (ObjectList *ol, const char *object_id)
|
||||
{
|
||||
return (g_hash_table_lookup(ol->obj_hash, object_id) != NULL);
|
||||
}
|
||||
|
||||
inline static int
|
||||
object_list_length (ObjectList *ol)
|
||||
{
|
||||
return ol->obj_ids->len;
|
||||
}
|
||||
|
||||
#endif
|
52
common/processors/objecttx-common.h
Normal file
52
common/processors/objecttx-common.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef OBJECTTX_COMMON_H
|
||||
#define OBJECTTX_COMMON_H
|
||||
|
||||
#define SC_GET_OBJECT "301"
|
||||
#define SS_GET_OBJECT "Get Object"
|
||||
#define SC_OBJECT "302"
|
||||
#define SS_OBJECT "Object"
|
||||
#define SC_END "303"
|
||||
#define SS_END "END"
|
||||
#define SC_COMMIT_IDS "304"
|
||||
#define SS_COMMIT_IDS "Commit IDs"
|
||||
#define SC_ACK "305"
|
||||
#define SS_ACK "Ack"
|
||||
|
||||
#define SC_OBJ_SEG "306"
|
||||
#define SS_OBJ_SEG "Object Segment"
|
||||
#define SC_OBJ_SEG_END "307"
|
||||
#define SS_OBJ_SEG_END "Object Segment End"
|
||||
|
||||
#define SC_OBJ_LIST_SEG "308"
|
||||
#define SS_OBJ_LIST_SEG "Object List Segment"
|
||||
#define SC_OBJ_LIST_SEG_END "309"
|
||||
#define SS_OBJ_LIST_SEG_END "Object List Segment End"
|
||||
|
||||
#define SC_NOT_FOUND "401"
|
||||
#define SS_NOT_FOUND "Object not found"
|
||||
#define SC_BAD_OL "402"
|
||||
#define SS_BAD_OL "Bad Object List"
|
||||
#define SC_BAD_OBJECT "403"
|
||||
#define SS_BAD_OBJECT "Bad Object"
|
||||
|
||||
#define SC_ACCESS_DENIED "410"
|
||||
#define SS_ACCESS_DENIED "Access denied"
|
||||
|
||||
/* for fs transfer */
|
||||
#define SC_ROOT "304"
|
||||
#define SS_ROOT "FS Root"
|
||||
#define SC_ROOT_END "305"
|
||||
#define SS_ROOT_END "FS Root End"
|
||||
|
||||
/* max fs object segment size */
|
||||
#define MAX_OBJ_SEG_SIZE 64000
|
||||
|
||||
|
||||
typedef struct {
|
||||
char id[41];
|
||||
uint8_t object[0];
|
||||
} __attribute__((__packed__)) ObjectPack;
|
||||
|
||||
#endif
|
5118
common/rpc-service.c
Normal file
5118
common/rpc-service.c
Normal file
File diff suppressed because it is too large
Load diff
979
common/seaf-db.c
Normal file
979
common/seaf-db.c
Normal file
|
@ -0,0 +1,979 @@
|
|||
|
||||
#include "common.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include "db-wrapper/db-wrapper.h"
|
||||
#include "seaf-db.h"
|
||||
|
||||
#define MAX_GET_CONNECTION_RETRIES 3
|
||||
|
||||
struct SeafDB {
|
||||
int type;
|
||||
DBConnPool *pool;
|
||||
};
|
||||
|
||||
struct SeafDBRow {
|
||||
ResultSet *res;
|
||||
};
|
||||
|
||||
struct SeafDBTrans {
|
||||
DBConnection *conn;
|
||||
};
|
||||
|
||||
SeafDB *
|
||||
seaf_db_new_mysql (const char *host,
|
||||
int port,
|
||||
const char *user,
|
||||
const char *passwd,
|
||||
const char *db_name,
|
||||
const char *unix_socket,
|
||||
gboolean use_ssl,
|
||||
const char *charset,
|
||||
int max_connections)
|
||||
{
|
||||
SeafDB *db;
|
||||
|
||||
db = g_new0 (SeafDB, 1);
|
||||
if (!db) {
|
||||
seaf_warning ("Failed to alloc db structure.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
db->type = SEAF_DB_TYPE_MYSQL;
|
||||
|
||||
db->pool = db_conn_pool_new_mysql (host, user, passwd, port, db_name,
|
||||
unix_socket, use_ssl, charset, max_connections);
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
SeafDB *
|
||||
seaf_db_new_pgsql (const char *host,
|
||||
const char *user,
|
||||
const char *passwd,
|
||||
const char *db_name,
|
||||
const char *unix_socket)
|
||||
{
|
||||
SeafDB *db;
|
||||
|
||||
db = g_new0 (SeafDB, 1);
|
||||
if (!db) {
|
||||
seaf_warning ("Failed to alloc db structure.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
db->type = SEAF_DB_TYPE_PGSQL;
|
||||
|
||||
db->pool = db_conn_pool_new_pgsql (host, user, passwd, db_name, unix_socket);
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
SeafDB *
|
||||
seaf_db_new_sqlite (const char *db_path, int max_connections)
|
||||
{
|
||||
SeafDB *db;
|
||||
|
||||
db = g_new0 (SeafDB, 1);
|
||||
if (!db) {
|
||||
seaf_warning ("Failed to alloc db structure.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
db->type = SEAF_DB_TYPE_SQLITE;
|
||||
|
||||
db->pool = db_conn_pool_new_sqlite (db_path, max_connections);
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_db_free (SeafDB *db)
|
||||
{
|
||||
db_conn_pool_free (db->pool);
|
||||
g_free (db);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_type (SeafDB *db)
|
||||
{
|
||||
return db->type;
|
||||
}
|
||||
|
||||
static DBConnection *
|
||||
get_db_connection (SeafDB *db)
|
||||
{
|
||||
DBConnection *conn;
|
||||
GError *error = NULL;
|
||||
|
||||
conn = db_conn_pool_get_connection (db->pool, &error);
|
||||
|
||||
if (!conn) {
|
||||
seaf_warning ("Failed to get database connection: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_query (SeafDB *db, const char *sql)
|
||||
{
|
||||
GError *error = NULL;
|
||||
int ret = 0;
|
||||
|
||||
DBConnection *conn = get_db_connection (db);
|
||||
if (!conn)
|
||||
return -1;
|
||||
|
||||
if (!db_connection_execute (conn, sql, &error)) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
db_connection_close (conn);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_db_check_for_existence (SeafDB *db, const char *sql, gboolean *db_err)
|
||||
{
|
||||
DBConnection *conn;
|
||||
ResultSet *result;
|
||||
gboolean ret = TRUE;
|
||||
GError *error = NULL;
|
||||
|
||||
*db_err = FALSE;
|
||||
|
||||
conn = get_db_connection (db);
|
||||
if (!conn) {
|
||||
*db_err = TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
result = db_connection_execute_query (conn, sql, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
*db_err = TRUE;
|
||||
ret = FALSE;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = result_set_next (result, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
*db_err = TRUE;
|
||||
}
|
||||
|
||||
out:
|
||||
db_connection_close (conn);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_foreach_selected_row (SeafDB *db, const char *sql,
|
||||
SeafDBRowFunc callback, void *data)
|
||||
{
|
||||
DBConnection *conn;
|
||||
ResultSet *result;
|
||||
SeafDBRow seaf_row;
|
||||
int n_rows = 0;
|
||||
GError *error = NULL;
|
||||
|
||||
conn = get_db_connection (db);
|
||||
if (!conn)
|
||||
return -1;
|
||||
|
||||
result = db_connection_execute_query (conn, sql, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
n_rows = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
seaf_row.res = result;
|
||||
|
||||
while (result_set_next (result, &error)) {
|
||||
n_rows++;
|
||||
if (!callback (&seaf_row, data))
|
||||
break;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
n_rows = -1;
|
||||
}
|
||||
|
||||
out:
|
||||
db_connection_close (conn);
|
||||
return n_rows;
|
||||
}
|
||||
|
||||
const char *
|
||||
seaf_db_row_get_column_text (SeafDBRow *row, guint32 idx)
|
||||
{
|
||||
GError *error = NULL;
|
||||
g_return_val_if_fail (idx < result_set_get_column_count(row->res), NULL);
|
||||
|
||||
return result_set_get_string (row->res, idx, &error);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_row_get_column_int (SeafDBRow *row, guint32 idx)
|
||||
{
|
||||
GError *error = NULL;
|
||||
g_return_val_if_fail (idx < result_set_get_column_count(row->res), -1);
|
||||
|
||||
return result_set_get_int (row->res, idx, &error);
|
||||
}
|
||||
|
||||
gint64
|
||||
seaf_db_row_get_column_int64 (SeafDBRow *row, guint32 idx)
|
||||
{
|
||||
GError *error = NULL;
|
||||
g_return_val_if_fail (idx < result_set_get_column_count(row->res), -1);
|
||||
|
||||
return result_set_get_int64 (row->res, idx, &error);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_get_int (SeafDB *db, const char *sql)
|
||||
{
|
||||
int ret = -1;
|
||||
DBConnection *conn;
|
||||
ResultSet *result;
|
||||
SeafDBRow seaf_row;
|
||||
GError *error = NULL;
|
||||
|
||||
conn = get_db_connection (db);
|
||||
if (!conn)
|
||||
return -1;
|
||||
|
||||
result = db_connection_execute_query (conn, sql, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
goto out;
|
||||
}
|
||||
|
||||
seaf_row.res = result;
|
||||
if (!result_set_next (result, &error)) {
|
||||
if (error) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = seaf_db_row_get_column_int (&seaf_row, 0);
|
||||
|
||||
out:
|
||||
db_connection_close (conn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gint64
|
||||
seaf_db_get_int64 (SeafDB *db, const char *sql)
|
||||
{
|
||||
gint64 ret = -1;
|
||||
DBConnection *conn;
|
||||
ResultSet *result;
|
||||
SeafDBRow seaf_row;
|
||||
GError *error = NULL;
|
||||
|
||||
conn = get_db_connection (db);
|
||||
if (!conn)
|
||||
return -1;
|
||||
|
||||
result = db_connection_execute_query (conn, sql, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
goto out;
|
||||
}
|
||||
|
||||
seaf_row.res = result;
|
||||
if (!result_set_next (result, &error)) {
|
||||
if (error) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = seaf_db_row_get_column_int64 (&seaf_row, 0);
|
||||
|
||||
out:
|
||||
db_connection_close (conn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *
|
||||
seaf_db_get_string (SeafDB *db, const char *sql)
|
||||
{
|
||||
char *ret = NULL;
|
||||
const char *s;
|
||||
DBConnection *conn;
|
||||
ResultSet *result;
|
||||
SeafDBRow seaf_row;
|
||||
GError *error = NULL;
|
||||
|
||||
conn = get_db_connection (db);
|
||||
if (!conn)
|
||||
return NULL;
|
||||
|
||||
result = db_connection_execute_query (conn, sql, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
goto out;
|
||||
}
|
||||
|
||||
seaf_row.res = result;
|
||||
if (!result_set_next (result, &error)) {
|
||||
if (error) {
|
||||
seaf_warning ("Error exec query %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
s = seaf_db_row_get_column_text (&seaf_row, 0);
|
||||
ret = g_strdup(s);
|
||||
|
||||
out:
|
||||
db_connection_close (conn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *
|
||||
seaf_db_escape_string (SeafDB *db, const char *from)
|
||||
{
|
||||
const char *p = from;
|
||||
char *to, *q;
|
||||
|
||||
to = g_malloc0 (2*strlen(from)+1);
|
||||
q = to;
|
||||
|
||||
while (*p != '\0') {
|
||||
if (*p == '\'' || *p == '\\' || *p == '"') {
|
||||
*q = *p;
|
||||
*(++q) = *p;
|
||||
} else
|
||||
*q = *p;
|
||||
++p;
|
||||
++q;
|
||||
}
|
||||
|
||||
return to;
|
||||
}
|
||||
|
||||
gboolean
|
||||
pgsql_index_exists (SeafDB *db, const char *index_name)
|
||||
{
|
||||
char sql[256];
|
||||
gboolean db_err = FALSE;
|
||||
|
||||
snprintf (sql, sizeof(sql),
|
||||
"SELECT 1 FROM pg_class WHERE relname='%s'",
|
||||
index_name);
|
||||
return seaf_db_check_for_existence (db, sql, &db_err);
|
||||
}
|
||||
|
||||
/* Prepared Statements */
|
||||
|
||||
struct SeafDBStatement {
|
||||
DBStmt *p;
|
||||
DBConnection *conn;
|
||||
};
|
||||
typedef struct SeafDBStatement SeafDBStatement;
|
||||
|
||||
SeafDBStatement *
|
||||
seaf_db_prepare_statement (SeafDB *db, const char *sql)
|
||||
{
|
||||
DBStmt *p;
|
||||
SeafDBStatement *ret = g_new0 (SeafDBStatement, 1);
|
||||
GError *error = NULL;
|
||||
|
||||
DBConnection *conn = get_db_connection (db);
|
||||
if (!conn) {
|
||||
g_free (ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p = db_connection_prepare_statement (conn, sql, &error);
|
||||
if (!p) {
|
||||
seaf_warning ("Error prepare statement %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
g_free (ret);
|
||||
db_connection_close (conn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret->p = p;
|
||||
ret->conn = conn;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_db_statement_free (SeafDBStatement *p)
|
||||
{
|
||||
db_connection_close (p->conn);
|
||||
g_free (p);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_statement_set_int (DBStmt *p, int idx, int x)
|
||||
{
|
||||
GError *error = NULL;
|
||||
|
||||
if (!db_stmt_set_int (p, idx, x, &error)) {
|
||||
seaf_warning ("Error set int in prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_statement_set_string (DBStmt *p, int idx, const char *s)
|
||||
{
|
||||
GError *error = NULL;
|
||||
|
||||
if (!db_stmt_set_string (p, idx, s, &error)) {
|
||||
seaf_warning ("Error set string in prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_statement_set_int64 (DBStmt *p, int idx, gint64 x)
|
||||
{
|
||||
GError *error = NULL;
|
||||
|
||||
if (!db_stmt_set_int64 (p, idx, x, &error)) {
|
||||
seaf_warning ("Error set int64 in prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
set_parameters_va (DBStmt *p, int n, va_list args)
|
||||
{
|
||||
int i;
|
||||
const char *type;
|
||||
|
||||
for (i = 0; i < n; ++i) {
|
||||
type = va_arg (args, const char *);
|
||||
if (strcmp(type, "int") == 0) {
|
||||
int x = va_arg (args, int);
|
||||
if (seaf_db_statement_set_int (p, i, x) < 0)
|
||||
return -1;
|
||||
} else if (strcmp (type, "int64") == 0) {
|
||||
gint64 x = va_arg (args, gint64);
|
||||
if (seaf_db_statement_set_int64 (p, i, x) < 0)
|
||||
return -1;
|
||||
} else if (strcmp (type, "string") == 0) {
|
||||
const char *s = va_arg (args, const char *);
|
||||
if (seaf_db_statement_set_string (p, i, s) < 0)
|
||||
return -1;
|
||||
} else {
|
||||
seaf_warning ("BUG: invalid prep stmt parameter type %s.\n", type);
|
||||
g_return_val_if_reached (-1);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_statement_query (SeafDB *db, const char *sql, int n, ...)
|
||||
{
|
||||
SeafDBStatement *p;
|
||||
int ret = 0;
|
||||
GError *error = NULL;
|
||||
|
||||
p = seaf_db_prepare_statement (db, sql);
|
||||
if (!p)
|
||||
return -1;
|
||||
|
||||
va_list args;
|
||||
va_start (args, n);
|
||||
if (set_parameters_va (p->p, n, args) < 0) {
|
||||
seaf_db_statement_free (p);
|
||||
va_end (args);
|
||||
return -1;
|
||||
}
|
||||
va_end (args);
|
||||
|
||||
if (!db_stmt_execute (p->p, &error)) {
|
||||
seaf_warning ("Error execute prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
seaf_db_statement_free (p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_db_statement_exists (SeafDB *db, const char *sql, gboolean *db_err, int n, ...)
|
||||
{
|
||||
SeafDBStatement *p;
|
||||
ResultSet *result;
|
||||
gboolean ret = TRUE;
|
||||
GError *error = NULL;
|
||||
|
||||
*db_err = FALSE;
|
||||
|
||||
p = seaf_db_prepare_statement (db, sql);
|
||||
if (!p) {
|
||||
*db_err = TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
va_list args;
|
||||
va_start (args, n);
|
||||
if (set_parameters_va (p->p, n, args) < 0) {
|
||||
seaf_db_statement_free (p);
|
||||
*db_err = TRUE;
|
||||
va_end (args);
|
||||
return FALSE;
|
||||
}
|
||||
va_end (args);
|
||||
|
||||
result = db_stmt_execute_query (p->p, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
*db_err = TRUE;
|
||||
ret = FALSE;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!result_set_next (result, &error))
|
||||
ret = FALSE;
|
||||
|
||||
if (error) {
|
||||
seaf_warning ("Error get next result from prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
*db_err = TRUE;
|
||||
}
|
||||
|
||||
out:
|
||||
seaf_db_statement_free (p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_statement_foreach_row (SeafDB *db,
|
||||
const char *sql,
|
||||
SeafDBRowFunc callback, void *data,
|
||||
int n, ...)
|
||||
{
|
||||
SeafDBStatement *p;
|
||||
ResultSet *result;
|
||||
SeafDBRow seaf_row;
|
||||
int n_rows = 0;
|
||||
GError *error = NULL;
|
||||
|
||||
p = seaf_db_prepare_statement (db, sql);
|
||||
if (!p)
|
||||
return -1;
|
||||
|
||||
va_list args;
|
||||
va_start (args, n);
|
||||
if (set_parameters_va (p->p, n, args) < 0) {
|
||||
seaf_db_statement_free (p);
|
||||
va_end (args);
|
||||
return -1;
|
||||
}
|
||||
va_end (args);
|
||||
|
||||
result = db_stmt_execute_query (p->p, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
n_rows = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
seaf_row.res = result;
|
||||
|
||||
while (result_set_next (result, &error)) {
|
||||
n_rows++;
|
||||
if (!callback (&seaf_row, data))
|
||||
break;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
seaf_warning ("Error get next result from prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
n_rows = -1;
|
||||
}
|
||||
|
||||
out:
|
||||
seaf_db_statement_free (p);
|
||||
return n_rows;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_statement_get_int (SeafDB *db, const char *sql, int n, ...)
|
||||
{
|
||||
SeafDBStatement *p;
|
||||
int ret = -1;
|
||||
ResultSet *result;
|
||||
SeafDBRow seaf_row;
|
||||
GError *error = NULL;
|
||||
|
||||
p = seaf_db_prepare_statement (db, sql);
|
||||
if (!p)
|
||||
return -1;
|
||||
|
||||
va_list args;
|
||||
va_start (args, n);
|
||||
if (set_parameters_va (p->p, n, args) < 0) {
|
||||
seaf_db_statement_free (p);
|
||||
va_end (args);
|
||||
return -1;
|
||||
}
|
||||
va_end (args);
|
||||
|
||||
result = db_stmt_execute_query (p->p, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
goto out;
|
||||
}
|
||||
|
||||
seaf_row.res = result;
|
||||
if (!result_set_next (result, &error)) {
|
||||
if (error) {
|
||||
seaf_warning ("Error get next result from prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = seaf_db_row_get_column_int (&seaf_row, 0);
|
||||
|
||||
out:
|
||||
seaf_db_statement_free (p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gint64
|
||||
seaf_db_statement_get_int64 (SeafDB *db, const char *sql, int n, ...)
|
||||
{
|
||||
SeafDBStatement *p;
|
||||
gint64 ret = -1;
|
||||
ResultSet *result;
|
||||
SeafDBRow seaf_row;
|
||||
GError *error = NULL;
|
||||
|
||||
p = seaf_db_prepare_statement (db, sql);
|
||||
if (!p)
|
||||
return -1;
|
||||
|
||||
va_list args;
|
||||
va_start (args, n);
|
||||
if (set_parameters_va (p->p, n, args) < 0) {
|
||||
seaf_db_statement_free (p);
|
||||
va_end (args);
|
||||
return -1;
|
||||
}
|
||||
va_end (args);
|
||||
|
||||
result = db_stmt_execute_query (p->p, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
goto out;
|
||||
}
|
||||
|
||||
seaf_row.res = result;
|
||||
if (!result_set_next (result, &error)) {
|
||||
if (error) {
|
||||
seaf_warning ("Error get next result from prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = seaf_db_row_get_column_int64 (&seaf_row, 0);
|
||||
|
||||
out:
|
||||
seaf_db_statement_free (p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *
|
||||
seaf_db_statement_get_string (SeafDB *db, const char *sql, int n, ...)
|
||||
{
|
||||
SeafDBStatement *p;
|
||||
char *ret = NULL;
|
||||
const char *s;
|
||||
ResultSet *result;
|
||||
SeafDBRow seaf_row;
|
||||
GError *error = NULL;
|
||||
|
||||
p = seaf_db_prepare_statement (db, sql);
|
||||
if (!p)
|
||||
return NULL;
|
||||
|
||||
va_list args;
|
||||
va_start (args, n);
|
||||
if (set_parameters_va (p->p, n, args) < 0) {
|
||||
seaf_db_statement_free (p);
|
||||
va_end (args);
|
||||
return NULL;
|
||||
}
|
||||
va_end (args);
|
||||
|
||||
result = db_stmt_execute_query (p->p, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
goto out;
|
||||
}
|
||||
|
||||
seaf_row.res = result;
|
||||
if (!result_set_next (result, &error)) {
|
||||
if (error) {
|
||||
seaf_warning ("Error get next result from prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
s = seaf_db_row_get_column_text (&seaf_row, 0);
|
||||
ret = g_strdup(s);
|
||||
|
||||
out:
|
||||
seaf_db_statement_free (p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Transaction */
|
||||
|
||||
SeafDBTrans *
|
||||
seaf_db_begin_transaction (SeafDB *db)
|
||||
{
|
||||
DBConnection *conn;
|
||||
SeafDBTrans *trans;
|
||||
GError *error = NULL;
|
||||
|
||||
trans = g_new0 (SeafDBTrans, 1);
|
||||
if (!trans)
|
||||
return NULL;
|
||||
|
||||
conn = get_db_connection (db);
|
||||
if (!conn) {
|
||||
g_free (trans);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
trans->conn = conn;
|
||||
if (!db_connection_begin_transaction (trans->conn, &error)) {
|
||||
seaf_warning ("Start transaction failed: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
db_connection_close (trans->conn);
|
||||
g_free (trans);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return trans;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_db_trans_close (SeafDBTrans *trans)
|
||||
{
|
||||
db_connection_close (trans->conn);
|
||||
g_free (trans);
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_commit (SeafDBTrans *trans)
|
||||
{
|
||||
DBConnection *conn = trans->conn;
|
||||
GError *error = NULL;
|
||||
|
||||
if (!db_connection_commit (conn, &error)) {
|
||||
seaf_warning ("Commit failed: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_rollback (SeafDBTrans *trans)
|
||||
{
|
||||
DBConnection *conn = trans->conn;
|
||||
GError *error = NULL;
|
||||
|
||||
if (!db_connection_commit (conn, &error)) {
|
||||
seaf_warning ("Rollback failed: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DBStmt *
|
||||
trans_prepare_statement (DBConnection *conn, const char *sql)
|
||||
{
|
||||
DBStmt *p;
|
||||
GError *error = NULL;
|
||||
|
||||
p = db_connection_prepare_statement (conn, sql, &error);
|
||||
if (!p) {
|
||||
seaf_warning ("Error prepare statement %s: %s.\n", sql, error->message);
|
||||
g_clear_error (&error);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_trans_query (SeafDBTrans *trans, const char *sql, int n, ...)
|
||||
{
|
||||
DBStmt *p;
|
||||
GError *error = NULL;
|
||||
|
||||
p = trans_prepare_statement (trans->conn, sql);
|
||||
if (!p)
|
||||
return -1;
|
||||
|
||||
va_list args;
|
||||
va_start (args, n);
|
||||
if (set_parameters_va (p, n, args) < 0) {
|
||||
va_end (args);
|
||||
return -1;
|
||||
}
|
||||
va_end (args);
|
||||
|
||||
if (!db_stmt_execute (p, &error)) {
|
||||
seaf_warning ("Error exec prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_db_trans_check_for_existence (SeafDBTrans *trans,
|
||||
const char *sql,
|
||||
gboolean *db_err,
|
||||
int n, ...)
|
||||
{
|
||||
ResultSet *result;
|
||||
gboolean ret = TRUE;
|
||||
GError *error = NULL;
|
||||
|
||||
*db_err = FALSE;
|
||||
|
||||
DBStmt *p;
|
||||
|
||||
p = trans_prepare_statement (trans->conn, sql);
|
||||
if (!p) {
|
||||
*db_err = TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
va_list args;
|
||||
va_start (args, n);
|
||||
if (set_parameters_va (p, n, args) < 0) {
|
||||
*db_err = TRUE;
|
||||
va_end (args);
|
||||
return FALSE;
|
||||
}
|
||||
va_end (args);
|
||||
|
||||
result = db_stmt_execute_query (p, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
*db_err = TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!result_set_next (result, &error))
|
||||
ret = FALSE;
|
||||
|
||||
if (error) {
|
||||
seaf_warning ("Error get next result from prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
*db_err = TRUE;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_db_trans_foreach_selected_row (SeafDBTrans *trans, const char *sql,
|
||||
SeafDBRowFunc callback, void *data,
|
||||
int n, ...)
|
||||
{
|
||||
ResultSet *result;
|
||||
SeafDBRow seaf_row;
|
||||
int n_rows = 0;
|
||||
GError *error = NULL;
|
||||
|
||||
DBStmt *p;
|
||||
|
||||
p = trans_prepare_statement (trans->conn, sql);
|
||||
if (!p)
|
||||
return FALSE;
|
||||
|
||||
va_list args;
|
||||
va_start (args, n);
|
||||
if (set_parameters_va (p, n, args) < 0) {
|
||||
va_end (args);
|
||||
return -1;
|
||||
}
|
||||
va_end (args);
|
||||
|
||||
result = db_stmt_execute_query (p, &error);
|
||||
if (error) {
|
||||
seaf_warning ("Error exec prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
seaf_row.res = result;
|
||||
|
||||
while (result_set_next (result, &error)) {
|
||||
n_rows++;
|
||||
if (!callback (&seaf_row, data))
|
||||
break;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
seaf_warning ("Error get next result from prep stmt: %s.\n", error->message);
|
||||
g_clear_error (&error);
|
||||
n_rows = -1;
|
||||
}
|
||||
|
||||
return n_rows;
|
||||
}
|
129
common/seaf-db.h
Normal file
129
common/seaf-db.h
Normal file
|
@ -0,0 +1,129 @@
|
|||
#ifndef SEAF_DB_H
|
||||
#define SEAF_DB_H
|
||||
|
||||
enum {
|
||||
SEAF_DB_TYPE_SQLITE,
|
||||
SEAF_DB_TYPE_MYSQL,
|
||||
SEAF_DB_TYPE_PGSQL,
|
||||
};
|
||||
|
||||
typedef struct SeafDB SeafDB;
|
||||
typedef struct SeafDBRow SeafDBRow;
|
||||
typedef struct SeafDBTrans SeafDBTrans;
|
||||
|
||||
typedef gboolean (*SeafDBRowFunc) (SeafDBRow *, void *);
|
||||
|
||||
SeafDB *
|
||||
seaf_db_new_mysql (const char *host,
|
||||
int port,
|
||||
const char *user,
|
||||
const char *passwd,
|
||||
const char *db,
|
||||
const char *unix_socket,
|
||||
gboolean use_ssl,
|
||||
const char *charset,
|
||||
int max_connections);
|
||||
|
||||
SeafDB *
|
||||
seaf_db_new_pgsql (const char *host,
|
||||
const char *user,
|
||||
const char *passwd,
|
||||
const char *db_name,
|
||||
const char *unix_socket);
|
||||
|
||||
SeafDB *
|
||||
seaf_db_new_sqlite (const char *db_path, int max_connections);
|
||||
|
||||
void
|
||||
seaf_db_free (SeafDB *db);
|
||||
|
||||
int
|
||||
seaf_db_type (SeafDB *db);
|
||||
|
||||
int
|
||||
seaf_db_query (SeafDB *db, const char *sql);
|
||||
|
||||
gboolean
|
||||
seaf_db_check_for_existence (SeafDB *db, const char *sql, gboolean *db_err);
|
||||
|
||||
int
|
||||
seaf_db_foreach_selected_row (SeafDB *db, const char *sql,
|
||||
SeafDBRowFunc callback, void *data);
|
||||
|
||||
const char *
|
||||
seaf_db_row_get_column_text (SeafDBRow *row, guint32 idx);
|
||||
|
||||
int
|
||||
seaf_db_row_get_column_int (SeafDBRow *row, guint32 idx);
|
||||
|
||||
gint64
|
||||
seaf_db_row_get_column_int64 (SeafDBRow *row, guint32 idx);
|
||||
|
||||
int
|
||||
seaf_db_get_int (SeafDB *db, const char *sql);
|
||||
|
||||
gint64
|
||||
seaf_db_get_int64 (SeafDB *db, const char *sql);
|
||||
|
||||
char *
|
||||
seaf_db_get_string (SeafDB *db, const char *sql);
|
||||
|
||||
/* Transaction related */
|
||||
|
||||
SeafDBTrans *
|
||||
seaf_db_begin_transaction (SeafDB *db);
|
||||
|
||||
void
|
||||
seaf_db_trans_close (SeafDBTrans *trans);
|
||||
|
||||
int
|
||||
seaf_db_commit (SeafDBTrans *trans);
|
||||
|
||||
int
|
||||
seaf_db_rollback (SeafDBTrans *trans);
|
||||
|
||||
int
|
||||
seaf_db_trans_query (SeafDBTrans *trans, const char *sql, int n, ...);
|
||||
|
||||
gboolean
|
||||
seaf_db_trans_check_for_existence (SeafDBTrans *trans,
|
||||
const char *sql,
|
||||
gboolean *db_err,
|
||||
int n, ...);
|
||||
|
||||
int
|
||||
seaf_db_trans_foreach_selected_row (SeafDBTrans *trans, const char *sql,
|
||||
SeafDBRowFunc callback, void *data,
|
||||
int n, ...);
|
||||
|
||||
/* Escape a string contant by doubling '\" characters.
|
||||
*/
|
||||
char *
|
||||
seaf_db_escape_string (SeafDB *db, const char *from);
|
||||
|
||||
gboolean
|
||||
pgsql_index_exists (SeafDB *db, const char *index_name);
|
||||
|
||||
/* Prepared Statements */
|
||||
|
||||
int
|
||||
seaf_db_statement_query (SeafDB *db, const char *sql, int n, ...);
|
||||
|
||||
gboolean
|
||||
seaf_db_statement_exists (SeafDB *db, const char *sql, gboolean *db_err, int n, ...);
|
||||
|
||||
int
|
||||
seaf_db_statement_foreach_row (SeafDB *db, const char *sql,
|
||||
SeafDBRowFunc callback, void *data,
|
||||
int n, ...);
|
||||
|
||||
int
|
||||
seaf_db_statement_get_int (SeafDB *db, const char *sql, int n, ...);
|
||||
|
||||
gint64
|
||||
seaf_db_statement_get_int64 (SeafDB *db, const char *sql, int n, ...);
|
||||
|
||||
char *
|
||||
seaf_db_statement_get_string (SeafDB *db, const char *sql, int n, ...);
|
||||
|
||||
#endif
|
201
common/seaf-utils.c
Normal file
201
common/seaf-utils.c
Normal file
|
@ -0,0 +1,201 @@
|
|||
#include "common.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include "seafile-session.h"
|
||||
#include "seaf-utils.h"
|
||||
#include "seaf-db.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *
|
||||
seafile_session_get_tmp_file_path (SeafileSession *session,
|
||||
const char *basename,
|
||||
char path[])
|
||||
{
|
||||
int path_len;
|
||||
|
||||
path_len = strlen (session->tmp_file_dir);
|
||||
memcpy (path, session->tmp_file_dir, path_len + 1);
|
||||
path[path_len] = '/';
|
||||
strcpy (path + path_len + 1, basename);
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
#ifdef SEAFILE_SERVER
|
||||
|
||||
#define SQLITE_DB_NAME "seafile.db"
|
||||
|
||||
#define DEFAULT_MAX_CONNECTIONS 100
|
||||
|
||||
static int
|
||||
sqlite_db_start (SeafileSession *session)
|
||||
{
|
||||
char *db_path;
|
||||
int max_connections = 0;
|
||||
|
||||
max_connections = g_key_file_get_integer (session->config,
|
||||
"database", "max_connections",
|
||||
NULL);
|
||||
if (max_connections <= 0)
|
||||
max_connections = DEFAULT_MAX_CONNECTIONS;
|
||||
|
||||
db_path = g_build_filename (session->seaf_dir, SQLITE_DB_NAME, NULL);
|
||||
session->db = seaf_db_new_sqlite (db_path, max_connections);
|
||||
if (!session->db) {
|
||||
seaf_warning ("Failed to start sqlite db.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define MYSQL_DEFAULT_PORT 3306
|
||||
|
||||
static int
|
||||
mysql_db_start (SeafileSession *session)
|
||||
{
|
||||
char *host, *user, *passwd, *db, *unix_socket, *charset;
|
||||
int port;
|
||||
gboolean use_ssl = FALSE;
|
||||
int max_connections = 0;
|
||||
GError *error = NULL;
|
||||
|
||||
host = g_key_file_get_string (session->config, "database", "host", &error);
|
||||
if (!host) {
|
||||
seaf_warning ("DB host not set in config.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
port = g_key_file_get_integer (session->config, "database", "port", &error);
|
||||
if (error) {
|
||||
port = MYSQL_DEFAULT_PORT;
|
||||
}
|
||||
|
||||
user = g_key_file_get_string (session->config, "database", "user", &error);
|
||||
if (!user) {
|
||||
seaf_warning ("DB user not set in config.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
passwd = g_key_file_get_string (session->config, "database", "password", &error);
|
||||
if (!passwd) {
|
||||
seaf_warning ("DB passwd not set in config.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
db = g_key_file_get_string (session->config, "database", "db_name", &error);
|
||||
if (!db) {
|
||||
seaf_warning ("DB name not set in config.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
unix_socket = g_key_file_get_string (session->config,
|
||||
"database", "unix_socket", NULL);
|
||||
|
||||
use_ssl = g_key_file_get_boolean (session->config,
|
||||
"database", "use_ssl", NULL);
|
||||
|
||||
charset = g_key_file_get_string (session->config,
|
||||
"database", "connection_charset", NULL);
|
||||
|
||||
max_connections = g_key_file_get_integer (session->config,
|
||||
"database", "max_connections",
|
||||
NULL);
|
||||
if (max_connections <= 0)
|
||||
max_connections = DEFAULT_MAX_CONNECTIONS;
|
||||
|
||||
session->db = seaf_db_new_mysql (host, port, user, passwd, db, unix_socket, use_ssl, charset, max_connections);
|
||||
if (!session->db) {
|
||||
seaf_warning ("Failed to start mysql db.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_free (host);
|
||||
g_free (user);
|
||||
g_free (passwd);
|
||||
g_free (db);
|
||||
g_free (unix_socket);
|
||||
g_free (charset);
|
||||
if (error)
|
||||
g_clear_error (&error);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
pgsql_db_start (SeafileSession *session)
|
||||
{
|
||||
char *host, *user, *passwd, *db, *unix_socket;
|
||||
GError *error = NULL;
|
||||
|
||||
host = g_key_file_get_string (session->config, "database", "host", &error);
|
||||
if (!host) {
|
||||
seaf_warning ("DB host not set in config.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
user = g_key_file_get_string (session->config, "database", "user", &error);
|
||||
if (!user) {
|
||||
seaf_warning ("DB user not set in config.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
passwd = g_key_file_get_string (session->config, "database", "password", &error);
|
||||
if (!passwd) {
|
||||
seaf_warning ("DB passwd not set in config.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
db = g_key_file_get_string (session->config, "database", "db_name", &error);
|
||||
if (!db) {
|
||||
seaf_warning ("DB name not set in config.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
unix_socket = g_key_file_get_string (session->config,
|
||||
"database", "unix_socket", &error);
|
||||
|
||||
session->db = seaf_db_new_pgsql (host, user, passwd, db, unix_socket);
|
||||
if (!session->db) {
|
||||
seaf_warning ("Failed to start pgsql db.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_free (host);
|
||||
g_free (user);
|
||||
g_free (passwd);
|
||||
g_free (db);
|
||||
g_free (unix_socket);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
load_database_config (SeafileSession *session)
|
||||
{
|
||||
char *type;
|
||||
GError *error = NULL;
|
||||
int ret = 0;
|
||||
|
||||
type = g_key_file_get_string (session->config, "database", "type", &error);
|
||||
/* Default to use sqlite if not set. */
|
||||
if (!type || strcasecmp (type, "sqlite") == 0) {
|
||||
ret = sqlite_db_start (session);
|
||||
} else if (strcasecmp (type, "mysql") == 0) {
|
||||
ret = mysql_db_start (session);
|
||||
} else if (strcasecmp (type, "pgsql") == 0) {
|
||||
ret = pgsql_db_start (session);
|
||||
} else {
|
||||
seaf_warning ("Unsupported db type %s.\n", type);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
g_free (type);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
17
common/seaf-utils.h
Normal file
17
common/seaf-utils.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef SEAF_UTILS_H
|
||||
#define SEAF_UTILS_H
|
||||
|
||||
struct _SeafileSession;
|
||||
|
||||
|
||||
char *
|
||||
seafile_session_get_tmp_file_path (struct _SeafileSession *session,
|
||||
const char *basename,
|
||||
char path[]);
|
||||
|
||||
#ifdef SEAFILE_SERVER
|
||||
int
|
||||
load_database_config (struct _SeafileSession *session);
|
||||
#endif
|
||||
|
||||
#endif
|
481
common/seafile-crypt.c
Normal file
481
common/seafile-crypt.c
Normal file
|
@ -0,0 +1,481 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
#include "seafile-crypt.h"
|
||||
#include <openssl/rand.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "log.h"
|
||||
|
||||
/*
|
||||
The EVP_EncryptXXX and EVP_DecryptXXX series of functions have a
|
||||
weird choice of returned value.
|
||||
*/
|
||||
#define ENC_SUCCESS 1
|
||||
#define ENC_FAILURE 0
|
||||
#define DEC_SUCCESS 1
|
||||
#define DEC_FAILURE 0
|
||||
|
||||
#define KEYGEN_ITERATION 1 << 19
|
||||
#define KEYGEN_ITERATION2 1000
|
||||
/* Should generate random salt for each repo. */
|
||||
static unsigned char salt[8] = { 0xda, 0x90, 0x45, 0xc3, 0x06, 0xc7, 0xcc, 0x26 };
|
||||
|
||||
SeafileCrypt *
|
||||
seafile_crypt_new (int version, unsigned char *key, unsigned char *iv)
|
||||
{
|
||||
SeafileCrypt *crypt = g_new0 (SeafileCrypt, 1);
|
||||
crypt->version = version;
|
||||
if (version == 1)
|
||||
memcpy (crypt->key, key, 16);
|
||||
else
|
||||
memcpy (crypt->key, key, 32);
|
||||
memcpy (crypt->iv, iv, 16);
|
||||
return crypt;
|
||||
}
|
||||
|
||||
int
|
||||
seafile_derive_key (const char *data_in, int in_len, int version,
|
||||
unsigned char *key, unsigned char *iv)
|
||||
{
|
||||
if (version == 2) {
|
||||
PKCS5_PBKDF2_HMAC (data_in, in_len,
|
||||
salt, sizeof(salt),
|
||||
KEYGEN_ITERATION2,
|
||||
EVP_sha256(),
|
||||
32, key);
|
||||
PKCS5_PBKDF2_HMAC ((char *)key, 32,
|
||||
salt, sizeof(salt),
|
||||
10,
|
||||
EVP_sha256(),
|
||||
16, iv);
|
||||
return 0;
|
||||
} else if (version == 1)
|
||||
return EVP_BytesToKey (EVP_aes_128_cbc(), /* cipher mode */
|
||||
EVP_sha1(), /* message digest */
|
||||
salt, /* salt */
|
||||
(unsigned char*)data_in,
|
||||
in_len,
|
||||
KEYGEN_ITERATION, /* iteration times */
|
||||
key, /* the derived key */
|
||||
iv); /* IV, initial vector */
|
||||
else
|
||||
return EVP_BytesToKey (EVP_aes_128_ecb(), /* cipher mode */
|
||||
EVP_sha1(), /* message digest */
|
||||
NULL, /* salt */
|
||||
(unsigned char*)data_in,
|
||||
in_len,
|
||||
3, /* iteration times */
|
||||
key, /* the derived key */
|
||||
iv); /* IV, initial vector */
|
||||
}
|
||||
|
||||
void
|
||||
seafile_generate_random_key (const char *passwd, char *random_key)
|
||||
{
|
||||
SeafileCrypt *crypt;
|
||||
unsigned char secret_key[32], *rand_key;
|
||||
int outlen;
|
||||
unsigned char key[32], iv[16];
|
||||
|
||||
int rc = RAND_bytes (secret_key, sizeof(secret_key));
|
||||
if (rc != 1) {
|
||||
seaf_warning ("Failed to generate secret key for repo encryption "
|
||||
"with RAND_bytes(), use RAND_pseudo_bytes().\n");
|
||||
RAND_pseudo_bytes (secret_key, sizeof(secret_key));
|
||||
}
|
||||
|
||||
seafile_derive_key (passwd, strlen(passwd), 2, key, iv);
|
||||
|
||||
crypt = seafile_crypt_new (2, key, iv);
|
||||
|
||||
seafile_encrypt ((char **)&rand_key, &outlen,
|
||||
(char *)secret_key, sizeof(secret_key), crypt);
|
||||
|
||||
rawdata_to_hex (rand_key, random_key, 48);
|
||||
|
||||
g_free (crypt);
|
||||
g_free (rand_key);
|
||||
}
|
||||
|
||||
void
|
||||
seafile_generate_magic (int version, const char *repo_id,
|
||||
const char *passwd, char *magic)
|
||||
{
|
||||
GString *buf = g_string_new (NULL);
|
||||
unsigned char key[32], iv[16];
|
||||
|
||||
/* Compute a "magic" string from repo_id and passwd.
|
||||
* This is used to verify the password given by user before decrypting
|
||||
* data.
|
||||
*/
|
||||
g_string_append_printf (buf, "%s%s", repo_id, passwd);
|
||||
|
||||
seafile_derive_key (buf->str, buf->len, version, key, iv);
|
||||
|
||||
g_string_free (buf, TRUE);
|
||||
rawdata_to_hex (key, magic, 32);
|
||||
}
|
||||
|
||||
int
|
||||
seafile_verify_repo_passwd (const char *repo_id,
|
||||
const char *passwd,
|
||||
const char *magic,
|
||||
int version)
|
||||
{
|
||||
GString *buf = g_string_new (NULL);
|
||||
unsigned char key[32], iv[16];
|
||||
char hex[65];
|
||||
|
||||
if (version != 1 && version != 2) {
|
||||
seaf_warning ("Unsupported enc_version %d.\n", version);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Recompute the magic and compare it with the one comes with the repo. */
|
||||
g_string_append_printf (buf, "%s%s", repo_id, passwd);
|
||||
|
||||
seafile_derive_key (buf->str, buf->len, version, key, iv);
|
||||
|
||||
g_string_free (buf, TRUE);
|
||||
if (version == 2)
|
||||
rawdata_to_hex (key, hex, 32);
|
||||
else
|
||||
rawdata_to_hex (key, hex, 16);
|
||||
|
||||
if (g_strcmp0 (hex, magic) == 0)
|
||||
return 0;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
seafile_decrypt_repo_enc_key (int enc_version,
|
||||
const char *passwd, const char *random_key,
|
||||
unsigned char *key_out, unsigned char *iv_out)
|
||||
{
|
||||
unsigned char key[32], iv[16];
|
||||
|
||||
seafile_derive_key (passwd, strlen(passwd), enc_version, key, iv);
|
||||
|
||||
if (enc_version == 1) {
|
||||
memcpy (key_out, key, 16);
|
||||
memcpy (iv_out, iv, 16);
|
||||
return 0;
|
||||
} else if (enc_version == 2) {
|
||||
unsigned char enc_random_key[48], *dec_random_key;
|
||||
int outlen;
|
||||
SeafileCrypt *crypt;
|
||||
|
||||
if (random_key == NULL || random_key[0] == 0) {
|
||||
seaf_warning ("Empty random key.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
hex_to_rawdata (random_key, enc_random_key, 48);
|
||||
|
||||
crypt = seafile_crypt_new (enc_version, key, iv);
|
||||
if (seafile_decrypt ((char **)&dec_random_key, &outlen,
|
||||
(char *)enc_random_key, 48,
|
||||
crypt) < 0) {
|
||||
seaf_warning ("Failed to decrypt random key.\n");
|
||||
g_free (crypt);
|
||||
return -1;
|
||||
}
|
||||
g_free (crypt);
|
||||
|
||||
seafile_derive_key ((char *)dec_random_key, 32, enc_version,
|
||||
key, iv);
|
||||
memcpy (key_out, key, 32);
|
||||
memcpy (iv_out, iv, 16);
|
||||
|
||||
g_free (dec_random_key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
seafile_update_random_key (const char *old_passwd, const char *old_random_key,
|
||||
const char *new_passwd, char *new_random_key)
|
||||
{
|
||||
unsigned char key[32], iv[16];
|
||||
unsigned char random_key_raw[48], *secret_key, *new_random_key_raw;
|
||||
int secret_key_len, random_key_len;
|
||||
SeafileCrypt *crypt;
|
||||
|
||||
/* First, use old_passwd to decrypt secret key from old_random_key. */
|
||||
seafile_derive_key (old_passwd, strlen(old_passwd), 2, key, iv);
|
||||
|
||||
hex_to_rawdata (old_random_key, random_key_raw, 48);
|
||||
|
||||
crypt = seafile_crypt_new (2, key, iv);
|
||||
if (seafile_decrypt ((char **)&secret_key, &secret_key_len,
|
||||
(char *)random_key_raw, 48,
|
||||
crypt) < 0) {
|
||||
seaf_warning ("Failed to decrypt random key.\n");
|
||||
g_free (crypt);
|
||||
return -1;
|
||||
}
|
||||
g_free (crypt);
|
||||
|
||||
/* Second, use new_passwd to encrypt secret key. */
|
||||
seafile_derive_key (new_passwd, strlen(new_passwd), 2, key, iv);
|
||||
|
||||
crypt = seafile_crypt_new (2, key, iv);
|
||||
|
||||
seafile_encrypt ((char **)&new_random_key_raw, &random_key_len,
|
||||
(char *)secret_key, secret_key_len, crypt);
|
||||
|
||||
rawdata_to_hex (new_random_key_raw, new_random_key, 48);
|
||||
|
||||
g_free (secret_key);
|
||||
g_free (new_random_key_raw);
|
||||
g_free (crypt);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seafile_encrypt (char **data_out,
|
||||
int *out_len,
|
||||
const char *data_in,
|
||||
const int in_len,
|
||||
SeafileCrypt *crypt)
|
||||
{
|
||||
*data_out = NULL;
|
||||
*out_len = -1;
|
||||
|
||||
/* check validation */
|
||||
if ( data_in == NULL || in_len <= 0 || crypt == NULL) {
|
||||
seaf_warning ("Invalid params.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
EVP_CIPHER_CTX ctx;
|
||||
int ret;
|
||||
int blks;
|
||||
|
||||
/* Prepare CTX for encryption. */
|
||||
EVP_CIPHER_CTX_init (&ctx);
|
||||
|
||||
if (crypt->version == 2)
|
||||
ret = EVP_EncryptInit_ex (&ctx,
|
||||
EVP_aes_256_cbc(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
crypt->key, /* derived key */
|
||||
crypt->iv); /* initial vector */
|
||||
else if (crypt->version == 1)
|
||||
ret = EVP_EncryptInit_ex (&ctx,
|
||||
EVP_aes_128_cbc(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
crypt->key, /* derived key */
|
||||
crypt->iv); /* initial vector */
|
||||
else
|
||||
ret = EVP_EncryptInit_ex (&ctx,
|
||||
EVP_aes_128_ecb(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
crypt->key, /* derived key */
|
||||
crypt->iv); /* initial vector */
|
||||
|
||||
if (ret == ENC_FAILURE)
|
||||
return -1;
|
||||
|
||||
/* Allocating output buffer. */
|
||||
|
||||
/*
|
||||
For EVP symmetric encryption, padding is always used __even if__
|
||||
data size is a multiple of block size, in which case the padding
|
||||
length is the block size. so we have the following:
|
||||
*/
|
||||
|
||||
blks = (in_len / BLK_SIZE) + 1;
|
||||
|
||||
*data_out = (char *)g_malloc (blks * BLK_SIZE);
|
||||
|
||||
if (*data_out == NULL) {
|
||||
seaf_warning ("failed to allocate the output buffer.\n");
|
||||
goto enc_error;
|
||||
}
|
||||
|
||||
int update_len, final_len;
|
||||
|
||||
/* Do the encryption. */
|
||||
ret = EVP_EncryptUpdate (&ctx,
|
||||
(unsigned char*)*data_out,
|
||||
&update_len,
|
||||
(unsigned char*)data_in,
|
||||
in_len);
|
||||
|
||||
if (ret == ENC_FAILURE)
|
||||
goto enc_error;
|
||||
|
||||
|
||||
/* Finish the possible partial block. */
|
||||
ret = EVP_EncryptFinal_ex (&ctx,
|
||||
(unsigned char*)*data_out + update_len,
|
||||
&final_len);
|
||||
|
||||
*out_len = update_len + final_len;
|
||||
|
||||
/* out_len should be equal to the allocated buffer size. */
|
||||
if (ret == ENC_FAILURE || *out_len != (blks * BLK_SIZE))
|
||||
goto enc_error;
|
||||
|
||||
EVP_CIPHER_CTX_cleanup (&ctx);
|
||||
|
||||
return 0;
|
||||
|
||||
enc_error:
|
||||
|
||||
EVP_CIPHER_CTX_cleanup (&ctx);
|
||||
|
||||
*out_len = -1;
|
||||
|
||||
if (*data_out != NULL)
|
||||
g_free (*data_out);
|
||||
|
||||
*data_out = NULL;
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int
|
||||
seafile_decrypt (char **data_out,
|
||||
int *out_len,
|
||||
const char *data_in,
|
||||
const int in_len,
|
||||
SeafileCrypt *crypt)
|
||||
{
|
||||
*data_out = NULL;
|
||||
*out_len = -1;
|
||||
|
||||
/* Check validation. Because padding is always used, in_len must
|
||||
* be a multiple of BLK_SIZE */
|
||||
if ( data_in == NULL || in_len <= 0 || in_len % BLK_SIZE != 0 ||
|
||||
crypt == NULL) {
|
||||
|
||||
seaf_warning ("Invalid param(s).\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
EVP_CIPHER_CTX ctx;
|
||||
int ret;
|
||||
|
||||
/* Prepare CTX for decryption. */
|
||||
EVP_CIPHER_CTX_init (&ctx);
|
||||
|
||||
if (crypt->version == 2)
|
||||
ret = EVP_DecryptInit_ex (&ctx,
|
||||
EVP_aes_256_cbc(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
crypt->key, /* derived key */
|
||||
crypt->iv); /* initial vector */
|
||||
else if (crypt->version == 1)
|
||||
ret = EVP_DecryptInit_ex (&ctx,
|
||||
EVP_aes_128_cbc(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
crypt->key, /* derived key */
|
||||
crypt->iv); /* initial vector */
|
||||
else
|
||||
ret = EVP_DecryptInit_ex (&ctx,
|
||||
EVP_aes_128_ecb(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
crypt->key, /* derived key */
|
||||
crypt->iv); /* initial vector */
|
||||
|
||||
if (ret == DEC_FAILURE)
|
||||
return -1;
|
||||
|
||||
/* Allocating output buffer. */
|
||||
|
||||
*data_out = (char *)g_malloc (in_len);
|
||||
|
||||
if (*data_out == NULL) {
|
||||
seaf_warning ("failed to allocate the output buffer.\n");
|
||||
goto dec_error;
|
||||
}
|
||||
|
||||
int update_len, final_len;
|
||||
|
||||
/* Do the decryption. */
|
||||
ret = EVP_DecryptUpdate (&ctx,
|
||||
(unsigned char*)*data_out,
|
||||
&update_len,
|
||||
(unsigned char*)data_in,
|
||||
in_len);
|
||||
|
||||
if (ret == DEC_FAILURE)
|
||||
goto dec_error;
|
||||
|
||||
|
||||
/* Finish the possible partial block. */
|
||||
ret = EVP_DecryptFinal_ex (&ctx,
|
||||
(unsigned char*)*data_out + update_len,
|
||||
&final_len);
|
||||
|
||||
*out_len = update_len + final_len;
|
||||
|
||||
/* out_len should be smaller than in_len. */
|
||||
if (ret == DEC_FAILURE || *out_len > in_len)
|
||||
goto dec_error;
|
||||
|
||||
EVP_CIPHER_CTX_cleanup (&ctx);
|
||||
|
||||
return 0;
|
||||
|
||||
dec_error:
|
||||
|
||||
EVP_CIPHER_CTX_cleanup (&ctx);
|
||||
|
||||
*out_len = -1;
|
||||
if (*data_out != NULL)
|
||||
g_free (*data_out);
|
||||
|
||||
*data_out = NULL;
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
seafile_decrypt_init (EVP_CIPHER_CTX *ctx,
|
||||
int version,
|
||||
const unsigned char *key,
|
||||
const unsigned char *iv)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* Prepare CTX for decryption. */
|
||||
EVP_CIPHER_CTX_init (ctx);
|
||||
|
||||
if (version == 2)
|
||||
ret = EVP_DecryptInit_ex (ctx,
|
||||
EVP_aes_256_cbc(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
key, /* derived key */
|
||||
iv); /* initial vector */
|
||||
else if (version == 1)
|
||||
ret = EVP_DecryptInit_ex (ctx,
|
||||
EVP_aes_128_cbc(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
key, /* derived key */
|
||||
iv); /* initial vector */
|
||||
else
|
||||
ret = EVP_DecryptInit_ex (ctx,
|
||||
EVP_aes_128_ecb(), /* cipher mode */
|
||||
NULL, /* engine, NULL for default */
|
||||
key, /* derived key */
|
||||
iv); /* initial vector */
|
||||
|
||||
if (ret == DEC_FAILURE)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
106
common/seafile-crypt.h
Normal file
106
common/seafile-crypt.h
Normal file
|
@ -0,0 +1,106 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
/*
|
||||
Description:
|
||||
|
||||
The function pair "seafile_encrypt/seafile_decrypt" are used to
|
||||
encrypt/decrypt data in the seafile system, using AES 128 bit ecb
|
||||
algorithm provided by openssl.
|
||||
*/
|
||||
|
||||
#ifndef _SEAFILE_CRYPT_H
|
||||
#define _SEAFILE_CRYPT_H
|
||||
|
||||
#include <openssl/aes.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
|
||||
/* Block size, in bytes. For AES it can only be 16 bytes. */
|
||||
#define BLK_SIZE 16
|
||||
#define ENCRYPT_BLK_SIZE BLK_SIZE
|
||||
|
||||
struct SeafileCrypt {
|
||||
int version;
|
||||
unsigned char key[32]; /* set when enc_version >= 1 */
|
||||
unsigned char iv[16];
|
||||
};
|
||||
|
||||
typedef struct SeafileCrypt SeafileCrypt;
|
||||
|
||||
SeafileCrypt *
|
||||
seafile_crypt_new (int version, unsigned char *key, unsigned char *iv);
|
||||
|
||||
/*
|
||||
Derive key and iv used by AES encryption from @data_in.
|
||||
key and iv is 16 bytes for version 1, and 32 bytes for version 2.
|
||||
|
||||
@data_out: pointer to the output of the encrpyted/decrypted data,
|
||||
whose content must be freed by g_free when not used.
|
||||
|
||||
@out_len: pointer to length of output, in bytes
|
||||
|
||||
@data_in: address of input buffer
|
||||
|
||||
@in_len: length of data to be encrpyted/decrypted, in bytes
|
||||
|
||||
@crypt: container of crypto info.
|
||||
|
||||
RETURN VALUES:
|
||||
|
||||
On success, 0 is returned, and the encrpyted/decrypted data is in
|
||||
*data_out, with out_len set to its length. On failure, -1 is returned
|
||||
and *data_out is set to NULL, with out_len set to -1;
|
||||
*/
|
||||
|
||||
int
|
||||
seafile_derive_key (const char *data_in, int in_len, int version,
|
||||
unsigned char *key, unsigned char *iv);
|
||||
|
||||
/*
|
||||
* Generate the real key used to encrypt data.
|
||||
* The key 32 bytes long and encrpted with @passwd.
|
||||
*/
|
||||
void
|
||||
seafile_generate_random_key (const char *passwd, char *random_key);
|
||||
|
||||
void
|
||||
seafile_generate_magic (int version, const char *repo_id,
|
||||
const char *passwd, char *magic);
|
||||
|
||||
int
|
||||
seafile_verify_repo_passwd (const char *repo_id,
|
||||
const char *passwd,
|
||||
const char *magic,
|
||||
int version);
|
||||
|
||||
int
|
||||
seafile_decrypt_repo_enc_key (int enc_version,
|
||||
const char *passwd, const char *random_key,
|
||||
unsigned char *key_out, unsigned char *iv_out);
|
||||
|
||||
int
|
||||
seafile_update_random_key (const char *old_passwd, const char *old_random_key,
|
||||
const char *new_passwd, char *new_random_key);
|
||||
|
||||
int
|
||||
seafile_encrypt (char **data_out,
|
||||
int *out_len,
|
||||
const char *data_in,
|
||||
const int in_len,
|
||||
SeafileCrypt *crypt);
|
||||
|
||||
|
||||
int
|
||||
seafile_decrypt (char **data_out,
|
||||
int *out_len,
|
||||
const char *data_in,
|
||||
const int in_len,
|
||||
SeafileCrypt *crypt);
|
||||
|
||||
int
|
||||
seafile_decrypt_init (EVP_CIPHER_CTX *ctx,
|
||||
int version,
|
||||
const unsigned char *key,
|
||||
const unsigned char *iv);
|
||||
|
||||
#endif /* _SEAFILE_CRYPT_H */
|
20
common/sync-repo-common.h
Normal file
20
common/sync-repo-common.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef SYNC_REPO_COMMON
|
||||
#define SYNC_REPO_COMMON
|
||||
|
||||
#define SC_COMMIT_ID "300"
|
||||
#define SS_COMMIT_ID "Commit ID"
|
||||
#define SC_NO_REPO "301"
|
||||
#define SS_NO_REPO "No such repo"
|
||||
#define SC_NO_BRANCH "302"
|
||||
#define SS_NO_BRANCH "No such branch"
|
||||
#define SC_NO_DSYNC "303"
|
||||
#define SS_NO_DSYNC "Not double sync"
|
||||
#define SC_REPO_CORRUPT "304"
|
||||
#define SS_REPO_CORRUPT "Repo corrupted"
|
||||
|
||||
#define SC_SERVER_ERROR "401"
|
||||
#define SS_SERVER_ERROR "Internal server error"
|
||||
|
||||
#endif
|
637
common/vc-common.c
Normal file
637
common/vc-common.c
Normal file
|
@ -0,0 +1,637 @@
|
|||
#include "common.h"
|
||||
|
||||
#include "seafile-session.h"
|
||||
#include "vc-common.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "seafile-error.h"
|
||||
|
||||
static GList *
|
||||
merge_bases_many (SeafCommit *one, int n, SeafCommit **twos);
|
||||
|
||||
static gint
|
||||
compare_commit_by_time (gconstpointer a, gconstpointer b, gpointer unused)
|
||||
{
|
||||
const SeafCommit *commit_a = a;
|
||||
const SeafCommit *commit_b = b;
|
||||
|
||||
/* Latest commit comes first in the list. */
|
||||
return (commit_b->ctime - commit_a->ctime);
|
||||
}
|
||||
|
||||
static gint
|
||||
compare_commit (gconstpointer a, gconstpointer b)
|
||||
{
|
||||
const SeafCommit *commit_a = a;
|
||||
const SeafCommit *commit_b = b;
|
||||
|
||||
return strcmp (commit_a->commit_id, commit_b->commit_id);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
add_to_commit_hash (SeafCommit *commit, void *vhash, gboolean *stop)
|
||||
{
|
||||
GHashTable *hash = vhash;
|
||||
|
||||
char *key = g_strdup (commit->commit_id);
|
||||
g_hash_table_replace (hash, key, key);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static GHashTable *
|
||||
commit_tree_to_hash (SeafCommit *head)
|
||||
{
|
||||
GHashTable *hash;
|
||||
gboolean res;
|
||||
|
||||
hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
|
||||
|
||||
res = seaf_commit_manager_traverse_commit_tree (seaf->commit_mgr,
|
||||
head->repo_id,
|
||||
head->version,
|
||||
head->commit_id,
|
||||
add_to_commit_hash,
|
||||
hash, FALSE);
|
||||
if (!res)
|
||||
goto fail;
|
||||
|
||||
return hash;
|
||||
|
||||
fail:
|
||||
g_hash_table_destroy (hash);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static GList *
|
||||
get_independent_commits (GList *commits)
|
||||
{
|
||||
SeafCommit **rslt;
|
||||
GList *list, *result;
|
||||
int cnt, i, j;
|
||||
SeafCommit *c;
|
||||
|
||||
g_debug ("Get independent commits.\n");
|
||||
|
||||
cnt = g_list_length (commits);
|
||||
|
||||
rslt = calloc(cnt, sizeof(*rslt));
|
||||
for (list = commits, i = 0; list; list = list->next)
|
||||
rslt[i++] = list->data;
|
||||
g_list_free (commits);
|
||||
|
||||
for (i = 0; i < cnt - 1; i++) {
|
||||
for (j = i+1; j < cnt; j++) {
|
||||
if (!rslt[i] || !rslt[j])
|
||||
continue;
|
||||
result = merge_bases_many(rslt[i], 1, &rslt[j]);
|
||||
for (list = result; list; list = list->next) {
|
||||
c = list->data;
|
||||
/* If two commits have fast-forward relationship,
|
||||
* drop the older one.
|
||||
*/
|
||||
if (strcmp (rslt[i]->commit_id, c->commit_id) == 0) {
|
||||
seaf_commit_unref (rslt[i]);
|
||||
rslt[i] = NULL;
|
||||
}
|
||||
if (strcmp (rslt[j]->commit_id, c->commit_id) == 0) {
|
||||
seaf_commit_unref (rslt[j]);
|
||||
rslt[j] = NULL;
|
||||
}
|
||||
seaf_commit_unref (c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Surviving ones in rslt[] are the independent results */
|
||||
result = NULL;
|
||||
for (i = 0; i < cnt; i++) {
|
||||
if (rslt[i])
|
||||
result = g_list_insert_sorted_with_data (result, rslt[i],
|
||||
compare_commit_by_time,
|
||||
NULL);
|
||||
}
|
||||
free(rslt);
|
||||
return result;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
GList *result;
|
||||
GHashTable *commit_hash;
|
||||
} MergeTraverseData;
|
||||
|
||||
static gboolean
|
||||
get_merge_bases (SeafCommit *commit, void *vdata, gboolean *stop)
|
||||
{
|
||||
MergeTraverseData *data = vdata;
|
||||
|
||||
/* Found a common ancestor.
|
||||
* Dont traverse its parenets.
|
||||
*/
|
||||
if (g_hash_table_lookup (data->commit_hash, commit->commit_id)) {
|
||||
if (!g_list_find_custom (data->result, commit, compare_commit)) {
|
||||
data->result = g_list_insert_sorted_with_data (data->result, commit,
|
||||
compare_commit_by_time,
|
||||
NULL);
|
||||
seaf_commit_ref (commit);
|
||||
}
|
||||
*stop = TRUE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Merge "one" with commits in "twos".
|
||||
* The ancestors returned may not be ancestors for all the input commits.
|
||||
* They are common ancestors for one and some commits in twos array.
|
||||
*/
|
||||
static GList *
|
||||
merge_bases_many (SeafCommit *one, int n, SeafCommit **twos)
|
||||
{
|
||||
GHashTable *commit_hash;
|
||||
GList *result = NULL;
|
||||
SeafCommit *commit;
|
||||
int i;
|
||||
MergeTraverseData data;
|
||||
gboolean res;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
if (one == twos[i])
|
||||
return g_list_append (result, one);
|
||||
}
|
||||
|
||||
/* First construct a hash table of all commit ids rooted at one. */
|
||||
commit_hash = commit_tree_to_hash (one);
|
||||
if (!commit_hash) {
|
||||
g_warning ("Failed to load commit hash.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data.commit_hash = commit_hash;
|
||||
data.result = NULL;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
res = seaf_commit_manager_traverse_commit_tree (seaf->commit_mgr,
|
||||
twos[i]->repo_id,
|
||||
twos[i]->version,
|
||||
twos[i]->commit_id,
|
||||
get_merge_bases,
|
||||
&data, FALSE);
|
||||
if (!res)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
g_hash_table_destroy (commit_hash);
|
||||
result = data.result;
|
||||
|
||||
if (!result || !result->next)
|
||||
return result;
|
||||
|
||||
/* There are more than one. Try to find out independent ones. */
|
||||
result = get_independent_commits (result);
|
||||
|
||||
return result;
|
||||
|
||||
fail:
|
||||
result = data.result;
|
||||
while (result) {
|
||||
commit = result->data;
|
||||
seaf_commit_unref (commit);
|
||||
result = g_list_delete_link (result, result);
|
||||
}
|
||||
g_hash_table_destroy (commit_hash);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns common ancesstor for two branches.
|
||||
* Any two commits should have a common ancestor.
|
||||
* So returning NULL indicates an error, for e.g. corupt commit.
|
||||
*/
|
||||
SeafCommit *
|
||||
get_merge_base (SeafCommit *head, SeafCommit *remote)
|
||||
{
|
||||
GList *result, *iter;
|
||||
SeafCommit *one, **twos;
|
||||
int n, i;
|
||||
SeafCommit *ret = NULL;
|
||||
|
||||
one = head;
|
||||
twos = (SeafCommit **) calloc (1, sizeof(SeafCommit *));
|
||||
twos[0] = remote;
|
||||
n = 1;
|
||||
result = merge_bases_many (one, n, twos);
|
||||
free (twos);
|
||||
if (!result || !result->next)
|
||||
goto done;
|
||||
|
||||
/*
|
||||
* More than one common ancestors.
|
||||
* Loop until the oldest common ancestor is found.
|
||||
*/
|
||||
while (1) {
|
||||
n = g_list_length (result) - 1;
|
||||
one = result->data;
|
||||
twos = calloc (n, sizeof(SeafCommit *));
|
||||
for (iter = result->next, i = 0; i < n; iter = iter->next, i++) {
|
||||
twos[i] = iter->data;
|
||||
}
|
||||
g_list_free (result);
|
||||
|
||||
result = merge_bases_many (one, n, twos);
|
||||
free (twos);
|
||||
if (!result || !result->next)
|
||||
break;
|
||||
}
|
||||
|
||||
done:
|
||||
if (result)
|
||||
ret = result->data;
|
||||
g_list_free (result);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns true if src_head is ahead of dst_head.
|
||||
*/
|
||||
gboolean
|
||||
is_fast_forward (const char *repo_id, int version,
|
||||
const char *src_head, const char *dst_head)
|
||||
{
|
||||
VCCompareResult res;
|
||||
|
||||
res = vc_compare_commits (repo_id, version, src_head, dst_head);
|
||||
|
||||
return (res == VC_FAST_FORWARD);
|
||||
}
|
||||
|
||||
VCCompareResult
|
||||
vc_compare_commits (const char *repo_id, int version,
|
||||
const char *c1, const char *c2)
|
||||
{
|
||||
SeafCommit *commit1, *commit2, *ca;
|
||||
VCCompareResult ret;
|
||||
|
||||
/* Treat the same as up-to-date. */
|
||||
if (strcmp (c1, c2) == 0)
|
||||
return VC_UP_TO_DATE;
|
||||
|
||||
commit1 = seaf_commit_manager_get_commit (seaf->commit_mgr, repo_id, version, c1);
|
||||
if (!commit1)
|
||||
return VC_INDEPENDENT;
|
||||
|
||||
commit2 = seaf_commit_manager_get_commit (seaf->commit_mgr, repo_id, version, c2);
|
||||
if (!commit2) {
|
||||
seaf_commit_unref (commit1);
|
||||
return VC_INDEPENDENT;
|
||||
}
|
||||
|
||||
ca = get_merge_base (commit1, commit2);
|
||||
|
||||
if (!ca)
|
||||
ret = VC_INDEPENDENT;
|
||||
else if (strcmp(ca->commit_id, commit1->commit_id) == 0)
|
||||
ret = VC_UP_TO_DATE;
|
||||
else if (strcmp(ca->commit_id, commit2->commit_id) == 0)
|
||||
ret = VC_FAST_FORWARD;
|
||||
else
|
||||
ret = VC_INDEPENDENT;
|
||||
|
||||
if (ca) seaf_commit_unref (ca);
|
||||
seaf_commit_unref (commit1);
|
||||
seaf_commit_unref (commit2);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Diff a specific file with parent(s).
|
||||
* If @commit is a merge, both parents will be compared.
|
||||
* @commit must have this file and it's id is given in @file_id.
|
||||
*
|
||||
* Returns 0 if there is no difference; 1 otherwise.
|
||||
* If returns 0, @parent will point to the next commit to traverse.
|
||||
* If I/O error occurs, @error will be set.
|
||||
*/
|
||||
static int
|
||||
diff_parents_with_path (SeafCommit *commit,
|
||||
const char *repo_id,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *path,
|
||||
const char *file_id,
|
||||
char *parent,
|
||||
GError **error)
|
||||
{
|
||||
SeafCommit *p1 = NULL, *p2 = NULL;
|
||||
char *file_id_p1 = NULL, *file_id_p2 = NULL;
|
||||
int ret = 0;
|
||||
|
||||
p1 = seaf_commit_manager_get_commit (seaf->commit_mgr,
|
||||
commit->repo_id,
|
||||
commit->version,
|
||||
commit->parent_id);
|
||||
if (!p1) {
|
||||
g_warning ("Failed to find commit %s.\n", commit->parent_id);
|
||||
g_set_error (error, SEAFILE_DOMAIN, SEAF_ERR_GENERAL, " ");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp (p1->root_id, EMPTY_SHA1) == 0) {
|
||||
seaf_commit_unref (p1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (commit->second_parent_id) {
|
||||
p2 = seaf_commit_manager_get_commit (seaf->commit_mgr,
|
||||
commit->repo_id,
|
||||
commit->version,
|
||||
commit->second_parent_id);
|
||||
if (!p2) {
|
||||
g_warning ("Failed to find commit %s.\n", commit->second_parent_id);
|
||||
seaf_commit_unref (p1);
|
||||
g_set_error (error, SEAFILE_DOMAIN, SEAF_ERR_GENERAL, " ");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!p2) {
|
||||
file_id_p1 = seaf_fs_manager_path_to_obj_id (seaf->fs_mgr,
|
||||
store_id,
|
||||
version,
|
||||
p1->root_id, path,
|
||||
NULL,
|
||||
error);
|
||||
if (*error)
|
||||
goto out;
|
||||
if (!file_id_p1 || strcmp (file_id, file_id_p1) != 0)
|
||||
ret = 1;
|
||||
else
|
||||
memcpy (parent, p1->commit_id, 41);
|
||||
} else {
|
||||
file_id_p1 = seaf_fs_manager_path_to_obj_id (seaf->fs_mgr,
|
||||
store_id,
|
||||
version,
|
||||
p1->root_id, path,
|
||||
NULL, error);
|
||||
if (*error)
|
||||
goto out;
|
||||
file_id_p2 = seaf_fs_manager_path_to_obj_id (seaf->fs_mgr,
|
||||
store_id,
|
||||
version,
|
||||
p2->root_id, path,
|
||||
NULL, error);
|
||||
if (*error)
|
||||
goto out;
|
||||
|
||||
if (file_id_p1 && file_id_p2) {
|
||||
if (strcmp(file_id, file_id_p1) != 0 &&
|
||||
strcmp(file_id, file_id_p2) != 0)
|
||||
ret = 1;
|
||||
else if (strcmp(file_id, file_id_p1) == 0)
|
||||
memcpy (parent, p1->commit_id, 41);
|
||||
else
|
||||
memcpy (parent, p2->commit_id, 41);
|
||||
} else if (file_id_p1 && !file_id_p2) {
|
||||
if (strcmp(file_id, file_id_p1) != 0)
|
||||
ret = 1;
|
||||
else
|
||||
memcpy (parent, p1->commit_id, 41);
|
||||
} else if (!file_id_p1 && file_id_p2) {
|
||||
if (strcmp(file_id, file_id_p2) != 0)
|
||||
ret = 1;
|
||||
else
|
||||
memcpy (parent, p2->commit_id, 41);
|
||||
} else {
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
g_free (file_id_p1);
|
||||
g_free (file_id_p2);
|
||||
|
||||
if (p1)
|
||||
seaf_commit_unref (p1);
|
||||
if (p2)
|
||||
seaf_commit_unref (p2);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
get_file_modifier_mtime_v0 (const char *repo_id, const char *store_id, int version,
|
||||
const char *head, const char *path,
|
||||
char **modifier, gint64 *mtime)
|
||||
{
|
||||
char commit_id[41];
|
||||
SeafCommit *commit = NULL;
|
||||
char *file_id = NULL;
|
||||
int changed;
|
||||
int ret = 0;
|
||||
GError *error = NULL;
|
||||
|
||||
*modifier = NULL;
|
||||
*mtime = 0;
|
||||
|
||||
memcpy (commit_id, head, 41);
|
||||
|
||||
while (1) {
|
||||
commit = seaf_commit_manager_get_commit (seaf->commit_mgr,
|
||||
repo_id, version,
|
||||
commit_id);
|
||||
if (!commit) {
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* We hit the initial commit. */
|
||||
if (!commit->parent_id)
|
||||
break;
|
||||
|
||||
file_id = seaf_fs_manager_path_to_obj_id (seaf->fs_mgr,
|
||||
store_id, version,
|
||||
commit->root_id,
|
||||
path,
|
||||
NULL,
|
||||
&error);
|
||||
if (error) {
|
||||
g_clear_error (&error);
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
/* We expect commit to have this file. */
|
||||
if (!file_id) {
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
changed = diff_parents_with_path (commit,
|
||||
repo_id, store_id, version,
|
||||
path, file_id,
|
||||
commit_id, &error);
|
||||
if (error) {
|
||||
g_clear_error (&error);
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
*modifier = g_strdup (commit->creator_name);
|
||||
*mtime = commit->ctime;
|
||||
break;
|
||||
} else {
|
||||
/* If this commit doesn't change the file, commit_id will be set
|
||||
* to the parent commit to traverse.
|
||||
*/
|
||||
g_free (file_id);
|
||||
seaf_commit_unref (commit);
|
||||
}
|
||||
}
|
||||
|
||||
g_free (file_id);
|
||||
if (commit)
|
||||
seaf_commit_unref (commit);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
get_file_modifier_mtime_v1 (const char *repo_id, const char *store_id, int version,
|
||||
const char *head, const char *path,
|
||||
char **modifier, gint64 *mtime)
|
||||
{
|
||||
SeafCommit *commit = NULL;
|
||||
SeafDir *dir = NULL;
|
||||
SeafDirent *dent = NULL;
|
||||
int ret = 0;
|
||||
|
||||
commit = seaf_commit_manager_get_commit (seaf->commit_mgr,
|
||||
repo_id, version,
|
||||
head);
|
||||
if (!commit) {
|
||||
seaf_warning ("Failed to get commit %s.\n", head);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *parent = g_path_get_dirname (path);
|
||||
if (strcmp(parent, ".") == 0) {
|
||||
g_free (parent);
|
||||
parent = g_strdup("");
|
||||
}
|
||||
char *filename = g_path_get_basename (path);
|
||||
|
||||
dir = seaf_fs_manager_get_seafdir_by_path (seaf->fs_mgr,
|
||||
store_id, version,
|
||||
commit->root_id,
|
||||
parent, NULL);
|
||||
if (!dir) {
|
||||
seaf_warning ("dir %s doesn't exist in repo %s.\n", parent, repo_id);
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
GList *p;
|
||||
for (p = dir->entries; p; p = p->next) {
|
||||
SeafDirent *d = p->data;
|
||||
if (strcmp (d->name, filename) == 0) {
|
||||
dent = d;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!dent) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
*modifier = g_strdup(dent->modifier);
|
||||
*mtime = dent->mtime;
|
||||
|
||||
out:
|
||||
g_free (parent);
|
||||
g_free (filename);
|
||||
seaf_commit_unref (commit);
|
||||
seaf_dir_free (dir);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user who last changed a file and the mtime.
|
||||
* @head: head commit to start the search.
|
||||
* @path: path of the file.
|
||||
*/
|
||||
int
|
||||
get_file_modifier_mtime (const char *repo_id,
|
||||
const char *store_id,
|
||||
int version,
|
||||
const char *head,
|
||||
const char *path,
|
||||
char **modifier,
|
||||
gint64 *mtime)
|
||||
{
|
||||
if (version > 0)
|
||||
return get_file_modifier_mtime_v1 (repo_id, store_id, version,
|
||||
head, path,
|
||||
modifier, mtime);
|
||||
else
|
||||
return get_file_modifier_mtime_v0 (repo_id, store_id, version,
|
||||
head, path,
|
||||
modifier, mtime);
|
||||
}
|
||||
|
||||
char *
|
||||
gen_conflict_path (const char *origin_path,
|
||||
const char *modifier,
|
||||
gint64 mtime)
|
||||
{
|
||||
char time_buf[64];
|
||||
time_t t = (time_t)mtime;
|
||||
char *copy = g_strdup (origin_path);
|
||||
GString *conflict_path = g_string_new (NULL);
|
||||
char *dot, *ext;
|
||||
|
||||
strftime(time_buf, 64, "%Y-%m-%d-%H-%M-%S", localtime(&t));
|
||||
|
||||
dot = strrchr (copy, '.');
|
||||
|
||||
if (dot != NULL) {
|
||||
*dot = '\0';
|
||||
ext = dot + 1;
|
||||
if (modifier)
|
||||
g_string_printf (conflict_path, "%s (SFConflict %s %s).%s",
|
||||
copy, modifier, time_buf, ext);
|
||||
else
|
||||
g_string_printf (conflict_path, "%s (SFConflict %s).%s",
|
||||
copy, time_buf, ext);
|
||||
} else {
|
||||
if (modifier)
|
||||
g_string_printf (conflict_path, "%s (SFConflict %s %s)",
|
||||
copy, modifier, time_buf);
|
||||
else
|
||||
g_string_printf (conflict_path, "%s (SFConflict %s)",
|
||||
copy, time_buf);
|
||||
}
|
||||
|
||||
g_free (copy);
|
||||
return g_string_free (conflict_path, FALSE);
|
||||
}
|
||||
|
||||
char *
|
||||
gen_conflict_path_wrapper (const char *repo_id, int version,
|
||||
const char *head, const char *in_repo_path,
|
||||
const char *original_path)
|
||||
{
|
||||
char *modifier;
|
||||
gint64 mtime;
|
||||
|
||||
/* XXX: this function is only used in client, so store_id is always
|
||||
* the same as repo_id. This can be changed if it's also called in
|
||||
* server.
|
||||
*/
|
||||
if (get_file_modifier_mtime (repo_id, repo_id, version, head, in_repo_path,
|
||||
&modifier, &mtime) < 0)
|
||||
return NULL;
|
||||
|
||||
return gen_conflict_path (original_path, modifier, mtime);
|
||||
}
|
51
common/vc-common.h
Normal file
51
common/vc-common.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
#ifndef VC_COMMON_H
|
||||
#define VC_COMMON_H
|
||||
|
||||
#include "commit-mgr.h"
|
||||
|
||||
SeafCommit *
|
||||
get_merge_base (SeafCommit *head, SeafCommit *remote);
|
||||
|
||||
/*
|
||||
* Returns true if src_head is ahead of dst_head.
|
||||
*/
|
||||
gboolean
|
||||
is_fast_forward (const char *repo_id, int version,
|
||||
const char *src_head, const char *dst_head);
|
||||
|
||||
typedef enum {
|
||||
VC_UP_TO_DATE,
|
||||
VC_FAST_FORWARD,
|
||||
VC_INDEPENDENT,
|
||||
} VCCompareResult;
|
||||
|
||||
/*
|
||||
* Compares commits c1 and c2 as if we were going to merge c1 into c2.
|
||||
*
|
||||
* Returns:
|
||||
* VC_UP_TO_DATE: if c2 is ahead of c1, or c1 == c2;
|
||||
* VC_FAST_FORWARD: if c1 is ahead of c2;
|
||||
* VC_INDEPENDENT: if c1 and c2 has no inheritent relationship.
|
||||
* Returns VC_INDEPENDENT if c1 or c2 doesn't exist.
|
||||
*/
|
||||
VCCompareResult
|
||||
vc_compare_commits (const char *repo_id, int version,
|
||||
const char *c1, const char *c2);
|
||||
|
||||
char *
|
||||
gen_conflict_path (const char *original_path,
|
||||
const char *modifier,
|
||||
gint64 mtime);
|
||||
|
||||
int
|
||||
get_file_modifier_mtime (const char *repo_id, const char *store_id, int version,
|
||||
const char *head, const char *path,
|
||||
char **modifier, gint64 *mtime);
|
||||
|
||||
/* Wrapper around the above two functions */
|
||||
char *
|
||||
gen_conflict_path_wrapper (const char *repo_id, int version,
|
||||
const char *head, const char *in_repo_path,
|
||||
const char *original_path);
|
||||
|
||||
#endif
|
360
configure.ac
Normal file
360
configure.ac
Normal file
|
@ -0,0 +1,360 @@
|
|||
dnl Process this file with autoconf to produce a configure script.
|
||||
|
||||
|
||||
AC_PREREQ(2.61)
|
||||
AC_INIT([seafile], [5.1.4], [freeplant@gmail.com])
|
||||
AC_CONFIG_HEADER([config.h])
|
||||
|
||||
AC_CONFIG_MACRO_DIR([m4])
|
||||
|
||||
AM_INIT_AUTOMAKE([1.9 foreign])
|
||||
|
||||
#AC_MINGW32
|
||||
AC_CANONICAL_BUILD
|
||||
|
||||
dnl enable the build of share library by default
|
||||
AC_ENABLE_SHARED
|
||||
|
||||
AC_SUBST(LIBTOOL_DEPS)
|
||||
|
||||
# Checks for programs.
|
||||
AC_PROG_CC
|
||||
#AM_C_PROTOTYPES
|
||||
AC_C_CONST
|
||||
AC_PROG_MAKE_SET
|
||||
# AC_PROG_RANLIB
|
||||
LT_INIT
|
||||
|
||||
# Checks for headers.
|
||||
#AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h libintl.h limits.h locale.h netdb.h netinet/in.h stdint.h stdlib.h string.h strings.h sys/ioctl.h sys/socket.h sys/time.h termios.h unistd.h utime.h utmp.h])
|
||||
|
||||
# Checks for typedefs, structures, and compiler characteristics.
|
||||
AC_SYS_LARGEFILE
|
||||
|
||||
# Checks for library functions.
|
||||
#AC_CHECK_FUNCS([alarm dup2 ftruncate getcwd gethostbyname gettimeofday memmove memset mkdir rmdir select setlocale socket strcasecmp strchr strdup strrchr strstr strtol uname utime strtok_r sendfile])
|
||||
|
||||
# check platform
|
||||
AC_MSG_CHECKING(for WIN32)
|
||||
if test "$build_os" = "mingw32" -o "$build_os" = "mingw64"; then
|
||||
bwin32=true
|
||||
AC_MSG_RESULT(compile in mingw)
|
||||
else
|
||||
AC_MSG_RESULT(no)
|
||||
fi
|
||||
|
||||
AC_MSG_CHECKING(for Mac)
|
||||
if test "$(uname)" = "Darwin"; then
|
||||
bmac=true
|
||||
AC_MSG_RESULT(compile in mac)
|
||||
else
|
||||
AC_MSG_RESULT(no)
|
||||
fi
|
||||
|
||||
AC_MSG_CHECKING(for Linux)
|
||||
if test "$bmac" != "true" -a "$bwin32" != "true"; then
|
||||
blinux=true
|
||||
AC_MSG_RESULT(compile in linux)
|
||||
else
|
||||
AC_MSG_RESULT(no)
|
||||
fi
|
||||
|
||||
# test which sub-component to compile
|
||||
|
||||
if test "$bwin32" = true; then
|
||||
compile_tools=no
|
||||
fi
|
||||
|
||||
if test "$bmac" = true; then
|
||||
compile_tools=no
|
||||
fi
|
||||
|
||||
if test "$blinux" = true; then
|
||||
compile_tools=yes
|
||||
fi
|
||||
|
||||
if test "$bwin32" != true; then
|
||||
AC_ARG_ENABLE(fuse, AC_HELP_STRING([--enable-fuse], [enable fuse virtual file system]),
|
||||
[compile_fuse=$enableval],[compile_fuse="yes"])
|
||||
fi
|
||||
|
||||
AC_ARG_ENABLE(python,
|
||||
AC_HELP_STRING([--enable-python],[build ccnet python binding]),
|
||||
[compile_python=$enableval],
|
||||
[compile_python=yes])
|
||||
|
||||
AM_CONDITIONAL([COMPILE_TOOLS], [test "${compile_tools}" = "yes"])
|
||||
AM_CONDITIONAL([COMPILE_PYTHON], [test "${compile_python}" = "yes"])
|
||||
AM_CONDITIONAL([COMPILE_FUSE], [test "${compile_fuse}" = "yes"])
|
||||
|
||||
AM_CONDITIONAL([WIN32], [test "$bwin32" = "true"])
|
||||
AM_CONDITIONAL([MACOS], [test "$bmac" = "true"])
|
||||
AM_CONDITIONAL([LINUX], [test "$blinux" = "true"])
|
||||
|
||||
|
||||
# check libraries
|
||||
if test "$bwin32" != true; then
|
||||
if test "$bmac" = true; then
|
||||
AC_CHECK_LIB(c, uuid_generate, [echo "found library uuid"],
|
||||
AC_MSG_ERROR([*** Unable to find uuid_generate in libc]), )
|
||||
else
|
||||
AC_CHECK_LIB(uuid, uuid_generate, [echo "found library uuid"],
|
||||
AC_MSG_ERROR([*** Unable to find uuid library]), )
|
||||
fi
|
||||
fi
|
||||
|
||||
AC_CHECK_LIB(pthread, pthread_create, [echo "found library pthread"], AC_MSG_ERROR([*** Unable to find pthread library]), )
|
||||
AC_CHECK_LIB(sqlite3, sqlite3_open,[echo "found library sqlite3"] , AC_MSG_ERROR([*** Unable to find sqlite3 library]), )
|
||||
AC_CHECK_LIB(crypto, SHA1_Init, [echo "found library crypto"], AC_MSG_ERROR([*** Unable to find openssl crypto library]), )
|
||||
|
||||
dnl Do we need to use AX_LIB_SQLITE3 to check sqlite?
|
||||
dnl AX_LIB_SQLITE3
|
||||
|
||||
CONSOLE=
|
||||
if test "$bwin32" = "true"; then
|
||||
AC_ARG_ENABLE(console, AC_HELP_STRING([--enable-console], [enable console]),
|
||||
[console=$enableval],[console="yes"])
|
||||
if test x${console} != xyes ; then
|
||||
CONSOLE="-Wl,--subsystem,windows -Wl,--entry,_mainCRTStartup"
|
||||
fi
|
||||
fi
|
||||
AC_SUBST(CONSOLE)
|
||||
|
||||
if test "$bwin32" = true; then
|
||||
LIB_WS32=-lws2_32
|
||||
LIB_GDI32=-lgdi32
|
||||
LIB_RT=
|
||||
LIB_INTL=-lintl
|
||||
LIBS=
|
||||
LIB_RESOLV=
|
||||
LIB_UUID=-lRpcrt4
|
||||
LIB_IPHLPAPI=-liphlpapi
|
||||
LIB_SHELL32=-lshell32
|
||||
LIB_PSAPI=-lpsapi
|
||||
LIB_MAC=
|
||||
MSVC_CFLAGS="-D__MSVCRT__ -D__MSVCRT_VERSION__=0x0601"
|
||||
LIB_CRYPT32=-lcrypt32
|
||||
elif test "$bmac" = true ; then
|
||||
LIB_WS32=
|
||||
LIB_GDI32=
|
||||
LIB_RT=
|
||||
LIB_INTL=
|
||||
LIB_RESOLV=-lresolv
|
||||
LIB_UUID=
|
||||
LIB_IPHLPAPI=
|
||||
LIB_SHELL32=
|
||||
LIB_PSAPI=
|
||||
MSVC_CFLAGS=
|
||||
LIB_MAC="-framework CoreServices"
|
||||
LIB_CRYPT32=
|
||||
LIB_ICONV=-liconv
|
||||
else
|
||||
LIB_WS32=
|
||||
LIB_GDI32=
|
||||
LIB_RT=
|
||||
LIB_INTL=
|
||||
LIB_RESOLV=-lresolv
|
||||
LIB_UUID=-luuid
|
||||
LIB_IPHLPAPI=
|
||||
LIB_SHELL32=
|
||||
LIB_PSAPI=
|
||||
LIB_MAC=
|
||||
MSVC_CFLAGS=
|
||||
LIB_CRYPT32=
|
||||
fi
|
||||
|
||||
AC_SUBST(LIB_WS32)
|
||||
AC_SUBST(LIB_GDI32)
|
||||
AC_SUBST(LIB_RT)
|
||||
AC_SUBST(LIB_INTL)
|
||||
AC_SUBST(LIB_RESOLV)
|
||||
AC_SUBST(LIB_UUID)
|
||||
AC_SUBST(LIB_IPHLPAPI)
|
||||
AC_SUBST(LIB_SHELL32)
|
||||
AC_SUBST(LIB_PSAPI)
|
||||
AC_SUBST(LIB_MAC)
|
||||
AC_SUBST(MSVC_CFLAGS)
|
||||
AC_SUBST(LIB_CRYPT32)
|
||||
AC_SUBST(LIB_ICONV)
|
||||
|
||||
|
||||
LIBEVENT_REQUIRED=2.0
|
||||
GLIB_REQUIRED=2.16.0
|
||||
CCNET_REQUIRED=0.9.3
|
||||
SEARPC_REQUIRED=1.0
|
||||
JANSSON_REQUIRED=2.2.1
|
||||
ZDB_REQUIRED=2.10
|
||||
#LIBNAUTILUS_EXTENSION_REQUIRED=2.30.1
|
||||
CURL_REQUIRED=7.17
|
||||
FUSE_REQUIRED=2.7.3
|
||||
ZLIB_REQUIRED=1.2.0
|
||||
|
||||
PKG_CHECK_MODULES(SSL, [openssl])
|
||||
AC_SUBST(SSL_CFLAGS)
|
||||
AC_SUBST(SSL_LIBS)
|
||||
|
||||
PKG_CHECK_MODULES(GLIB2, [glib-2.0 >= $GLIB_REQUIRED])
|
||||
AC_SUBST(GLIB2_CFLAGS)
|
||||
AC_SUBST(GLIB2_LIBS)
|
||||
|
||||
PKG_CHECK_MODULES(GOBJECT, [gobject-2.0 >= $GLIB_REQUIRED])
|
||||
AC_SUBST(GOBJECT_CFLAGS)
|
||||
AC_SUBST(GOBJECT_LIBS)
|
||||
|
||||
PKG_CHECK_MODULES(CCNET, [libccnet >= $CCNET_REQUIRED])
|
||||
AC_SUBST(CCNET_CFLAGS)
|
||||
AC_SUBST(CCNET_LIBS)
|
||||
|
||||
PKG_CHECK_MODULES(SEARPC, [libsearpc >= $SEARPC_REQUIRED])
|
||||
AC_SUBST(SEARPC_CFLAGS)
|
||||
AC_SUBST(SEARPC_LIBS)
|
||||
|
||||
PKG_CHECK_MODULES(JANSSON, [jansson >= $JANSSON_REQUIRED])
|
||||
AC_SUBST(JANSSON_CFLAGS)
|
||||
AC_SUBST(JANSSON_LIBS)
|
||||
|
||||
PKG_CHECK_MODULES(LIBEVENT, [libevent >= $LIBEVENT_REQUIRED])
|
||||
AC_SUBST(LIBEVENT_CFLAGS)
|
||||
AC_SUBST(LIBEVENT_LIBS)
|
||||
|
||||
PKG_CHECK_MODULES(ZLIB, [zlib >= $ZLIB_REQUIRED])
|
||||
AC_SUBST(ZLIB_CFLAGS)
|
||||
AC_SUBST(ZLIB_LIBS)
|
||||
|
||||
if test x${compile_python} = xyes; then
|
||||
AM_PATH_PYTHON([2.6])
|
||||
|
||||
if test "$bwin32" = true; then
|
||||
# set pyexecdir to somewhere like /c/Python26/Lib/site-packages
|
||||
pyexecdir=${PYTHON_DIR}/Lib/site-packages
|
||||
pythondir=${pyexecdir}
|
||||
pkgpyexecdir=${pyexecdir}/${PACKAGE}
|
||||
pkgpythondir=${pythondir}/${PACKAGE}
|
||||
|
||||
fi # end for bwin32
|
||||
|
||||
fi
|
||||
|
||||
mysql="yes"
|
||||
check_mysql_config()
|
||||
{
|
||||
AC_PATH_PROG([MYSQLCONFIG], [mysql_config], [no], [$PATH:/usr/local/bin:/usr/local/mysql/bin])
|
||||
if test "x$MYSQLCONFIG" = "xno"
|
||||
then
|
||||
AC_MSG_WARN([mysql_config is required to build seafile server with mysql])
|
||||
mysql="no"
|
||||
fi
|
||||
}
|
||||
AC_MSG_CHECKING(for mysql)
|
||||
AC_ARG_WITH([mysql],
|
||||
AS_HELP_STRING([--with-mysql(=<path>)],
|
||||
[Path is optional and if given should specify the full path to the MySQL
|
||||
configure script, mysql_config. E.g. --with-mysql=/<path>/mysql_config]),
|
||||
[
|
||||
if test "xno" = "x$with_mysql"; then
|
||||
AC_MSG_RESULT([no])
|
||||
mysql="no"
|
||||
else
|
||||
AC_MSG_RESULT([yes])
|
||||
AC_CHECK_FILE([$with_mysql], [MYSQLCONFIG=$with_mysql], [check_mysql_config])
|
||||
fi
|
||||
],
|
||||
[
|
||||
AC_MSG_RESULT([yes])
|
||||
check_mysql_config
|
||||
])
|
||||
if test "xyes" = "x$mysql"; then
|
||||
tmp_CPPFLAGS=$CPPFLAGS
|
||||
tmp_LDFLAGS=$LDFLAGS
|
||||
CPPFLAGS="`$MYSQLCONFIG --include` $CPPFLAGS"
|
||||
LDFLAGS="`$MYSQLCONFIG --libs` $LDFLAGS"
|
||||
AC_CHECK_HEADERS([mysql.h])
|
||||
if test "xyes" = "x$mysql"; then
|
||||
echo "found mysql client library"
|
||||
MYSQL_CFLAGS=`$MYSQLCONFIG --include`
|
||||
MYSQL_LIBS=`$MYSQLCONFIG --libs`
|
||||
AC_SUBST(MYSQL_CFLAGS)
|
||||
AC_SUBST(MYSQL_LIBS)
|
||||
fi
|
||||
CPPFLAGS=$tmp_CPPFLAGS
|
||||
LDFLAGS=$tmp_LDFLAGS
|
||||
fi
|
||||
|
||||
postgresql="yes"
|
||||
check_postgres_config()
|
||||
{
|
||||
AC_PATH_PROG([PGCONFIG], [pg_config], [no], [$PATH:/usr/local/bin:/usr/local/pgsql/bin])
|
||||
if test "x$PGCONFIG" = "xno"
|
||||
then
|
||||
AC_MSG_WARN([pg_config is required to build seafile server with postgresql])
|
||||
postgresql="no"
|
||||
fi
|
||||
}
|
||||
AC_MSG_CHECKING(for postgresql)
|
||||
AC_ARG_WITH([postgresql],
|
||||
AS_HELP_STRING([--with-postgresql(=<path>)],
|
||||
[Path is optional and if given should specify the full path to the PostgreSQL
|
||||
configure script, pg_config. E.g. --with-postgresql=/<path>/pg_config]),
|
||||
[
|
||||
if test "xno" = "x$with_postgresql"; then
|
||||
AC_MSG_RESULT([no])
|
||||
postgresql="no"
|
||||
else
|
||||
AC_MSG_RESULT([yes])
|
||||
AC_CHECK_FILE([$with_postgresql], [PGCONFIG=$with_postgresql],[check_postgres_config])
|
||||
fi
|
||||
],
|
||||
[
|
||||
AC_MSG_RESULT([yes])
|
||||
check_postgres_config
|
||||
])
|
||||
if test "xyes" = "x$postgresql"; then
|
||||
tmp_CPPFLAGS=$CPPFLAGS
|
||||
tmp_LDFLAGS=$LDFLAGS
|
||||
CPPFLAGS="-I`$PGCONFIG --includedir` $CPPFLAGS"
|
||||
LDFLAGS="-L`$PGCONFIG --libdir` $LDFLAGS"
|
||||
AC_CHECK_HEADERS([libpq-fe.h], [], [postgresql="no"])
|
||||
if test "xyes" = "x$postgresql"; then
|
||||
echo "found postgresql client library"
|
||||
PGSQL_CFLAGS="-I`$PGCONFIG --includedir`"
|
||||
PGSQL_LIBS="-L`$PGCONFIG --libdir` -lpq"
|
||||
AC_SUBST(PGSQL_CFLAGS)
|
||||
AC_SUBST(PGSQL_LIBS)
|
||||
fi
|
||||
CPPFLAGS=$tmp_CPPFLAGS
|
||||
LDFLAGS=$tmp_LDFLAGS
|
||||
fi
|
||||
|
||||
if test "${compile_fuse}" = "yes"; then
|
||||
PKG_CHECK_MODULES(FUSE, [fuse >= $FUSE_REQUIRED])
|
||||
AC_SUBST(FUSE_CFLAGS)
|
||||
AC_SUBST(FUSE_LIBS)
|
||||
fi
|
||||
|
||||
dnl check libarchive
|
||||
LIBARCHIVE_REQUIRED=2.8.5
|
||||
PKG_CHECK_MODULES(LIBARCHIVE, [libarchive >= $LIBARCHIVE_REQUIRED])
|
||||
AC_SUBST(LIBARCHIVE_CFLAGS)
|
||||
AC_SUBST(LIBARCHIVE_LIBS)
|
||||
|
||||
ac_configure_args="$ac_configure_args -q"
|
||||
|
||||
AC_CONFIG_FILES(
|
||||
Makefile
|
||||
include/Makefile
|
||||
fuse/Makefile
|
||||
lib/Makefile
|
||||
lib/libseafile.pc
|
||||
common/Makefile
|
||||
common/cdc/Makefile
|
||||
common/db-wrapper/Makefile
|
||||
server/Makefile
|
||||
server/gc/Makefile
|
||||
python/Makefile
|
||||
python/seafile/Makefile
|
||||
python/seaserv/Makefile
|
||||
controller/Makefile
|
||||
tools/Makefile
|
||||
)
|
||||
|
||||
AC_OUTPUT
|
21
controller/Makefile.am
Normal file
21
controller/Makefile.am
Normal file
|
@ -0,0 +1,21 @@
|
|||
bin_PROGRAMS = seafile-controller
|
||||
|
||||
AM_CFLAGS = \
|
||||
-DSEAFILE_SERVER \
|
||||
-I$(top_srcdir)/include \
|
||||
-I$(top_srcdir)/lib \
|
||||
-I$(top_builddir)/lib \
|
||||
-I$(top_srcdir)/common \
|
||||
@CCNET_CFLAGS@ \
|
||||
@SEARPC_CFLAGS@ \
|
||||
@GLIB2_CFLAGS@ \
|
||||
-Wall
|
||||
|
||||
noinst_HEADERS = seafile-controller.h ../common/log.h
|
||||
|
||||
seafile_controller_SOURCES = seafile-controller.c ../common/log.c
|
||||
|
||||
seafile_controller_LDADD = @CCNET_LIBS@ \
|
||||
$(top_builddir)/lib/libseafile_common.la \
|
||||
@GLIB2_LIBS@ @GOBJECT_LIBS@ @SSL_LIBS@ @LIB_RT@ @LIB_UUID@ @LIBEVENT_LIBS@ \
|
||||
@SEARPC_LIBS@ @JANSSON_LIBS@ @ZLIB_LIBS@
|
1030
controller/seafile-controller.c
Normal file
1030
controller/seafile-controller.c
Normal file
File diff suppressed because it is too large
Load diff
60
controller/seafile-controller.h
Normal file
60
controller/seafile-controller.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
/*
|
||||
* Seafile-controller is responsible for:
|
||||
*
|
||||
* 1. Start: start server processes:
|
||||
*
|
||||
* - ccnet-server
|
||||
* - seaf-server
|
||||
* - seaf-mon
|
||||
*
|
||||
* 2. Repair:
|
||||
*
|
||||
* - ensure ccnet process availability by watching client->connfd
|
||||
* - ensure server processes availablity by checking process is running periodically
|
||||
* If some process has stopped working, try to restart it.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SEAFILE_CONTROLLER_H
|
||||
#define SEAFILE_CONTROLLER_H
|
||||
|
||||
typedef struct _SeafileController SeafileController;
|
||||
|
||||
enum {
|
||||
PID_CCNET = 0,
|
||||
PID_SERVER,
|
||||
PID_SEAFDAV,
|
||||
N_PID
|
||||
};
|
||||
|
||||
typedef struct SeafDavConfig {
|
||||
gboolean enabled;
|
||||
gboolean fastcgi;
|
||||
int port;
|
||||
char *host;
|
||||
|
||||
} SeafDavConfig;
|
||||
|
||||
struct _SeafileController {
|
||||
char *central_config_dir;
|
||||
char *config_dir;
|
||||
char *seafile_dir;
|
||||
char *logdir;
|
||||
|
||||
CcnetClient *client;
|
||||
CcnetClient *sync_client;
|
||||
CcnetMqclientProc *mqclient_proc;
|
||||
|
||||
guint check_process_timer;
|
||||
guint client_io_id;
|
||||
/* Decide whether to start seaf-server in cloud mode */
|
||||
gboolean cloud_mode;
|
||||
|
||||
int pid[N_PID];
|
||||
char *pidfile[N_PID];
|
||||
|
||||
SeafDavConfig seafdav_config;
|
||||
};
|
||||
#endif
|
46
fuse/Makefile.am
Normal file
46
fuse/Makefile.am
Normal file
|
@ -0,0 +1,46 @@
|
|||
AM_CFLAGS = -DPKGDATADIR=\"$(pkgdatadir)\" \
|
||||
-DPACKAGE_DATA_DIR=\""$(pkgdatadir)"\" \
|
||||
-DSEAFILE_SERVER \
|
||||
-I$(top_srcdir)/include \
|
||||
-I$(top_srcdir)/lib \
|
||||
-I$(top_builddir)/lib \
|
||||
-I$(top_srcdir)/common \
|
||||
@CCNET_CFLAGS@ \
|
||||
@SEARPC_CFLAGS@ \
|
||||
@GLIB2_CFLAGS@ \
|
||||
@FUSE_CFLAGS@ \
|
||||
-Wall
|
||||
|
||||
bin_PROGRAMS = seaf-fuse
|
||||
|
||||
noinst_HEADERS = seaf-fuse.h seafile-session.h repo-mgr.h
|
||||
|
||||
seaf_fuse_SOURCES = seaf-fuse.c \
|
||||
seafile-session.c \
|
||||
file.c \
|
||||
getattr.c \
|
||||
readdir.c \
|
||||
repo-mgr.c \
|
||||
../common/block-mgr.c \
|
||||
../common/block-backend.c \
|
||||
../common/block-backend-fs.c \
|
||||
../common/branch-mgr.c \
|
||||
../common/commit-mgr.c \
|
||||
../common/fs-mgr.c \
|
||||
../common/log.c \
|
||||
../common/seaf-db.c \
|
||||
../common/seaf-utils.c \
|
||||
../common/obj-store.c \
|
||||
../common/obj-backend-fs.c \
|
||||
../common/obj-backend-riak.c \
|
||||
../common/seafile-crypt.c
|
||||
|
||||
seaf_fuse_LDADD = @CCNET_LIBS@ \
|
||||
$(top_builddir)/lib/libseafile.la \
|
||||
@GLIB2_LIBS@ @GOBJECT_LIBS@ @SSL_LIBS@ @LIB_RT@ @LIB_UUID@ \
|
||||
-lsqlite3 @LIBEVENT_LIBS@ \
|
||||
$(top_builddir)/common/cdc/libcdc.la \
|
||||
$(top_builddir)/common/db-wrapper/libdbwrapper.la \
|
||||
@SEARPC_LIBS@ @JANSSON_LIBS@ @FUSE_LIBS@ @ZLIB_LIBS@ \
|
||||
@MYSQL_LIBS@ @PGSQL_LIBS@
|
||||
|
107
fuse/file.c
Normal file
107
fuse/file.c
Normal file
|
@ -0,0 +1,107 @@
|
|||
#include "common.h"
|
||||
|
||||
#define FUSE_USE_VERSION 26
|
||||
#include <fuse.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib-object.h>
|
||||
|
||||
#include <ccnet.h>
|
||||
#include <seaf-db.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "seaf-fuse.h"
|
||||
|
||||
int read_file(SeafileSession *seaf,
|
||||
const char *store_id, int version,
|
||||
Seafile *file,
|
||||
char *buf, size_t size,
|
||||
off_t offset, struct fuse_file_info *info)
|
||||
{
|
||||
BlockHandle *handle = NULL;;
|
||||
BlockMetadata *bmd;
|
||||
char *blkid;
|
||||
char *ptr;
|
||||
off_t off = 0, nleft;
|
||||
int i, n, ret = -EIO;
|
||||
|
||||
for (i = 0; i < file->n_blocks; i++) {
|
||||
blkid = file->blk_sha1s[i];
|
||||
|
||||
bmd = seaf_block_manager_stat_block(seaf->block_mgr, store_id, version, blkid);
|
||||
if (!bmd)
|
||||
return -EIO;
|
||||
|
||||
if (offset < off + bmd->size) {
|
||||
g_free (bmd);
|
||||
break;
|
||||
}
|
||||
|
||||
off += bmd->size;
|
||||
g_free (bmd);
|
||||
}
|
||||
|
||||
/* beyond the file size */
|
||||
if (i == file->n_blocks)
|
||||
return 0;
|
||||
|
||||
nleft = size;
|
||||
ptr = buf;
|
||||
while (nleft > 0 && i < file->n_blocks) {
|
||||
blkid = file->blk_sha1s[i];
|
||||
|
||||
handle = seaf_block_manager_open_block(seaf->block_mgr,
|
||||
store_id, version,
|
||||
blkid, BLOCK_READ);
|
||||
if (!handle) {
|
||||
seaf_warning ("Failed to open block %s:%s.\n", store_id, blkid);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
/* trim the offset in a block */
|
||||
if (offset > off) {
|
||||
char *tmp = (char *)malloc(sizeof(char) * (offset - off));
|
||||
if (!tmp)
|
||||
return -ENOMEM;
|
||||
|
||||
n = seaf_block_manager_read_block(seaf->block_mgr, handle,
|
||||
tmp, offset-off);
|
||||
if (n != offset - off) {
|
||||
seaf_warning ("Failed to read block %s:%s.\n", store_id, blkid);
|
||||
free (tmp);
|
||||
goto out;
|
||||
}
|
||||
|
||||
off += n;
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
if ((n = seaf_block_manager_read_block(seaf->block_mgr,
|
||||
handle, ptr, nleft)) < 0) {
|
||||
seaf_warning ("Failed to read block %s:%s.\n", store_id, blkid);
|
||||
goto out;
|
||||
}
|
||||
|
||||
nleft -= n;
|
||||
ptr += n;
|
||||
off += n;
|
||||
++i;
|
||||
|
||||
/* At this point we should have read all the content of the block or
|
||||
* have read up to @size bytes. So it's safe to close the block.
|
||||
*/
|
||||
seaf_block_manager_close_block(seaf->block_mgr, handle);
|
||||
seaf_block_manager_block_handle_free (seaf->block_mgr, handle);
|
||||
}
|
||||
|
||||
return size - nleft;
|
||||
|
||||
out:
|
||||
if (handle) {
|
||||
seaf_block_manager_close_block(seaf->block_mgr, handle);
|
||||
seaf_block_manager_block_handle_free (seaf->block_mgr, handle);
|
||||
}
|
||||
return ret;
|
||||
}
|
190
fuse/getattr.c
Normal file
190
fuse/getattr.c
Normal file
|
@ -0,0 +1,190 @@
|
|||
#include "common.h"
|
||||
|
||||
#define FUSE_USE_VERSION 26
|
||||
#include <fuse.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib-object.h>
|
||||
|
||||
#include <ccnet.h>
|
||||
#include <ccnet/ccnet-object.h>
|
||||
#include <seaf-db.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "seaf-fuse.h"
|
||||
#include "seafile-session.h"
|
||||
|
||||
static CcnetEmailUser *get_user_from_ccnet (SearpcClient *client, const char *user)
|
||||
{
|
||||
return (CcnetEmailUser *)searpc_client_call__object (client,
|
||||
"get_emailuser", CCNET_TYPE_EMAIL_USER, NULL,
|
||||
1, "string", user);
|
||||
}
|
||||
|
||||
static int getattr_root(SeafileSession *seaf, struct stat *stbuf)
|
||||
{
|
||||
stbuf->st_mode = S_IFDIR | 0755;
|
||||
stbuf->st_nlink = 2;
|
||||
stbuf->st_size = 4096;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getattr_user(SeafileSession *seaf, const char *user, struct stat *stbuf)
|
||||
{
|
||||
SearpcClient *client;
|
||||
CcnetEmailUser *emailuser;
|
||||
|
||||
client = ccnet_create_pooled_rpc_client (seaf->client_pool,
|
||||
NULL,
|
||||
"ccnet-threaded-rpcserver");
|
||||
if (!client) {
|
||||
seaf_warning ("Failed to alloc rpc client.\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
emailuser = get_user_from_ccnet (client, user);
|
||||
if (!emailuser) {
|
||||
ccnet_rpc_client_free (client);
|
||||
return -ENOENT;
|
||||
}
|
||||
g_object_unref (emailuser);
|
||||
ccnet_rpc_client_free (client);
|
||||
|
||||
stbuf->st_mode = S_IFDIR | 0755;
|
||||
stbuf->st_nlink = 2;
|
||||
stbuf->st_size = 4096;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getattr_repo(SeafileSession *seaf,
|
||||
const char *user, const char *repo_id, const char *repo_path,
|
||||
struct stat *stbuf)
|
||||
{
|
||||
SeafRepo *repo = NULL;
|
||||
SeafBranch *branch;
|
||||
SeafCommit *commit = NULL;
|
||||
guint32 mode = 0;
|
||||
char *id = NULL;
|
||||
int ret = 0;
|
||||
|
||||
repo = seaf_repo_manager_get_repo(seaf->repo_mgr, repo_id);
|
||||
if (!repo) {
|
||||
seaf_warning ("Failed to get repo %s.\n", repo_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
branch = repo->head;
|
||||
commit = seaf_commit_manager_get_commit(seaf->commit_mgr,
|
||||
repo->id, repo->version,
|
||||
branch->commit_id);
|
||||
if (!commit) {
|
||||
seaf_warning ("Failed to get commit %s:%.8s.\n", repo->id, branch->commit_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
id = seaf_fs_manager_path_to_obj_id(seaf->fs_mgr,
|
||||
repo->store_id, repo->version,
|
||||
commit->root_id,
|
||||
repo_path, &mode, NULL);
|
||||
if (!id) {
|
||||
seaf_warning ("Path %s doesn't exist in repo %s.\n", repo_path, repo_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (S_ISDIR(mode)) {
|
||||
SeafDir *dir;
|
||||
GList *l;
|
||||
int cnt = 2; /* '.' and '..' */
|
||||
|
||||
dir = seaf_fs_manager_get_seafdir(seaf->fs_mgr,
|
||||
repo->store_id, repo->version, id);
|
||||
if (dir) {
|
||||
for (l = dir->entries; l; l = l->next)
|
||||
cnt++;
|
||||
}
|
||||
|
||||
if (strcmp (repo_path, "/") != 0) {
|
||||
// get dirent of the dir
|
||||
SeafDirent *dirent = seaf_fs_manager_get_dirent_by_path (seaf->fs_mgr,
|
||||
repo->store_id,
|
||||
repo->version,
|
||||
commit->root_id,
|
||||
repo_path, NULL);
|
||||
if (dirent && repo->version != 0)
|
||||
stbuf->st_mtime = dirent->mtime;
|
||||
|
||||
seaf_dirent_free (dirent);
|
||||
}
|
||||
|
||||
stbuf->st_size += cnt * sizeof(SeafDirent);
|
||||
stbuf->st_mode = mode | 0755;
|
||||
stbuf->st_nlink = 2;
|
||||
|
||||
seaf_dir_free (dir);
|
||||
} else if (S_ISREG(mode)) {
|
||||
Seafile *file;
|
||||
|
||||
file = seaf_fs_manager_get_seafile(seaf->fs_mgr,
|
||||
repo->store_id, repo->version, id);
|
||||
if (file)
|
||||
stbuf->st_size = file->file_size;
|
||||
|
||||
SeafDirent *dirent = seaf_fs_manager_get_dirent_by_path (seaf->fs_mgr,
|
||||
repo->store_id,
|
||||
repo->version,
|
||||
commit->root_id,
|
||||
repo_path, NULL);
|
||||
if (dirent && repo->version != 0)
|
||||
stbuf->st_mtime = dirent->mtime;
|
||||
|
||||
stbuf->st_mode = mode | 0644;
|
||||
stbuf->st_nlink = 1;
|
||||
|
||||
seaf_dirent_free (dirent);
|
||||
seafile_unref (file);
|
||||
} else {
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
out:
|
||||
g_free (id);
|
||||
seaf_repo_unref (repo);
|
||||
seaf_commit_unref (commit);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int do_getattr(SeafileSession *seaf, const char *path, struct stat *stbuf)
|
||||
{
|
||||
int n_parts;
|
||||
char *user, *repo_id, *repo_path;
|
||||
int ret = 0;
|
||||
|
||||
if (parse_fuse_path (path, &n_parts, &user, &repo_id, &repo_path) < 0) {
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
switch (n_parts) {
|
||||
case 0:
|
||||
ret = getattr_root(seaf, stbuf);
|
||||
break;
|
||||
case 1:
|
||||
ret = getattr_user(seaf, user, stbuf);
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
ret = getattr_repo(seaf, user, repo_id, repo_path, stbuf);
|
||||
break;
|
||||
}
|
||||
|
||||
g_free (user);
|
||||
g_free (repo_id);
|
||||
g_free (repo_path);
|
||||
return ret;
|
||||
}
|
237
fuse/readdir.c
Normal file
237
fuse/readdir.c
Normal file
|
@ -0,0 +1,237 @@
|
|||
#include "common.h"
|
||||
|
||||
#define FUSE_USE_VERSION 26
|
||||
#include <fuse.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib-object.h>
|
||||
|
||||
#include <ccnet.h>
|
||||
#include <ccnet/ccnet-object.h>
|
||||
#include <seaf-db.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "seaf-fuse.h"
|
||||
#include "seafile-session.h"
|
||||
|
||||
static char *replace_slash (const char *repo_name)
|
||||
{
|
||||
char *ret = g_strdup(repo_name);
|
||||
char *p;
|
||||
|
||||
for (p = ret; *p != 0; ++p)
|
||||
if (*p == '/')
|
||||
*p = '_';
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static GList *get_users_from_ccnet (SearpcClient *client, const char *source)
|
||||
{
|
||||
return searpc_client_call__objlist (client,
|
||||
"get_emailusers", CCNET_TYPE_EMAIL_USER, NULL,
|
||||
3, "string", source, "int", -1, "int", -1);
|
||||
}
|
||||
|
||||
static CcnetEmailUser *get_user_from_ccnet (SearpcClient *client, const char *user)
|
||||
{
|
||||
return (CcnetEmailUser *)searpc_client_call__object (client,
|
||||
"get_emailuser", CCNET_TYPE_EMAIL_USER, NULL,
|
||||
1, "string", user);
|
||||
}
|
||||
|
||||
static int readdir_root(SeafileSession *seaf,
|
||||
void *buf, fuse_fill_dir_t filler, off_t offset,
|
||||
struct fuse_file_info *info)
|
||||
{
|
||||
SearpcClient *client = NULL;
|
||||
GList *users, *p;
|
||||
CcnetEmailUser *user;
|
||||
const char *email;
|
||||
GHashTable *user_hash;
|
||||
int dummy;
|
||||
|
||||
client = ccnet_create_pooled_rpc_client (seaf->client_pool,
|
||||
NULL,
|
||||
"ccnet-threaded-rpcserver");
|
||||
if (!client) {
|
||||
seaf_warning ("Failed to alloc rpc client.\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
user_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
|
||||
|
||||
users = get_users_from_ccnet (client, "DB");
|
||||
for (p = users; p; p = p->next) {
|
||||
user = p->data;
|
||||
email = ccnet_email_user_get_email (user);
|
||||
g_hash_table_insert (user_hash, g_strdup(email), &dummy);
|
||||
g_object_unref (user);
|
||||
}
|
||||
g_list_free (users);
|
||||
|
||||
users = get_users_from_ccnet (client, "LDAP");
|
||||
for (p = users; p; p = p->next) {
|
||||
user = p->data;
|
||||
email = ccnet_email_user_get_email (user);
|
||||
g_hash_table_insert (user_hash, g_strdup(email), &dummy);
|
||||
g_object_unref (user);
|
||||
}
|
||||
g_list_free (users);
|
||||
|
||||
users = g_hash_table_get_keys (user_hash);
|
||||
for (p = users; p; p = p->next) {
|
||||
email = p->data;
|
||||
filler (buf, email, NULL, 0);
|
||||
}
|
||||
g_list_free (users);
|
||||
|
||||
g_hash_table_destroy (user_hash);
|
||||
ccnet_rpc_client_free (client);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int readdir_user(SeafileSession *seaf, const char *user,
|
||||
void *buf, fuse_fill_dir_t filler, off_t offset,
|
||||
struct fuse_file_info *info)
|
||||
{
|
||||
SearpcClient *client;
|
||||
CcnetEmailUser *emailuser;
|
||||
GList *list = NULL, *p;
|
||||
GString *name;
|
||||
|
||||
client = ccnet_create_pooled_rpc_client (seaf->client_pool,
|
||||
NULL,
|
||||
"ccnet-threaded-rpcserver");
|
||||
if (!client) {
|
||||
seaf_warning ("Failed to alloc rpc client.\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
emailuser = get_user_from_ccnet (client, user);
|
||||
if (!emailuser) {
|
||||
ccnet_rpc_client_free (client);
|
||||
return -ENOENT;
|
||||
}
|
||||
g_object_unref (emailuser);
|
||||
ccnet_rpc_client_free (client);
|
||||
|
||||
list = seaf_repo_manager_get_repos_by_owner (seaf->repo_mgr, user);
|
||||
if (!list)
|
||||
return 0;
|
||||
|
||||
for (p = list; p; p = p->next) {
|
||||
SeafRepo *repo = (SeafRepo *)p->data;
|
||||
|
||||
/* Don't list virtual repos. */
|
||||
if (seaf_repo_manager_is_virtual_repo(seaf->repo_mgr, repo->id)) {
|
||||
seaf_repo_unref (repo);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't list encrypted repo
|
||||
if (repo->encrypted) {
|
||||
continue;
|
||||
}
|
||||
|
||||
char *clean_repo_name = replace_slash (repo->name);
|
||||
|
||||
name = g_string_new ("");
|
||||
g_string_printf (name, "%s_%s", repo->id, clean_repo_name);
|
||||
filler(buf, name->str, NULL, 0);
|
||||
g_string_free (name, TRUE);
|
||||
g_free (clean_repo_name);
|
||||
|
||||
seaf_repo_unref (repo);
|
||||
}
|
||||
|
||||
g_list_free (list);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int readdir_repo(SeafileSession *seaf,
|
||||
const char *user, const char *repo_id, const char *repo_path,
|
||||
void *buf, fuse_fill_dir_t filler, off_t offset,
|
||||
struct fuse_file_info *info)
|
||||
{
|
||||
SeafRepo *repo = NULL;
|
||||
SeafBranch *branch;
|
||||
SeafCommit *commit = NULL;
|
||||
SeafDir *dir = NULL;
|
||||
GList *l;
|
||||
int ret = 0;
|
||||
|
||||
repo = seaf_repo_manager_get_repo(seaf->repo_mgr, repo_id);
|
||||
if (!repo) {
|
||||
seaf_warning ("Failed to get repo %s.\n", repo_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
branch = repo->head;
|
||||
commit = seaf_commit_manager_get_commit(seaf->commit_mgr,
|
||||
repo->id, repo->version,
|
||||
branch->commit_id);
|
||||
if (!commit) {
|
||||
seaf_warning ("Failed to get commit %s:%.8s.\n", repo->id, branch->commit_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
dir = seaf_fs_manager_get_seafdir_by_path(seaf->fs_mgr,
|
||||
repo->store_id, repo->version,
|
||||
commit->root_id,
|
||||
repo_path, NULL);
|
||||
if (!dir) {
|
||||
seaf_warning ("Path %s doesn't exist in repo %s.\n", repo_path, repo_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
for (l = dir->entries; l; l = l->next) {
|
||||
SeafDirent *seaf_dent = (SeafDirent *) l->data;
|
||||
/* FIXME: maybe we need to return stbuf */
|
||||
filler(buf, seaf_dent->name, NULL, 0);
|
||||
}
|
||||
|
||||
out:
|
||||
seaf_repo_unref (repo);
|
||||
seaf_commit_unref (commit);
|
||||
seaf_dir_free (dir);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int do_readdir(SeafileSession *seaf, const char *path, void *buf,
|
||||
fuse_fill_dir_t filler, off_t offset,
|
||||
struct fuse_file_info *info)
|
||||
{
|
||||
int n_parts;
|
||||
char *user, *repo_id, *repo_path;
|
||||
int ret = 0;
|
||||
|
||||
if (parse_fuse_path (path, &n_parts, &user, &repo_id, &repo_path) < 0) {
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
switch (n_parts) {
|
||||
case 0:
|
||||
ret = readdir_root(seaf, buf, filler, offset, info);
|
||||
break;
|
||||
case 1:
|
||||
ret = readdir_user(seaf, user, buf, filler, offset, info);
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
ret = readdir_repo(seaf, user, repo_id, repo_path, buf, filler, offset, info);
|
||||
break;
|
||||
}
|
||||
|
||||
g_free (user);
|
||||
g_free (repo_id);
|
||||
g_free (repo_path);
|
||||
return ret;
|
||||
}
|
428
fuse/repo-mgr.c
Normal file
428
fuse/repo-mgr.c
Normal file
|
@ -0,0 +1,428 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include "common.h"
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#include <ccnet.h>
|
||||
#include "utils.h"
|
||||
#include "log.h"
|
||||
|
||||
#include "seafile-session.h"
|
||||
#include "commit-mgr.h"
|
||||
#include "branch-mgr.h"
|
||||
#include "repo-mgr.h"
|
||||
#include "fs-mgr.h"
|
||||
#include "seafile-error.h"
|
||||
|
||||
#include "seaf-db.h"
|
||||
|
||||
#define INDEX_DIR "index"
|
||||
|
||||
struct _SeafRepoManagerPriv {
|
||||
|
||||
};
|
||||
|
||||
static SeafRepo *
|
||||
load_repo (SeafRepoManager *manager, const char *repo_id);
|
||||
|
||||
gboolean
|
||||
is_repo_id_valid (const char *id)
|
||||
{
|
||||
if (!id)
|
||||
return FALSE;
|
||||
|
||||
return is_uuid_valid (id);
|
||||
}
|
||||
|
||||
SeafRepo*
|
||||
seaf_repo_new (const char *id, const char *name, const char *desc)
|
||||
{
|
||||
SeafRepo* repo;
|
||||
|
||||
/* valid check */
|
||||
|
||||
|
||||
repo = g_new0 (SeafRepo, 1);
|
||||
memcpy (repo->id, id, 36);
|
||||
repo->id[36] = '\0';
|
||||
|
||||
repo->name = g_strdup(name);
|
||||
repo->desc = g_strdup(desc);
|
||||
|
||||
repo->ref_cnt = 1;
|
||||
|
||||
return repo;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_repo_free (SeafRepo *repo)
|
||||
{
|
||||
if (repo->name) g_free (repo->name);
|
||||
if (repo->desc) g_free (repo->desc);
|
||||
if (repo->category) g_free (repo->category);
|
||||
if (repo->head) seaf_branch_unref (repo->head);
|
||||
g_free (repo);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_repo_ref (SeafRepo *repo)
|
||||
{
|
||||
g_atomic_int_inc (&repo->ref_cnt);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_repo_unref (SeafRepo *repo)
|
||||
{
|
||||
if (!repo)
|
||||
return;
|
||||
|
||||
if (g_atomic_int_dec_and_test (&repo->ref_cnt))
|
||||
seaf_repo_free (repo);
|
||||
}
|
||||
|
||||
static void
|
||||
set_head_common (SeafRepo *repo, SeafBranch *branch)
|
||||
{
|
||||
if (repo->head)
|
||||
seaf_branch_unref (repo->head);
|
||||
repo->head = branch;
|
||||
seaf_branch_ref(branch);
|
||||
}
|
||||
|
||||
void
|
||||
seaf_repo_from_commit (SeafRepo *repo, SeafCommit *commit)
|
||||
{
|
||||
repo->name = g_strdup (commit->repo_name);
|
||||
repo->desc = g_strdup (commit->repo_desc);
|
||||
repo->encrypted = commit->encrypted;
|
||||
if (repo->encrypted) {
|
||||
repo->enc_version = commit->enc_version;
|
||||
if (repo->enc_version >= 1)
|
||||
memcpy (repo->magic, commit->magic, 33);
|
||||
}
|
||||
repo->no_local_history = commit->no_local_history;
|
||||
repo->version = commit->version;
|
||||
}
|
||||
|
||||
void
|
||||
seaf_repo_to_commit (SeafRepo *repo, SeafCommit *commit)
|
||||
{
|
||||
commit->repo_name = g_strdup (repo->name);
|
||||
commit->repo_desc = g_strdup (repo->desc);
|
||||
commit->encrypted = repo->encrypted;
|
||||
if (commit->encrypted) {
|
||||
commit->enc_version = repo->enc_version;
|
||||
if (commit->enc_version >= 1)
|
||||
commit->magic = g_strdup (repo->magic);
|
||||
}
|
||||
commit->no_local_history = repo->no_local_history;
|
||||
commit->version = repo->version;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
collect_commit (SeafCommit *commit, void *vlist, gboolean *stop)
|
||||
{
|
||||
GList **commits = vlist;
|
||||
|
||||
/* The traverse function will unref the commit, so we need to ref it.
|
||||
*/
|
||||
seaf_commit_ref (commit);
|
||||
*commits = g_list_prepend (*commits, commit);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
GList *
|
||||
seaf_repo_get_commits (SeafRepo *repo)
|
||||
{
|
||||
GList *branches;
|
||||
GList *ptr;
|
||||
SeafBranch *branch;
|
||||
GList *commits = NULL;
|
||||
|
||||
branches = seaf_branch_manager_get_branch_list (seaf->branch_mgr, repo->id);
|
||||
if (branches == NULL) {
|
||||
seaf_warning ("Failed to get branch list of repo %s.\n", repo->id);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (ptr = branches; ptr != NULL; ptr = ptr->next) {
|
||||
branch = ptr->data;
|
||||
gboolean res = seaf_commit_manager_traverse_commit_tree (seaf->commit_mgr,
|
||||
repo->id,
|
||||
repo->version,
|
||||
branch->commit_id,
|
||||
collect_commit,
|
||||
&commits,
|
||||
FALSE);
|
||||
if (!res) {
|
||||
for (ptr = commits; ptr != NULL; ptr = ptr->next)
|
||||
seaf_commit_unref ((SeafCommit *)(ptr->data));
|
||||
g_list_free (commits);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
commits = g_list_reverse (commits);
|
||||
|
||||
out:
|
||||
for (ptr = branches; ptr != NULL; ptr = ptr->next) {
|
||||
seaf_branch_unref ((SeafBranch *)ptr->data);
|
||||
}
|
||||
return commits;
|
||||
}
|
||||
|
||||
static int
|
||||
compare_repo (const SeafRepo *srepo, const SeafRepo *trepo)
|
||||
{
|
||||
return g_strcmp0 (srepo->id, trepo->id);
|
||||
}
|
||||
|
||||
SeafRepoManager*
|
||||
seaf_repo_manager_new (SeafileSession *seaf)
|
||||
{
|
||||
SeafRepoManager *mgr = g_new0 (SeafRepoManager, 1);
|
||||
|
||||
mgr->priv = g_new0 (SeafRepoManagerPriv, 1);
|
||||
mgr->seaf = seaf;
|
||||
|
||||
return mgr;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_repo_manager_init (SeafRepoManager *mgr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_repo_manager_start (SeafRepoManager *mgr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
repo_exists_in_db (SeafDB *db, const char *id)
|
||||
{
|
||||
char sql[256];
|
||||
gboolean db_err = FALSE;
|
||||
|
||||
snprintf (sql, sizeof(sql), "SELECT repo_id FROM Repo WHERE repo_id = '%s'",
|
||||
id);
|
||||
return seaf_db_check_for_existence (db, sql, &db_err);
|
||||
}
|
||||
|
||||
SeafRepo*
|
||||
seaf_repo_manager_get_repo (SeafRepoManager *manager, const gchar *id)
|
||||
{
|
||||
SeafRepo repo;
|
||||
int len = strlen(id);
|
||||
|
||||
if (len >= 37)
|
||||
return NULL;
|
||||
|
||||
memcpy (repo.id, id, len + 1);
|
||||
|
||||
if (repo_exists_in_db (manager->seaf->db, id)) {
|
||||
SeafRepo *ret = load_repo (manager, id);
|
||||
if (!ret)
|
||||
return NULL;
|
||||
/* seaf_repo_ref (ret); */
|
||||
return ret;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_repo_manager_repo_exists (SeafRepoManager *manager, const gchar *id)
|
||||
{
|
||||
SeafRepo repo;
|
||||
memcpy (repo.id, id, 37);
|
||||
|
||||
return repo_exists_in_db (manager->seaf->db, id);
|
||||
}
|
||||
|
||||
static void
|
||||
load_repo_commit (SeafRepoManager *manager,
|
||||
SeafRepo *repo,
|
||||
SeafBranch *branch)
|
||||
{
|
||||
SeafCommit *commit;
|
||||
|
||||
commit = seaf_commit_manager_get_commit_compatible (manager->seaf->commit_mgr,
|
||||
repo->id,
|
||||
branch->commit_id);
|
||||
if (!commit) {
|
||||
seaf_warning ("Commit %s is missing\n", branch->commit_id);
|
||||
repo->is_corrupted = TRUE;
|
||||
return;
|
||||
}
|
||||
|
||||
set_head_common (repo, branch);
|
||||
seaf_repo_from_commit (repo, commit);
|
||||
|
||||
seaf_commit_unref (commit);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
load_virtual_info (SeafDBRow *row, void *vrepo_id)
|
||||
{
|
||||
char *ret_repo_id = vrepo_id;
|
||||
const char *origin_repo_id;
|
||||
|
||||
origin_repo_id = seaf_db_row_get_column_text (row, 0);
|
||||
memcpy (ret_repo_id, origin_repo_id, 37);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
char *
|
||||
get_origin_repo_id (SeafRepoManager *mgr, const char *repo_id)
|
||||
{
|
||||
char sql[256];
|
||||
char origin_repo_id[37];
|
||||
|
||||
memset (origin_repo_id, 0, 37);
|
||||
|
||||
snprintf (sql, 256,
|
||||
"SELECT origin_repo FROM VirtualRepo "
|
||||
"WHERE repo_id = '%s'", repo_id);
|
||||
seaf_db_foreach_selected_row (seaf->db, sql, load_virtual_info, origin_repo_id);
|
||||
|
||||
if (origin_repo_id[0] != 0)
|
||||
return g_strdup(origin_repo_id);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static SeafRepo *
|
||||
load_repo (SeafRepoManager *manager, const char *repo_id)
|
||||
{
|
||||
SeafRepo *repo;
|
||||
SeafBranch *branch;
|
||||
|
||||
repo = seaf_repo_new(repo_id, NULL, NULL);
|
||||
if (!repo) {
|
||||
seaf_warning ("[repo mgr] failed to alloc repo.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
repo->manager = manager;
|
||||
|
||||
branch = seaf_branch_manager_get_branch (seaf->branch_mgr, repo_id, "master");
|
||||
if (!branch) {
|
||||
seaf_warning ("Failed to get master branch of repo %.8s.\n", repo_id);
|
||||
repo->is_corrupted = TRUE;
|
||||
} else {
|
||||
load_repo_commit (manager, repo, branch);
|
||||
seaf_branch_unref (branch);
|
||||
}
|
||||
|
||||
if (repo->is_corrupted) {
|
||||
seaf_warning ("Repo %.8s is corrupted.\n", repo->id);
|
||||
seaf_repo_free (repo);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *origin_repo_id = get_origin_repo_id (manager, repo->id);
|
||||
if (origin_repo_id)
|
||||
memcpy (repo->store_id, origin_repo_id, 36);
|
||||
else
|
||||
memcpy (repo->store_id, repo->id, 36);
|
||||
g_free (origin_repo_id);
|
||||
|
||||
return repo;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
collect_repo_id (SeafDBRow *row, void *data)
|
||||
{
|
||||
GList **p_ids = data;
|
||||
const char *repo_id;
|
||||
|
||||
repo_id = seaf_db_row_get_column_text (row, 0);
|
||||
*p_ids = g_list_prepend (*p_ids, g_strdup(repo_id));
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
GList *
|
||||
seaf_repo_manager_get_repo_id_list (SeafRepoManager *mgr)
|
||||
{
|
||||
GList *ret = NULL;
|
||||
char sql[256];
|
||||
|
||||
snprintf (sql, 256, "SELECT repo_id FROM Repo");
|
||||
|
||||
if (seaf_db_foreach_selected_row (mgr->seaf->db, sql,
|
||||
collect_repo_id, &ret) < 0)
|
||||
return NULL;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
GList *
|
||||
seaf_repo_manager_get_repo_list (SeafRepoManager *mgr, int start, int limit)
|
||||
{
|
||||
GList *id_list = NULL, *ptr;
|
||||
GList *ret = NULL;
|
||||
SeafRepo *repo;
|
||||
char sql[256];
|
||||
|
||||
if (start == -1 && limit == -1)
|
||||
snprintf (sql, 256, "SELECT repo_id FROM Repo");
|
||||
else
|
||||
snprintf (sql, 256, "SELECT repo_id FROM Repo LIMIT %d, %d", start, limit);
|
||||
|
||||
if (seaf_db_foreach_selected_row (mgr->seaf->db, sql,
|
||||
collect_repo_id, &id_list) < 0)
|
||||
return NULL;
|
||||
|
||||
for (ptr = id_list; ptr; ptr = ptr->next) {
|
||||
char *repo_id = ptr->data;
|
||||
repo = seaf_repo_manager_get_repo (mgr, repo_id);
|
||||
if (repo != NULL)
|
||||
ret = g_list_prepend (ret, repo);
|
||||
}
|
||||
|
||||
string_list_free (id_list);
|
||||
return g_list_reverse (ret);
|
||||
}
|
||||
|
||||
GList *
|
||||
seaf_repo_manager_get_repos_by_owner (SeafRepoManager *mgr,
|
||||
const char *email)
|
||||
{
|
||||
GList *id_list = NULL, *ptr;
|
||||
GList *ret = NULL;
|
||||
char sql[256];
|
||||
|
||||
snprintf (sql, 256, "SELECT repo_id FROM RepoOwner WHERE owner_id='%s'",
|
||||
email);
|
||||
|
||||
if (seaf_db_foreach_selected_row (mgr->seaf->db, sql,
|
||||
collect_repo_id, &id_list) < 0)
|
||||
return NULL;
|
||||
|
||||
for (ptr = id_list; ptr; ptr = ptr->next) {
|
||||
char *repo_id = ptr->data;
|
||||
SeafRepo *repo = seaf_repo_manager_get_repo (mgr, repo_id);
|
||||
if (repo != NULL)
|
||||
ret = g_list_prepend (ret, repo);
|
||||
}
|
||||
|
||||
string_list_free (id_list);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
seaf_repo_manager_is_virtual_repo (SeafRepoManager *mgr, const char *repo_id)
|
||||
{
|
||||
char sql[256];
|
||||
gboolean db_err;
|
||||
|
||||
snprintf (sql, 256,
|
||||
"SELECT 1 FROM VirtualRepo WHERE repo_id = '%s'", repo_id);
|
||||
return seaf_db_check_for_existence (seaf->db, sql, &db_err);
|
||||
}
|
100
fuse/repo-mgr.h
Normal file
100
fuse/repo-mgr.h
Normal file
|
@ -0,0 +1,100 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef SEAF_REPO_MGR_H
|
||||
#define SEAF_REPO_MGR_H
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include "seafile-object.h"
|
||||
#include "commit-mgr.h"
|
||||
#include "branch-mgr.h"
|
||||
|
||||
struct _SeafRepoManager;
|
||||
typedef struct _SeafRepo SeafRepo;
|
||||
|
||||
struct _SeafRepo {
|
||||
struct _SeafRepoManager *manager;
|
||||
|
||||
gchar id[37];
|
||||
gchar *name;
|
||||
gchar *desc;
|
||||
gchar *category; /* not used yet */
|
||||
gboolean encrypted;
|
||||
int enc_version;
|
||||
gchar magic[33]; /* hash(repo_id + passwd), key stretched. */
|
||||
gboolean no_local_history;
|
||||
|
||||
SeafBranch *head;
|
||||
|
||||
gboolean is_corrupted;
|
||||
gboolean delete_pending;
|
||||
int ref_cnt;
|
||||
|
||||
int version;
|
||||
/* Used to access fs and block sotre.
|
||||
* This id is different from repo_id when this repo is virtual.
|
||||
* Virtual repos share fs and block store with its origin repo.
|
||||
* However, commit store for each repo is always independent.
|
||||
* So always use repo_id to access commit store.
|
||||
*/
|
||||
gchar store_id[37];
|
||||
};
|
||||
|
||||
gboolean is_repo_id_valid (const char *id);
|
||||
|
||||
SeafRepo*
|
||||
seaf_repo_new (const char *id, const char *name, const char *desc);
|
||||
|
||||
void
|
||||
seaf_repo_free (SeafRepo *repo);
|
||||
|
||||
void
|
||||
seaf_repo_ref (SeafRepo *repo);
|
||||
|
||||
void
|
||||
seaf_repo_unref (SeafRepo *repo);
|
||||
|
||||
typedef struct _SeafRepoManager SeafRepoManager;
|
||||
typedef struct _SeafRepoManagerPriv SeafRepoManagerPriv;
|
||||
|
||||
struct _SeafRepoManager {
|
||||
struct _SeafileSession *seaf;
|
||||
|
||||
SeafRepoManagerPriv *priv;
|
||||
};
|
||||
|
||||
SeafRepoManager*
|
||||
seaf_repo_manager_new (struct _SeafileSession *seaf);
|
||||
|
||||
int
|
||||
seaf_repo_manager_init (SeafRepoManager *mgr);
|
||||
|
||||
int
|
||||
seaf_repo_manager_start (SeafRepoManager *mgr);
|
||||
|
||||
int
|
||||
seaf_repo_manager_add_repo (SeafRepoManager *mgr, SeafRepo *repo);
|
||||
|
||||
int
|
||||
seaf_repo_manager_del_repo (SeafRepoManager *mgr, SeafRepo *repo);
|
||||
|
||||
SeafRepo*
|
||||
seaf_repo_manager_get_repo (SeafRepoManager *manager, const gchar *id);
|
||||
|
||||
gboolean
|
||||
seaf_repo_manager_repo_exists (SeafRepoManager *manager, const gchar *id);
|
||||
|
||||
GList*
|
||||
seaf_repo_manager_get_repo_list (SeafRepoManager *mgr, int start, int limit);
|
||||
|
||||
GList *
|
||||
seaf_repo_manager_get_repo_id_list (SeafRepoManager *mgr);
|
||||
|
||||
GList *
|
||||
seaf_repo_manager_get_repos_by_owner (SeafRepoManager *mgr,
|
||||
const char *email);
|
||||
|
||||
gboolean
|
||||
seaf_repo_manager_is_virtual_repo (SeafRepoManager *mgr, const char *repo_id);
|
||||
|
||||
#endif
|
355
fuse/seaf-fuse.c
Normal file
355
fuse/seaf-fuse.c
Normal file
|
@ -0,0 +1,355 @@
|
|||
#include "common.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#define FUSE_USE_VERSION 26
|
||||
#include <fuse.h>
|
||||
#include <fuse_opt.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib-object.h>
|
||||
|
||||
#include <ccnet.h>
|
||||
#include <seaf-db.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "seaf-fuse.h"
|
||||
|
||||
CcnetClient *ccnet_client = NULL;
|
||||
SeafileSession *seaf = NULL;
|
||||
|
||||
static char *parse_repo_id (const char *repo_id_name)
|
||||
{
|
||||
if (strlen(repo_id_name) < 36)
|
||||
return NULL;
|
||||
return g_strndup(repo_id_name, 36);
|
||||
}
|
||||
|
||||
/*
|
||||
* Path format can be:
|
||||
* 1. / --> list all users
|
||||
* 2. /user --> list libraries owned by user
|
||||
* 3. /user/repo-id_name --> list root of the library
|
||||
* 4. /user/repo-id_name/repo_path --> list library content
|
||||
*/
|
||||
int parse_fuse_path (const char *path,
|
||||
int *n_parts, char **user, char **repo_id, char **repo_path)
|
||||
{
|
||||
char **tokens;
|
||||
int n;
|
||||
int ret = 0;
|
||||
|
||||
*user = NULL;
|
||||
*repo_id = NULL;
|
||||
*repo_path = NULL;
|
||||
|
||||
if (*path == '/')
|
||||
++path;
|
||||
|
||||
tokens = g_strsplit (path, "/", 3);
|
||||
n = g_strv_length (tokens);
|
||||
*n_parts = n;
|
||||
|
||||
switch (n) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
*user = g_strdup(tokens[0]);
|
||||
break;
|
||||
case 2:
|
||||
*repo_id = parse_repo_id(tokens[1]);
|
||||
if (*repo_id == NULL) {
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
*user = g_strdup(tokens[0]);
|
||||
*repo_path = g_strdup("/");
|
||||
break;
|
||||
case 3:
|
||||
*repo_id = parse_repo_id(tokens[1]);
|
||||
if (*repo_id == NULL) {
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
*user = g_strdup(tokens[0]);
|
||||
*repo_path = g_strdup(tokens[2]);
|
||||
break;
|
||||
}
|
||||
|
||||
g_strfreev (tokens);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int seaf_fuse_getattr(const char *path, struct stat *stbuf)
|
||||
{
|
||||
memset(stbuf, 0, sizeof(struct stat));
|
||||
return do_getattr(seaf, path, stbuf);
|
||||
}
|
||||
|
||||
static int seaf_fuse_readdir(const char *path, void *buf,
|
||||
fuse_fill_dir_t filler, off_t offset,
|
||||
struct fuse_file_info *info)
|
||||
{
|
||||
filler(buf, ".", NULL, 0);
|
||||
filler(buf, "..", NULL, 0);
|
||||
|
||||
return do_readdir(seaf, path, buf, filler, offset, info);
|
||||
}
|
||||
|
||||
static int seaf_fuse_open(const char *path, struct fuse_file_info *info)
|
||||
{
|
||||
int n_parts;
|
||||
char *user, *repo_id, *repo_path;
|
||||
SeafRepo *repo = NULL;
|
||||
SeafBranch *branch = NULL;
|
||||
SeafCommit *commit = NULL;
|
||||
guint32 mode = 0;
|
||||
int ret = 0;
|
||||
|
||||
/* Now we only support read-only mode */
|
||||
if ((info->flags & 3) != O_RDONLY)
|
||||
return -EACCES;
|
||||
|
||||
if (parse_fuse_path (path, &n_parts, &user, &repo_id, &repo_path) < 0) {
|
||||
seaf_warning ("Invalid input path %s.\n", path);
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
if (n_parts != 2 && n_parts != 3) {
|
||||
seaf_warning ("Invalid input path for open: %s.\n", path);
|
||||
ret = -EACCES;
|
||||
goto out;
|
||||
}
|
||||
|
||||
repo = seaf_repo_manager_get_repo(seaf->repo_mgr, repo_id);
|
||||
if (!repo) {
|
||||
seaf_warning ("Failed to get repo %s.\n", repo_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
branch = repo->head;
|
||||
commit = seaf_commit_manager_get_commit(seaf->commit_mgr,
|
||||
repo->id,
|
||||
repo->version,
|
||||
branch->commit_id);
|
||||
if (!commit) {
|
||||
seaf_warning ("Failed to get commit %s:%.8s.\n", repo->id, branch->commit_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
char *id = seaf_fs_manager_path_to_obj_id(seaf->fs_mgr,
|
||||
repo->store_id, repo->version,
|
||||
commit->root_id,
|
||||
repo_path, &mode, NULL);
|
||||
if (!id) {
|
||||
seaf_warning ("Path %s doesn't exist in repo %s.\n", repo_path, repo_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
g_free (id);
|
||||
|
||||
if (!S_ISREG(mode))
|
||||
return -EACCES;
|
||||
|
||||
out:
|
||||
g_free (user);
|
||||
g_free (repo_id);
|
||||
g_free (repo_path);
|
||||
seaf_repo_unref (repo);
|
||||
seaf_commit_unref (commit);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int seaf_fuse_read(const char *path, char *buf, size_t size,
|
||||
off_t offset, struct fuse_file_info *info)
|
||||
{
|
||||
int n_parts;
|
||||
char *user, *repo_id, *repo_path;
|
||||
SeafRepo *repo = NULL;
|
||||
SeafBranch *branch = NULL;
|
||||
SeafCommit *commit = NULL;
|
||||
Seafile *file = NULL;
|
||||
char *file_id = NULL;
|
||||
int ret = 0;
|
||||
|
||||
/* Now we only support read-only mode */
|
||||
if ((info->flags & 3) != O_RDONLY)
|
||||
return -EACCES;
|
||||
|
||||
if (parse_fuse_path (path, &n_parts, &user, &repo_id, &repo_path) < 0) {
|
||||
seaf_warning ("Invalid input path %s.\n", path);
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
if (n_parts != 2 && n_parts != 3) {
|
||||
seaf_warning ("Invalid input path for open: %s.\n", path);
|
||||
ret = -EACCES;
|
||||
goto out;
|
||||
}
|
||||
|
||||
repo = seaf_repo_manager_get_repo(seaf->repo_mgr, repo_id);
|
||||
if (!repo) {
|
||||
seaf_warning ("Failed to get repo %s.\n", repo_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
branch = repo->head;
|
||||
commit = seaf_commit_manager_get_commit(seaf->commit_mgr,
|
||||
repo->id,
|
||||
repo->version,
|
||||
branch->commit_id);
|
||||
if (!commit) {
|
||||
seaf_warning ("Failed to get commit %s:%.8s.\n", repo->id, branch->commit_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
file_id = seaf_fs_manager_get_seafile_id_by_path(seaf->fs_mgr,
|
||||
repo->store_id, repo->version,
|
||||
commit->root_id,
|
||||
repo_path, NULL);
|
||||
if (!file_id) {
|
||||
seaf_warning ("Path %s doesn't exist in repo %s.\n", repo_path, repo_id);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
file = seaf_fs_manager_get_seafile(seaf->fs_mgr,
|
||||
repo->store_id, repo->version, file_id);
|
||||
if (!file) {
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = read_file(seaf, repo->store_id, repo->version,
|
||||
file, buf, size, offset, info);
|
||||
seafile_unref (file);
|
||||
|
||||
out:
|
||||
g_free (user);
|
||||
g_free (repo_id);
|
||||
g_free (repo_path);
|
||||
g_free (file_id);
|
||||
seaf_repo_unref (repo);
|
||||
seaf_commit_unref (commit);
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct options {
|
||||
char *central_config_dir;
|
||||
char *config_dir;
|
||||
char *seafile_dir;
|
||||
char *log_file;
|
||||
} options;
|
||||
|
||||
#define SEAF_FUSE_OPT_KEY(t, p, v) { t, offsetof(struct options, p), v }
|
||||
|
||||
enum {
|
||||
KEY_VERSION,
|
||||
KEY_HELP,
|
||||
};
|
||||
|
||||
static struct fuse_opt seaf_fuse_opts[] = {
|
||||
SEAF_FUSE_OPT_KEY("-c %s", config_dir, 0),
|
||||
SEAF_FUSE_OPT_KEY("--config %s", config_dir, 0),
|
||||
SEAF_FUSE_OPT_KEY("-F %s", central_config_dir, 0),
|
||||
SEAF_FUSE_OPT_KEY("--central-config-dir %s", central_config_dir, 0),
|
||||
SEAF_FUSE_OPT_KEY("-d %s", seafile_dir, 0),
|
||||
SEAF_FUSE_OPT_KEY("--seafdir %s", seafile_dir, 0),
|
||||
SEAF_FUSE_OPT_KEY("-l %s", log_file, 0),
|
||||
SEAF_FUSE_OPT_KEY("--logfile %s", log_file, 0),
|
||||
|
||||
FUSE_OPT_KEY("-V", KEY_VERSION),
|
||||
FUSE_OPT_KEY("--version", KEY_VERSION),
|
||||
FUSE_OPT_KEY("-h", KEY_HELP),
|
||||
FUSE_OPT_KEY("--help", KEY_HELP),
|
||||
FUSE_OPT_END
|
||||
};
|
||||
|
||||
static struct fuse_operations seaf_fuse_ops = {
|
||||
.getattr = seaf_fuse_getattr,
|
||||
.readdir = seaf_fuse_readdir,
|
||||
.open = seaf_fuse_open,
|
||||
.read = seaf_fuse_read,
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
|
||||
const char *debug_str = NULL;
|
||||
char *config_dir = DEFAULT_CONFIG_DIR;
|
||||
char *central_config_dir = NULL;
|
||||
char *seafile_dir = NULL;
|
||||
char *logfile = NULL;
|
||||
char *ccnet_debug_level_str = "info";
|
||||
char *seafile_debug_level_str = "debug";
|
||||
int ret;
|
||||
|
||||
memset(&options, 0, sizeof(struct options));
|
||||
|
||||
if (fuse_opt_parse(&args, &options, seaf_fuse_opts, NULL) == -1) {
|
||||
seaf_warning("Parse argument Failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
g_type_init();
|
||||
|
||||
config_dir = options.config_dir ? : DEFAULT_CONFIG_DIR;
|
||||
config_dir = ccnet_expand_path (config_dir);
|
||||
central_config_dir = options.central_config_dir;
|
||||
|
||||
if (!debug_str)
|
||||
debug_str = g_getenv("SEAFILE_DEBUG");
|
||||
seafile_debug_set_flags_string(debug_str);
|
||||
|
||||
if (!options.seafile_dir)
|
||||
seafile_dir = g_build_filename(config_dir, "seafile", NULL);
|
||||
else
|
||||
seafile_dir = options.seafile_dir;
|
||||
|
||||
if (!options.log_file)
|
||||
logfile = g_build_filename(seafile_dir, "seaf-fuse.log", NULL);
|
||||
else
|
||||
logfile = options.log_file;
|
||||
|
||||
if (seafile_log_init(logfile, ccnet_debug_level_str,
|
||||
seafile_debug_level_str) < 0) {
|
||||
fprintf(stderr, "Failed to init log.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ccnet_client = ccnet_client_new();
|
||||
if ((ccnet_client_load_confdir(ccnet_client, central_config_dir, config_dir)) < 0) {
|
||||
seaf_warning("Read config dir error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
seaf = seafile_session_new(central_config_dir, seafile_dir, ccnet_client);
|
||||
if (!seaf) {
|
||||
seaf_warning("Failed to create seafile session.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (seafile_session_init(seaf) < 0) {
|
||||
seaf_warning("Failed to init seafile session.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
seaf->client_pool = ccnet_client_pool_new(central_config_dir, config_dir);
|
||||
if (!seaf->client_pool) {
|
||||
seaf_warning("Failed to creat client pool\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
set_syslog_config (seaf->config);
|
||||
|
||||
ret = fuse_main(args.argc, args.argv, &seaf_fuse_ops, NULL);
|
||||
fuse_opt_free_args(&args);
|
||||
return ret;
|
||||
}
|
29
fuse/seaf-fuse.h
Normal file
29
fuse/seaf-fuse.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#ifndef SEAF_FUSE_H
|
||||
#define SEAF_FUSE_H
|
||||
|
||||
#include "seafile-session.h"
|
||||
|
||||
int parse_fuse_path (const char *path,
|
||||
int *n_parts, char **user, char **repo_id, char **repo_path);
|
||||
|
||||
SeafDirent *
|
||||
fuse_get_dirent_by_path (SeafFSManager *mgr,
|
||||
const char *repo_id,
|
||||
int version,
|
||||
const char *root_id,
|
||||
const char *path);
|
||||
|
||||
/* file.c */
|
||||
int read_file(SeafileSession *seaf, const char *store_id, int version,
|
||||
Seafile *file, char *buf, size_t size,
|
||||
off_t offset, struct fuse_file_info *info);
|
||||
|
||||
/* getattr.c */
|
||||
int do_getattr(SeafileSession *seaf, const char *path, struct stat *stbuf);
|
||||
|
||||
/* readdir.c */
|
||||
int do_readdir(SeafileSession *seaf, const char *path, void *buf,
|
||||
fuse_fill_dir_t filler, off_t offset,
|
||||
struct fuse_file_info *info);
|
||||
|
||||
#endif /* SEAF_FUSE_H */
|
120
fuse/seafile-session.c
Normal file
120
fuse/seafile-session.c
Normal file
|
@ -0,0 +1,120 @@
|
|||
#include "common.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <ccnet.h>
|
||||
#include <utils.h>
|
||||
#include <locale.h>
|
||||
|
||||
#include "seafile-session.h"
|
||||
#include "seaf-utils.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
SeafileSession *
|
||||
seafile_session_new(const char *central_config_dir,
|
||||
const char *seafile_dir,
|
||||
CcnetClient *ccnet_session)
|
||||
{
|
||||
char *abs_central_config_dir = NULL;
|
||||
char *abs_seafile_dir;
|
||||
char *tmp_file_dir;
|
||||
char *config_file_path;
|
||||
struct stat st;
|
||||
GKeyFile *config;
|
||||
SeafileSession *session = NULL;
|
||||
|
||||
if (!ccnet_session)
|
||||
return NULL;
|
||||
|
||||
abs_seafile_dir = ccnet_expand_path (seafile_dir);
|
||||
tmp_file_dir = g_build_filename(abs_seafile_dir, "tmpfiles", NULL);
|
||||
if (central_config_dir) {
|
||||
abs_central_config_dir = ccnet_expand_path (central_config_dir);
|
||||
}
|
||||
config_file_path = g_build_filename(
|
||||
abs_central_config_dir ? abs_central_config_dir : abs_seafile_dir,
|
||||
"seafile.conf", NULL);
|
||||
|
||||
if (g_stat(abs_seafile_dir, &st) < 0 || !S_ISDIR(st.st_mode)) {
|
||||
seaf_warning ("Seafile data dir %s does not exist and is unable to create\n",
|
||||
abs_seafile_dir);
|
||||
goto onerror;
|
||||
}
|
||||
|
||||
if (g_stat(tmp_file_dir, &st) < 0 || !S_ISDIR(st.st_mode)) {
|
||||
seaf_warning("Seafile tmp dir %s does not exist and is unable to create\n",
|
||||
tmp_file_dir);
|
||||
goto onerror;
|
||||
}
|
||||
|
||||
GError *error = NULL;
|
||||
config = g_key_file_new ();
|
||||
if (!g_key_file_load_from_file (config, config_file_path,
|
||||
G_KEY_FILE_NONE, &error)) {
|
||||
seaf_warning ("Failed to load config file.\n");
|
||||
g_key_file_free (config);
|
||||
goto onerror;
|
||||
}
|
||||
|
||||
session = g_new0(SeafileSession, 1);
|
||||
session->seaf_dir = abs_seafile_dir;
|
||||
session->tmp_file_dir = tmp_file_dir;
|
||||
session->session = ccnet_session;
|
||||
session->config = config;
|
||||
|
||||
if (load_database_config (session) < 0) {
|
||||
seaf_warning ("Failed to load database config.\n");
|
||||
goto onerror;
|
||||
}
|
||||
|
||||
session->fs_mgr = seaf_fs_manager_new (session, abs_seafile_dir);
|
||||
if (!session->fs_mgr)
|
||||
goto onerror;
|
||||
session->block_mgr = seaf_block_manager_new (session, abs_seafile_dir);
|
||||
if (!session->block_mgr)
|
||||
goto onerror;
|
||||
session->commit_mgr = seaf_commit_manager_new (session);
|
||||
if (!session->commit_mgr)
|
||||
goto onerror;
|
||||
session->repo_mgr = seaf_repo_manager_new (session);
|
||||
if (!session->repo_mgr)
|
||||
goto onerror;
|
||||
session->branch_mgr = seaf_branch_manager_new (session);
|
||||
if (!session->branch_mgr)
|
||||
goto onerror;
|
||||
|
||||
return session;
|
||||
|
||||
onerror:
|
||||
free (abs_seafile_dir);
|
||||
g_free (config_file_path);
|
||||
g_free (session);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
seafile_session_init (SeafileSession *session)
|
||||
{
|
||||
if (seaf_commit_manager_init (session->commit_mgr) < 0)
|
||||
return -1;
|
||||
|
||||
if (seaf_fs_manager_init (session->fs_mgr) < 0)
|
||||
return -1;
|
||||
|
||||
if (seaf_branch_manager_init (session->branch_mgr) < 0)
|
||||
return -1;
|
||||
|
||||
if (seaf_repo_manager_init (session->repo_mgr) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
seafile_session_start (SeafileSession *session)
|
||||
{
|
||||
return 0;
|
||||
}
|
52
fuse/seafile-session.h
Normal file
52
fuse/seafile-session.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
#ifndef SEAFILE_SESSION_H
|
||||
#define SEAFILE_SESSION_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include <seaf-db.h>
|
||||
|
||||
#include "block-mgr.h"
|
||||
#include "fs-mgr.h"
|
||||
#include "branch-mgr.h"
|
||||
#include "commit-mgr.h"
|
||||
#include "repo-mgr.h"
|
||||
|
||||
struct _CcnetClient;
|
||||
|
||||
typedef struct _SeafileSession SeafileSession;
|
||||
|
||||
struct CcnetClientPool;
|
||||
|
||||
struct _SeafileSession {
|
||||
struct _CcnetClient *session;
|
||||
|
||||
char *seaf_dir;
|
||||
char *tmp_file_dir;
|
||||
/* Config that's only loaded on start */
|
||||
GKeyFile *config;
|
||||
SeafDB *db;
|
||||
|
||||
struct CcnetClientPool *client_pool;
|
||||
|
||||
SeafBlockManager *block_mgr;
|
||||
SeafFSManager *fs_mgr;
|
||||
SeafBranchManager *branch_mgr;
|
||||
SeafCommitManager *commit_mgr;
|
||||
SeafRepoManager *repo_mgr;
|
||||
};
|
||||
|
||||
extern SeafileSession *seaf;
|
||||
|
||||
SeafileSession *
|
||||
seafile_session_new(const char *central_config_dir,
|
||||
const char *seafile_dir,
|
||||
struct _CcnetClient *ccnet_session);
|
||||
|
||||
int
|
||||
seafile_session_init (SeafileSession *session);
|
||||
|
||||
int
|
||||
seafile_session_start (SeafileSession *session);
|
||||
|
||||
#endif
|
6
include/Makefile.am
Normal file
6
include/Makefile.am
Normal file
|
@ -0,0 +1,6 @@
|
|||
|
||||
seafiledir = $(includedir)/seafile
|
||||
|
||||
noinst_HEADERS = seafile-error.h
|
||||
|
||||
seafile_HEADERS = seafile-rpc.h monitor-rpc.h seafile.h
|
13
include/monitor-rpc.h
Normal file
13
include/monitor-rpc.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef MONITOR_RPC_H
|
||||
#define MONITOR_RPC_H
|
||||
|
||||
/**
|
||||
* monitor_compute_repo_size:
|
||||
* @repo_id: repo id
|
||||
*
|
||||
* Returns 0 if successfully scheduled computation.
|
||||
*/
|
||||
int
|
||||
monitor_compute_repo_size (const char *repo_id, GError **error);
|
||||
|
||||
#endif
|
22
include/seafile-error.h
Normal file
22
include/seafile-error.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#ifndef SEAFILE_ERROR_H
|
||||
#define SEAFILE_ERROR_H
|
||||
|
||||
#define SEAF_ERR_GENERAL 500
|
||||
#define SEAF_ERR_BAD_REPO 501
|
||||
#define SEAF_ERR_BAD_COMMIT 502
|
||||
#define SEAF_ERR_BAD_ARGS 503
|
||||
#define SEAF_ERR_INTERNAL 504
|
||||
#define SEAF_ERR_BAD_FILE 505
|
||||
#define SEAF_ERR_BAD_RELAY 506
|
||||
#define SEAF_ERR_LIST_COMMITS 507
|
||||
#define SEAF_ERR_REPO_AUTH 508
|
||||
#define SEAF_ERR_GC_NOT_STARTED 509
|
||||
#define SEAF_ERR_MONITOR_NOT_CONNECTED 510
|
||||
#define SEAF_ERR_BAD_DIR_ID 511
|
||||
#define SEAF_ERR_NO_WORKTREE 512
|
||||
#define SEAF_ERR_BAD_PEER_ID 513
|
||||
#define SEAF_ERR_REPO_LOCKED 514
|
||||
#define SEAF_ERR_DIR_MISSING 515
|
||||
#define SEAF_ERR_PATH_NO_EXIST 516 /* the dir or file pointed by this path not exists */
|
||||
|
||||
#endif
|
1029
include/seafile-rpc.h
Normal file
1029
include/seafile-rpc.h
Normal file
File diff suppressed because it is too large
Load diff
187
include/seafile.h
Normal file
187
include/seafile.h
Normal file
|
@ -0,0 +1,187 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef SEAFILE_H
|
||||
#define SEAFILE_H
|
||||
|
||||
char *
|
||||
seafile_create_repo (SearpcClient *client,
|
||||
const gchar *name,
|
||||
const gchar *description,
|
||||
const gchar *worktree,
|
||||
const gchar *passwd,
|
||||
const gchar *relay_id,
|
||||
int keep_local_history, GError **error);
|
||||
|
||||
int
|
||||
seafile_create_repo_async (SearpcClient *client,
|
||||
const gchar *name,
|
||||
const gchar *description,
|
||||
const gchar *worktree,
|
||||
const gchar *passwd,
|
||||
const gchar *relay_id,
|
||||
int keep_local_history,
|
||||
AsyncCallback callback, void *user_data);
|
||||
|
||||
int seafile_destroy_repo (SearpcClient *client,
|
||||
const char *repo_id, GError **error);
|
||||
|
||||
int seafile_set_repo_token (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
const char *token,
|
||||
GError **error);
|
||||
|
||||
char *
|
||||
seafile_get_repo_token (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
GError **error);
|
||||
|
||||
|
||||
int
|
||||
seafile_set_repo_property (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
const char *key,
|
||||
const char *value,
|
||||
GError **error);
|
||||
|
||||
GList *
|
||||
seafile_get_repo_list (SearpcClient *client,
|
||||
int offset,
|
||||
int limit, GError **error);
|
||||
|
||||
GObject *
|
||||
seafile_get_repo (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
GError **error);
|
||||
|
||||
|
||||
char *seafile_get_config (SearpcClient *client, const char *key, GError **error);
|
||||
|
||||
int seafile_get_config_async (SearpcClient *client, const char *key,
|
||||
AsyncCallback callback, void *user_data);
|
||||
|
||||
int seafile_set_config_async (SearpcClient *client,
|
||||
const char *key, const char *value,
|
||||
AsyncCallback callback, void *user_data);
|
||||
|
||||
int seafile_calc_dir_size (SearpcClient *client, const char *path, GError **error);
|
||||
|
||||
|
||||
/* server */
|
||||
int seafile_add_chunk_server (SearpcClient *client, const char *server_id,
|
||||
GError **error);
|
||||
int seafile_del_chunk_server (SearpcClient *client, const char *server_id,
|
||||
GError **error);
|
||||
char *seafile_list_chunk_servers (SearpcClient *client, GError **error);
|
||||
|
||||
char *
|
||||
seafile_repo_query_access_property (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
GError **error);
|
||||
|
||||
GObject *
|
||||
seafile_web_query_access_token (SearpcClient *client,
|
||||
const char *token,
|
||||
GError **error);
|
||||
|
||||
GObject *
|
||||
seafile_get_decrypt_key (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
const char *user,
|
||||
GError **error);
|
||||
|
||||
char *
|
||||
seafile_put_file (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
const char *file_path,
|
||||
const char *parent_dir,
|
||||
const char *file_name,
|
||||
const char *user,
|
||||
const char *head_id,
|
||||
GError **error);
|
||||
|
||||
char *
|
||||
seafile_put_file_blocks (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
const char *parent_dir,
|
||||
const char *file_name,
|
||||
const char *blockids_json,
|
||||
const char *paths_json,
|
||||
const char *user,
|
||||
const char *head_id,
|
||||
gint64 file_size,
|
||||
GError **error);
|
||||
|
||||
|
||||
int
|
||||
seafile_post_file (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
const char *file_path,
|
||||
const char *parent_dir,
|
||||
const char *file_name,
|
||||
const char *user,
|
||||
GError **error);
|
||||
|
||||
#define POST_FILE_ERR_FILENAME 401
|
||||
#define POST_FILE_ERR_BLOCK_MISSING 402
|
||||
|
||||
char *
|
||||
seafile_post_file_blocks (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
const char *parent_dir,
|
||||
const char *file_name,
|
||||
const char *blockids_json,
|
||||
const char *paths_json,
|
||||
const char *user,
|
||||
gint64 file_size,
|
||||
int replace_existed,
|
||||
GError **error);
|
||||
|
||||
char *
|
||||
seafile_post_multi_files (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
const char *parent_dir,
|
||||
const char *filenames_json,
|
||||
const char *paths_json,
|
||||
const char *user,
|
||||
int replace_existed,
|
||||
GError **error);
|
||||
|
||||
int
|
||||
seafile_set_user_quota (SearpcClient *client,
|
||||
const char *user,
|
||||
gint64 quota,
|
||||
GError **error);
|
||||
|
||||
int
|
||||
seafile_set_org_quota (SearpcClient *client,
|
||||
int org_id,
|
||||
gint64 quota,
|
||||
GError **error);
|
||||
|
||||
int
|
||||
seafile_set_org_user_quota (SearpcClient *client,
|
||||
int org_id,
|
||||
const char *user,
|
||||
gint64 quota,
|
||||
GError **error);
|
||||
|
||||
int
|
||||
seafile_check_quota (SearpcClient *client,
|
||||
const char *repo_id,
|
||||
GError **error);
|
||||
|
||||
int
|
||||
seafile_disable_auto_sync_async (SearpcClient *client,
|
||||
AsyncCallback callback,
|
||||
void *user_data);
|
||||
int
|
||||
seafile_enable_auto_sync_async (SearpcClient *client,
|
||||
AsyncCallback callback,
|
||||
void *user_data);
|
||||
|
||||
int
|
||||
seafile_is_auto_sync_enabled_async (SearpcClient *client,
|
||||
AsyncCallback callback,
|
||||
void *user_data);
|
||||
|
||||
#endif
|
3
integration-tests/README.md
Normal file
3
integration-tests/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
### Seafile Integration Tests
|
||||
|
||||
The purpose of integration tests is to build a seafile release package and run tests against it.
|
259
integration-tests/autosetup.py
Executable file
259
integration-tests/autosetup.py
Executable file
|
@ -0,0 +1,259 @@
|
|||
#!/usr/bin/env python
|
||||
#coding: UTF-8
|
||||
|
||||
import os
|
||||
from os.path import abspath, basename, exists, dirname, join
|
||||
import sys
|
||||
import argparse
|
||||
import re
|
||||
from collections import namedtuple
|
||||
|
||||
import requests
|
||||
from pexpect import spawn
|
||||
|
||||
from utils import green, red, debug, info, warning, cd, shell, chdir, setup_logging
|
||||
|
||||
USERNAME = 'test@seafiletest.com'
|
||||
PASSWORD = 'testtest'
|
||||
ADMIN_USERNAME = 'admin@seafiletest.com'
|
||||
ADMIN_PASSWORD = 'adminadmin'
|
||||
MYSQL_ROOT_PASSWD = 's123'
|
||||
|
||||
ServerConfig = namedtuple('ServerConfig', [
|
||||
'installdir',
|
||||
'tarball',
|
||||
'version',
|
||||
'initmode',
|
||||
])
|
||||
|
||||
|
||||
def setup_server(cfg, db):
|
||||
'''Setup seafile server with the setup-seafile.sh script. We use pexpect to
|
||||
interactive with the setup process of the script.
|
||||
'''
|
||||
info('uncompressing server tarball')
|
||||
shell('tar xf seafile-server_{}_x86-64.tar.gz -C {}'
|
||||
.format(cfg.version, cfg.installdir))
|
||||
if db == 'mysql':
|
||||
autosetup_mysql(cfg)
|
||||
else:
|
||||
autosetup_sqlite3(cfg)
|
||||
|
||||
with open(join(cfg.installdir, 'conf/seahub_settings.py'), 'a') as fp:
|
||||
fp.write('\n')
|
||||
fp.write('DEBUG = True')
|
||||
fp.write('\n')
|
||||
fp.write('''\
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_THROTTLE_RATES': {
|
||||
'ping': '600/minute',
|
||||
'anon': '1000/minute',
|
||||
'user': '1000/minute',
|
||||
},
|
||||
}''')
|
||||
fp.write('\n')
|
||||
|
||||
|
||||
def autosetup_sqlite3(cfg):
|
||||
setup_script = get_script(cfg, 'setup-seafile.sh')
|
||||
shell('''sed -i -e '/^check_root;.*/d' "{}"'''.format(setup_script))
|
||||
|
||||
if cfg.initmode == 'prompt':
|
||||
setup_sqlite3_prompt(setup_script)
|
||||
else:
|
||||
setup_sqlite3_auto(setup_script)
|
||||
|
||||
def setup_sqlite3_prompt(setup_script):
|
||||
info('setting up seafile server with pexepct, script %s', setup_script)
|
||||
answers = [
|
||||
('ENTER', ''),
|
||||
# server name
|
||||
('server name', 'my-seafile'),
|
||||
# ip or domain
|
||||
('ip or domain', '127.0.0.1'),
|
||||
# seafile data dir
|
||||
('seafile-data', ''),
|
||||
# fileserver port
|
||||
('seafile fileserver', ''),
|
||||
('ENTER', ''),
|
||||
('ENTER', ''),
|
||||
]
|
||||
_answer_questions(setup_script, answers)
|
||||
|
||||
def setup_sqlite3_auto(setup_script):
|
||||
info('setting up seafile server in auto mode, script %s', setup_script)
|
||||
env = os.environ.copy()
|
||||
env['SERVER_IP'] = '127.0.0.1'
|
||||
shell('%s auto -n my-seafile' % setup_script, env=env)
|
||||
|
||||
def createdbs():
|
||||
sql = '''\
|
||||
create database `ccnet-existing` character set = 'utf8';
|
||||
create database `seafile-existing` character set = 'utf8';
|
||||
create database `seahub-existing` character set = 'utf8';
|
||||
|
||||
create user 'seafile'@'localhost' identified by 'seafile';
|
||||
|
||||
GRANT ALL PRIVILEGES ON `ccnet-existing`.* to `seafile`@localhost;
|
||||
GRANT ALL PRIVILEGES ON `seafile-existing`.* to `seafile`@localhost;
|
||||
GRANT ALL PRIVILEGES ON `seahub-existing`.* to `seafile`@localhost;
|
||||
'''
|
||||
|
||||
shell('mysql -u root -p%s' % MYSQL_ROOT_PASSWD, inputdata=sql)
|
||||
|
||||
|
||||
def autosetup_mysql(cfg):
|
||||
setup_script = get_script(cfg, 'setup-seafile-mysql.sh')
|
||||
if not exists(setup_script):
|
||||
print 'please specify seafile script path'
|
||||
|
||||
if cfg.initmode == 'prompt':
|
||||
createdbs()
|
||||
setup_mysql_prompt(setup_script)
|
||||
else :
|
||||
# in auto mode, test create new db
|
||||
setup_mysql_auto(setup_script)
|
||||
|
||||
def setup_mysql_prompt(setup_script):
|
||||
info('setting up seafile server with pexepct, script %s', setup_script)
|
||||
answers = [
|
||||
('ENTER', ''),
|
||||
# server name
|
||||
('server name', 'my-seafile'),
|
||||
# ip or domain
|
||||
('ip or domain', '127.0.0.1'),
|
||||
# seafile data dir
|
||||
('seafile-data', ''),
|
||||
# fileserver port
|
||||
('seafile fileserver', ''),
|
||||
# use existing
|
||||
('choose a way to initialize seafile databases', '2'),
|
||||
('host of mysql server', ''),
|
||||
('port of mysql server', ''),
|
||||
('Which mysql user', 'seafile'),
|
||||
('password for mysql user', 'seafile'),
|
||||
('ccnet database', 'ccnet-existing'),
|
||||
('seafile database', 'seafile-existing'),
|
||||
('seahub database', 'seahub-existing'),
|
||||
('ENTER', ''),
|
||||
]
|
||||
_answer_questions(abspath(setup_script), answers)
|
||||
|
||||
def setup_mysql_auto(setup_script):
|
||||
info('setting up seafile server in auto mode, script %s', setup_script)
|
||||
env = os.environ.copy()
|
||||
env['MYSQL_USER'] = 'seafile-new'
|
||||
env['MYSQL_USER_PASSWD'] = 'seafile'
|
||||
env['MYSQL_ROOT_PASSWD']= MYSQL_ROOT_PASSWD
|
||||
env['CCNET_DB'] = 'ccnet-new'
|
||||
env['SEAFILE_DB'] = 'seafile-new'
|
||||
env['SEAHUB_DB'] = 'seahub-new'
|
||||
shell('%s auto -n my-seafile -e 0' % setup_script, env=env)
|
||||
|
||||
def start_server(cfg):
|
||||
with cd(cfg.installdir):
|
||||
shell('find . -maxdepth 2 | sort | xargs ls -lhd')
|
||||
seafile_sh = get_script(cfg, 'seafile.sh')
|
||||
shell('{} start'.format(seafile_sh))
|
||||
|
||||
info('starting seahub')
|
||||
seahub_sh = get_script(cfg, 'seahub.sh')
|
||||
answers = [
|
||||
# admin email/pass
|
||||
('admin email', ADMIN_USERNAME),
|
||||
('admin password', ADMIN_PASSWORD),
|
||||
('admin password again', ADMIN_PASSWORD),
|
||||
]
|
||||
_answer_questions('{} start'.format(abspath(seahub_sh)), answers)
|
||||
with cd(cfg.installdir):
|
||||
shell('find . -maxdepth 2 | sort | xargs ls -lhd')
|
||||
# shell('sqlite3 ccnet/PeerMgr/usermgr.db "select * from EmailUser"', cwd=INSTALLDIR)
|
||||
shell('http -v localhost:8000/api2/server-info/ || true')
|
||||
# shell('http -v -f POST localhost:8000/api2/auth-token/ username=admin@seafiletest.com password=adminadmin || true')
|
||||
shell('netstat -nltp')
|
||||
|
||||
|
||||
def _answer_questions(cmd, answers):
|
||||
info('expect: spawing %s', cmd)
|
||||
child = spawn(cmd)
|
||||
child.logfile = sys.stdout
|
||||
|
||||
def autofill(pattern, line):
|
||||
child.expect(pattern)
|
||||
child.sendline(line)
|
||||
|
||||
for k, v in answers:
|
||||
autofill(k, v)
|
||||
child.sendline('')
|
||||
child.logfile = None
|
||||
child.interact()
|
||||
|
||||
|
||||
def get_script(cfg, path):
|
||||
"""
|
||||
:type cfg: ServerConfig
|
||||
"""
|
||||
return join(server_dir(cfg), path)
|
||||
|
||||
|
||||
def server_dir(cfg):
|
||||
"""
|
||||
:type cfg: ServerConfig
|
||||
"""
|
||||
return join(cfg.installdir, 'seafile-server-{}'.format(cfg.version))
|
||||
|
||||
|
||||
def apiurl(path):
|
||||
path = path.lstrip('/')
|
||||
root = os.environ.get('SEAFILE_SERVER', 'http://127.0.0.1:8000')
|
||||
return '{}/api2/{}'.format(root, path)
|
||||
|
||||
|
||||
def create_test_user(cfg):
|
||||
data = {'username': ADMIN_USERNAME, 'password': ADMIN_PASSWORD, }
|
||||
res = requests.post(apiurl('/auth-token/'), data=data)
|
||||
debug('%s %s', res.status_code, res.text)
|
||||
token = res.json()['token']
|
||||
data = {'password': PASSWORD, }
|
||||
headers = {'Authorization': 'Token ' + token}
|
||||
res = requests.put(
|
||||
apiurl('/accounts/{}/'.format(USERNAME)),
|
||||
data=data,
|
||||
headers=headers)
|
||||
assert res.status_code == 201
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument('-v', '--verbose', action='store_true')
|
||||
ap.add_argument('--db', choices=('sqlite3', 'mysql'), default='sqlite3')
|
||||
ap.add_argument('installdir')
|
||||
ap.add_argument('tarball')
|
||||
args = ap.parse_args()
|
||||
|
||||
if not exists(args.installdir):
|
||||
print 'directory {} does not exist'.format(args.installdir)
|
||||
sys.exit(1)
|
||||
|
||||
if os.listdir(args.installdir):
|
||||
print 'directory {} is not empty'.format(args.installdir)
|
||||
sys.exit(1)
|
||||
|
||||
if not exists(args.tarball):
|
||||
print 'file {} does not exist'.format(args.tarball)
|
||||
sys.exit(1)
|
||||
|
||||
m = re.match(r'^.*?_([\d\.]+).*?\.tar\.gz$', basename(args.tarball))
|
||||
version = m.group(1)
|
||||
|
||||
cfg = ServerConfig(installdir=args.installdir,
|
||||
tarball=args.tarball,
|
||||
version=version)
|
||||
setup_server(cfg, args.db)
|
||||
start_server(cfg)
|
||||
create_test_user(cfg)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
setup_logging()
|
||||
main()
|
72
integration-tests/install-deps.sh
Executable file
72
integration-tests/install-deps.sh
Executable file
|
@ -0,0 +1,72 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e -x
|
||||
|
||||
pip install http://effbot.org/media/downloads/PIL-1.1.7.tar.gz
|
||||
pip install -r ./integration-tests/requirements.txt
|
||||
|
||||
pushd $HOME
|
||||
|
||||
# download precompiled libevhtp
|
||||
libevhtp_bin=libevhtp-bin_1.2.0.tar.gz
|
||||
wget https://dl.bintray.com/lins05/generic/libevhtp-bin/$libevhtp_bin
|
||||
tar xf $libevhtp_bin
|
||||
find $HOME/opt
|
||||
|
||||
# download precompiled libzdb
|
||||
# zdb_bin=libzdb-bin_2.11.1.tar.gz
|
||||
# wget https://dl.bintray.com/lins05/generic/libzdb-bin/$zdb_bin
|
||||
# tar xf $zdb_bin
|
||||
# sed -i -e "s|prefix=/opt/local|prefix=$HOME/opt/local|g" $HOME/opt/local/lib/pkgconfig/zdb.pc
|
||||
# find $HOME/opt
|
||||
pushd /tmp/
|
||||
git clone --depth=1 https://github.com/haiwen/libzdb.git
|
||||
cd libzdb
|
||||
./bootstrap
|
||||
./configure --prefix=$HOME/opt/local
|
||||
make -j2
|
||||
make install
|
||||
popd
|
||||
|
||||
# download seahub thirdpart python libs
|
||||
WGET="wget --no-check-certificate"
|
||||
downloads=$HOME/downloads
|
||||
thirdpart=$HOME/thirdpart
|
||||
|
||||
mkdir -p $downloads $thirdpart
|
||||
cd $thirdpart
|
||||
save_pythonpath=$PYTHONPATH
|
||||
export PYTHONPATH=.
|
||||
urls=(
|
||||
https://pypi.python.org/packages/source/p/pytz/pytz-2016.1.tar.gz
|
||||
https://www.djangoproject.com/m/releases/1.8/Django-1.8.10.tar.gz
|
||||
https://pypi.python.org/packages/source/d/django-statici18n/django-statici18n-1.1.3.tar.gz
|
||||
https://pypi.python.org/packages/source/d/djangorestframework/djangorestframework-3.3.2.tar.gz
|
||||
https://pypi.python.org/packages/source/d/django_compressor/django_compressor-1.4.tar.gz
|
||||
|
||||
https://pypi.python.org/packages/source/j/jsonfield/jsonfield-1.0.3.tar.gz
|
||||
https://pypi.python.org/packages/source/d/django-post_office/django-post_office-2.0.6.tar.gz
|
||||
|
||||
http://pypi.python.org/packages/source/g/gunicorn/gunicorn-19.4.5.tar.gz
|
||||
http://pypi.python.org/packages/source/f/flup/flup-1.0.2.tar.gz
|
||||
https://pypi.python.org/packages/source/c/chardet/chardet-2.3.0.tar.gz
|
||||
https://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz
|
||||
https://pypi.python.org/packages/source/s/six/six-1.9.0.tar.gz
|
||||
|
||||
https://pypi.python.org/packages/source/d/django-picklefield/django-picklefield-0.3.2.tar.gz
|
||||
https://pypi.python.org/packages/source/d/django-constance/django-constance-1.0.1.tar.gz
|
||||
|
||||
https://pypi.python.org/packages/source/j/jdcal/jdcal-1.2.tar.gz
|
||||
https://pypi.python.org/packages/source/e/et_xmlfile/et_xmlfile-1.0.1.tar.gz
|
||||
https://pypi.python.org/packages/source/o/openpyxl/openpyxl-2.3.0.tar.gz
|
||||
)
|
||||
for url in ${urls[*]}; do
|
||||
path="${downloads}/$(basename $url)"
|
||||
if [[ ! -e $path ]]; then
|
||||
$WGET -O $path $url
|
||||
fi
|
||||
easy_install -d . $path
|
||||
done
|
||||
export PYTHONPATH=$save_pythonpath
|
||||
|
||||
popd
|
7
integration-tests/requirements.txt
Normal file
7
integration-tests/requirements.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
termcolor==1.1.0
|
||||
prettytable==0.7.2
|
||||
pexpect==4.0
|
||||
requests==2.8.0
|
||||
httpie
|
||||
django-constance[database]
|
||||
MySQL-python==1.2.5
|
299
integration-tests/run.py
Executable file
299
integration-tests/run.py
Executable file
|
@ -0,0 +1,299 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import os
|
||||
from os.path import abspath, basename, exists, expanduser, join
|
||||
import sys
|
||||
import re
|
||||
import glob
|
||||
import json
|
||||
import logging
|
||||
import requests
|
||||
|
||||
import termcolor
|
||||
from pexpect import spawn
|
||||
from utils import green, red, debug, info, warning, cd, shell, chdir, setup_logging
|
||||
from autosetup import (setup_server, ServerConfig, get_script, server_dir,
|
||||
start_server, create_test_user, MYSQL_ROOT_PASSWD)
|
||||
|
||||
TOPDIR = abspath(join(os.getcwd(), '..'))
|
||||
PREFIX = expanduser('~/opt/local')
|
||||
SRCDIR = '/tmp/src'
|
||||
INSTALLDIR = '/tmp/haiwen'
|
||||
THIRDPARTDIR = expanduser('~/thirdpart')
|
||||
|
||||
logger = logging.getLogger(__file__)
|
||||
seafile_version = ''
|
||||
|
||||
TRAVIS_BRANCH = os.environ.get('TRAVIS_BRANCH', 'master')
|
||||
|
||||
|
||||
def make_build_env():
|
||||
env = dict(os.environ)
|
||||
libsearpc_dir = abspath(join(TOPDIR, 'libsearpc'))
|
||||
ccnet_dir = abspath(join(TOPDIR, 'ccnet'))
|
||||
|
||||
def _env_add(*a, **kw):
|
||||
kw['env'] = env
|
||||
return prepend_env_value(*a, **kw)
|
||||
|
||||
_env_add('CPPFLAGS', '-I%s' % join(PREFIX, 'include'), seperator=' ')
|
||||
|
||||
_env_add('LDFLAGS', '-L%s' % os.path.join(PREFIX, 'lib'), seperator=' ')
|
||||
|
||||
_env_add('LDFLAGS', '-L%s' % os.path.join(PREFIX, 'lib64'), seperator=' ')
|
||||
|
||||
_env_add('PATH', os.path.join(PREFIX, 'bin'))
|
||||
_env_add('PATH', THIRDPARTDIR)
|
||||
_env_add('PKG_CONFIG_PATH', os.path.join(PREFIX, 'lib', 'pkgconfig'))
|
||||
_env_add('PKG_CONFIG_PATH', os.path.join(PREFIX, 'lib64', 'pkgconfig'))
|
||||
_env_add('PKG_CONFIG_PATH', libsearpc_dir)
|
||||
_env_add('PKG_CONFIG_PATH', ccnet_dir)
|
||||
|
||||
for key in ('PATH', 'PKG_CONFIG_PATH', 'CPPFLAGS', 'LDFLAGS',
|
||||
'PYTHONPATH'):
|
||||
info('%s: %s', key, env.get(key, ''))
|
||||
return env
|
||||
|
||||
|
||||
def prepend_env_value(name, value, seperator=':', env=None):
|
||||
'''append a new value to a list'''
|
||||
env = env or os.environ
|
||||
current_value = env.get(name, '')
|
||||
new_value = value
|
||||
if current_value:
|
||||
new_value += seperator + current_value
|
||||
|
||||
env[name] = new_value
|
||||
return env
|
||||
|
||||
|
||||
def get_project_branch(project, default_branch='master'):
|
||||
if project.name == 'seafile':
|
||||
return TRAVIS_BRANCH
|
||||
conf = json.loads(requests.get(
|
||||
'https://raw.githubusercontent.com/haiwen/seafile-test-deploy/master/branches.json').text)
|
||||
return conf.get(TRAVIS_BRANCH, {}).get(project.name,
|
||||
default_branch)
|
||||
|
||||
|
||||
class Project(object):
|
||||
configure_cmd = './configure'
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.version = ''
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return 'https://www.github.com/haiwen/{}.git'.format(self.name)
|
||||
|
||||
@property
|
||||
def projectdir(self):
|
||||
return join(TOPDIR, self.name)
|
||||
|
||||
@property
|
||||
def branch(self):
|
||||
return get_project_branch(self)
|
||||
|
||||
def clone(self):
|
||||
if exists(self.name):
|
||||
with cd(self.name):
|
||||
shell('git fetch origin --tags')
|
||||
else:
|
||||
shell('git clone --depth=1 --branch {} {}'.format(self.branch,
|
||||
self.url))
|
||||
|
||||
@chdir
|
||||
def make_dist(self):
|
||||
info('making tarball for %s', self.name)
|
||||
if exists('./autogen.sh'):
|
||||
shell('./autogen.sh')
|
||||
shell(self.configure_cmd, env=make_build_env())
|
||||
shell('make dist')
|
||||
|
||||
@chdir
|
||||
def copy_dist(self):
|
||||
self.make_dist()
|
||||
tarball = glob.glob('*.tar.gz')[0]
|
||||
info('copying %s to %s', tarball, SRCDIR)
|
||||
shell('cp {} {}'.format(tarball, SRCDIR))
|
||||
m = re.match('{}-(.*).tar.gz'.format(self.name), basename(tarball))
|
||||
if m:
|
||||
self.version = m.group(1)
|
||||
|
||||
@chdir
|
||||
def use_branch(self, branch):
|
||||
shell('git checkout {}'.format(branch))
|
||||
|
||||
|
||||
class Ccnet(Project):
|
||||
def __init__(self):
|
||||
super(Ccnet, self).__init__('ccnet')
|
||||
|
||||
|
||||
class Seafile(Project):
|
||||
configure_cmd = './configure --enable-client --enable-server'
|
||||
|
||||
def __init__(self):
|
||||
super(Seafile, self).__init__('seafile')
|
||||
|
||||
@chdir
|
||||
def copy_dist(self):
|
||||
super(Seafile, self).copy_dist()
|
||||
global seafile_version
|
||||
seafile_version = self.version
|
||||
|
||||
|
||||
class Seahub(Project):
|
||||
def __init__(self):
|
||||
super(Seahub, self).__init__('seahub')
|
||||
|
||||
@chdir
|
||||
def make_dist(self):
|
||||
cmds = [
|
||||
# 'git add -f media/css/*.css',
|
||||
# 'git commit -a -m "%s"' % msg,
|
||||
'./tools/gen-tarball.py --version={} --branch=HEAD >/dev/null'
|
||||
.format(seafile_version),
|
||||
]
|
||||
for cmd in cmds:
|
||||
shell(cmd, env=make_build_env())
|
||||
|
||||
|
||||
class SeafDAV(Project):
|
||||
def __init__(self):
|
||||
super(SeafDAV, self).__init__('seafdav')
|
||||
|
||||
@chdir
|
||||
def make_dist(self):
|
||||
shell('make')
|
||||
|
||||
|
||||
class SeafObj(Project):
|
||||
def __init__(self):
|
||||
super(SeafObj, self).__init__('seafobj')
|
||||
|
||||
@chdir
|
||||
def make_dist(self):
|
||||
shell('make dist')
|
||||
|
||||
|
||||
def build_server(libsearpc, ccnet, seafile):
|
||||
cmd = [
|
||||
'python',
|
||||
join(TOPDIR, 'seafile/scripts/build/build-server.py'),
|
||||
'--yes',
|
||||
'--version=%s' % seafile.version,
|
||||
'--libsearpc_version=%s' % libsearpc.version,
|
||||
'--ccnet_version=%s' % ccnet.version,
|
||||
'--seafile_version=%s' % seafile.version,
|
||||
'--thirdpartdir=%s' % THIRDPARTDIR,
|
||||
'--srcdir=%s' % SRCDIR,
|
||||
'--jobs=4',
|
||||
]
|
||||
shell(cmd, shell=False, env=make_build_env())
|
||||
|
||||
|
||||
def fetch_and_build():
|
||||
libsearpc = Project('libsearpc')
|
||||
ccnet = Ccnet()
|
||||
seafile = Seafile()
|
||||
seahub = Seahub()
|
||||
seafobj = SeafObj()
|
||||
seafdav = SeafDAV()
|
||||
|
||||
for project in (libsearpc, ccnet, seafile, seahub, seafdav, seafobj):
|
||||
if project.name != 'seafile':
|
||||
project.clone()
|
||||
project.copy_dist()
|
||||
|
||||
build_server(libsearpc, ccnet, seafile)
|
||||
|
||||
|
||||
def run_tests(cfg):
|
||||
# run_python_seafile_tests()
|
||||
# run_seafdav_tests(cfg)
|
||||
# must stop seafile server before running seaf-gc
|
||||
shell('{} stop'.format(get_script(cfg, 'seafile.sh')))
|
||||
shell('{} stop'.format(get_script(cfg, 'seahub.sh')))
|
||||
shell('{} --verbose --rm-deleted'.format(get_script(cfg, 'seaf-gc.sh')))
|
||||
|
||||
|
||||
def run_python_seafile_tests():
|
||||
python_seafile = Project('python-seafile')
|
||||
if not exists(python_seafile.projectdir):
|
||||
python_seafile.clone()
|
||||
shell('pip install -r {}/requirements.txt'.format(
|
||||
python_seafile.projectdir))
|
||||
|
||||
with cd(python_seafile.projectdir):
|
||||
# install python-seafile because seafdav tests needs it
|
||||
shell('python setup.py install')
|
||||
shell('py.test')
|
||||
|
||||
|
||||
def _seafdav_env(cfg):
|
||||
env = dict(os.environ)
|
||||
env['CCNET_CONF_DIR'] = join(INSTALLDIR, 'ccnet')
|
||||
env['SEAFILE_CONF_DIR'] = join(INSTALLDIR, 'seafile-data')
|
||||
env['SEAFILE_CENTRAL_CONF_DIR'] = join(INSTALLDIR, 'conf')
|
||||
for path in glob.glob(join(
|
||||
server_dir(cfg), 'seafile/lib*/python*/*-packages')):
|
||||
prepend_env_value('PYTHONPATH', path, env=env)
|
||||
return env
|
||||
|
||||
|
||||
def run_seafdav_tests(cfg):
|
||||
seafdav = SeafDAV()
|
||||
shell('pip install -r {}/test-requirements.txt'.format(seafdav.projectdir))
|
||||
with cd(seafdav.projectdir):
|
||||
shell('nosetests -v -s', env=_seafdav_env(cfg))
|
||||
|
||||
|
||||
def _mkdirs(*paths):
|
||||
for path in paths:
|
||||
if not exists(path):
|
||||
os.mkdir(path)
|
||||
|
||||
|
||||
def main():
|
||||
_mkdirs(SRCDIR, INSTALLDIR)
|
||||
setup_logging()
|
||||
fetch_and_build()
|
||||
for db in ('sqlite3', 'mysql'):
|
||||
if db == 'mysql':
|
||||
shell('mysqladmin -u root password %s' % MYSQL_ROOT_PASSWD)
|
||||
for i in ('prompt', 'auto'):
|
||||
shell('rm -rf {}/*'.format(INSTALLDIR))
|
||||
setup_and_test(db, i)
|
||||
|
||||
|
||||
def setup_and_test(db, initmode):
|
||||
cfg = ServerConfig(
|
||||
installdir=INSTALLDIR,
|
||||
tarball=join(TOPDIR, 'seafile-server_{}_x86-64.tar.gz'.format(
|
||||
seafile_version)),
|
||||
version=seafile_version,
|
||||
initmode=initmode)
|
||||
info('Setting up seafile server with %s database', db)
|
||||
setup_server(cfg, db)
|
||||
# enable webdav, we're going to seafdav tests later
|
||||
shell('''sed -i -e "s/enabled = false/enabled = true/g" {}'''
|
||||
.format(join(INSTALLDIR, 'conf/seafdav.conf')))
|
||||
try:
|
||||
start_server(cfg)
|
||||
info('Testing seafile server with %s database', db)
|
||||
create_test_user(cfg)
|
||||
run_tests(cfg)
|
||||
except:
|
||||
for logfile in glob.glob('{}/logs/*.log'.format(INSTALLDIR)):
|
||||
shell('echo {0}; cat {0}'.format(logfile))
|
||||
for logfile in glob.glob('{}/seafile-server-{}/runtime/*.log'.format(
|
||||
INSTALLDIR, seafile_version)):
|
||||
shell('echo {0}; cat {0}'.format(logfile))
|
||||
raise
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
os.chdir(TOPDIR)
|
||||
main()
|
81
integration-tests/utils.py
Normal file
81
integration-tests/utils.py
Normal file
|
@ -0,0 +1,81 @@
|
|||
#coding: UTF-8
|
||||
|
||||
import os
|
||||
from os.path import abspath, basename, exists, expanduser, join
|
||||
import sys
|
||||
import re
|
||||
import logging
|
||||
from contextlib import contextmanager
|
||||
from subprocess import Popen, PIPE, CalledProcessError
|
||||
|
||||
import termcolor
|
||||
import requests
|
||||
from pexpect import spawn
|
||||
|
||||
logger = logging.getLogger(__file__)
|
||||
|
||||
def _color(s, color):
|
||||
return s if not os.isatty(sys.stdout.fileno()) \
|
||||
else termcolor.colored(str(s), color)
|
||||
|
||||
|
||||
def green(s):
|
||||
return _color(s, 'green')
|
||||
|
||||
|
||||
def red(s):
|
||||
return _color(s, 'red')
|
||||
|
||||
|
||||
def debug(fmt, *a):
|
||||
logger.debug(green(fmt), *a)
|
||||
|
||||
|
||||
def info(fmt, *a):
|
||||
logger.info(green(fmt), *a)
|
||||
|
||||
|
||||
def warning(fmt, *a):
|
||||
logger.warn(red(fmt), *a)
|
||||
|
||||
|
||||
def shell(cmd, inputdata=None, **kw):
|
||||
info('calling "%s" in %s', cmd, kw.get('cwd', os.getcwd()))
|
||||
kw['shell'] = not isinstance(cmd, list)
|
||||
kw['stdin'] = PIPE if inputdata else None
|
||||
p = Popen(cmd, **kw)
|
||||
if inputdata:
|
||||
p.communicate(inputdata)
|
||||
p.wait()
|
||||
if p.returncode:
|
||||
raise CalledProcessError(p.returncode, cmd)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def cd(path):
|
||||
olddir = os.getcwd()
|
||||
os.chdir(path)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
os.chdir(olddir)
|
||||
|
||||
|
||||
def chdir(func):
|
||||
def wrapped(self, *w, **kw):
|
||||
with cd(self.projectdir):
|
||||
return func(self, *w, **kw)
|
||||
|
||||
return wrapped
|
||||
|
||||
def setup_logging():
|
||||
kw = {
|
||||
'format': '[%(asctime)s][%(module)s]: %(message)s',
|
||||
'datefmt': '%m/%d/%Y %H:%M:%S',
|
||||
'level': logging.DEBUG,
|
||||
'stream': sys.stdout,
|
||||
}
|
||||
|
||||
logging.basicConfig(**kw)
|
||||
logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(
|
||||
logging.WARNING)
|
94
lib/Makefile.am
Normal file
94
lib/Makefile.am
Normal file
|
@ -0,0 +1,94 @@
|
|||
pcfiles = libseafile.pc
|
||||
pkgconfig_DATA = $(pcfiles)
|
||||
pkgconfigdir = $(libdir)/pkgconfig
|
||||
|
||||
AM_CPPFLAGS = @GLIB2_CFLAGS@ -I$(top_srcdir)/include \
|
||||
-I$(top_srcdir)/lib \
|
||||
-I$(top_srcdir)/common \
|
||||
@CCNET_CFLAGS@ \
|
||||
@SEARPC_CFLAGS@ \
|
||||
@MSVC_CFLAGS@ \
|
||||
-Wall
|
||||
|
||||
BUILT_SOURCES = gensource
|
||||
|
||||
## source file rules
|
||||
seafile_object_define = repo.vala commit.vala dirent.vala dir.vala \
|
||||
task.vala branch.vala crypt.vala webaccess.vala copy-task.vala
|
||||
|
||||
seafile_object_gen = $(seafile_object_define:.vala=.c)
|
||||
|
||||
valac_gen = ${seafile_object_gen} seafile-object.h
|
||||
|
||||
EXTRA_DIST = ${seafile_object_define} rpc_table.py $(pcfiles) vala.stamp
|
||||
|
||||
utils_headers = net.h bloom-filter.h utils.h db.h
|
||||
|
||||
utils_srcs = $(utils_headers:.h=.c)
|
||||
|
||||
noinst_HEADERS = ${utils_headers} include.h
|
||||
|
||||
seafiledir = $(includedir)/seafile
|
||||
seafile_HEADERS = seafile-object.h
|
||||
|
||||
seafile-rpc-wrapper.c: seafile-object.h
|
||||
|
||||
seafile-object.h: ${seafile_object_define}
|
||||
rm -f $@
|
||||
valac --pkg posix ${seafile_object_define} -C -H seafile-object.h
|
||||
|
||||
DISTCLEANFILES = ${searpc_gen}
|
||||
|
||||
## library rules
|
||||
lib_LTLIBRARIES = libseafile.la
|
||||
|
||||
libseafile_la_SOURCES = ${seafile_object_gen} seafile-rpc-wrapper.c
|
||||
|
||||
libseafile_la_LDFLAGS = -no-undefined
|
||||
libseafile_la_LIBADD = @GLIB2_LIBS@ @GOBJECT_LIBS@ @SEARPC_LIBS@
|
||||
|
||||
noinst_LTLIBRARIES = libseafile_common.la
|
||||
|
||||
libseafile_common_la_SOURCES = ${seafile_object_gen} ${utils_srcs}
|
||||
libseafile_common_la_LDFLAGS = -no-undefined
|
||||
libseafile_common_la_LIBADD = @GLIB2_LIBS@ @GOBJECT_LIBS@ @SSL_LIBS@ -lcrypto @LIB_GDI32@ \
|
||||
@LIB_UUID@ @LIB_WS32@ @LIB_PSAPI@ -lsqlite3 \
|
||||
@LIBEVENT_LIBS@ @SEARPC_LIBS@ @LIB_SHELL32@ \
|
||||
@ZLIB_LIBS@
|
||||
|
||||
searpc_gen = searpc-signature.h searpc-marshal.h
|
||||
|
||||
gensource: ${searpc_gen} ${valac_gen}
|
||||
|
||||
rpc_table.stamp: ${top_srcdir}/lib/rpc_table.py
|
||||
@rm -f rpc_table.tmp
|
||||
@touch rpc_table.tmp
|
||||
@echo "[libsearpc]: generating rpc header files"
|
||||
@PYTHON@ `which searpc-codegen.py` ${top_srcdir}/lib/rpc_table.py
|
||||
@echo "[libsearpc]: done"
|
||||
@mv -f rpc_table.tmp $@
|
||||
|
||||
${searpc_gen}: rpc_table.stamp
|
||||
|
||||
vala.stamp: ${seafile_object_define}
|
||||
rm -f ${seafile_object_gen}
|
||||
@rm -f vala.tmp
|
||||
@touch vala.tmp
|
||||
valac -C --pkg posix $^
|
||||
@mv -f vala.tmp $@
|
||||
|
||||
${seafile_object_gen}: vala.stamp
|
||||
|
||||
clean-local:
|
||||
rm -f ${searpc_gen}
|
||||
rm -f rpc_table.pyc
|
||||
rm -f rpc_table.stamp
|
||||
rm -f rpc_table.tmp
|
||||
rm -f vala.tmp vala.stamp ${valac_gen}
|
||||
|
||||
install-data-local:
|
||||
if MACOS
|
||||
sed -i '' -e "s|(DESTDIR)|${DESTDIR}|g" $(pcfiles)
|
||||
else
|
||||
${SED} -i "s|(DESTDIR)|${DESTDIR}|g" $(pcfiles)
|
||||
endif
|
176
lib/bloom-filter.c
Normal file
176
lib/bloom-filter.c
Normal file
|
@ -0,0 +1,176 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "bloom-filter.h"
|
||||
|
||||
#define SETBIT(a, n) (a[n/CHAR_BIT] |= (1<<(n%CHAR_BIT)))
|
||||
#define CLEARBIT(a, n) (a[n/CHAR_BIT] &= ~(1<<(n%CHAR_BIT)))
|
||||
#define GETBIT(a, n) (a[n/CHAR_BIT] & (1<<(n%CHAR_BIT)))
|
||||
|
||||
Bloom* bloom_create(size_t size, int k, int counting)
|
||||
{
|
||||
Bloom *bloom;
|
||||
size_t csize = 0;
|
||||
|
||||
if (k <=0 || k > 4) return NULL;
|
||||
|
||||
if ( !(bloom = malloc(sizeof(Bloom))) ) return NULL;
|
||||
if ( !(bloom->a = calloc((size+CHAR_BIT-1)/CHAR_BIT, sizeof(char))) )
|
||||
{
|
||||
free (bloom);
|
||||
return NULL;
|
||||
}
|
||||
if (counting) {
|
||||
csize = size*4;
|
||||
bloom->counters = calloc((csize+CHAR_BIT-1)/CHAR_BIT, sizeof(char));
|
||||
if (!bloom->counters) {
|
||||
free (bloom);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bloom->asize = size;
|
||||
bloom->csize = csize;
|
||||
bloom->k = k;
|
||||
bloom->counting = counting;
|
||||
|
||||
return bloom;
|
||||
}
|
||||
|
||||
int bloom_destroy(Bloom *bloom)
|
||||
{
|
||||
free (bloom->a);
|
||||
if (bloom->counting) free (bloom->counters);
|
||||
free (bloom);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
incr_bit (Bloom *bf, unsigned int bit_idx)
|
||||
{
|
||||
unsigned int char_idx, offset;
|
||||
unsigned char value;
|
||||
unsigned int high;
|
||||
unsigned int low;
|
||||
|
||||
SETBIT (bf->a, bit_idx);
|
||||
|
||||
if (!bf->counting) return;
|
||||
|
||||
char_idx = bit_idx / 2;
|
||||
offset = bit_idx % 2;
|
||||
|
||||
value = bf->counters[char_idx];
|
||||
low = value & 0xF;
|
||||
high = (value & 0xF0) >> 4;
|
||||
|
||||
if (offset == 0) {
|
||||
if (low < 0xF)
|
||||
low++;
|
||||
} else {
|
||||
if (high < 0xF)
|
||||
high++;
|
||||
}
|
||||
value = ((high << 4) | low);
|
||||
|
||||
bf->counters[char_idx] = value;
|
||||
}
|
||||
|
||||
static void
|
||||
decr_bit (Bloom *bf, unsigned int bit_idx)
|
||||
{
|
||||
unsigned int char_idx, offset;
|
||||
unsigned char value;
|
||||
unsigned int high;
|
||||
unsigned int low;
|
||||
|
||||
if (!bf->counting) {
|
||||
CLEARBIT (bf->a, bit_idx);
|
||||
return;
|
||||
}
|
||||
|
||||
char_idx = bit_idx / 2;
|
||||
offset = bit_idx % 2;
|
||||
|
||||
value = bf->counters[char_idx];
|
||||
low = value & 0xF;
|
||||
high = (value & 0xF0) >> 4;
|
||||
|
||||
/* decrement, but once we have reached the max, never go back! */
|
||||
if (offset == 0) {
|
||||
if ((low > 0) && (low < 0xF))
|
||||
low--;
|
||||
if (low == 0) {
|
||||
CLEARBIT (bf->a, bit_idx);
|
||||
}
|
||||
} else {
|
||||
if ((high > 0) && (high < 0xF))
|
||||
high--;
|
||||
if (high == 0) {
|
||||
CLEARBIT (bf->a, bit_idx);
|
||||
}
|
||||
}
|
||||
value = ((high << 4) | low);
|
||||
|
||||
bf->counters[char_idx] = value;
|
||||
}
|
||||
|
||||
int bloom_add(Bloom *bloom, const char *s)
|
||||
{
|
||||
int i;
|
||||
SHA256_CTX c;
|
||||
unsigned char sha256[SHA256_DIGEST_LENGTH];
|
||||
size_t *sha_int = (size_t *)&sha256;
|
||||
|
||||
SHA256_Init(&c);
|
||||
SHA256_Update(&c, s, strlen(s));
|
||||
SHA256_Final (sha256, &c);
|
||||
|
||||
for (i=0; i < bloom->k; ++i)
|
||||
incr_bit (bloom, sha_int[i] % bloom->asize);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bloom_remove(Bloom *bloom, const char *s)
|
||||
{
|
||||
int i;
|
||||
SHA256_CTX c;
|
||||
unsigned char sha256[SHA256_DIGEST_LENGTH];
|
||||
size_t *sha_int = (size_t *)&sha256;
|
||||
|
||||
if (!bloom->counting)
|
||||
return -1;
|
||||
|
||||
SHA256_Init(&c);
|
||||
SHA256_Update(&c, s, strlen(s));
|
||||
SHA256_Final (sha256, &c);
|
||||
|
||||
for (i=0; i < bloom->k; ++i)
|
||||
decr_bit (bloom, sha_int[i] % bloom->asize);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bloom_test(Bloom *bloom, const char *s)
|
||||
{
|
||||
int i;
|
||||
SHA256_CTX c;
|
||||
unsigned char sha256[SHA256_DIGEST_LENGTH];
|
||||
size_t *sha_int = (size_t *)&sha256;
|
||||
|
||||
SHA256_Init(&c);
|
||||
SHA256_Update(&c, s, strlen(s));
|
||||
SHA256_Final (sha256, &c);
|
||||
|
||||
for (i=0; i < bloom->k; ++i)
|
||||
if(!(GETBIT(bloom->a, sha_int[i] % bloom->asize))) return 0;
|
||||
|
||||
return 1;
|
||||
}
|
23
lib/bloom-filter.h
Normal file
23
lib/bloom-filter.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef __BLOOM_H__
|
||||
#define __BLOOM_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
size_t asize;
|
||||
unsigned char *a;
|
||||
size_t csize;
|
||||
unsigned char *counters;
|
||||
int k;
|
||||
char counting:1;
|
||||
} Bloom;
|
||||
|
||||
Bloom *bloom_create (size_t size, int k, int counting);
|
||||
int bloom_destroy (Bloom *bloom);
|
||||
int bloom_add (Bloom *bloom, const char *s);
|
||||
int bloom_remove (Bloom *bloom, const char *s);
|
||||
int bloom_test (Bloom *bloom, const char *s);
|
||||
|
||||
#endif
|
26
lib/branch.vala
Normal file
26
lib/branch.vala
Normal file
|
@ -0,0 +1,26 @@
|
|||
// compile this file with `valac --pkg posix repo.vala -C -H repo.h`
|
||||
|
||||
namespace Seafile {
|
||||
|
||||
public class Branch : Object {
|
||||
|
||||
public string _name;
|
||||
public string name {
|
||||
get { return _name; }
|
||||
set { _name = value; }
|
||||
}
|
||||
|
||||
public string _commit_id;
|
||||
public string commit_id {
|
||||
get { return _commit_id; }
|
||||
set { _commit_id = value; }
|
||||
}
|
||||
|
||||
public string _repo_id;
|
||||
public string repo_id {
|
||||
get { return _repo_id; }
|
||||
set { _repo_id = value; }
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
70
lib/commit.vala
Normal file
70
lib/commit.vala
Normal file
|
@ -0,0 +1,70 @@
|
|||
// compile this file with `valac --pkg posix repo.vala -C -H repo.h`
|
||||
|
||||
namespace Seafile {
|
||||
|
||||
public class Commit : Object {
|
||||
|
||||
// _id is for fast access from c code. id is for
|
||||
// vala to automatically generate a property. Note,
|
||||
// if a Vala property is start with _, it is not
|
||||
// translated into a GObject property.
|
||||
public char _id[41];
|
||||
public string id {
|
||||
get { return (string)_id; }
|
||||
set { Posix.memcpy(_id, value, 40); _id[40] = '\0'; }
|
||||
}
|
||||
|
||||
public string creator_name { get; set; }
|
||||
|
||||
public string _creator; // creator
|
||||
public string creator {
|
||||
get { return _creator; }
|
||||
set { _creator = value; }
|
||||
}
|
||||
|
||||
public string _desc; // description: what does this commit change
|
||||
public string desc {
|
||||
get { return _desc; }
|
||||
set { _desc = value; }
|
||||
}
|
||||
|
||||
public int64 _ctime; // create time
|
||||
public int64 ctime {
|
||||
get { return _ctime; }
|
||||
set { _ctime = value; }
|
||||
}
|
||||
|
||||
public string parent_id { get; set;}
|
||||
|
||||
public string second_parent_id { get; set; }
|
||||
|
||||
public string _repo_id;
|
||||
public string repo_id {
|
||||
get { return _repo_id; }
|
||||
set { _repo_id = value; }
|
||||
}
|
||||
|
||||
|
||||
// A commit point to a file or dir, not both.
|
||||
|
||||
public string _root_id;
|
||||
public string root_id {
|
||||
get { return _root_id; }
|
||||
set { _root_id = value; }
|
||||
}
|
||||
|
||||
// Repo data-format version of this commit
|
||||
public int version { get; set; }
|
||||
public bool new_merge { get; set; }
|
||||
public bool conflict { get; set; }
|
||||
|
||||
// Used for returning file revision
|
||||
public string rev_file_id { get; set; }
|
||||
public int64 rev_file_size { get; set; }
|
||||
// Set if this commit renames a revision of a file
|
||||
public string rev_renamed_old_path { get; set; }
|
||||
|
||||
public string device_name { get; set; }
|
||||
}
|
||||
|
||||
} // namespace
|
16
lib/copy-task.vala
Normal file
16
lib/copy-task.vala
Normal file
|
@ -0,0 +1,16 @@
|
|||
namespace Seafile {
|
||||
|
||||
public class CopyTask : Object {
|
||||
public int64 done { set; get; }
|
||||
public int64 total { set; get; }
|
||||
public bool canceled { set; get; }
|
||||
public bool failed { set; get; }
|
||||
public bool successful { set; get; }
|
||||
}
|
||||
|
||||
public class CopyResult : Object {
|
||||
public bool background { set; get; }
|
||||
public string task_id { set; get; }
|
||||
}
|
||||
|
||||
}
|
8
lib/crypt.vala
Normal file
8
lib/crypt.vala
Normal file
|
@ -0,0 +1,8 @@
|
|||
namespace Seafile {
|
||||
|
||||
public class CryptKey : Object {
|
||||
public string key { set; get; }
|
||||
public string iv { set; get; }
|
||||
}
|
||||
|
||||
}
|
231
lib/db.c
Normal file
231
lib/db.c
Normal file
|
@ -0,0 +1,231 @@
|
|||
|
||||
#include <glib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "db.h"
|
||||
|
||||
int
|
||||
sqlite_open_db (const char *db_path, sqlite3 **db)
|
||||
{
|
||||
int result;
|
||||
const char *errmsg;
|
||||
|
||||
result = sqlite3_open (db_path, db);
|
||||
if (result) {
|
||||
errmsg = sqlite3_errmsg (*db);
|
||||
|
||||
g_warning ("Couldn't open database:'%s', %s\n",
|
||||
db_path, errmsg ? errmsg : "no error given");
|
||||
|
||||
sqlite3_close (*db);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sqlite_close_db (sqlite3 *db)
|
||||
{
|
||||
return sqlite3_close (db);
|
||||
}
|
||||
|
||||
sqlite3_stmt *
|
||||
sqlite_query_prepare (sqlite3 *db, const char *sql)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
int result;
|
||||
|
||||
result = sqlite3_prepare_v2 (db, sql, -1, &stmt, NULL);
|
||||
|
||||
if (result != SQLITE_OK) {
|
||||
const gchar *str = sqlite3_errmsg (db);
|
||||
|
||||
g_warning ("Couldn't prepare query, error:%d->'%s'\n\t%s\n",
|
||||
result, str ? str : "no error given", sql);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
int
|
||||
sqlite_query_exec (sqlite3 *db, const char *sql)
|
||||
{
|
||||
char *errmsg = NULL;
|
||||
int result;
|
||||
|
||||
result = sqlite3_exec (db, sql, NULL, NULL, &errmsg);
|
||||
|
||||
if (result != SQLITE_OK) {
|
||||
if (errmsg != NULL) {
|
||||
g_warning ("SQL error: %d - %s\n:\t%s\n", result, errmsg, sql);
|
||||
sqlite3_free (errmsg);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sqlite_begin_transaction (sqlite3 *db)
|
||||
{
|
||||
char *sql = "BEGIN TRANSACTION;";
|
||||
return sqlite_query_exec (db, sql);
|
||||
}
|
||||
|
||||
int
|
||||
sqlite_end_transaction (sqlite3 *db)
|
||||
{
|
||||
char *sql = "END TRANSACTION;";
|
||||
return sqlite_query_exec (db, sql);
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
sqlite_check_for_existence (sqlite3 *db, const char *sql)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
int result;
|
||||
|
||||
stmt = sqlite_query_prepare (db, sql);
|
||||
if (!stmt)
|
||||
return FALSE;
|
||||
|
||||
result = sqlite3_step (stmt);
|
||||
if (result == SQLITE_ERROR) {
|
||||
const gchar *str = sqlite3_errmsg (db);
|
||||
|
||||
g_warning ("Couldn't execute query, error: %d->'%s'\n",
|
||||
result, str ? str : "no error given");
|
||||
sqlite3_finalize (stmt);
|
||||
return FALSE;
|
||||
}
|
||||
sqlite3_finalize (stmt);
|
||||
|
||||
if (result == SQLITE_ROW)
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int
|
||||
sqlite_foreach_selected_row (sqlite3 *db, const char *sql,
|
||||
SqliteRowFunc callback, void *data)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
int result;
|
||||
int n_rows = 0;
|
||||
|
||||
stmt = sqlite_query_prepare (db, sql);
|
||||
if (!stmt) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
result = sqlite3_step (stmt);
|
||||
if (result != SQLITE_ROW)
|
||||
break;
|
||||
n_rows++;
|
||||
if (!callback (stmt, data))
|
||||
break;
|
||||
}
|
||||
|
||||
if (result == SQLITE_ERROR) {
|
||||
const gchar *s = sqlite3_errmsg (db);
|
||||
|
||||
g_warning ("Couldn't execute query, error: %d->'%s'\n",
|
||||
result, s ? s : "no error given");
|
||||
sqlite3_finalize (stmt);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sqlite3_finalize (stmt);
|
||||
return n_rows;
|
||||
}
|
||||
|
||||
int sqlite_get_int (sqlite3 *db, const char *sql)
|
||||
{
|
||||
int ret = -1;
|
||||
int result;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
if ( !(stmt = sqlite_query_prepare(db, sql)) )
|
||||
return 0;
|
||||
|
||||
result = sqlite3_step (stmt);
|
||||
if (result == SQLITE_ROW) {
|
||||
ret = sqlite3_column_int (stmt, 0);
|
||||
sqlite3_finalize (stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (result == SQLITE_ERROR) {
|
||||
const gchar *str = sqlite3_errmsg (db);
|
||||
g_warning ("Couldn't execute query, error: %d->'%s'\n",
|
||||
result, str ? str : "no error given");
|
||||
sqlite3_finalize (stmt);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gint64 sqlite_get_int64 (sqlite3 *db, const char *sql)
|
||||
{
|
||||
gint64 ret = -1;
|
||||
int result;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
if ( !(stmt = sqlite_query_prepare(db, sql)) )
|
||||
return 0;
|
||||
|
||||
result = sqlite3_step (stmt);
|
||||
if (result == SQLITE_ROW) {
|
||||
ret = sqlite3_column_int64 (stmt, 0);
|
||||
sqlite3_finalize (stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (result == SQLITE_ERROR) {
|
||||
const gchar *str = sqlite3_errmsg (db);
|
||||
g_warning ("Couldn't execute query, error: %d->'%s'\n",
|
||||
result, str ? str : "no error given");
|
||||
sqlite3_finalize (stmt);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *sqlite_get_string (sqlite3 *db, const char *sql)
|
||||
{
|
||||
const char *res = NULL;
|
||||
int result;
|
||||
sqlite3_stmt *stmt;
|
||||
char *ret;
|
||||
|
||||
if ( !(stmt = sqlite_query_prepare(db, sql)) )
|
||||
return NULL;
|
||||
|
||||
result = sqlite3_step (stmt);
|
||||
if (result == SQLITE_ROW) {
|
||||
res = (const char *)sqlite3_column_text (stmt, 0);
|
||||
ret = g_strdup(res);
|
||||
sqlite3_finalize (stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (result == SQLITE_ERROR) {
|
||||
const gchar *str = sqlite3_errmsg (db);
|
||||
g_warning ("Couldn't execute query, error: %d->'%s'\n",
|
||||
result, str ? str : "no error given");
|
||||
sqlite3_finalize (stmt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return NULL;
|
||||
}
|
33
lib/db.h
Normal file
33
lib/db.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef DB_UTILS_H
|
||||
#define DB_UTILS_H
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
int sqlite_open_db (const char *db_path, sqlite3 **db);
|
||||
|
||||
int sqlite_close_db (sqlite3 *db);
|
||||
|
||||
sqlite3_stmt *sqlite_query_prepare (sqlite3 *db, const char *sql);
|
||||
|
||||
int sqlite_query_exec (sqlite3 *db, const char *sql);
|
||||
int sqlite_begin_transaction (sqlite3 *db);
|
||||
int sqlite_end_transaction (sqlite3 *db);
|
||||
|
||||
gboolean sqlite_check_for_existence (sqlite3 *db, const char *sql);
|
||||
|
||||
typedef gboolean (*SqliteRowFunc) (sqlite3_stmt *stmt, void *data);
|
||||
|
||||
int
|
||||
sqlite_foreach_selected_row (sqlite3 *db, const char *sql,
|
||||
SqliteRowFunc callback, void *data);
|
||||
|
||||
int sqlite_get_int (sqlite3 *db, const char *sql);
|
||||
|
||||
gint64 sqlite_get_int64 (sqlite3 *db, const char *sql);
|
||||
|
||||
char *sqlite_get_string (sqlite3 *db, const char *sql);
|
||||
|
||||
|
||||
#endif
|
19
lib/dir.vala
Normal file
19
lib/dir.vala
Normal file
|
@ -0,0 +1,19 @@
|
|||
namespace Seafile {
|
||||
|
||||
public class Dir : Object {
|
||||
|
||||
// _id is for fast access from c code. id is for
|
||||
// vala to automatically generate a property. Note,
|
||||
// if a Vala property is start with _, it is not
|
||||
// translated into a GObject property.
|
||||
public char _id[41];
|
||||
public string id {
|
||||
get { return (string)_id; }
|
||||
set { Posix.memcpy(_id, value, 40); _id[40] = '\0'; }
|
||||
}
|
||||
|
||||
public List<Dirent> entries;
|
||||
public int version { set; get; }
|
||||
}
|
||||
|
||||
} // namespace
|
36
lib/dirent.vala
Normal file
36
lib/dirent.vala
Normal file
|
@ -0,0 +1,36 @@
|
|||
namespace Seafile {
|
||||
|
||||
public class Dirent : Object {
|
||||
|
||||
// _id is for fast access from c code. id is for
|
||||
// vala to automatically generate a property. Note,
|
||||
// if a Vala property is start with _, it is not
|
||||
// translated into a GObject property.
|
||||
public string obj_id { set; get; }
|
||||
|
||||
public string obj_name { set; get; }
|
||||
|
||||
public int mode { set; get; }
|
||||
|
||||
public int version { set; get; }
|
||||
public int64 mtime { set; get; }
|
||||
public int64 size { set; get; }
|
||||
public string modifier { set; get;}
|
||||
|
||||
public string permission { set; get; }
|
||||
|
||||
public bool is_locked { set; get; }
|
||||
public string lock_owner { set; get; }
|
||||
public int64 lock_time { set; get; }
|
||||
|
||||
public bool is_shared { set; get; }
|
||||
}
|
||||
|
||||
public class FileLastModifiedInfo : Object {
|
||||
|
||||
public string file_name { set; get; }
|
||||
|
||||
public int64 last_modified { set; get; }
|
||||
}
|
||||
|
||||
} // namespace
|
18
lib/file.vala
Normal file
18
lib/file.vala
Normal file
|
@ -0,0 +1,18 @@
|
|||
namespace Seafile {
|
||||
|
||||
public class File : Object {
|
||||
|
||||
// _id is for fast access from c code. id is for
|
||||
// vala to automatically generate a property. Note,
|
||||
// if a Vala property is start with _, it is not
|
||||
// translated into a GObject property.
|
||||
public char _id[41];
|
||||
public string id {
|
||||
get { return (string)_id; }
|
||||
set { Posix.memcpy(_id, id, 40); _id[40] = '\0'; }
|
||||
}
|
||||
|
||||
public uint64 size;
|
||||
}
|
||||
|
||||
} // namespace
|
34
lib/include.h
Normal file
34
lib/include.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
|
||||
#include <config.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include <ccnet/valid-check.h>
|
||||
|
||||
#ifndef ccnet_warning
|
||||
#define ccnet_warning(fmt, ...) g_warning( "%s: " fmt, __func__ , ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#ifndef ccnet_error
|
||||
#define ccnet_error(fmt, ...) g_error( "%s: " fmt, __func__ , ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#ifndef ccnet_message
|
||||
#define ccnet_message(fmt, ...) g_message(fmt, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#ifndef ccnet_debug
|
||||
#define ccnet_debug(fmt, ...) g_debug(fmt, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef ENABLE_DEBUG
|
||||
#undef g_debug
|
||||
#define g_debug(...)
|
||||
#endif
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue