Skip to content

Commit

Permalink
S3: implement 'deletebucket' subcommand in xs3
Browse files Browse the repository at this point in the history
  • Loading branch information
apeters1971 committed May 22, 2024
1 parent f03a9f2 commit 138ad78
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/XrdS3/app/xs3
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def main():
addbucket_parser.add_argument('bucketname', help='Bucket name')
addbucket_parser.add_argument('bucketpath', help='Bucket path')

# Add the 'deletebucket' subcommand
deletebucket_parser = subparsers.add_parser('deletebucket', help='Delete an existing bucket')
deletebucket_parser.add_argument('username', help='Username')
deletebucket_parser.add_argument('bucketname', help='Bucket name')

# Parse the arguments
args = parser.parse_args()

Expand All @@ -70,6 +75,8 @@ def main():
handle_deleteuser(args)
elif args.subcommand == 'addbucket':
handle_addbucket(args)
elif args.subcommand == 'deletebucket':
handle_deletebucket(args)
else:
parser.print_help()

Expand Down Expand Up @@ -354,13 +361,37 @@ def handle_addbucket(args):
print(f"Error: Failed to read the config file '{config_file}'. {e}")
return

# Check if the bucket file already exists
buckets_dir = os.path.join(base_path, 'buckets')
bucket_file = os.path.join(buckets_dir, bucketname)
if os.path.exists(bucket_file):
try:
owner_attr = os.getxattr(bucket_file, 'user.s3.owner')
path_attr = os.getxattr(bucket_file, 'user.s3.path')
owner = owner_attr.decode()
path = path_attr.decode()
print(f"Error: Bucket '{bucketname}' already exists. Owner: {owner}, Path: {path}")
except OSError as e:
print(f"Error: Failed to retrieve extended attributes from bucket file '{bucket_file}'. {e}")
return

# Check if the user directory exists
users_dir = os.path.join(base_path, 'users')
user_dir = os.path.join(users_dir, username)
if not os.path.exists(user_dir):
print("Error: User does not exist - use 'adduser' first.")
return

# Create an empty file under the users directory with the username
user_bucket_file = os.path.join(user_dir, bucketname)
if not os.path.exists(user_bucket_file):
try:
with open(user_bucket_file, 'w') as f:
f.write('')
print(f"Info: Empty file '{user_bucket_file}' created successfully under users directory.")
except OSError as e:
print(f"Error: Failed to create empty file '{user_bucket_file}' under users directory. {e}")

# Create the bucket directory
buckets_dir = os.path.join(base_path, 'buckets')
bucket_file = os.path.join(buckets_dir, bucketname)
Expand All @@ -384,6 +415,69 @@ def handle_addbucket(args):
except OSError as e:
print(f"Error: Failed to set extended attributes on '{bucket_file}'. {e}")

def handle_deletebucket(args):
username = args.username
bucketname = args.bucketname

# Determine the base path from the config file
config_dir = os.path.join(os.path.expanduser('~'), '.xs3')
config_file = os.path.join(config_dir, 'config')

if not os.path.exists(config_file):
print("Error: Configuration file does not exist. Please run 'config' subcommand first.")
return

try:
with open(config_file, 'r') as f:
config_data = json.load(f)
base_path = config_data.get('base_path')
if not base_path:
print("Error: Base path is not configured properly.")
return
except (IOError, json.JSONDecodeError) as e:
print(f"Error: Failed to read the config file '{config_file}'. {e}")
return

# Check if the user directory exists
users_dir = os.path.join(base_path, 'users')
user_dir = os.path.join(users_dir, username)
if not os.path.exists(user_dir):
print("Error: User does not exist - use adduser first.")
return

# Check if the bucket file exists
buckets_dir = os.path.join(base_path, 'buckets')
bucket_file = os.path.join(buckets_dir, bucketname)
if not os.path.exists(bucket_file):
print(f"Error: Bucket '{bucketname}' does not exist.")
return

# Check if the user is the owner of the bucket
try:
owner_attr = os.getxattr(bucket_file, 'user.s3.owner')
if owner_attr.decode() != username:
print("Error: User is not the owner of the given bucket.")
return
except OSError as e:
print(f"Error: Failed to retrieve extended attribute from bucket file '{bucket_file}'. {e}")
return

# Delete the bucket file
try:
os.remove(bucket_file)
print(f"Info: Bucket file '{bucket_file}' deleted successfully.")
except OSError as e:
print(f"Error: Failed to delete bucket file '{bucket_file}'. {e}")

# Delete the empty file under the users directory with the bucket name
user_bucket_file = os.path.join(user_dir, bucketname)
if os.path.exists(user_bucket_file):
try:
os.remove(user_bucket_file)
print(f"Info: Empty file '{user_bucket_file}' deleted successfully from users directory.")
except OSError as e:
print(f"Error: Failed to delete empty file '{user_bucket_file}' from users directory. {e}")


if __name__ == '__main__':
main()
Expand Down

0 comments on commit 138ad78

Please sign in to comment.