summaryrefslogtreecommitdiffstats
path: root/src/locking.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2012-07-16 16:07:01 +0200
committerGravatar Nikias Bassen2012-07-16 16:07:01 +0200
commit4211f24424a4b22b5941a52e8acd988a500488ee (patch)
treeae323f6f0d9131da50547194b1daaecc292d8a01 /src/locking.c
parenteb2db6e2bedefa426fba43cc7a9d47d868a9d897 (diff)
downloadidevicerestore-4211f24424a4b22b5941a52e8acd988a500488ee.tar.gz
idevicerestore-4211f24424a4b22b5941a52e8acd988a500488ee.tar.bz2
ipsw: implemented file locking for on-demand downloading
Diffstat (limited to 'src/locking.c')
-rw-r--r--src/locking.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/locking.c b/src/locking.c
new file mode 100644
index 0000000..dbbbd7c
--- /dev/null
+++ b/src/locking.c
@@ -0,0 +1,116 @@
+/*
+ * locking.c
+ * locking extras
+ *
+ * Copyright (c) 2012 Nikias Bassen. All Rights Reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#ifdef WIN32
+#include <windows.h>
+#else
+#include <errno.h>
+#endif
+
+#include "locking.h"
+#include "common.h"
+
+int lock_file(const char* filename, lock_info_t* lockinfo)
+{
+ if (!lockinfo) {
+ return -1;
+ }
+#ifdef WIN32
+ lockinfo->fp = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (lockinfo->fp == INVALID_HANDLE_VALUE) {
+ debug("ERROR: could not open or create lockfile '%s'\n", filename);
+ return -1;
+ }
+
+ lockinfo->ldata.Offset = 0;
+ lockinfo->ldata.OffsetHigh = 0;
+
+ if (!LockFileEx(lockinfo->fp, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &lockinfo->ldata)) {
+ debug("ERROR: can't lock file, error %d\n", GetLastError());
+ CloseHandle(lockinfo->fp);
+ lockinfo->fp = INVALID_HANDLE_VALUE;
+ return -1;
+ }
+#else
+ lockinfo->fp = fopen(filename, "a+");
+
+ if (!lockinfo->fp) {
+ debug("ERROR: could not open or create lockfile '%s'\n", filename);
+ return -1;
+ }
+
+ lockinfo->ldata.l_type = F_WRLCK;
+ lockinfo->ldata.l_whence = SEEK_SET;
+ lockinfo->ldata.l_start = 0;
+ lockinfo->ldata.l_len = 0;
+
+ if (fcntl(fileno(lockinfo->fp), F_SETLKW, &lockinfo->ldata) < 0) {
+ debug("ERROR: can't lock file, error %d\n", errno);
+ fclose(lockinfo->fp);
+ lockinfo->fp = NULL;
+ return -1;
+ }
+#endif
+ return 0;
+}
+
+int unlock_file(lock_info_t* lockinfo)
+{
+ if (!lockinfo) {
+ return -1;
+ }
+#ifdef WIN32
+ if (lockinfo->fp == INVALID_HANDLE_VALUE) {
+ return -1;
+ }
+
+ lockinfo->ldata.Offset = 0;
+ lockinfo->ldata.OffsetHigh = 0;
+
+ if (!UnlockFileEx(lockinfo->fp, 0, 1, 0, &lockinfo->ldata)) {
+ debug("ERROR: can't unlock file, error %d\n", GetLastError());
+ CloseHandle(lockinfo->fp);
+ lockinfo->fp = INVALID_HANDLE_VALUE;
+ return -1;
+ }
+ CloseHandle(lockinfo->fp);
+ lockinfo->fp = INVALID_HANDLE_VALUE;
+#else
+ if (!lockinfo->fp) {
+ return -1;
+ }
+
+ lockinfo->ldata.l_type = F_UNLCK;
+ lockinfo->ldata.l_whence = SEEK_SET;
+ lockinfo->ldata.l_start = 0;
+ lockinfo->ldata.l_len = 0;
+
+ if (fcntl(fileno(lockinfo->fp), F_SETLK, &lockinfo->ldata) < 0) {
+ debug("ERROR: can't unlock file, error %d\n", errno);
+ fclose(lockinfo->fp);
+ lockinfo->fp = NULL;
+ return -1;
+ }
+ fclose(lockinfo->fp);
+ lockinfo->fp = NULL;
+#endif
+ return 0;
+}
+