backup_file.c revision 9d7d4458d94d0aac0a7edf999368eb18f89cb76a
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen Copyright (C) Simo Sorce 2009
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen This program is free software; you can redistribute it and/or modify
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen it under the terms of the GNU General Public License as published by
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen the Free Software Foundation; either version 3 of the License, or
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen (at your option) any later version.
3a2f2adf5679aa383a2cab09f739d59233cada95Timo Sirainen This program is distributed in the hope that it will be useful,
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen but WITHOUT ANY WARRANTY; without even the implied warranty of
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen GNU General Public License for more details.
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen You should have received a copy of the GNU General Public License
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainen along with this program. If not, see <http://www.gnu.org/licenses/>.
83d0c8152f694ae5b5d1798abda5728dc886102aTimo Sirainenint backup_file(const char *src_file, int dbglvl)
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen DEBUG(dbglvl, ("Error (%d [%s]) opening source file %s\n",
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainen /* try a few times to come up with a new backup file, then give up */
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainen for (i = 0; i < 10; i++) {
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainen if (i == 0) {
d4847b921058734e0668bc7690465c91523d9ec0Martti Rannanjärvi dst_file = talloc_asprintf(tmp_ctx, "%s.bak", src_file);
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainen dst_file = talloc_asprintf(tmp_ctx, "%s.bak%d", src_file, i);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen dst_fd = open(dst_file, O_CREAT|O_EXCL|O_WRONLY, 0600);
f3391d65cc830eab22ca6c5941774de682716edbTimo Sirainen if (dst_fd >= 0) break;
8528048c7b82c745bcfa5a6b1394251b3aaeb129Josef 'Jeff' Sipek DEBUG(dbglvl, ("Error (%d [%s]) opening destination file %s\n",
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen DEBUG(dbglvl, ("Error (%d [%s]) opening destination file %s\n",
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen /* copy file contents */
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen numread = sss_atomic_read_s(src_fd, buf, BUFFER_SIZE);
b056d19b7a07400b897104b146c8768280d24009Timo Sirainen DEBUG(dbglvl, ("Error (%d [%s]) reading from source %s\n",
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen if (numread == 0) break;
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen written = sss_atomic_write_s(dst_fd, buf, numread);
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen DEBUG(dbglvl, ("Error (%d [%s]) writing to destination %s\n",
3a2f2adf5679aa383a2cab09f739d59233cada95Timo Sirainen DEBUG(dbglvl, ("Wrote %d bytes expected %d bytes\n",