<commit_before>/* SCSI commands to USB Mass storage devices port library for Linux
 *
 *   Copyright (c) 2010-2012 <NAME> <<EMAIL>>
 *
 * This program 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 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#define _DEFAULT_SOURCE	/* for flock */

#include "config.h"
#include <gphoto2/gphoto2-port-library.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
# ifndef LOCK_SH
#  define LOCK_SH 1
# endif
# ifndef LOCK_EX
#  define LOCK_EX 2
# endif
# ifndef LOCK_NB
#  define LOCK_NB 4
# endif
# ifndef LOCK_UN
#  define LOCK_UN 4
# endif
#endif
#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_SCSI_SG_H
# include <scsi/sg.h>
#endif

#include <gphoto2/gphoto2-port-result.h>
#include <gphoto2/gphoto2-port-log.h>
#include <gphoto2/gphoto2-port.h>

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (GETTEXT_PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define textdomain(String) (String)
#  define gettext(String) (String)
#  define dgettext(Domain,Message) (Message)
#  define dcgettext(Domain,Message,Type) (Message)
#  define bindtextdomain(Domain,Directory) (Domain)
#  define _(String) (String)
#  define N_(String) (String)
#endif

#define CHECK(result) {int r=(result); if (r<0) return (r);}

struct _GPPortPrivateLibrary {
	int fd;       /* Device handle */
};

GPPortType
gp_port_library_type ()
{
	return GP_PORT_USB_SCSI;
}

static int
gp_port_usbscsi_lock (GPPort *port)
{
#if HAVE_FLOCK
	GP_LOG_D ("Trying to lock '%s'...", port->settings.usbscsi.path);

	if (flock(port->pl->fd, LOCK_EX | LOCK_NB) != 0) {
		switch (errno) {
		case EWOULDBLOCK:
			gp_port_set_error (port,
				_("Device '%s' is locked by another app."),
				port->settings.usbscsi.path);
			return GP_ERROR_IO_LOCK;
		default:
			gp_port_set_error (port,
				_("Failed to lock '%s' (%m)."),
				port->settings.usbscsi.path);
			return GP_ERROR_IO;
		}
	}
#else
	GP_LOG_D ("Locking '%s' not possible, flock not availbale.", port->settings.usbscsi.path);
#endif
	return GP_OK;
}

static int
gp_port_usbscsi_unlock (GPPort *port)
{
#ifdef HAVE_FLOCK
	if (flock(port->pl->fd, LOCK_UN) != 0) {
		gp_port_set_error (port, _("Failed to unlock '%s' (%m)."),
				   port->settings.usbscsi.path);
		return GP_ERROR_IO;<commit_msg>Fix typo in error message<commit_after>/* SCSI commands to USB Mass storage devices port library for Linux
 *
 *   Copyright (c) 2010-2012 <NAME> <<EMAIL>>
 *
 * This program 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 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#define _DEFAULT_SOURCE	/* for flock */

#include "config.h"
#include <gphoto2/gphoto2-port-library.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
# ifndef LOCK_SH
#  define LOCK_SH 1
# endif
# ifndef LOCK_EX
#  define LOCK_EX 2
# endif
# ifndef LOCK_NB
#  define LOCK_NB 4
# endif
# ifndef LOCK_UN
#  define LOCK_UN 4
# endif
#endif
#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_SCSI_SG_H
# include <scsi/sg.h>
#endif

#include <gphoto2/gphoto2-port-result.h>
#include <gphoto2/gphoto2-port-log.h>
#include <gphoto2/gphoto2-port.h>

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (GETTEXT_PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define textdomain(String) (String)
#  define gettext(String) (String)
#  define dgettext(Domain,Message) (Message)
#  define dcgettext(Domain,Message,Type) (Message)
#  define bindtextdomain(Domain,Directory) (Domain)
#  define _(String) (String)
#  define N_(String) (String)
#endif

#define CHECK(result) {int r=(result); if (r<0) return (r);}

struct _GPPortPrivateLibrary {
	int fd;       /* Device handle */
};

GPPortType
gp_port_library_type ()
{
	return GP_PORT_USB_SCSI;
}

static int
gp_port_usbscsi_lock (GPPort *port)
{
#if HAVE_FLOCK
	GP_LOG_D ("Trying to lock '%s'...", port->settings.usbscsi.path);

	if (flock(port->pl->fd, LOCK_EX | LOCK_NB) != 0) {
		switch (errno) {
		case EWOULDBLOCK:
			gp_port_set_error (port,
				_("Device '%s' is locked by another app."),
				port->settings.usbscsi.path);
			return GP_ERROR_IO_LOCK;
		default:
			gp_port_set_error (port,
				_("Failed to lock '%s' (%m)."),
				port->settings.usbscsi.path);
			return GP_ERROR_IO;
		}
	}
#else
	GP_LOG_D ("Locking '%s' not possible, flock not available.", port->settings.usbscsi.path);
#endif
	return GP_OK;
}

static int
gp_port_usbscsi_unlock (GPPort *port)
{
#ifdef HAVE_FLOCK
	if (flock(port->pl->fd, LOCK_UN) != 0) {
		gp_port_set_error (port, _("Failed to unlock '%s' (%m)."),
				   port->settings.usbscsi.path);
		return GP_ERROR_IO;
{'fraction_non_alphanumeric': 0.1208358570563295, 'fraction_numerical': 0.010902483343428226, 'mean_word_length': 4.32232070910556, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4894433', 'n_tokens_mistral': 2519, 'n_tokens_neox': 2386, 'n_words': 711}